diff --git a/ginkgo/internal/compile.go b/ginkgo/internal/compile.go index 496ec4a28..86da7340d 100644 --- a/ginkgo/internal/compile.go +++ b/ginkgo/internal/compile.go @@ -25,7 +25,16 @@ func CompileSuite(suite TestSuite, goFlagsConfig types.GoFlagsConfig) TestSuite return suite } - args, err := types.GenerateGoTestCompileArgs(goFlagsConfig, path, "./") + ginkgoInvocationPath, _ := os.Getwd() + ginkgoInvocationPath, _ = filepath.Abs(ginkgoInvocationPath) + packagePath := suite.AbsPath() + pathToInvocationPath, err := filepath.Rel(packagePath, ginkgoInvocationPath) + if err != nil { + suite.State = TestSuiteStateFailedToCompile + suite.CompilationError = fmt.Errorf("Failed to get relative path from package to the current working directory:\n%s", err.Error()) + return suite + } + args, err := types.GenerateGoTestCompileArgs(goFlagsConfig, path, "./", pathToInvocationPath) if err != nil { suite.State = TestSuiteStateFailedToCompile suite.CompilationError = fmt.Errorf("Failed to generate go test compile flags:\n%s", err.Error()) diff --git a/go.sum b/go.sum index 14a056b7e..7986352ab 100644 --- a/go.sum +++ b/go.sum @@ -9,8 +9,7 @@ github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= @@ -18,8 +17,6 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/onsi/gomega v1.27.2 h1:SKU0CXeKE/WVgIV1T61kSa3+IRE8Ekrv9rdXDwwTqnY= -github.com/onsi/gomega v1.27.2/go.mod h1:5mR3phAHpkAVIDkHEUBY6HGVsU+cpcEscrGPB4oPlZI= github.com/onsi/gomega v1.27.3 h1:5VwIwnBY3vbBDOJrNtA4rVdiTZCsq9B5F12pvy1Drmk= github.com/onsi/gomega v1.27.3/go.mod h1:5vG284IBtfDAmDyrK+eGyZmUgUlmi+Wngqo557cZ6Gw= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/integration/_fixtures/coverage_fixture/additional_spec/additional_spec_suite_test.go b/integration/_fixtures/coverage_fixture/additional_spec/additional_spec_suite_test.go new file mode 100644 index 000000000..04afa3241 --- /dev/null +++ b/integration/_fixtures/coverage_fixture/additional_spec/additional_spec_suite_test.go @@ -0,0 +1,26 @@ +package additional_spec_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + . "github.com/onsi/ginkgo/v2/integration/_fixtures/coverage_fixture" + . "github.com/onsi/ginkgo/v2/integration/_fixtures/coverage_fixture/external_coverage" + + "testing" +) + +func TestAdditionalSpecSuite(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "AdditionalSpec Suite") +} + +var _ = Describe("CoverageFixture", func() { + It("should test E", func() { + Ω(E()).Should(Equal("tested by additional")) + }) + + It("should test external package", func() { + Ω(TestedByAdditional()).Should(Equal("tested by additional")) + }) +}) diff --git a/integration/_fixtures/coverage_fixture/coverage.go b/integration/_fixtures/coverage_fixture/coverage.go index c8ce0ac1f..85236ae45 100644 --- a/integration/_fixtures/coverage_fixture/coverage.go +++ b/integration/_fixtures/coverage_fixture/coverage.go @@ -21,5 +21,5 @@ func D() string { } func E() string { - return "untested" + return "tested by additional" } diff --git a/integration/_fixtures/coverage_fixture/external_coverage/external_coverage.go b/integration/_fixtures/coverage_fixture/external_coverage/external_coverage.go index 5280d4ddf..9d285a267 100644 --- a/integration/_fixtures/coverage_fixture/external_coverage/external_coverage.go +++ b/integration/_fixtures/coverage_fixture/external_coverage/external_coverage.go @@ -4,6 +4,6 @@ func Tested() string { return "tested" } -func Untested() string { - return "untested" +func TestedByAdditional() string { + return "tested by additional" } diff --git a/integration/profiling_test.go b/integration/profiling_test.go index 25040fc5c..97730d66c 100644 --- a/integration/profiling_test.go +++ b/integration/profiling_test.go @@ -116,6 +116,21 @@ var _ = Describe("Profiling Specs", func() { Ω(parallelCoverage).Should(Equal(seriesCoverage)) }) + + It("supports ./...", func() { + seriesSession := startGinkgo(fm.PathTo("coverage"), "-coverpkg=./...", "-r") + Eventually(seriesSession).Should(gexec.Exit(0)) + Ω(seriesSession.Out).Should(gbytes.Say(`composite coverage: 100\.0% of statements`)) + seriesCoverage := processCoverageProfile(fm.PathTo("coverage", "coverprofile.out")) + fm.RemoveFile("coverage", "coverprofile.out") + + parallelSession := startGinkgo(fm.PathTo("coverage"), "--no-color", "--procs=2", "-coverpkg=./...", "-r") + Eventually(parallelSession).Should(gexec.Exit(0)) + Ω(parallelSession.Out).Should(gbytes.Say(`composite coverage: 100\.0% of statements`)) + parallelCoverage := processCoverageProfile(fm.PathTo("coverage", "coverprofile.out")) + + Ω(parallelCoverage).Should(Equal(seriesCoverage)) + }) }) Context("with a custom profile name", func() { diff --git a/types/config.go b/types/config.go index 1efd77d39..1014c7b49 100644 --- a/types/config.go +++ b/types/config.go @@ -8,6 +8,7 @@ package types import ( "flag" "os" + "path/filepath" "runtime" "strconv" "strings" @@ -600,13 +601,29 @@ func VetAndInitializeCLIAndGoConfig(cliConfig CLIConfig, goFlagsConfig GoFlagsCo } // GenerateGoTestCompileArgs is used by the Ginkgo CLI to generate command line arguments to pass to the go test -c command when compiling the test -func GenerateGoTestCompileArgs(goFlagsConfig GoFlagsConfig, destination string, packageToBuild string) ([]string, error) { +func GenerateGoTestCompileArgs(goFlagsConfig GoFlagsConfig, destination string, packageToBuild string, pathToInvocationPath string) ([]string, error) { // if the user has set the CoverProfile run-time flag make sure to set the build-time cover flag to make sure // the built test binary can generate a coverprofile if goFlagsConfig.CoverProfile != "" { goFlagsConfig.Cover = true } + if goFlagsConfig.CoverPkg != "" { + coverPkgs := strings.Split(goFlagsConfig.CoverPkg, ",") + adjustedCoverPkgs := make([]string, len(coverPkgs)) + for i, coverPkg := range coverPkgs { + coverPkg = strings.Trim(coverPkg, " ") + if strings.HasPrefix(coverPkg, "./") { + // this is a relative coverPkg - we need to reroot it + adjustedCoverPkgs[i] = "./" + filepath.Join(pathToInvocationPath, strings.TrimPrefix(coverPkg, "./")) + } else { + // this is a package name - don't touch it + adjustedCoverPkgs[i] = coverPkg + } + } + goFlagsConfig.CoverPkg = strings.Join(adjustedCoverPkgs, ",") + } + args := []string{"test", "-c", "-o", destination, packageToBuild} goArgs, err := GenerateFlagArgs( GoBuildFlags,