-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain_test.go
327 lines (277 loc) · 7.84 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"bytes"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"braces.dev/errtrace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.abhg.dev/doc2go/internal/iotest"
"golang.org/x/tools/go/packages/packagestest"
)
func TestMainCmd_help(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
args []string
}{
{
desc: "default help",
args: []string{"-h"},
},
{
desc: "default help long form",
args: []string{"--help"},
},
{
desc: "help topic",
args: []string{"-h=frontmatter"},
},
{
desc: "help topic separate arg",
args: []string{"-h", "frontmatter"},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
exitCode := (&mainCmd{
Stdout: iotest.Writer(t),
Stderr: iotest.Writer(t),
}).Run(tt.args)
assert.Zero(t, exitCode, "-h should have zero status code")
})
}
}
func TestMainCmd_version(t *testing.T) {
t.Parallel()
var buff bytes.Buffer
exitCode := (&mainCmd{
Stdout: &buff,
Stderr: iotest.Writer(t),
}).Run([]string{"-version"})
assert.Zero(t, exitCode, "-version should have zero status code")
assert.Contains(t, buff.String(), "doc2go")
assert.Contains(t, buff.String(), _version)
}
func TestMainCmd_listThemes(t *testing.T) {
t.Parallel()
var buff bytes.Buffer
exitCode := (&mainCmd{
Stdout: &buff,
Stderr: iotest.Writer(t),
}).Run([]string{"-highlight-list-themes"})
assert.Zero(t, exitCode)
assert.NotEmpty(t, buff.String())
}
func TestMainCmd_writeCSS(t *testing.T) {
t.Parallel()
var buff bytes.Buffer
exitCode := (&mainCmd{
Stdout: &buff,
Stderr: iotest.Writer(t),
}).Run([]string{"-highlight-print-css", "-highlight=plain"})
assert.Zero(t, exitCode)
assert.NotEmpty(t, buff.String())
}
func TestMainCmd_writeCSS_unknown(t *testing.T) {
t.Parallel()
var buff bytes.Buffer
exitCode := (&mainCmd{
Stdout: iotest.Writer(t),
Stderr: &buff,
}).Run([]string{"-highlight-print-css", "-highlight=this-theme-does-not-exist"})
assert.NotZero(t, exitCode)
assert.Contains(t, buff.String(), `unknown theme "this-theme-does-not-exist"`)
}
func TestMainCmd_unknownFlag(t *testing.T) {
t.Parallel()
exitCode := (&mainCmd{
Stdout: iotest.Writer(t),
Stderr: iotest.Writer(t),
}).Run([]string{"--this-flag-does-not-exist"})
assert.NotZero(t, exitCode, "unknown flag should have non-zero status code")
}
func TestMainCmd_badTemplate(t *testing.T) {
t.Parallel()
var buff bytes.Buffer
exitCode := (&mainCmd{
Stdout: iotest.Writer(t),
Stderr: &buff,
}).Run([]string{"-pkg-doc", "foo=bar{{.baz", "./..."})
assert.NotZero(t, exitCode)
assert.Contains(t, buff.String(), "bad package documentation template")
}
func TestMainCmd_generate(t *testing.T) {
t.Parallel()
packagestest.TestAll(t, testMainCmdGenerate)
}
func testMainCmdGenerate(t *testing.T, exporter packagestest.Exporter) {
tests := []struct {
desc string
flags []string
basename string
}{
{
desc: "default",
basename: "index.html",
},
{
desc: "different basename",
flags: []string{"-basename", "_index.html"},
basename: "_index.html",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
exported := packagestest.Export(t, exporter, []packagestest.Module{
{
Name: "example.com/foo/bar",
Files: map[string]any{
"doc.go": "// Package bar does things.\npackage bar\n",
"types.go": "package bar\n" +
"// Bar implements the core logic.\n" +
"type Bar struct{}",
},
},
})
outDir := t.TempDir()
args := append(tt.flags, "-out", outDir, "-debug", "-embed", "./...")
exitCode := (&mainCmd{
Stdout: iotest.Writer(t),
Stderr: iotest.Writer(t),
packagesConfig: exported.Config,
}).Run(args)
require.Zero(t, exitCode, "expected success")
fsys := os.DirFS(outDir)
gotFiles := make(map[string]string)
err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return errtrace.Wrap(err)
}
got, err := fs.ReadFile(fsys, path)
if err != nil {
return errtrace.Wrap(err)
}
gotFiles[filepath.ToSlash(path)] = string(got)
t.Logf("Found file %v", path)
return nil
})
require.NoError(t, err)
getFile := func(p string) (string, bool) {
body, ok := gotFiles[p+"/"+tt.basename]
assert.True(t, ok, "file %v not found", p)
return body, ok
}
if body, ok := getFile("example.com/foo/bar"); ok {
assert.Contains(t, body, "Package bar does things")
assert.Contains(t, body, "Bar implements the core logic")
}
if body, ok := getFile("example.com/foo"); ok {
assert.Contains(t, body, `href="bar"`)
assert.Contains(t, body, "Package bar does things")
}
if body, ok := getFile("example.com"); ok {
assert.Contains(t, body, ">foo/bar<")
assert.Contains(t, body, "Package bar does things")
}
})
}
}
func TestMainCmd_frontmatter(t *testing.T) {
t.Parallel()
const template = "---\ntitle: {{.Path}}\n---"
frontmatterFile := filepath.Join(t.TempDir(), "frontmatter.txt")
require.NoError(t,
os.WriteFile(frontmatterFile, []byte(template), 0o644))
exported := packagestest.Export(t,
packagestest.Modules, []packagestest.Module{
{
Name: "foo/bar",
Files: map[string]any{
"bar.go": "package bar",
},
},
})
outDir := t.TempDir()
assertFilePrefix := func(path, want string) {
bs, err := os.ReadFile(filepath.Join(outDir, path))
require.NoError(t, err)
got := string(bs)
if !strings.HasPrefix(got, want) {
t.Errorf("File %v must start with %q\nGot:\n%v", path, want, got)
}
}
exitCode := (&mainCmd{
Stdout: iotest.Writer(t),
Stderr: iotest.Writer(t),
packagesConfig: exported.Config,
}).Run([]string{"-frontmatter", frontmatterFile, "-out", outDir, "./..."})
require.Zero(t, exitCode, "expected success")
assertFilePrefix("foo/index.html", "---\ntitle: foo\n---\n\n")
assertFilePrefix("foo/bar/index.html", "---\ntitle: foo/bar\n---\n\n")
}
func TestMainCmd_frontmatter_errors(t *testing.T) {
t.Parallel()
t.Run("bad syntax", func(t *testing.T) {
t.Parallel()
fm := filepath.Join(t.TempDir(), "frontmatter.txt")
require.NoError(t, os.WriteFile(fm, []byte("{{"), 0o644))
var buff bytes.Buffer
exitCode := (&mainCmd{
Stdout: iotest.Writer(t),
Stderr: &buff,
}).Run([]string{"-frontmatter", fm, "./..."})
require.NotZero(t, exitCode, "expected success")
assert.Contains(t, buff.String(), "bad frontmatter template")
})
t.Run("file does not exist", func(t *testing.T) {
t.Parallel()
var buff bytes.Buffer
exitCode := (&mainCmd{
Stdout: iotest.Writer(t),
Stderr: &buff,
}).Run([]string{"-frontmatter", "does-not-exist.txt", "./..."})
require.NotZero(t, exitCode, "expected success")
switch out := buff.String(); {
case strings.Contains(out, "no such file or directory"),
strings.Contains(out, "The system cannot find the file"):
default:
t.Errorf("Output must contain an informative message:\n%s", out)
}
})
}
func TestMainCmd_home(t *testing.T) {
t.Parallel()
exported := packagestest.Export(t,
packagestest.Modules, []packagestest.Module{
{
Name: "foo",
Files: map[string]any{
"bar/bar.go": "package bar",
"bar/baz/baz.go": "package baz",
"qux/qux.go": "package qux",
},
},
})
outDir := t.TempDir()
exitCode := (&mainCmd{
Stdout: iotest.Writer(t),
Stderr: iotest.Writer(t),
packagesConfig: exported.Config,
}).Run([]string{"-home", "foo/bar", "-out", outDir, "./..."})
require.Zero(t, exitCode, "expected success")
assertFileContains := func(p, c string) {
bs, err := os.ReadFile(filepath.Join(outDir, p))
require.NoError(t, err)
assert.Contains(t, string(bs), c)
}
assertFileContains("index.html", "package bar")
assertFileContains("baz/index.html", "package baz")
}