Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle -coverprofile flag #355

Merged
merged 8 commits into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
TODO
tmp/**/*
*.coverprofile
*.coverprofile
.idea/
28 changes: 22 additions & 6 deletions ginkgo/testrunner/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,30 @@ func (t *TestRunner) runParallelGinkgoSuite() RunResult {
return res
}

const CoverProfileSuffix = ".coverprofile"

func (t *TestRunner) cmd(ginkgoArgs []string, stream io.Writer, node int) *exec.Cmd {
args := []string{"--test.timeout=24h"}
if *t.goOpts["cover"].(*bool) || *t.goOpts["coverpkg"].(*string) != "" || *t.goOpts["covermode"].(*string) != "" {
coverprofile := "--test.coverprofile=" + t.Suite.PackageName + ".coverprofile"

coverMode := *t.goOpts["covermode"].(*string)
coverPackage := *t.goOpts["coverpkg"].(*string)
cover := *t.goOpts["cover"].(*bool)
coverProfile := *t.goOpts["coverprofile"].(*string)

if cover || coverPackage != "" || coverMode != "" {
testCoverProfile := "--test.coverprofile="

// Set default name for coverage results
if coverProfile == "" {
testCoverProfile += t.Suite.PackageName + CoverProfileSuffix
} else {
testCoverProfile += coverProfile
}

if t.numCPU > 1 {
coverprofile = fmt.Sprintf("%s.%d", coverprofile, node)
testCoverProfile = fmt.Sprintf("%s.%d", testCoverProfile, node)
}
args = append(args, coverprofile)
args = append(args, testCoverProfile)
}

args = append(args, ginkgoArgs...)
Expand Down Expand Up @@ -473,7 +489,7 @@ func (t *TestRunner) run(cmd *exec.Cmd, completions chan RunResult) RunResult {
func (t *TestRunner) combineCoverprofiles() {
profiles := []string{}
for cpu := 1; cpu <= t.numCPU; cpu++ {
coverFile := fmt.Sprintf("%s.coverprofile.%d", t.Suite.PackageName, cpu)
coverFile := fmt.Sprintf("%s%s.%d", t.Suite.PackageName, CoverProfileSuffix, cpu)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this works any more, no?

If you don't use the default name combineCoverprofiles won't find the cover profile you asked for. have you tried running with ginkgo -p -coverprofile="foo" to see it work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@onsi Done

coverFile = filepath.Join(t.Suite.Path, coverFile)
coverProfile, err := ioutil.ReadFile(coverFile)
os.Remove(coverFile)
Expand Down Expand Up @@ -509,5 +525,5 @@ func (t *TestRunner) combineCoverprofiles() {
output = append(output, fmt.Sprintf("%s %d", line, lines[line]))
}
finalOutput := strings.Join(output, "\n")
ioutil.WriteFile(filepath.Join(t.Suite.Path, fmt.Sprintf("%s.coverprofile", t.Suite.PackageName)), []byte(finalOutput), 0666)
ioutil.WriteFile(filepath.Join(t.Suite.Path, fmt.Sprintf("%s%s", t.Suite.PackageName, CoverProfileSuffix)), []byte(finalOutput), 0666)
}
14 changes: 14 additions & 0 deletions integration/coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ var _ = Describe("Coverage Specs", func() {

Ω(parallelCoverProfileOutput).Should(Equal(serialCoverProfileOutput))
})

It("validates coverprofile sets custom profile name", func() {
session := startGinkgo("./_fixtures/coverage_fixture", "-cover", "-coverprofile=coverage.txt")

Eventually(session).Should(gexec.Exit(0))

// Check that the correct file was created
_, err := os.Stat("./_fixtures/coverage_fixture/coverage.txt")

Ω(err).ShouldNot(HaveOccurred())

// Cleanup
os.RemoveAll("./_fixtures/coverage_fixture/coverage.txt")
})
})