Skip to content

Commit

Permalink
Add a preset for testing.Short
Browse files Browse the repository at this point in the history
The preset seems to fix the problem where the formatting of the statement had
strange newlines added by the ast writer.
  • Loading branch information
dnephin committed May 15, 2020
1 parent 82aae80 commit afef6d5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
12 changes: 8 additions & 4 deletions cmd/tool/slowest/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ func normalizePkgName(name string) string {
return strings.TrimSuffix(name, "_test")
}

// TODO: sometimes this writes the new AST with strange indentation. It appears
// to be non-deterministic. Given the same input, it only happens sometimes.
func writeFile(path string, file *ast.File, fset *token.FileSet) error {
fh, err := os.Create(path)
if err != nil {
Expand All @@ -72,9 +70,15 @@ 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) {
switch text {
case "default", "testing.Short":
text = `
if testing.Short() {
t.Skip("too slow for testing.Short")
}
`
}
// Add some required boilerplate around the statement to make it a valid file
text = "package stub\nfunc Stub() {\n" + text + "\n}\n"
file, err := parser.ParseFile(token.NewFileSet(), "fragment", text, 0)
Expand Down
21 changes: 21 additions & 0 deletions cmd/tool/slowest/ast_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package slowest

import (
"bytes"
"go/format"
"go/token"
"testing"

"gotest.tools/v3/assert"
)

func TestParseSkipStatement_Preset_testingShort(t *testing.T) {
stmt, err := parseSkipStatement("testing.Short")
assert.NilError(t, err)
expected := `if testing.Short() {
t.Skip("too slow for testing.Short")
}`
buf := new(bytes.Buffer)
format.Node(buf, token.NewFileSet(), stmt)
assert.DeepEqual(t, buf.String(), expected)
}
21 changes: 18 additions & 3 deletions cmd/tool/slowest/slowest.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,34 @@ func setupFlags(name string) (*pflag.FlagSet, *options) {
%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'.
The json file can be created with '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
will be added to Go test files as the first statement in all the test functions
which are slower than threshold.
Example - use testing.Short():
The --skip-stmt flag may be set to the name of a predefine statement, or a
source code which will be parsed as a go/ast.Stmt. Currently there is only one
predefined statement: testing.Short:
skip_stmt='if testing.Short() { t.Skip("too slow for short run") }'
if testing.Short() {
t.Skip("too slow for testing.Short")
}
Example - using a custom --skip-stmt:
skip_stmt='
if os.Getenv("TEST_FAST") {
t.Skip("too slow for TEST_FAST")
}
'
go test -json -short ./... | %s --skip-stmt "$skip_stmt"
Note that this tool does not add imports, so using a custom statement may require
you to add any necessary imports to the file.
Flags:
`, name, name)
flags.PrintDefaults()
Expand Down

0 comments on commit afef6d5

Please sign in to comment.