@@ -773,29 +773,6 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
773773 }
774774 testBinary := elem + ".test"
775775
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-
799776 // Should we apply coverage analysis locally,
800777 // only for this package and only for this test?
801778 // 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
812789 ptest .Internal .Target = ""
813790 ptest .Imports = str .StringList (p .Imports , p .TestImports )
814791 ptest .Internal .Imports = append (append ([]* load.Package {}, p .Internal .Imports ... ), imports ... )
815- ptest .Internal .Pkgdir = testDir
816792 ptest .Internal .Fake = true
817793 ptest .Internal .ForceLibrary = true
818794 ptest .Stale = true
@@ -857,7 +833,6 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
857833 ImportPos : p .Internal .Build .XTestImportPos ,
858834 },
859835 Imports : ximports ,
860- Pkgdir : testDir ,
861836 Fake : true ,
862837 External : true ,
863838 },
@@ -867,19 +842,23 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
867842 }
868843 }
869844
845+ testDir := b .NewObjdir ()
846+ if err := b .Mkdir (testDir ); err != nil {
847+ return nil , nil , nil , err
848+ }
849+
870850 // Action for building pkg.test.
871851 pmain = & load.Package {
872852 PackagePublic : load.PackagePublic {
873853 Name : "main" ,
874854 Dir : testDir ,
875855 GoFiles : []string {"_testmain.go" },
876- ImportPath : " testmain" ,
856+ ImportPath : p . ImportPath + " ( testmain) " ,
877857 Root : p .Root ,
878858 Stale : true ,
879859 },
880860 Internal : load.PackageInternal {
881861 Build : & build.Package {Name : "main" },
882- Pkgdir : testDir ,
883862 Fake : true ,
884863 OmitDebug : ! testC && ! testNeedBinary ,
885864 },
@@ -942,18 +921,21 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
942921
943922 if ptest != p && localCover {
944923 // 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.
948927 // This makes sure that q sees the modifications to p.
949928 // Strictly speaking, the rebuild is only necessary if the
950929 // modifications to p change its export metadata, but
951930 // 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.
952934 //
953935 // This will cause extra compilation, so for now we only do it
954936 // when testCover is set. The conditions are more general, though,
955937 // 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 )
957939 }
958940
959941 t .NeedCgo = forceCgo
@@ -965,9 +947,9 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
965947 }
966948
967949 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 {
971953 return nil , nil , nil , err
972954 }
973955 }
@@ -976,23 +958,11 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
976958
977959 if ptest != p {
978960 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
982961 a .Link = false
983962 }
984963
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-
992964 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
996966 if cfg .Goos == "windows" {
997967 // There are many reserved words on Windows that,
998968 // 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
1018988 // we could just do this always on Windows.
1019989 for _ , bad := range windowsBadWords {
1020990 if strings .Contains (testBinary , bad ) {
1021- a .Target = filepath . Join ( testDir , "test.test" ) + cfg .ExeSuffix
991+ a .Target = testDir + "test.test" + cfg .ExeSuffix
1022992 break
1023993 }
1024994 }
@@ -1056,6 +1026,7 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
10561026 Func : builderCleanTest ,
10571027 Deps : []* work.Action {runAction },
10581028 Package : p ,
1029+ Objdir : testDir ,
10591030 }
10601031 printAction = & work.Action {
10611032 Func : builderPrintTest ,
@@ -1085,7 +1056,7 @@ Search:
10851056 return stk
10861057}
10871058
1088- func recompileForTest (pmain , preal , ptest * load.Package , testDir string ) {
1059+ func recompileForTest (pmain , preal , ptest * load.Package ) {
10891060 // The "test copy" of preal is ptest.
10901061 // For each package that depends on preal, make a "test copy"
10911062 // that depends on ptest. And so on, up the dependency tree.
@@ -1098,19 +1069,19 @@ func recompileForTest(pmain, preal, ptest *load.Package, testDir string) {
10981069 return
10991070 }
11001071 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" )
11131074 }
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"
11141085 }
11151086
11161087 // Update p.Internal.Imports to use test copies.
@@ -1288,9 +1259,10 @@ func builderCleanTest(b *work.Builder, a *work.Action) error {
12881259 if cfg .BuildWork {
12891260 return nil
12901261 }
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 )
12941266 return nil
12951267}
12961268
0 commit comments