Skip to content

Commit 9b7ddfe

Browse files
author
Nishanth Shanmugham
committed
faster test running times by simplifying imports and using -testing.short
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.
1 parent 4563fc3 commit 9b7ddfe

File tree

7 files changed

+54
-44
lines changed

7 files changed

+54
-44
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ build:
1111

1212
.PHONY: test
1313
test:
14-
go test ./...
14+
go test -count=1 ./...
15+
16+
.PHONY: testshort
17+
testshort:
18+
go test -short -count=1 ./...
1519

1620
.PHONY: cover
1721
cover:

exhaustive_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ func runTest(t *testing.T, pattern string, setup ...func()) {
2727
}
2828

2929
func TestExhaustive(t *testing.T) {
30-
// Analysis of code that uses complex packages, such as package os and
31-
// package reflect, should not fail.
32-
runTest(t, "complexpkg/...")
30+
if !testing.Short() {
31+
// Analysis of code that uses complex packages, such as package os and
32+
// package reflect, should not fail.
33+
runTest(t, "complexpkg/...")
34+
}
3335

3436
// Enum discovery, enum types.
3537
runTest(t, "enum/...")

testdata/src/enforce-comment/enforce_comment_map.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package enforcecomment
22

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

56
var _ = map[Direction]int{
67
N: 1,
@@ -22,7 +23,7 @@ var (
2223
_ = map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
2324
N: 1,
2425
}[N]
25-
_ = fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
26+
_ = callMe("something", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
2627
N: 1,
2728
})
2829
)
@@ -35,7 +36,7 @@ var (
3536
_ = &map[Direction]int{
3637
N: 1,
3738
}
38-
_ = fmt.Errorf("%v", map[Direction]int{
39+
_ = callMe("something", map[Direction]int{
3940
N: 1,
4041
})
4142
)
@@ -124,35 +125,35 @@ func returnFuncCallWithMap() error {
124125
// some other comment
125126
//exhaustive:enforce
126127
// some other comment
127-
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$"
128+
return makeErr("something", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
128129
N: 1,
129-
})
130+
}).(error)
130131

131132
case 2:
132133
//exhaustive:enforce ... more arbitrary comment content (e.g. an explanation) ...
133-
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$"
134+
return makeErr("something", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
134135
N: 1,
135-
})
136+
}).(error)
136137

137138
case 3:
138-
return fmt.Errorf("%v", map[Direction]int{
139+
return makeErr("something", map[Direction]int{
139140
N: 1,
140-
})
141+
}).(error)
141142

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

149150
case 5:
150151
// this should report: according to go/ast, the comment is not considered to
151152
// be associated with the return node.
152-
return fmt.Errorf("%v", map[Direction]int{
153+
return makeErr("something", map[Direction]int{
153154
//exhaustive:enforce
154155
N: 1,
155-
})
156+
}).(error)
156157
}
157158
return nil
158159
}
@@ -304,7 +305,7 @@ func localVarDeclaration() {
304305
_ = map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
305306
N: 1,
306307
}[N]
307-
_ = fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
308+
_ = callMe("something", map[Direction]int{ // want "^missing keys in map of key type enforcecomment.Direction: enforcecomment.E, enforcecomment.S, enforcecomment.W, enforcecomment.directionInvalid$"
308309
N: 1,
309310
})
310311
)
@@ -317,7 +318,7 @@ func localVarDeclaration() {
317318
_ = &map[Direction]int{
318319
N: 1,
319320
}
320-
_ = fmt.Errorf("%v", map[Direction]int{
321+
_ = callMe("something", map[Direction]int{
321322
N: 1,
322323
})
323324
)

testdata/src/general/x/general_go116.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package x
77

88
import (
99
"io/fs"
10-
"log"
1110
"os"
1211
)
1312

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

21-
fi, err := os.Lstat(".")
22-
if err != nil {
23-
log.Fatal(err)
24-
}
20+
var fi fs.FileInfo
2521

2622
switch fi.Mode() { // want "^missing cases in switch of type fs.FileMode: fs.ModeDevice, fs.ModeSetuid, fs.ModeSetgid, fs.ModeType, fs.ModePerm$"
2723
case os.ModeDir:

testdata/src/general/x/irrelevant.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package x
22

33
import (
4-
"fmt"
54
barpkg "general/y"
65
)
76

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

65-
var s fmt.Stringer
64+
var s interface {
65+
String() string
66+
}
6667

6768
switch s.(type) {
6869
case WithMethod:

testdata/src/ignore-comment/ignore_comment_map.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ignorecomment
22

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

56
var _ = map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
67
N: 1,
@@ -22,7 +23,7 @@ var (
2223
_ = map[Direction]int{
2324
N: 1,
2425
}[N]
25-
_ = fmt.Errorf("%v", map[Direction]int{
26+
_ = callMe("something", map[Direction]int{
2627
N: 1,
2728
})
2829
)
@@ -35,7 +36,7 @@ var (
3536
_ = &map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
3637
N: 1,
3738
}
38-
_ = fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
39+
_ = callMe("something", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
3940
N: 1,
4041
})
4142
)
@@ -124,35 +125,35 @@ func returnFuncCallWithMap() error {
124125
// some other comment
125126
//exhaustive:ignore
126127
// some other comment
127-
return fmt.Errorf("%v", map[Direction]int{
128+
return makeErr("something", map[Direction]int{
128129
N: 1,
129-
})
130+
}).(error)
130131

131132
case 2:
132133
//exhaustive:ignore ... more arbitrary comment content (e.g. an explanation) ...
133-
return fmt.Errorf("%v", map[Direction]int{
134+
return makeErr("something", map[Direction]int{
134135
N: 1,
135-
})
136+
}).(error)
136137

137138
case 3:
138-
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$"
139+
return makeErr("something", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
139140
N: 1,
140-
})
141+
}).(error)
141142

142143
case 4:
143144
// this should report: according to go/ast, the comment is not considered to
144145
// be associated with the return node.
145-
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$"
146+
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$"
146147
N: 1,
147-
})
148+
}).(error)
148149

149150
case 5:
150151
// this should report: according to go/ast, the comment is not considered to
151152
// be associated with the return node.
152-
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$"
153+
return makeErr("something", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
153154
//exhaustive:ignore
154155
N: 1,
155-
})
156+
}).(error)
156157
}
157158
return nil
158159
}
@@ -304,7 +305,7 @@ func localVarDeclaration() {
304305
_ = map[Direction]int{
305306
N: 1,
306307
}[N]
307-
_ = fmt.Errorf("%v", map[Direction]int{
308+
_ = callMe("something", map[Direction]int{
308309
N: 1,
309310
})
310311
)
@@ -317,7 +318,7 @@ func localVarDeclaration() {
317318
_ = &map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
318319
N: 1,
319320
}
320-
_ = fmt.Errorf("%v", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
321+
_ = callMe("something", map[Direction]int{ // want "^missing keys in map of key type ignorecomment.Direction: ignorecomment.E, ignorecomment.S, ignorecomment.W, ignorecomment.directionInvalid$"
321322
N: 1,
322323
})
323324
)

testdata/src/typeparam/typeparam.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package typeparam
55

66
import (
7-
"fmt"
87
y "general/y"
98
)
109

@@ -42,16 +41,22 @@ const (
4241

4342
type NotEnumType uint8
4443

44+
type Stringer interface {
45+
String() string
46+
}
47+
4548
type II interface{ N | JJ }
4649
type JJ interface{ O }
4750
type KK interface {
4851
M
49-
fmt.Stringer
52+
Stringer
53+
error
5054
comparable
5155
}
5256
type LL interface {
5357
M | NotEnumType
54-
fmt.Stringer
58+
Stringer
59+
error
5560
}
5661
type MM interface {
5762
M

0 commit comments

Comments
 (0)