Skip to content

Commit 43392d5

Browse files
Alexey SoshinOnsi Fakhouri
authored andcommitted
Handle -coverprofile flag (#355)
* Handle -coverprofile flag Fixes #346 * Add tool for easier contribution * Cast byte[], otherwise failure will be displayed as array of bytes, not very useful * Fix combine not finding files if coverProfile was set * Add test for recursive coverage * Fix test imports to point to correct subpackages * Add test for cover profile in parallel mode
1 parent 9eda700 commit 43392d5

File tree

12 files changed

+247
-8
lines changed

12 files changed

+247
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
TODO
33
tmp/**/*
44
*.coverprofile
5-
.vscode
5+
.vscode
6+
.idea/

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,9 @@ Go explore!
115115
## License
116116

117117
Ginkgo is MIT-Licensed
118+
119+
## Contributing
120+
121+
Since Ginkgo tests also internal packages, when you fork, you'll have to replace imports with your repository.<br />
122+
Use `before_pr.sh` for that<br />
123+
After you finished your changes and before you push your pull request, use `after_pr.sh` to revert those changes

before_pr.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Take current path
2+
path=$(pwd)
3+
4+
# Split it
5+
IFS='\/'; arrIN=($path); unset IFS;
6+
7+
# Find directory before ginkgo
8+
len=${#arrIN[@]}
9+
10+
userDir=${arrIN[$len-2]}
11+
12+
# Replace onsi with userdir
13+
find . -type f -name '*.go' -exec sed -i '' s/github.com\\/onsi\\/ginkgo\\/internal/github.com\\/$userDir\\/ginkgo\\/internal/ {} +

ginkgo/testrunner/test_runner.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,30 @@ func (t *TestRunner) runParallelGinkgoSuite() RunResult {
387387
return res
388388
}
389389

390+
const CoverProfileSuffix = ".coverprofile"
391+
390392
func (t *TestRunner) cmd(ginkgoArgs []string, stream io.Writer, node int) *exec.Cmd {
391393
args := []string{"--test.timeout=" + t.timeout.String()}
392-
if *t.goOpts["cover"].(*bool) || *t.goOpts["coverpkg"].(*string) != "" || *t.goOpts["covermode"].(*string) != "" {
393-
coverprofile := "--test.coverprofile=" + t.Suite.PackageName + ".coverprofile"
394+
395+
coverMode := *t.goOpts["covermode"].(*string)
396+
coverPackage := *t.goOpts["coverpkg"].(*string)
397+
cover := *t.goOpts["cover"].(*bool)
398+
coverProfile := *t.goOpts["coverprofile"].(*string)
399+
400+
if cover || coverPackage != "" || coverMode != "" {
401+
testCoverProfile := "--test.coverprofile="
402+
403+
// Set default name for coverage results
404+
if coverProfile == "" {
405+
testCoverProfile += t.Suite.PackageName + CoverProfileSuffix
406+
} else {
407+
testCoverProfile += coverProfile
408+
}
409+
394410
if t.numCPU > 1 {
395-
coverprofile = fmt.Sprintf("%s.%d", coverprofile, node)
411+
testCoverProfile = fmt.Sprintf("%s.%d", testCoverProfile, node)
396412
}
397-
args = append(args, coverprofile)
413+
args = append(args, testCoverProfile)
398414
}
399415

400416
args = append(args, ginkgoArgs...)
@@ -447,8 +463,17 @@ func (t *TestRunner) run(cmd *exec.Cmd, completions chan RunResult) RunResult {
447463

448464
func (t *TestRunner) combineCoverprofiles() {
449465
profiles := []string{}
466+
467+
coverProfile := *t.goOpts["coverprofile"].(*string)
468+
450469
for cpu := 1; cpu <= t.numCPU; cpu++ {
451-
coverFile := fmt.Sprintf("%s.coverprofile.%d", t.Suite.PackageName, cpu)
470+
var coverFile string
471+
if coverProfile == "" {
472+
coverFile = fmt.Sprintf("%s%s.%d", t.Suite.PackageName, CoverProfileSuffix, cpu)
473+
} else {
474+
coverFile = fmt.Sprintf("%s.%d", coverProfile, cpu)
475+
}
476+
452477
coverFile = filepath.Join(t.Suite.Path, coverFile)
453478
coverProfile, err := ioutil.ReadFile(coverFile)
454479
os.Remove(coverFile)
@@ -484,5 +509,15 @@ func (t *TestRunner) combineCoverprofiles() {
484509
output = append(output, fmt.Sprintf("%s %d", line, lines[line]))
485510
}
486511
finalOutput := strings.Join(output, "\n")
487-
ioutil.WriteFile(filepath.Join(t.Suite.Path, fmt.Sprintf("%s.coverprofile", t.Suite.PackageName)), []byte(finalOutput), 0666)
512+
513+
finalFilename := ""
514+
515+
if coverProfile != "" {
516+
finalFilename = coverProfile
517+
} else {
518+
finalFilename = fmt.Sprintf("%s%s", t.Suite.PackageName, CoverProfileSuffix)
519+
}
520+
521+
ioutil.WriteFile(filepath.Join(t.Suite.Path, finalFilename),
522+
[]byte(finalOutput), 0666)
488523
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package first_package
2+
3+
func A() string {
4+
return "A"
5+
}
6+
7+
func B() string {
8+
return "B"
9+
}
10+
11+
func C() string {
12+
return "C"
13+
}
14+
15+
func D() string {
16+
return "D"
17+
}
18+
19+
func E() string {
20+
return "untested"
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package first_package_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
7+
"testing"
8+
)
9+
10+
func TestCoverageFixture(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "CombinedFixture First Suite")
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package first_package_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo/integration/_fixtures/combined_coverage_fixture/first_package"
5+
. "github.com/onsi/ginkgo/integration/_fixtures/combined_coverage_fixture/first_package/external_coverage_fixture"
6+
7+
. "github.com/onsi/ginkgo"
8+
. "github.com/onsi/gomega"
9+
)
10+
11+
var _ = Describe("CoverageFixture", func() {
12+
It("should test A", func() {
13+
Ω(A()).Should(Equal("A"))
14+
})
15+
16+
It("should test B", func() {
17+
Ω(B()).Should(Equal("B"))
18+
})
19+
20+
It("should test C", func() {
21+
Ω(C()).Should(Equal("C"))
22+
})
23+
24+
It("should test D", func() {
25+
Ω(D()).Should(Equal("D"))
26+
})
27+
28+
It("should test external package", func() {
29+
Ω(Tested()).Should(Equal("tested"))
30+
})
31+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package external_coverage
2+
3+
func Tested() string {
4+
return "tested"
5+
}
6+
7+
func Untested() string {
8+
return "untested"
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package second_package
2+
3+
func A() string {
4+
return "A"
5+
}
6+
7+
func B() string {
8+
return "B"
9+
}
10+
11+
func C() string {
12+
return "C"
13+
}
14+
15+
func D() string {
16+
return "D"
17+
}
18+
19+
func E() string {
20+
return "E"
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package second_package_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
7+
"testing"
8+
)
9+
10+
func TestCoverageFixture(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "CombinedFixture Second Suite")
13+
}

0 commit comments

Comments
 (0)