forked from cweill/gotests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
110 lines (101 loc) · 2.75 KB
/
process.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
// Package process is a thin wrapper around the gotests library. It is intended
// to be called from a binary and handle its arguments, flags, and output when
// generating tests.
package process
import (
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"github.com/cweill/gotests"
)
const newFilePerm os.FileMode = 0644
// Set of options to use when generating tests.
type Options struct {
OnlyFuncs string // Regexp string for filter matches.
ExclFuncs string // Regexp string for excluding matches.
ExportedFuncs bool // Only include exported functions.
AllFuncs bool // Include all non-tested functions.
PrintInputs bool // Print function parameters as part of error messages.
WriteOutput bool // Write output to test file(s).
}
// Generates tests for the Go files defined in args with the given options.
// Logs information and errors to out. By default outputs generated tests to
// out unless specified by opt.
func Run(out io.Writer, args []string, opts *Options) {
if opts == nil {
opts = &Options{}
}
opt := parseOptions(out, opts)
if opt == nil {
return
}
if len(args) == 0 {
fmt.Fprintln(out, "Please specify a file or directory containing the source")
return
}
for _, path := range args {
generateTests(out, path, opts.WriteOutput, opt)
}
}
func parseOptions(out io.Writer, opt *Options) *gotests.Options {
if opt.OnlyFuncs == "" && opt.ExclFuncs == "" && !opt.ExportedFuncs && !opt.AllFuncs {
fmt.Fprintln(out, "Please specify either the -only, -excl, -export, or -all flag")
return nil
}
onlyRE, err := parseRegexp(opt.OnlyFuncs)
if err != nil {
fmt.Fprintln(out, "Invalid -only regex:", err)
return nil
}
exclRE, err := parseRegexp(opt.ExclFuncs)
if err != nil {
fmt.Fprintln(out, "Invalid -excl regex:", err)
return nil
}
return &gotests.Options{
Only: onlyRE,
Exclude: exclRE,
Exported: opt.ExportedFuncs,
PrintInputs: opt.PrintInputs,
}
}
func parseRegexp(s string) (*regexp.Regexp, error) {
if s == "" {
return nil, nil
}
re, err := regexp.Compile(s)
if err != nil {
return nil, err
}
return re, nil
}
func generateTests(out io.Writer, path string, writeOutput bool, opt *gotests.Options) {
gts, err := gotests.GenerateTests(path, opt)
if err != nil {
fmt.Fprintln(out, err.Error())
return
}
if len(gts) == 0 {
fmt.Fprintln(out, "No tests generated for", path)
return
}
for _, t := range gts {
outputTest(out, t, writeOutput)
}
}
func outputTest(out io.Writer, t *gotests.GeneratedTest, writeOutput bool) {
if writeOutput {
if err := ioutil.WriteFile(t.Path, t.Output, newFilePerm); err != nil {
fmt.Fprintln(out, err)
return
}
}
for _, t := range t.Functions {
fmt.Fprintln(out, "Generated", t.TestName())
}
if !writeOutput {
out.Write(t.Output)
}
}