Skip to content

Commit 26ca1b5

Browse files
committedMar 12, 2023
Support -coverpkg=./...
1 parent c16f0b0 commit 26ca1b5

File tree

7 files changed

+73
-9
lines changed

7 files changed

+73
-9
lines changed
 

Diff for: ‎ginkgo/internal/compile.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@ func CompileSuite(suite TestSuite, goFlagsConfig types.GoFlagsConfig) TestSuite
2525
return suite
2626
}
2727

28-
args, err := types.GenerateGoTestCompileArgs(goFlagsConfig, path, "./")
28+
ginkgoInvocationPath, _ := os.Getwd()
29+
ginkgoInvocationPath, _ = filepath.Abs(ginkgoInvocationPath)
30+
packagePath := suite.AbsPath()
31+
pathToInvocationPath, err := filepath.Rel(packagePath, ginkgoInvocationPath)
32+
if err != nil {
33+
suite.State = TestSuiteStateFailedToCompile
34+
suite.CompilationError = fmt.Errorf("Failed to get relative path from package to the current working directory:\n%s", err.Error())
35+
return suite
36+
}
37+
args, err := types.GenerateGoTestCompileArgs(goFlagsConfig, path, "./", pathToInvocationPath)
2938
if err != nil {
3039
suite.State = TestSuiteStateFailedToCompile
3140
suite.CompilationError = fmt.Errorf("Failed to generate go test compile flags:\n%s", err.Error())

Diff for: ‎go.sum

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV
99
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
1010
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
1111
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
12-
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
13-
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
12+
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
1413
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
1514
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1615
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
1716
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1817
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
1918
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
2019
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
21-
github.com/onsi/gomega v1.27.2 h1:SKU0CXeKE/WVgIV1T61kSa3+IRE8Ekrv9rdXDwwTqnY=
22-
github.com/onsi/gomega v1.27.2/go.mod h1:5mR3phAHpkAVIDkHEUBY6HGVsU+cpcEscrGPB4oPlZI=
2320
github.com/onsi/gomega v1.27.3 h1:5VwIwnBY3vbBDOJrNtA4rVdiTZCsq9B5F12pvy1Drmk=
2421
github.com/onsi/gomega v1.27.3/go.mod h1:5vG284IBtfDAmDyrK+eGyZmUgUlmi+Wngqo557cZ6Gw=
2522
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package additional_spec_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
7+
. "github.com/onsi/ginkgo/v2/integration/_fixtures/coverage_fixture"
8+
. "github.com/onsi/ginkgo/v2/integration/_fixtures/coverage_fixture/external_coverage"
9+
10+
"testing"
11+
)
12+
13+
func TestAdditionalSpecSuite(t *testing.T) {
14+
RegisterFailHandler(Fail)
15+
RunSpecs(t, "AdditionalSpec Suite")
16+
}
17+
18+
var _ = Describe("CoverageFixture", func() {
19+
It("should test E", func() {
20+
Ω(E()).Should(Equal("tested by additional"))
21+
})
22+
23+
It("should test external package", func() {
24+
Ω(TestedByAdditional()).Should(Equal("tested by additional"))
25+
})
26+
})

Diff for: ‎integration/_fixtures/coverage_fixture/coverage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ func D() string {
2121
}
2222

2323
func E() string {
24-
return "untested"
24+
return "tested by additional"
2525
}

Diff for: ‎integration/_fixtures/coverage_fixture/external_coverage/external_coverage.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ func Tested() string {
44
return "tested"
55
}
66

7-
func Untested() string {
8-
return "untested"
7+
func TestedByAdditional() string {
8+
return "tested by additional"
99
}

Diff for: ‎integration/profiling_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ var _ = Describe("Profiling Specs", func() {
116116

117117
Ω(parallelCoverage).Should(Equal(seriesCoverage))
118118
})
119+
120+
It("supports ./...", func() {
121+
seriesSession := startGinkgo(fm.PathTo("coverage"), "-coverpkg=./...", "-r")
122+
Eventually(seriesSession).Should(gexec.Exit(0))
123+
Ω(seriesSession.Out).Should(gbytes.Say(`composite coverage: 100\.0% of statements`))
124+
seriesCoverage := processCoverageProfile(fm.PathTo("coverage", "coverprofile.out"))
125+
fm.RemoveFile("coverage", "coverprofile.out")
126+
127+
parallelSession := startGinkgo(fm.PathTo("coverage"), "--no-color", "--procs=2", "-coverpkg=./...", "-r")
128+
Eventually(parallelSession).Should(gexec.Exit(0))
129+
Ω(parallelSession.Out).Should(gbytes.Say(`composite coverage: 100\.0% of statements`))
130+
parallelCoverage := processCoverageProfile(fm.PathTo("coverage", "coverprofile.out"))
131+
132+
Ω(parallelCoverage).Should(Equal(seriesCoverage))
133+
})
119134
})
120135

121136
Context("with a custom profile name", func() {

Diff for: ‎types/config.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package types
88
import (
99
"flag"
1010
"os"
11+
"path/filepath"
1112
"runtime"
1213
"strconv"
1314
"strings"
@@ -600,13 +601,29 @@ func VetAndInitializeCLIAndGoConfig(cliConfig CLIConfig, goFlagsConfig GoFlagsCo
600601
}
601602

602603
// GenerateGoTestCompileArgs is used by the Ginkgo CLI to generate command line arguments to pass to the go test -c command when compiling the test
603-
func GenerateGoTestCompileArgs(goFlagsConfig GoFlagsConfig, destination string, packageToBuild string) ([]string, error) {
604+
func GenerateGoTestCompileArgs(goFlagsConfig GoFlagsConfig, destination string, packageToBuild string, pathToInvocationPath string) ([]string, error) {
604605
// if the user has set the CoverProfile run-time flag make sure to set the build-time cover flag to make sure
605606
// the built test binary can generate a coverprofile
606607
if goFlagsConfig.CoverProfile != "" {
607608
goFlagsConfig.Cover = true
608609
}
609610

611+
if goFlagsConfig.CoverPkg != "" {
612+
coverPkgs := strings.Split(goFlagsConfig.CoverPkg, ",")
613+
adjustedCoverPkgs := make([]string, len(coverPkgs))
614+
for i, coverPkg := range coverPkgs {
615+
coverPkg = strings.Trim(coverPkg, " ")
616+
if strings.HasPrefix(coverPkg, "./") {
617+
// this is a relative coverPkg - we need to reroot it
618+
adjustedCoverPkgs[i] = "./" + filepath.Join(pathToInvocationPath, strings.TrimPrefix(coverPkg, "./"))
619+
} else {
620+
// this is a package name - don't touch it
621+
adjustedCoverPkgs[i] = coverPkg
622+
}
623+
}
624+
goFlagsConfig.CoverPkg = strings.Join(adjustedCoverPkgs, ",")
625+
}
626+
610627
args := []string{"test", "-c", "-o", destination, packageToBuild}
611628
goArgs, err := GenerateFlagArgs(
612629
GoBuildFlags,

0 commit comments

Comments
 (0)
Please sign in to comment.