-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathmain_test.go
190 lines (166 loc) · 4.49 KB
/
main_test.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"bytes"
"errors"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func testRunMain(args []string) (string, string, int) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
status := run(args, stdout, stderr, io.Discard, "")
return stdout.String(), stderr.String(), status
}
func TestOKWriteStdout(t *testing.T) {
f := filepath.Join("testdata", "ok.md")
stdout, stderr, status := testRunMain([]string{f, "-"})
if status != 0 {
t.Fatalf("status was non-zero: %d: %q", status, stderr)
}
b, err := os.ReadFile(filepath.Join("testdata", "ok.go"))
if err != nil {
panic(err)
}
want := string(b)
if diff := cmp.Diff(stdout, want); diff != "" {
t.Fatal(diff)
}
}
func TestOKWriteFile(t *testing.T) {
in := filepath.Join("testdata", "ok.md")
out := filepath.Join("testdata", "_test_output.go")
stdout, stderr, status := testRunMain([]string{in, out})
if status != 0 {
t.Fatalf("status was non-zero: %d: %q", status, stderr)
}
defer os.Remove(out)
b, err := os.ReadFile(filepath.Join("testdata", "ok.go"))
if err != nil {
panic(err)
}
want := string(b)
if stdout != "" {
t.Fatalf("stdout is not empty: %q", stdout)
}
b, err = os.ReadFile(out)
if err != nil {
t.Fatalf("output file %q cannot be read: %v", out, err)
}
have := string(b)
if diff := cmp.Diff(want, have); diff != "" {
t.Fatal(diff)
}
}
func TestErrorGenerate(t *testing.T) {
testCases := []struct {
file string
want string
}{
{"no_heading.md", "no \"Context availability\" table was found"},
{"no_table.md", "no \"Context availability\" table was found"},
{"broken_table.md", "expected 3 rows in table but got"},
{"else_directive.md", "cannot strip template directives since it contains {% else %}"},
}
for _, tc := range testCases {
t.Run(tc.file, func(t *testing.T) {
f := filepath.Join("testdata", tc.file)
stdout, stderr, status := testRunMain([]string{f, "-"})
if status == 0 {
t.Fatalf("status was zero: %q", stdout)
}
if !strings.Contains(stderr, tc.want) {
t.Fatalf("wanted %q in stderr %q", tc.want, stderr)
}
})
}
}
func TestStripAndUnescape(t *testing.T) {
tests := []struct {
input string
want string
}{
{"", ""},
{"foo, bar", "foo, bar"},
{"<hello>", "<hello>"},
{"aaa{% ifhoge ... %}, foo, bar{% endif %}, bbb", "aaa, foo, bar, bbb"},
}
for _, tc := range tests {
t.Run(tc.input, func(t *testing.T) {
have, err := stripAndUnescape(tc.input)
if err != nil {
t.Fatal(err)
}
if have != tc.want {
t.Fatalf("wanted %q but got %q", tc.want, have)
}
})
}
}
var errTestDummy = errors.New("dummy write error")
type testErrorWriter struct{}
func (w testErrorWriter) Write(b []byte) (int, error) {
return 0, errTestDummy
}
func TestWriteError(t *testing.T) {
f := filepath.Join("testdata", "ok.md")
stderr := &bytes.Buffer{}
status := run([]string{f, "-"}, testErrorWriter{}, stderr, io.Discard, "")
if status == 0 {
t.Fatal("status was zero")
}
msg := stderr.String()
if !strings.Contains(msg, "dummy write error") {
t.Fatalf("write error did not occur: %q", msg)
}
}
func TestFetchError(t *testing.T) {
testCases := []struct {
what string
url string
want string
}{
{"not found", "https://raw.githubusercontent.com/rhysd/actionlint/main/this-file-does-not-exist.txt", "request was not successful"},
{"invalid url", "foo://bar", "could not fetch"},
}
for _, tc := range testCases {
t.Run(tc.what, func(t *testing.T) {
stderr := &bytes.Buffer{}
status := run([]string{"-"}, io.Discard, stderr, io.Discard, tc.url)
if status == 0 {
t.Fatal("status was zero")
}
msg := stderr.String()
if !strings.Contains(msg, tc.want) {
t.Fatalf("unexpected error: %v: %s", msg, tc.url)
}
})
}
}
func TestCmdError(t *testing.T) {
f := filepath.Join("testdata", "ok.md")
dirNotExist := filepath.Join("dir", "does", "not", "exist", "out.go")
testCases := []struct {
what string
args []string
want string
}{
{"too many args", []string{"foo", "bar", "piyo"}, "usage:"},
{"cannot read file", []string{"oops-this-file-does-not-exist.md", "-"}, "oops-this-file-does-not-exist.md"},
{"cannot write file", []string{f, dirNotExist}, dirNotExist},
}
for _, tc := range testCases {
t.Run(tc.what, func(t *testing.T) {
stdout, stderr, status := testRunMain(tc.args)
if status == 0 {
t.Fatalf("status was zero: %q", stdout)
}
if !strings.Contains(stderr, tc.want) {
t.Fatalf("stderr does not contain %q: %q", tc.want, stderr)
}
})
}
}