Skip to content

Commit

Permalink
Support -coverpkg=./...
Browse files Browse the repository at this point in the history
  • Loading branch information
onsi committed Mar 12, 2023
1 parent c16f0b0 commit 26ca1b5
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 9 deletions.
11 changes: 10 additions & 1 deletion ginkgo/internal/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
5 changes: 1 addition & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ 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=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
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=
Expand Down
Original file line number Diff line number Diff line change
@@ -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"))
})
})
2 changes: 1 addition & 1 deletion integration/_fixtures/coverage_fixture/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func D() string {
}

func E() string {
return "untested"
return "tested by additional"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ func Tested() string {
return "tested"
}

func Untested() string {
return "untested"
func TestedByAdditional() string {
return "tested by additional"
}
15 changes: 15 additions & 0 deletions integration/profiling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
19 changes: 18 additions & 1 deletion types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package types
import (
"flag"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 26ca1b5

Please sign in to comment.