@@ -777,56 +777,12 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
777
777
// pmain - pkg.test binary
778
778
var ptest , pxtest , pmain * load.Package
779
779
780
- var imports , ximports []* load.Package
781
- var stk load.ImportStack
782
- stk .Push (p .ImportPath + " (test)" )
783
- rawTestImports := str .StringList (p .TestImports )
784
- for i , path := range p .TestImports {
785
- p1 := load .LoadImport (path , p .Dir , p , & stk , p .Internal .Build .TestImportPos [path ], load .UseVendor )
786
- if p1 .Error != nil {
787
- return nil , nil , nil , p1 .Error
788
- }
789
- if len (p1 .DepsErrors ) > 0 {
790
- err := p1 .DepsErrors [0 ]
791
- err .Pos = "" // show full import stack
792
- return nil , nil , nil , err
793
- }
794
- if str .Contains (p1 .Deps , p .ImportPath ) || p1 .ImportPath == p .ImportPath {
795
- // Same error that loadPackage returns (via reusePackage) in pkg.go.
796
- // Can't change that code, because that code is only for loading the
797
- // non-test copy of a package.
798
- err := & load.PackageError {
799
- ImportStack : testImportStack (stk [0 ], p1 , p .ImportPath ),
800
- Err : "import cycle not allowed in test" ,
801
- IsImportCycle : true ,
802
- }
803
- return nil , nil , nil , err
804
- }
805
- p .TestImports [i ] = p1 .ImportPath
806
- imports = append (imports , p1 )
807
- }
808
- stk .Pop ()
809
- stk .Push (p .ImportPath + "_test" )
810
- pxtestNeedsPtest := false
811
- rawXTestImports := str .StringList (p .XTestImports )
812
- for i , path := range p .XTestImports {
813
- p1 := load .LoadImport (path , p .Dir , p , & stk , p .Internal .Build .XTestImportPos [path ], load .UseVendor )
814
- if p1 .Error != nil {
815
- return nil , nil , nil , p1 .Error
816
- }
817
- if len (p1 .DepsErrors ) > 0 {
818
- err := p1 .DepsErrors [0 ]
819
- err .Pos = "" // show full import stack
820
- return nil , nil , nil , err
821
- }
822
- if p1 .ImportPath == p .ImportPath {
823
- pxtestNeedsPtest = true
824
- } else {
825
- ximports = append (ximports , p1 )
826
- }
827
- p .XTestImports [i ] = p1 .ImportPath
780
+ localCover := testCover && testCoverPaths == nil
781
+
782
+ ptest , pxtest , err = load .TestPackagesFor (p , localCover || p .Name == "main" )
783
+ if err != nil {
784
+ return nil , nil , nil , err
828
785
}
829
- stk .Pop ()
830
786
831
787
// Use last element of import path, not package name.
832
788
// They differ when package name is "main".
@@ -844,81 +800,12 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
844
800
// only for this package and only for this test?
845
801
// Yes, if -cover is on but -coverpkg has not specified
846
802
// a list of packages for global coverage.
847
- localCover := testCover && testCoverPaths == nil
848
-
849
- // Test package.
850
- if len (p .TestGoFiles ) > 0 || localCover || p .Name == "main" {
851
- ptest = new (load.Package )
852
- * ptest = * p
853
- ptest .GoFiles = nil
854
- ptest .GoFiles = append (ptest .GoFiles , p .GoFiles ... )
855
- ptest .GoFiles = append (ptest .GoFiles , p .TestGoFiles ... )
856
- ptest .Target = ""
857
- // Note: The preparation of the vet config requires that common
858
- // indexes in ptest.Imports, ptest.Internal.Imports, and ptest.Internal.RawImports
859
- // all line up (but RawImports can be shorter than the others).
860
- // That is, for 0 ≤ i < len(RawImports),
861
- // RawImports[i] is the import string in the program text,
862
- // Imports[i] is the expanded import string (vendoring applied or relative path expanded away),
863
- // and Internal.Imports[i] is the corresponding *Package.
864
- // Any implicitly added imports appear in Imports and Internal.Imports
865
- // but not RawImports (because they were not in the source code).
866
- // We insert TestImports, imports, and rawTestImports at the start of
867
- // these lists to preserve the alignment.
868
- ptest .Imports = str .StringList (p .TestImports , p .Imports )
869
- ptest .Internal .Imports = append (imports , p .Internal .Imports ... )
870
- ptest .Internal .RawImports = str .StringList (rawTestImports , p .Internal .RawImports )
871
- ptest .Internal .ForceLibrary = true
872
- ptest .Internal .Build = new (build.Package )
873
- * ptest .Internal .Build = * p .Internal .Build
874
- m := map [string ][]token.Position {}
875
- for k , v := range p .Internal .Build .ImportPos {
876
- m [k ] = append (m [k ], v ... )
877
- }
878
- for k , v := range p .Internal .Build .TestImportPos {
879
- m [k ] = append (m [k ], v ... )
880
- }
881
- ptest .Internal .Build .ImportPos = m
882
-
883
- if localCover {
884
- ptest .Internal .CoverMode = testCoverMode
885
- var coverFiles []string
886
- coverFiles = append (coverFiles , ptest .GoFiles ... )
887
- coverFiles = append (coverFiles , ptest .CgoFiles ... )
888
- ptest .Internal .CoverVars = declareCoverVars (ptest .ImportPath , coverFiles ... )
889
- }
890
- } else {
891
- ptest = p
892
- }
893
-
894
- // External test package.
895
- if len (p .XTestGoFiles ) > 0 {
896
- pxtest = & load.Package {
897
- PackagePublic : load.PackagePublic {
898
- Name : p .Name + "_test" ,
899
- ImportPath : p .ImportPath + "_test" ,
900
- Root : p .Root ,
901
- Dir : p .Dir ,
902
- GoFiles : p .XTestGoFiles ,
903
- Imports : p .XTestImports ,
904
- },
905
- Internal : load.PackageInternal {
906
- LocalPrefix : p .Internal .LocalPrefix ,
907
- Build : & build.Package {
908
- ImportPos : p .Internal .Build .XTestImportPos ,
909
- },
910
- Imports : ximports ,
911
- RawImports : rawXTestImports ,
912
-
913
- Asmflags : p .Internal .Asmflags ,
914
- Gcflags : p .Internal .Gcflags ,
915
- Ldflags : p .Internal .Ldflags ,
916
- Gccgoflags : p .Internal .Gccgoflags ,
917
- },
918
- }
919
- if pxtestNeedsPtest {
920
- pxtest .Internal .Imports = append (pxtest .Internal .Imports , ptest )
921
- }
803
+ if localCover {
804
+ ptest .Internal .CoverMode = testCoverMode
805
+ var coverFiles []string
806
+ coverFiles = append (coverFiles , ptest .GoFiles ... )
807
+ coverFiles = append (coverFiles , ptest .CgoFiles ... )
808
+ ptest .Internal .CoverVars = declareCoverVars (ptest .ImportPath , coverFiles ... )
922
809
}
923
810
924
811
testDir := b .NewObjdir ()
@@ -948,6 +835,7 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
948
835
949
836
// The generated main also imports testing, regexp, and os.
950
837
// Also the linker introduces implicit dependencies reported by LinkerDeps.
838
+ var stk load.ImportStack
951
839
stk .Push ("testmain" )
952
840
deps := testMainDeps // cap==len, so safe for append
953
841
for _ , d := range load .LinkerDeps (p ) {
@@ -1150,24 +1038,6 @@ func addTestVet(b *work.Builder, p *load.Package, runAction, installAction *work
1150
1038
}
1151
1039
}
1152
1040
1153
- func testImportStack (top string , p * load.Package , target string ) []string {
1154
- stk := []string {top , p .ImportPath }
1155
- Search:
1156
- for p .ImportPath != target {
1157
- for _ , p1 := range p .Internal .Imports {
1158
- if p1 .ImportPath == target || str .Contains (p1 .Deps , target ) {
1159
- stk = append (stk , p1 .ImportPath )
1160
- p = p1
1161
- continue Search
1162
- }
1163
- }
1164
- // Can't happen, but in case it does...
1165
- stk = append (stk , "<lost path to cycle>" )
1166
- break
1167
- }
1168
- return stk
1169
- }
1170
-
1171
1041
func recompileForTest (pmain , preal , ptest , pxtest * load.Package ) {
1172
1042
// The "test copy" of preal is ptest.
1173
1043
// For each package that depends on preal, make a "test copy"
0 commit comments