@@ -70,7 +70,8 @@ type tester struct {
70
70
cgoEnabled bool
71
71
partial bool
72
72
73
- tests []distTest
73
+ tests []distTest // use addTest to extend
74
+ testNames map [string ]bool
74
75
timeoutScale int
75
76
76
77
worklist []* work
@@ -193,7 +194,7 @@ func (t *tester) run() {
193
194
}
194
195
195
196
for _ , name := range t .runNames {
196
- if ! t .isRegisteredTestName ( name ) {
197
+ if ! t .testNames [ name ] {
197
198
fatalf ("unknown test %q" , name )
198
199
}
199
200
}
@@ -471,31 +472,27 @@ func (t *tester) registerStdTest(pkg string) {
471
472
stdMatches = append (stdMatches , pkg )
472
473
}
473
474
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
485
483
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
492
489
}
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 )
499
496
})
500
497
}
501
498
@@ -504,25 +501,21 @@ func (t *tester) registerRaceBenchTest(pkg string) {
504
501
if t .runRx == nil || t .runRx .MatchString (testName ) == t .runRxWant {
505
502
benchMatches = append (benchMatches , pkg )
506
503
}
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 )
526
519
})
527
520
}
528
521
@@ -688,43 +681,39 @@ func (t *tester) registerTests() {
688
681
// Fails on Android, js/wasm and wasip1/wasm with an exec format error.
689
682
// Fails on plan9 with "cannot find GOROOT" (issue #21016).
690
683
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
725
700
}
726
701
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
728
717
})
729
718
}
730
719
@@ -868,15 +857,22 @@ func (t *tester) registerTests() {
868
857
}
869
858
}
870
859
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 )
878
866
}
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
+ })
880
876
}
881
877
882
878
type registerTestOpt interface {
@@ -902,29 +898,22 @@ func (t *tester) registerTest(name, heading string, test *goTest, opts ...regist
902
898
preFunc = opt .pre
903
899
}
904
900
}
905
- if t .isRegisteredTestName (name ) {
906
- panic ("duplicate registered test name " + name )
907
- }
908
901
if heading == "" {
909
902
if test .pkg == "" {
910
903
panic ("either heading or test.pkg must be set" )
911
904
}
912
905
heading = test .pkg
913
906
}
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 ) {
926
909
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
928
917
})
929
918
}
930
919
0 commit comments