Skip to content

Commit

Permalink
cmd/tool/slowest: add jsonfile flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed May 15, 2020
1 parent ddc609f commit 157af96
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/tool/slowest/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func writeFile(path string, file *ast.File, fset *token.FileSet) error {
return format.Node(fh, fset, file)
}

// TODO: support preset values. Maybe that will help with the problem
// of strange indentation described on writeFile.
func parseSkipStatement(text string) (ast.Stmt, error) {
// Add some required boilerplate around the statement to make it a valid file
text = "package stub\nfunc Stub() {\n" + text + "\n}\n"
Expand Down
25 changes: 24 additions & 1 deletion cmd/tool/slowest/slowest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package slowest
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"time"
Expand Down Expand Up @@ -33,6 +35,9 @@ func setupFlags(name string) (*pflag.FlagSet, *options) {
fmt.Fprintf(os.Stderr, `Usage:
%s [flags]
Read a json file and print or update tests which are slower than threshold.
The json file can be created by 'gotestsum --jsonfile' or 'go test -json'.
By default this command will print the list of tests slower than threshold to stdout.
If --skip-stmt is set, instead of printing the list of stdout, the AST for the
Go source code in the working directory tree will be modified. The --skip-stmt
Expand All @@ -48,6 +53,8 @@ Flags:
`, name, name)
flags.PrintDefaults()
}
flags.StringVar(&opts.jsonfile, "jsonfile", "",
"path to test2json output, defaults to stdin")
flags.DurationVar(&opts.threshold, "threshold", 100*time.Millisecond,
"tests faster than this threshold will be omitted from the output")
flags.StringVar(&opts.skipStatement, "skip-stmt", "",
Expand All @@ -59,6 +66,7 @@ Flags:

type options struct {
threshold time.Duration
jsonfile string
skipStatement string
debug bool
}
Expand All @@ -67,8 +75,14 @@ func run(opts *options) error {
if opts.debug {
log.SetLevel(log.DebugLevel)
}
in, err := jsonfileReader(opts.jsonfile)
if err != nil {
return fmt.Errorf("failed to read jsonfile: %w", err)
}
defer in.Close()

exec, err := testjson.ScanTestOutput(testjson.ScanConfig{
Stdout: os.Stdin,
Stdout: in,
Stderr: bytes.NewReader(nil),
})
if err != nil {
Expand Down Expand Up @@ -111,3 +125,12 @@ func slowTestCases(exec *testjson.Execution, threshold time.Duration) []testjson
})
return tests[:end]
}

func jsonfileReader(v string) (io.ReadCloser, error) {
switch v {
case "", "-":
return ioutil.NopCloser(os.Stdin), nil
default:
return os.Open(v)
}
}

0 comments on commit 157af96

Please sign in to comment.