Skip to content

Commit

Permalink
faster test running times by simplifying imports and using -testing.s…
Browse files Browse the repository at this point in the history
…hort

This commit improves uncached test running times for the full suite from
around 5 seconds on my machine to around 3 seconds. Most of the
improvement is due to removing imports to more complex packages, such as
fmt and log, in the testdata files.

With the -short flag: The dedicated complex packages usage test is moved
out of general and into its own package, and placed behind the -short
flag. This makes running tests with -short even faster at around 1.5
seconds on my machine.

The Makefile adds a testshort target.
  • Loading branch information
nishanths committed Nov 27, 2022
1 parent 4563fc3 commit 9b7ddfe
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 44 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ build:

.PHONY: test
test:
go test ./...
go test -count=1 ./...

.PHONY: testshort
testshort:
go test -short -count=1 ./...

.PHONY: cover
cover:
Expand Down
8 changes: 5 additions & 3 deletions exhaustive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func runTest(t *testing.T, pattern string, setup ...func()) {
}

func TestExhaustive(t *testing.T) {
// Analysis of code that uses complex packages, such as package os and
// package reflect, should not fail.
runTest(t, "complexpkg/...")
if !testing.Short() {
// Analysis of code that uses complex packages, such as package os and
// package reflect, should not fail.
runTest(t, "complexpkg/...")
}

// Enum discovery, enum types.
runTest(t, "enum/...")
Expand Down
31 changes: 16 additions & 15 deletions testdata/src/enforce-comment/enforce_comment_map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package enforcecomment

import "fmt"
func callMe(a string, x map[Direction]int) map[Direction]int { return x }
func makeErr(a string, x map[Direction]int) error { return nil }

var _ = map[Direction]int{
N: 1,
Expand All @@ -22,7 +23,7 @@ var (
_ = map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
N: 1,
}[N]
_ = fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
_ = callMe("something", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
N: 1,
})
)
Expand All @@ -35,7 +36,7 @@ var (
_ = &map[Direction]int{
N: 1,
}
_ = fmt.Errorf("%v", map[Direction]int{
_ = callMe("something", map[Direction]int{
N: 1,
})
)
Expand Down Expand Up @@ -124,35 +125,35 @@ func returnFuncCallWithMap() error {
// some other comment
//exhaustive:enforce
// some other comment
return fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
return makeErr("something", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
N: 1,
})
}).(error)

case 2:
//exhaustive:enforce ... more arbitrary comment content (e.g. an explanation) ...
return fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
return makeErr("something", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
N: 1,
})
}).(error)

case 3:
return fmt.Errorf("%v", map[Direction]int{
return makeErr("something", map[Direction]int{
N: 1,
})
}).(error)

case 4:
// this should report: according to go/ast, the comment is not considered to
// be associated with the return node.
return fmt.Errorf("%v", map[Direction]int{ //exhaustive:enforce
return makeErr("something", map[Direction]int{ //exhaustive:enforce
N: 1,
})
}).(error)

case 5:
// this should report: according to go/ast, the comment is not considered to
// be associated with the return node.
return fmt.Errorf("%v", map[Direction]int{
return makeErr("something", map[Direction]int{
//exhaustive:enforce
N: 1,
})
}).(error)
}
return nil
}
Expand Down Expand Up @@ -304,7 +305,7 @@ func localVarDeclaration() {
_ = map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
N: 1,
}[N]
_ = fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
_ = callMe("something", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
N: 1,
})
)
Expand All @@ -317,7 +318,7 @@ func localVarDeclaration() {
_ = &map[Direction]int{
N: 1,
}
_ = fmt.Errorf("%v", map[Direction]int{
_ = callMe("something", map[Direction]int{
N: 1,
})
)
Expand Down
6 changes: 1 addition & 5 deletions testdata/src/general/x/general_go116.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package x

import (
"io/fs"
"log"
"os"
)

Expand All @@ -18,10 +17,7 @@ func _q() {
// Of interest, note that e.g. listing os.ModeSocket in a case clause is
// equivalent to listing fs.ModeSocket (both have the same constant value).

fi, err := os.Lstat(".")
if err != nil {
log.Fatal(err)
}
var fi fs.FileInfo

switch fi.Mode() { // want "^missing cases in switch of type fs.FileMode: fs.ModeDevice, fs.ModeSetuid, fs.ModeSetgid, fs.ModeType, fs.ModePerm$"
case os.ModeDir:
Expand Down
5 changes: 3 additions & 2 deletions testdata/src/general/x/irrelevant.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package x

import (
"fmt"
barpkg "general/y"
)

Expand Down Expand Up @@ -62,7 +61,9 @@ func _v() {
// type switches should be ignored.
// as of go1.19 these have type *ast.TypeSwitchStmt.

var s fmt.Stringer
var s interface {
String() string
}

switch s.(type) {
case WithMethod:
Expand Down
31 changes: 16 additions & 15 deletions testdata/src/ignore-comment/ignore_comment_map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ignorecomment

import "fmt"
func callMe(a string, x map[Direction]int) map[Direction]int { return x }
func makeErr(a string, x map[Direction]int) error { return nil }

var _ = map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
N: 1,
Expand All @@ -22,7 +23,7 @@ var (
_ = map[Direction]int{
N: 1,
}[N]
_ = fmt.Errorf("%v", map[Direction]int{
_ = callMe("something", map[Direction]int{
N: 1,
})
)
Expand All @@ -35,7 +36,7 @@ var (
_ = &map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
N: 1,
}
_ = fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
_ = callMe("something", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
N: 1,
})
)
Expand Down Expand Up @@ -124,35 +125,35 @@ func returnFuncCallWithMap() error {
// some other comment
//exhaustive:ignore
// some other comment
return fmt.Errorf("%v", map[Direction]int{
return makeErr("something", map[Direction]int{
N: 1,
})
}).(error)

case 2:
//exhaustive:ignore ... more arbitrary comment content (e.g. an explanation) ...
return fmt.Errorf("%v", map[Direction]int{
return makeErr("something", map[Direction]int{
N: 1,
})
}).(error)

case 3:
return fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
return makeErr("something", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
N: 1,
})
}).(error)

case 4:
// this should report: according to go/ast, the comment is not considered to
// be associated with the return node.
return fmt.Errorf("%v", map[Direction]int{ //exhaustive:ignore // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
return makeErr("something", map[Direction]int{ //exhaustive:ignore // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
N: 1,
})
}).(error)

case 5:
// this should report: according to go/ast, the comment is not considered to
// be associated with the return node.
return fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
return makeErr("something", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
//exhaustive:ignore
N: 1,
})
}).(error)
}
return nil
}
Expand Down Expand Up @@ -304,7 +305,7 @@ func localVarDeclaration() {
_ = map[Direction]int{
N: 1,
}[N]
_ = fmt.Errorf("%v", map[Direction]int{
_ = callMe("something", map[Direction]int{
N: 1,
})
)
Expand All @@ -317,7 +318,7 @@ func localVarDeclaration() {
_ = &map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
N: 1,
}
_ = fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
_ = callMe("something", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
N: 1,
})
)
Expand Down
11 changes: 8 additions & 3 deletions testdata/src/typeparam/typeparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package typeparam

import (
"fmt"
y "general/y"
)

Expand Down Expand Up @@ -42,16 +41,22 @@ const (

type NotEnumType uint8

type Stringer interface {
String() string
}

type II interface{ N | JJ }
type JJ interface{ O }
type KK interface {
M
fmt.Stringer
Stringer
error
comparable
}
type LL interface {
M | NotEnumType
fmt.Stringer
Stringer
error
}
type MM interface {
M
Expand Down

0 comments on commit 9b7ddfe

Please sign in to comment.