-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathrunner_test.go
More file actions
146 lines (133 loc) · 3.4 KB
/
Copy pathrunner_test.go
File metadata and controls
146 lines (133 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package tomltest
import (
"context"
"fmt"
"strings"
"testing"
"testing/fstest"
)
func notInList(t *testing.T, list []string, str string) {
t.Helper()
for _, item := range list {
if item == str {
t.Fatalf("error: %q in list", str)
}
}
}
func TestVersion(t *testing.T) {
_, err := NewRunner(Runner{Version: "0.9"}).Run()
if err == nil {
t.Fatal("expected an error for version 0.9")
}
r := NewRunner(Runner{Version: "1.0.0"})
ls, err := r.List()
if err != nil {
t.Fatal(err)
}
notInList(t, ls, "valid/string/escape-esc")
r = NewRunner(Runner{Version: "1.0.0"})
ls, err = r.List()
if err != nil {
t.Fatal()
}
notInList(t, ls, "valid/string/escape-esc")
}
type testParser struct{}
func (t *testParser) Cmd() []string { return nil }
func (t *testParser) Run(ctx context.Context, input string) (pid int, output string, outputIsError bool, err error) {
switch input {
case `a=1`:
return 42, `{"a": {"type":"integer","value":"1"}}`, false, nil
case `a=`, `c=`:
return 42, `oh noes: error one`, true, nil
case `b=`:
return 42, `error two`, true, nil
default:
panic(fmt.Sprintf("unreachable: %q", input))
}
}
func TestErrors(t *testing.T) {
r := NewRunner(Runner{
Decoder: &testParser{},
Files: fstest.MapFS{
"valid/a.toml": &fstest.MapFile{Data: []byte(`a=1`)},
"valid/a.json": &fstest.MapFile{Data: []byte(`{"a": {"type":"integer","value":"1"}}`)},
"invalid/a.toml": &fstest.MapFile{Data: []byte(`a=`)},
"invalid/b.toml": &fstest.MapFile{Data: []byte(`b=`)},
"invalid/dir/c.toml": &fstest.MapFile{Data: []byte(`c=`)},
},
Errors: map[string]string{
"invalid/a": "oh noes",
"invalid/b": "don't match",
"dir/c.toml": "oh noes",
},
})
tt, err := r.Run()
if err != nil {
t.Error(err)
}
for _, test := range tt.Tests {
if test.Path == "invalid/b" {
if !test.Failed() {
t.Errorf("expected failure for %q, but got none", test.Path)
}
continue
}
if test.Failed() {
t.Errorf("\n%s: %s", test.Path, test.Failure)
}
}
t.Run("non-existent", func(t *testing.T) {
r := NewRunner(Runner{
Decoder: &testParser{},
Files: fstest.MapFS{},
Errors: map[string]string{
"file/doesn/exist": "oh noes",
},
})
_, err := r.Run()
if err == nil {
t.Fatal("error is nil")
}
if !strings.Contains(err.Error(), "didn't match anything") {
t.Fatalf("wrong error: %s", err)
}
})
}
func TestSkip(t *testing.T) {
r := NewRunner(Runner{
Decoder: &testParser{},
SkipTests: []string{"valid/a"},
Files: fstest.MapFS{
"valid/a.toml": &fstest.MapFile{Data: []byte(`a=`)},
},
})
tests, err := r.Run()
if err != nil {
t.Fatal(err)
}
if tests.FailedValid != 0 || tests.Skipped != 1 {
t.Fatalf("FailedValid=%d; Skipped=%d", tests.FailedValid, tests.Skipped)
}
}
func TestSkipMustError(t *testing.T) {
r := NewRunner(Runner{
Decoder: &testParser{},
SkipMustError: true,
SkipTests: []string{"valid/a"},
Files: fstest.MapFS{
"valid/a.toml": &fstest.MapFile{Data: []byte(`a=1`)},
"valid/a.json": &fstest.MapFile{Data: []byte(`{"a": {"type":"integer","value":"1"}}`)},
},
})
tests, err := r.Run()
if err != nil {
t.Fatal(err)
}
if tests.FailedValid != 1 || tests.Skipped != 0 {
t.Fatalf("FailedValid=%d; Skipped=%d", tests.FailedValid, tests.Skipped)
}
if tests.Tests[0].Failure != "Test skipped with -skip but didn't fail" {
t.Errorf("wrong failure message: %q", tests.Tests[0].Failure)
}
}