@@ -9,10 +9,12 @@ import (
9
9
"os"
10
10
"os/exec"
11
11
"path/filepath"
12
+ "regexp"
12
13
"strings"
13
14
"time"
14
15
15
16
"github.com/peterbourgon/ff/v3/ffcli"
17
+ "moul.io/godev"
16
18
"moul.io/motd"
17
19
)
18
20
@@ -28,15 +30,15 @@ func main() {
28
30
}
29
31
30
32
func run (args []string ) error {
31
- opts = Opts {
32
- verbose : false ,
33
- }
34
-
33
+ // flags
35
34
testFlags := flag .NewFlagSet ("testman test" , flag .ExitOnError )
36
- //testFlags.BoolVar(&opts.continueOnFailure, "continue-on-failure", opts.continueOnFailure, "Continue on failure")
37
- testFlags .BoolVar (& opts .verbose , "v" , opts .verbose , "verbose" )
35
+ testFlags .BoolVar (& opts .Verbose , "v" , false , "verbose" )
36
+ testFlags .StringVar (& opts .Run , "run" , "^(Test|Example)" , "regex to filter out tests and examples" )
37
+ //testFlags.IntVar(&opts.Retry, "retry", 0, "fail after N retries")
38
+ //testFlags.DurationVar(&opts.Timeout, "timeout", opts.Timeout, "max duration allowed to run the whole suite")
38
39
listFlags := flag .NewFlagSet ("testman list" , flag .ExitOnError )
39
- listFlags .BoolVar (& opts .verbose , "v" , opts .verbose , "verbose" )
40
+ listFlags .BoolVar (& opts .Verbose , "v" , false , "verbose" )
41
+ listFlags .StringVar (& opts .Run , "run" , "^(Test|Example)" , "regex to filter out tests and examples" )
40
42
41
43
root := & ffcli.Command {
42
44
ShortUsage : "testman <subcommand> [flags]" ,
@@ -85,6 +87,9 @@ func runList(ctx context.Context, args []string) error {
85
87
if err != nil {
86
88
return err
87
89
}
90
+ if len (tests ) == 0 {
91
+ continue
92
+ }
88
93
89
94
fmt .Println (pkg .ImportPath )
90
95
for _ , test := range tests {
@@ -99,7 +104,7 @@ func runTest(ctx context.Context, args []string) error {
99
104
return flag .ErrHelp
100
105
}
101
106
preRun ()
102
-
107
+ log . Printf ( "runTest opts=%s args=%s" , godev . JSON ( opts ), godev . JSON ( args ))
103
108
start := time .Now ()
104
109
105
110
// list packages
@@ -122,6 +127,9 @@ func runTest(ctx context.Context, args []string) error {
122
127
if err != nil {
123
128
return err
124
129
}
130
+ if len (tests ) == 0 {
131
+ continue
132
+ }
125
133
126
134
pkgStart := time .Now ()
127
135
// compile test binary
@@ -138,16 +146,16 @@ func runTest(ctx context.Context, args []string) error {
138
146
"-test.count=1" ,
139
147
"-test.timeout=300s" ,
140
148
}
141
- if opts .verbose {
149
+ if opts .Verbose {
142
150
args = append (args , "-test.v" )
143
151
}
144
152
args = append (args , "-test.run" , fmt .Sprintf ("^%s$" , test ))
145
153
cmd := exec .Command (bin , args ... )
146
154
log .Println (cmd .String ())
147
155
out , err := cmd .CombinedOutput ()
148
156
if err != nil {
149
- fmt .Printf ("FAIL\t %s\t [compile error: %v]\n " , pkg .ImportPath , err )
150
- if opts .verbose {
157
+ fmt .Printf ("FAIL\t %s.%s \t [compile error: %v]\n " , pkg .ImportPath , test , err )
158
+ if opts .Verbose {
151
159
fmt .Println (string (out ))
152
160
}
153
161
isPackageOK = false
@@ -167,7 +175,7 @@ func runTest(ctx context.Context, args []string) error {
167
175
}
168
176
169
177
func preRun () {
170
- if ! opts .verbose {
178
+ if ! opts .Verbose {
171
179
log .SetOutput (ioutil .Discard )
172
180
}
173
181
}
@@ -204,6 +212,15 @@ func listDirTests(dir string) ([]string, error) {
204
212
if strings .HasPrefix (line , "ok " ) {
205
213
continue
206
214
}
215
+ if opts .Run != "" {
216
+ matched , err := regexp .MatchString (opts .Run , line )
217
+ if err != nil {
218
+ return nil , err
219
+ }
220
+ if ! matched {
221
+ continue
222
+ }
223
+ }
207
224
tests = append (tests , line )
208
225
}
209
226
return tests , nil
@@ -239,11 +256,11 @@ type Package struct {
239
256
}
240
257
241
258
type Opts struct {
242
- verbose bool
243
- // run
244
- // timeout
259
+ Verbose bool
260
+ Run string
261
+ // Timeout time.Duration
262
+ // Retry int
245
263
// c
246
264
// debug
247
- // retries
248
265
// continueOnFailure vs failFast
249
266
}
0 commit comments