Skip to content

Commit 65306bc

Browse files
committed
cmd/dist: refactor adding to the test list into a method
Currently, there are four places that add distTests to the tester.tests list. That means we're already missing a few name uniqueness checks, and we're about to start enforcing some more requirements on tests that would be nice to have in one place. Hence, to prepare for this, this CL refactors the process of adding to the tester.tests list into a method. That also means we can trivially use a map to check name uniqueness rather than an n^2 slice search. For #37486. Change-Id: Ib7b64c7bbf65e5e870c4f4bfaca8c7f70983605c Reviewed-on: https://go-review.googlesource.com/c/go/+/495015 Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent e0ceba8 commit 65306bc

File tree

1 file changed

+92
-103
lines changed

1 file changed

+92
-103
lines changed

src/cmd/dist/test.go

Lines changed: 92 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ type tester struct {
7070
cgoEnabled bool
7171
partial bool
7272

73-
tests []distTest
73+
tests []distTest // use addTest to extend
74+
testNames map[string]bool
7475
timeoutScale int
7576

7677
worklist []*work
@@ -193,7 +194,7 @@ func (t *tester) run() {
193194
}
194195

195196
for _, name := range t.runNames {
196-
if !t.isRegisteredTestName(name) {
197+
if !t.testNames[name] {
197198
fatalf("unknown test %q", name)
198199
}
199200
}
@@ -471,31 +472,27 @@ func (t *tester) registerStdTest(pkg string) {
471472
stdMatches = append(stdMatches, pkg)
472473
}
473474

474-
t.tests = append(t.tests, distTest{
475-
name: testName,
476-
heading: heading,
477-
fn: func(dt *distTest) error {
478-
if ranGoTest {
479-
return nil
480-
}
481-
t.runPending(dt)
482-
timelog("start", dt.name)
483-
defer timelog("end", dt.name)
484-
ranGoTest = true
475+
t.addTest(testName, heading, func(dt *distTest) error {
476+
if ranGoTest {
477+
return nil
478+
}
479+
t.runPending(dt)
480+
timelog("start", dt.name)
481+
defer timelog("end", dt.name)
482+
ranGoTest = true
485483

486-
timeoutSec := 180 * time.Second
487-
for _, pkg := range stdMatches {
488-
if pkg == "cmd/go" {
489-
timeoutSec *= 3
490-
break
491-
}
484+
timeoutSec := 180 * time.Second
485+
for _, pkg := range stdMatches {
486+
if pkg == "cmd/go" {
487+
timeoutSec *= 3
488+
break
492489
}
493-
return (&goTest{
494-
timeout: timeoutSec,
495-
gcflags: gcflags,
496-
pkgs: stdMatches,
497-
}).run(t)
498-
},
490+
}
491+
return (&goTest{
492+
timeout: timeoutSec,
493+
gcflags: gcflags,
494+
pkgs: stdMatches,
495+
}).run(t)
499496
})
500497
}
501498

@@ -504,25 +501,21 @@ func (t *tester) registerRaceBenchTest(pkg string) {
504501
if t.runRx == nil || t.runRx.MatchString(testName) == t.runRxWant {
505502
benchMatches = append(benchMatches, pkg)
506503
}
507-
t.tests = append(t.tests, distTest{
508-
name: testName,
509-
heading: "Running benchmarks briefly.",
510-
fn: func(dt *distTest) error {
511-
if ranGoBench {
512-
return nil
513-
}
514-
t.runPending(dt)
515-
timelog("start", dt.name)
516-
defer timelog("end", dt.name)
517-
ranGoBench = true
518-
return (&goTest{
519-
timeout: 1200 * time.Second, // longer timeout for race with benchmarks
520-
race: true,
521-
bench: true,
522-
cpu: "4",
523-
pkgs: benchMatches,
524-
}).run(t)
525-
},
504+
t.addTest(testName, "Running benchmarks briefly.", func(dt *distTest) error {
505+
if ranGoBench {
506+
return nil
507+
}
508+
t.runPending(dt)
509+
timelog("start", dt.name)
510+
defer timelog("end", dt.name)
511+
ranGoBench = true
512+
return (&goTest{
513+
timeout: 1200 * time.Second, // longer timeout for race with benchmarks
514+
race: true,
515+
bench: true,
516+
cpu: "4",
517+
pkgs: benchMatches,
518+
}).run(t)
526519
})
527520
}
528521

@@ -688,43 +681,39 @@ func (t *tester) registerTests() {
688681
// Fails on Android, js/wasm and wasip1/wasm with an exec format error.
689682
// Fails on plan9 with "cannot find GOROOT" (issue #21016).
690683
if os.Getenv("GO_BUILDER_NAME") != "" && goos != "android" && !t.iOS() && goos != "plan9" && goos != "js" && goos != "wasip1" {
691-
t.tests = append(t.tests, distTest{
692-
name: "moved_goroot",
693-
heading: "moved GOROOT",
694-
fn: func(dt *distTest) error {
695-
t.runPending(dt)
696-
timelog("start", dt.name)
697-
defer timelog("end", dt.name)
698-
moved := goroot + "-moved"
699-
if err := os.Rename(goroot, moved); err != nil {
700-
if goos == "windows" {
701-
// Fails on Windows (with "Access is denied") if a process
702-
// or binary is in this directory. For instance, using all.bat
703-
// when run from c:\workdir\go\src fails here
704-
// if GO_BUILDER_NAME is set. Our builders invoke tests
705-
// a different way which happens to work when sharding
706-
// tests, but we should be tolerant of the non-sharded
707-
// all.bat case.
708-
log.Printf("skipping test on Windows")
709-
return nil
710-
}
711-
return err
712-
}
713-
714-
// Run `go test fmt` in the moved GOROOT, without explicitly setting
715-
// GOROOT in the environment. The 'go' command should find itself.
716-
cmd := (&goTest{
717-
goroot: moved,
718-
pkg: "fmt",
719-
}).command(t)
720-
unsetEnv(cmd, "GOROOT")
721-
err := cmd.Run()
722-
723-
if rerr := os.Rename(moved, goroot); rerr != nil {
724-
fatalf("failed to restore GOROOT: %v", rerr)
684+
t.addTest("moved_goroot", "moved GOROOT", func(dt *distTest) error {
685+
t.runPending(dt)
686+
timelog("start", dt.name)
687+
defer timelog("end", dt.name)
688+
moved := goroot + "-moved"
689+
if err := os.Rename(goroot, moved); err != nil {
690+
if goos == "windows" {
691+
// Fails on Windows (with "Access is denied") if a process
692+
// or binary is in this directory. For instance, using all.bat
693+
// when run from c:\workdir\go\src fails here
694+
// if GO_BUILDER_NAME is set. Our builders invoke tests
695+
// a different way which happens to work when sharding
696+
// tests, but we should be tolerant of the non-sharded
697+
// all.bat case.
698+
log.Printf("skipping test on Windows")
699+
return nil
725700
}
726701
return err
727-
},
702+
}
703+
704+
// Run `go test fmt` in the moved GOROOT, without explicitly setting
705+
// GOROOT in the environment. The 'go' command should find itself.
706+
cmd := (&goTest{
707+
goroot: moved,
708+
pkg: "fmt",
709+
}).command(t)
710+
unsetEnv(cmd, "GOROOT")
711+
err := cmd.Run()
712+
713+
if rerr := os.Rename(moved, goroot); rerr != nil {
714+
fatalf("failed to restore GOROOT: %v", rerr)
715+
}
716+
return err
728717
})
729718
}
730719

@@ -868,15 +857,22 @@ func (t *tester) registerTests() {
868857
}
869858
}
870859

871-
// isRegisteredTestName reports whether a test named testName has already
872-
// been registered.
873-
func (t *tester) isRegisteredTestName(testName string) bool {
874-
for _, tt := range t.tests {
875-
if tt.name == testName {
876-
return true
877-
}
860+
// addTest adds an arbitrary test callback to the test list.
861+
//
862+
// name must uniquely identify the test.
863+
func (t *tester) addTest(name, heading string, fn func(*distTest) error) {
864+
if t.testNames[name] {
865+
panic("duplicate registered test name " + name)
878866
}
879-
return false
867+
if t.testNames == nil {
868+
t.testNames = make(map[string]bool)
869+
}
870+
t.testNames[name] = true
871+
t.tests = append(t.tests, distTest{
872+
name: name,
873+
heading: heading,
874+
fn: fn,
875+
})
880876
}
881877

882878
type registerTestOpt interface {
@@ -902,29 +898,22 @@ func (t *tester) registerTest(name, heading string, test *goTest, opts ...regist
902898
preFunc = opt.pre
903899
}
904900
}
905-
if t.isRegisteredTestName(name) {
906-
panic("duplicate registered test name " + name)
907-
}
908901
if heading == "" {
909902
if test.pkg == "" {
910903
panic("either heading or test.pkg must be set")
911904
}
912905
heading = test.pkg
913906
}
914-
t.tests = append(t.tests, distTest{
915-
name: name,
916-
heading: heading,
917-
fn: func(dt *distTest) error {
918-
if preFunc != nil && !preFunc(dt) {
919-
return nil
920-
}
921-
w := &work{
922-
dt: dt,
923-
cmd: test.bgCommand(t),
924-
}
925-
t.worklist = append(t.worklist, w)
907+
t.addTest(name, heading, func(dt *distTest) error {
908+
if preFunc != nil && !preFunc(dt) {
926909
return nil
927-
},
910+
}
911+
w := &work{
912+
dt: dt,
913+
cmd: test.bgCommand(t),
914+
}
915+
t.worklist = append(t.worklist, w)
916+
return nil
928917
})
929918
}
930919

0 commit comments

Comments
 (0)