@@ -773,29 +773,6 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
773
773
}
774
774
testBinary := elem + ".test"
775
775
776
- // The ptest package needs to be importable under the
777
- // same import path that p has, but we cannot put it in
778
- // the usual place in the temporary tree, because then
779
- // other tests will see it as the real package.
780
- // Instead we make a _test directory under the import path
781
- // and then repeat the import path there. We tell the
782
- // compiler and linker to look in that _test directory first.
783
- //
784
- // That is, if the package under test is unicode/utf8,
785
- // then the normal place to write the package archive is
786
- // $WORK/unicode/utf8.a, but we write the test package archive to
787
- // $WORK/unicode/utf8/_test/unicode/utf8.a.
788
- // We write the external test package archive to
789
- // $WORK/unicode/utf8/_test/unicode/utf8_test.a.
790
- testDir := filepath .Join (b .WorkDir , filepath .FromSlash (p .ImportPath + "/_test" ))
791
- ptestObj := work .BuildToolchain .Pkgpath (testDir , p )
792
-
793
- // Create the directory for the .a files.
794
- ptestDir , _ := filepath .Split (ptestObj )
795
- if err := b .Mkdir (ptestDir ); err != nil {
796
- return nil , nil , nil , err
797
- }
798
-
799
776
// Should we apply coverage analysis locally,
800
777
// only for this package and only for this test?
801
778
// Yes, if -cover is on but -coverpkg has not specified
@@ -812,7 +789,6 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
812
789
ptest .Internal .Target = ""
813
790
ptest .Imports = str .StringList (p .Imports , p .TestImports )
814
791
ptest .Internal .Imports = append (append ([]* load.Package {}, p .Internal .Imports ... ), imports ... )
815
- ptest .Internal .Pkgdir = testDir
816
792
ptest .Internal .Fake = true
817
793
ptest .Internal .ForceLibrary = true
818
794
ptest .Stale = true
@@ -857,7 +833,6 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
857
833
ImportPos : p .Internal .Build .XTestImportPos ,
858
834
},
859
835
Imports : ximports ,
860
- Pkgdir : testDir ,
861
836
Fake : true ,
862
837
External : true ,
863
838
},
@@ -867,19 +842,23 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
867
842
}
868
843
}
869
844
845
+ testDir := b .NewObjdir ()
846
+ if err := b .Mkdir (testDir ); err != nil {
847
+ return nil , nil , nil , err
848
+ }
849
+
870
850
// Action for building pkg.test.
871
851
pmain = & load.Package {
872
852
PackagePublic : load.PackagePublic {
873
853
Name : "main" ,
874
854
Dir : testDir ,
875
855
GoFiles : []string {"_testmain.go" },
876
- ImportPath : " testmain" ,
856
+ ImportPath : p . ImportPath + " ( testmain) " ,
877
857
Root : p .Root ,
878
858
Stale : true ,
879
859
},
880
860
Internal : load.PackageInternal {
881
861
Build : & build.Package {Name : "main" },
882
- Pkgdir : testDir ,
883
862
Fake : true ,
884
863
OmitDebug : ! testC && ! testNeedBinary ,
885
864
},
@@ -942,18 +921,21 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
942
921
943
922
if ptest != p && localCover {
944
923
// We have made modifications to the package p being tested
945
- // and are rebuilding p (as ptest), writing it to the testDir tree .
946
- // Arrange to rebuild, writing to that same tree, all packages q
947
- // such that the test depends on q, and q depends on p.
924
+ // and are rebuilding p (as ptest).
925
+ // Arrange to rebuild all packages q such that
926
+ // the test depends on q and q depends on p.
948
927
// This makes sure that q sees the modifications to p.
949
928
// Strictly speaking, the rebuild is only necessary if the
950
929
// modifications to p change its export metadata, but
951
930
// determining that is a bit tricky, so we rebuild always.
931
+ // TODO(rsc): Once we get export metadata changes
932
+ // handled properly, look into the expense of dropping
933
+ // "&& localCover" above.
952
934
//
953
935
// This will cause extra compilation, so for now we only do it
954
936
// when testCover is set. The conditions are more general, though,
955
937
// and we may find that we need to do it always in the future.
956
- recompileForTest (pmain , p , ptest , testDir )
938
+ recompileForTest (pmain , p , ptest )
957
939
}
958
940
959
941
t .NeedCgo = forceCgo
@@ -965,9 +947,9 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
965
947
}
966
948
967
949
if ! cfg .BuildN {
968
- // writeTestmain writes _testmain.go. This must happen after recompileForTest ,
969
- // because recompileForTest modifies XXX .
970
- if err := writeTestmain (filepath . Join ( testDir , "_testmain.go" ) , t ); err != nil {
950
+ // writeTestmain writes _testmain.go,
951
+ // using the test description gathered in t .
952
+ if err := writeTestmain (testDir + "_testmain.go" , t ); err != nil {
971
953
return nil , nil , nil , err
972
954
}
973
955
}
@@ -976,23 +958,11 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
976
958
977
959
if ptest != p {
978
960
a := b .Action (work .ModeBuild , work .ModeBuild , ptest )
979
- a .Objdir = testDir + string (filepath .Separator ) + "_obj_test" + string (filepath .Separator )
980
- a .Objpkg = ptestObj
981
- a .Target = ptestObj
982
961
a .Link = false
983
962
}
984
963
985
- if pxtest != nil {
986
- a := b .Action (work .ModeBuild , work .ModeBuild , pxtest )
987
- a .Objdir = testDir + string (filepath .Separator ) + "_obj_xtest" + string (filepath .Separator )
988
- a .Objpkg = work .BuildToolchain .Pkgpath (testDir , pxtest )
989
- a .Target = a .Objpkg
990
- }
991
-
992
964
a := b .Action (work .ModeBuild , work .ModeBuild , pmain )
993
- a .Objdir = testDir + string (filepath .Separator )
994
- a .Objpkg = filepath .Join (testDir , "main.a" )
995
- a .Target = filepath .Join (testDir , testBinary ) + cfg .ExeSuffix
965
+ a .Target = testDir + testBinary + cfg .ExeSuffix
996
966
if cfg .Goos == "windows" {
997
967
// There are many reserved words on Windows that,
998
968
// if used in the name of an executable, cause Windows
@@ -1018,7 +988,7 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
1018
988
// we could just do this always on Windows.
1019
989
for _ , bad := range windowsBadWords {
1020
990
if strings .Contains (testBinary , bad ) {
1021
- a .Target = filepath . Join ( testDir , "test.test" ) + cfg .ExeSuffix
991
+ a .Target = testDir + "test.test" + cfg .ExeSuffix
1022
992
break
1023
993
}
1024
994
}
@@ -1056,6 +1026,7 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
1056
1026
Func : builderCleanTest ,
1057
1027
Deps : []* work.Action {runAction },
1058
1028
Package : p ,
1029
+ Objdir : testDir ,
1059
1030
}
1060
1031
printAction = & work.Action {
1061
1032
Func : builderPrintTest ,
@@ -1085,7 +1056,7 @@ Search:
1085
1056
return stk
1086
1057
}
1087
1058
1088
- func recompileForTest (pmain , preal , ptest * load.Package , testDir string ) {
1059
+ func recompileForTest (pmain , preal , ptest * load.Package ) {
1089
1060
// The "test copy" of preal is ptest.
1090
1061
// For each package that depends on preal, make a "test copy"
1091
1062
// that depends on ptest. And so on, up the dependency tree.
@@ -1098,19 +1069,19 @@ func recompileForTest(pmain, preal, ptest *load.Package, testDir string) {
1098
1069
return
1099
1070
}
1100
1071
didSplit = true
1101
- if p .Internal .Pkgdir != testDir {
1102
- p1 := new (load.Package )
1103
- testCopy [p ] = p1
1104
- * p1 = * p
1105
- p1 .Internal .Imports = make ([]* load.Package , len (p .Internal .Imports ))
1106
- copy (p1 .Internal .Imports , p .Internal .Imports )
1107
- p = p1
1108
- p .Internal .Pkgdir = testDir
1109
- p .Internal .Target = ""
1110
- p .Internal .Fake = true
1111
- p .Stale = true
1112
- p .StaleReason = "depends on package being tested"
1072
+ if testCopy [p ] != nil {
1073
+ panic ("recompileForTest loop" )
1113
1074
}
1075
+ p1 := new (load.Package )
1076
+ testCopy [p ] = p1
1077
+ * p1 = * p
1078
+ p1 .Internal .Imports = make ([]* load.Package , len (p .Internal .Imports ))
1079
+ copy (p1 .Internal .Imports , p .Internal .Imports )
1080
+ p = p1
1081
+ p .Internal .Target = ""
1082
+ p .Internal .Fake = true
1083
+ p .Stale = true
1084
+ p .StaleReason = "depends on package being tested"
1114
1085
}
1115
1086
1116
1087
// Update p.Internal.Imports to use test copies.
@@ -1288,9 +1259,10 @@ func builderCleanTest(b *work.Builder, a *work.Action) error {
1288
1259
if cfg .BuildWork {
1289
1260
return nil
1290
1261
}
1291
- run := a .Deps [0 ]
1292
- testDir := filepath .Join (b .WorkDir , filepath .FromSlash (run .Package .ImportPath + "/_test" ))
1293
- os .RemoveAll (testDir )
1262
+ if cfg .BuildX {
1263
+ b .Showcmd ("" , "rm -r %s" , a .Objdir )
1264
+ }
1265
+ os .RemoveAll (a .Objdir )
1294
1266
return nil
1295
1267
}
1296
1268
0 commit comments