This repository has been archived by the owner on Dec 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcli.go
436 lines (357 loc) · 10.4 KB
/
cli.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
)
//go:generate ./bin/gotests -godoc
// Exit codes are int values that represent an exit code for a particular error.
const (
ExitCodeOK int = 0
ExitCodeError int = 1 + iota
)
// defaultExcludes are default directory where walkFunc does not walk
var defaultExcludes = []string{".git", "Godep", "vendor"}
// CLI is the command line object
type CLI struct {
// outStream and errStream are the stdout and stderr
// to write message from the CLI.
outStream, errStream io.Writer
}
// Run invokes the CLI with the given arguments.
func (cli *CLI) Run(args []string) int {
var (
diff bool
write bool
list bool
includeUnexported bool
reverse bool
version bool
doc bool
)
// Define option flag parse
flags := flag.NewFlagSet(Name, flag.ContinueOnError)
flags.SetOutput(cli.errStream)
flags.Usage = func() {
fmt.Fprintf(cli.outStream, helpText)
}
flags.BoolVar(&diff, "diff", false, "")
flags.BoolVar(&diff, "d", false, "(Short)")
flags.BoolVar(&write, "write", false, "")
flags.BoolVar(&write, "w", false, "(Short)")
flags.BoolVar(&list, "list", false, "")
flags.BoolVar(&list, "l", false, "(Short)")
flags.BoolVar(&reverse, "reverse", false, "")
flags.BoolVar(&reverse, "r", false, "(Short)")
flags.BoolVar(&includeUnexported, "include-unexported", false, "")
flags.BoolVar(&includeUnexported, "i", false, "")
flags.BoolVar(&version, "version", false, "Print version information and quit.")
flags.BoolVar(&version, "v", false, "Print version information and quit.")
// This flag is only for developer to generate godoc via go generate.
flags.BoolVar(&doc, "godoc", false, "")
// Parse commandline flag
if err := flags.Parse(args[1:]); err != nil {
return ExitCodeError
}
// Show version
if version {
fmt.Fprintf(cli.errStream, "%s version %s\n", Name, Version)
return ExitCodeOK
}
// Generate godoc (only for developer)
if doc {
if err := godoc("doc.go"); err != nil {
fmt.Fprintf(cli.errStream, "Failed to generate godoc: %s", err)
return ExitCodeError
}
return ExitCodeOK
}
paths := flags.Args()
if len(paths) == 0 {
fmt.Fprintf(cli.errStream, "Invalid arguments. You must provide PATHs\n")
return ExitCodeError
}
// opts are option struct for processGenerate()
opts := &generateOpts{
diffOpts: &diffOpts{
Mode: Strict,
IncludeUnexported: includeUnexported,
},
diff: diff,
write: write,
list: list,
reverse: reverse,
}
// By default, statusCode is ExitCodeOK and Run() returns it.
// It is updated only when processGogenerate returns non-ExitCodeOK.
exitCode := ExitCodeOK
for _, path := range paths {
switch fi, err := os.Stat(path); {
case err != nil:
// Output the error and proceeds next (but Change status code).
fmt.Fprintf(cli.errStream, "Failed to get file info: %s", err)
exitCode = ExitCodeError
case fi.IsDir():
// walkFn is function for filepath.Walk. It walks through .go files
// (but non _test.go) and executes processGenerate() to each file.
// If error happens while processing, it display it to errStream
// and continues processing. This is same as gofmt does.
//
// It updates exitCode only when processGenerates returns not non-zero code.
walkFn := func(srcPath string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore if it's directory
if fi.IsDir() {
return nil
}
// Ignore .git and vendoring directory.
for _, exclude := range defaultExcludes {
if strings.Contains(srcPath, exclude) {
return nil
}
}
// Ignore non .go file and _test.go file.
if !isGoFile(fi) {
return nil
}
Debugf("Walk to %q", srcPath)
status := cli.processGenerate(srcPath, opts)
if status != ExitCodeOK {
exitCode = status
}
return nil
}
// Start walking.
if err := filepath.Walk(path, walkFn); err != nil {
fmt.Fprintf(cli.errStream, "Failed to walk: %s\n", err)
exitCode = ExitCodeError
}
default:
status := cli.processGenerate(path, opts)
if status != ExitCodeOK {
exitCode = status
}
}
}
return exitCode
}
type generateOpts struct {
diffOpts *diffOpts
diff bool
write bool
list bool
reverse bool
}
func (cli *CLI) processGenerate(srcPath string, opts *generateOpts) int {
var testPath string
if opts.reverse {
var err error
testPath = srcPath
srcPath, err = SrcFilePath(testPath)
if err != nil {
fmt.Fprintf(cli.errStream, "Failed to get src file path: %s\n", err)
return ExitCodeError
}
} else {
var err error
testPath, err = TestFilePath(srcPath)
if err != nil {
fmt.Fprintf(cli.errStream, "Failed to get go test file path: %s\n", err)
return ExitCodeError
}
}
// Run actual gotests to path
goTestFile, err := goTestGenerate(srcPath, testPath, opts)
if err != nil {
fmt.Fprintf(cli.errStream, "Failed to generate: %s\n", err)
return ExitCodeError
}
// Genreate results as a []byte
resBytes, err := goTestFile.Generate()
if err != nil {
fmt.Fprintf(cli.errStream, "Failed to generate result from ast: %s\n", err)
return ExitCodeError
}
// Handle diff/write only when there is diff between result and original code.
if !bytes.Equal(goTestFile.SrcBytes, resBytes) {
if opts.list {
path, err := fmtPath(goTestFile.FileName)
if err != nil {
fmt.Fprintf(cli.errStream, "Failed to format path: %s\n", err)
return ExitCodeError
}
fmt.Fprintf(cli.outStream, "%s\n", path)
}
if opts.diff {
data, err := doDiff(goTestFile.SrcBytes, resBytes)
if err != nil {
fmt.Fprintf(cli.errStream, "Failed to compute diff: %s\n", err)
return ExitCodeError
}
fmt.Fprintf(cli.outStream, "diff %s\n", goTestFile.FileName)
fmt.Fprintf(cli.outStream, "%s\n", data)
}
if opts.write {
err = ioutil.WriteFile(testPath, resBytes, 0644)
if err != nil {
fmt.Fprintf(cli.errStream, "Failed to write resutl to file: %s\n", err)
return ExitCodeError
}
}
}
if !opts.list && !opts.diff && !opts.write {
_, err := cli.outStream.Write(resBytes)
if err != nil {
fmt.Fprintf(cli.errStream, "Failed to write resutl: %s\n", err)
return ExitCodeError
}
}
return ExitCodeOK
}
func goTestGenerate(srcPath, testPath string, opts *generateOpts) (*GoFile, error) {
goFile, err := ParseFile(srcPath)
if err != nil {
return nil, fmt.Errorf("failed to parse go file: %s", err)
}
Debugf("%#v", goFile)
var goTestFile *GoFile
if _, err := os.Stat(testPath); os.IsNotExist(err) {
// If test file is not exist, create new one with the same pacakge
// declare with the source.
var err error
goTestFile, err = NewGoFile(testPath, goFile.PackageName)
if err != nil {
return nil, fmt.Errorf("failed to create new test file: %s", err)
}
} else {
// If test file is exist, just parse it.
var err error
goTestFile, err = ParseFile(testPath)
if err != nil {
return nil, fmt.Errorf("failed to parse go test file: %s", err)
}
}
Debugf("goTestFile: %#v", goTestFile)
diffFuncs, err := goFile.diffFuncs(goTestFile, opts.diffOpts)
if err != nil {
return nil, fmt.Errorf("failed to diff source file and test file: %s", err)
}
Debugf("Diff Funcs: %#v", diffFuncs)
funcTmpl := defaultExpectTestFuncTmpl
if err := goTestFile.addFuncTestFuncs(diffFuncs, funcTmpl); err != nil {
return nil, fmt.Errorf("failed to add func test funcs: %s", err)
}
diffMethods, err := goFile.diffMethods(goTestFile, opts.diffOpts)
if err != nil {
return nil, fmt.Errorf("failed to diff source file and test file: %s", err)
}
Debugf("Diff Methods: %#v", diffMethods)
funcTmpl = defaultExpectTestFuncMethodTmpl
if err := goTestFile.addMethodTestFuncs(diffMethods, funcTmpl); err != nil {
return nil, fmt.Errorf("failed to add method test funcs: %s", err)
}
return goTestFile, nil
}
func fmtPath(path string) (string, error) {
path, err := filepath.Abs(path)
if err != nil {
return "", err
}
currentPath, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Rel(currentPath, path)
}
func doDiff(b1, b2 []byte) ([]byte, error) {
f1, err := ioutil.TempFile("", Name)
if err != nil {
return nil, err
}
defer os.Remove(f1.Name())
defer f1.Close()
f2, err := ioutil.TempFile("", Name)
if err != nil {
return nil, err
}
defer os.Remove(f2.Name())
defer f2.Close()
f1.Write(b1)
f2.Write(b2)
data, err := exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
if err != nil {
if len(data) > 0 {
// diff exits with a non-zero status when the files don't match.
// Ignore that failure as long as we get output.
return data, nil
}
return nil, err
}
return data, err
}
// isGoFile returns true if file is go file and it's not test file.
func isGoFile(fi os.FileInfo) bool {
if fi.IsDir() {
return false
}
name := fi.Name()
if strings.HasPrefix(name, ".") {
return false
}
return strings.HasSuffix(name, ".go") && !strings.HasSuffix(name, "_test.go")
}
// godoc generates doc.go file for godoc to prevent from writing
// the same documentation twice. If any, return error.
func godoc(filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
tmpl, err := template.New("godoc").Parse(godocTmpl)
if err != nil {
return err
}
data := struct {
Content string
}{
Content: helpText,
}
if err := tmpl.Execute(f, data); err != nil {
return err
}
return nil
}
var godocTmpl = `// DON"T EDIT THIS FILE
// THIS IS GENERATED VIA GO GENERATE
/*
{{ .Content }}
*/
package main
`
var helpText = `gotests is tool to generate Go test functions from
the given source code.
https://github.com/tcnksm/gotests
Usage:
gotests [options] PATH ...
Options:
-diff, -d Display diffs instead of rewriting files.
-write, -w Write result to target file instead of stdout.
For example, if source file name is 'A.go',
target file will be 'A_test.go'.
-list, -l List test files to be updated/generated.
-i Include unexport function/method for generating target.
-reverse, -r (experimental) Allow to provide test file instead of source file.
By default, gotests expects source file PATH provided.
With this flag, the test file can be given.
For example, you can provide 'A_test.go' instead of 'A.go'.
This flag is useful for editor integration.
`