Skip to content

Commit 807213a

Browse files
committed
feat: add -run flag
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
1 parent f514228 commit 807213a

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.sum

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12+
"regexp"
1213
"strings"
1314
"time"
1415

1516
"github.com/peterbourgon/ff/v3/ffcli"
17+
"moul.io/godev"
1618
"moul.io/motd"
1719
)
1820

@@ -28,15 +30,15 @@ func main() {
2830
}
2931

3032
func run(args []string) error {
31-
opts = Opts{
32-
verbose: false,
33-
}
34-
33+
// flags
3534
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")
3839
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")
4042

4143
root := &ffcli.Command{
4244
ShortUsage: "testman <subcommand> [flags]",
@@ -85,6 +87,9 @@ func runList(ctx context.Context, args []string) error {
8587
if err != nil {
8688
return err
8789
}
90+
if len(tests) == 0 {
91+
continue
92+
}
8893

8994
fmt.Println(pkg.ImportPath)
9095
for _, test := range tests {
@@ -99,7 +104,7 @@ func runTest(ctx context.Context, args []string) error {
99104
return flag.ErrHelp
100105
}
101106
preRun()
102-
107+
log.Printf("runTest opts=%s args=%s", godev.JSON(opts), godev.JSON(args))
103108
start := time.Now()
104109

105110
// list packages
@@ -122,6 +127,9 @@ func runTest(ctx context.Context, args []string) error {
122127
if err != nil {
123128
return err
124129
}
130+
if len(tests) == 0 {
131+
continue
132+
}
125133

126134
pkgStart := time.Now()
127135
// compile test binary
@@ -138,16 +146,16 @@ func runTest(ctx context.Context, args []string) error {
138146
"-test.count=1",
139147
"-test.timeout=300s",
140148
}
141-
if opts.verbose {
149+
if opts.Verbose {
142150
args = append(args, "-test.v")
143151
}
144152
args = append(args, "-test.run", fmt.Sprintf("^%s$", test))
145153
cmd := exec.Command(bin, args...)
146154
log.Println(cmd.String())
147155
out, err := cmd.CombinedOutput()
148156
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 {
151159
fmt.Println(string(out))
152160
}
153161
isPackageOK = false
@@ -167,7 +175,7 @@ func runTest(ctx context.Context, args []string) error {
167175
}
168176

169177
func preRun() {
170-
if !opts.verbose {
178+
if !opts.Verbose {
171179
log.SetOutput(ioutil.Discard)
172180
}
173181
}
@@ -204,6 +212,15 @@ func listDirTests(dir string) ([]string, error) {
204212
if strings.HasPrefix(line, "ok ") {
205213
continue
206214
}
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+
}
207224
tests = append(tests, line)
208225
}
209226
return tests, nil
@@ -239,11 +256,11 @@ type Package struct {
239256
}
240257

241258
type Opts struct {
242-
verbose bool
243-
// run
244-
// timeout
259+
Verbose bool
260+
Run string
261+
// Timeout time.Duration
262+
// Retry int
245263
// c
246264
// debug
247-
// retries
248265
// continueOnFailure vs failFast
249266
}

0 commit comments

Comments
 (0)