Skip to content

Commit

Permalink
Fail fast when the outputdir of combined coverage profile does not exist
Browse files Browse the repository at this point in the history
This is a workaround for a change in behaviour between go <= 1.9 and go
1.10.

Prior to go 1.9, compiling a test would not fail if the specified
output dir does not exist. However, it does fail in go 1.10.

By failing when the the directory does not exist, we have a consistent
behaviour withing go versions.

See also golang/go#24588
  • Loading branch information
Andrea Nodari committed Apr 5, 2018
1 parent 03ffd3f commit c0b857d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
26 changes: 16 additions & 10 deletions ginkgo/run_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,25 @@ func (r *SpecRunner) RunSpecs(args []string, additionalArgs []string) {
runResult, numSuites = r.suiteRunner.RunSuites(randomizedRunners, r.commandFlags.NumCompilers, r.commandFlags.KeepGoing, nil)
}

for _, runner := range runners {
runner.CleanUp()
}

if r.isInCoverageMode() {
if r.getOutputDir() != "" {
// If coverprofile is set, combine coverages
if r.getCoverprofile() != "" {
r.combineCoverprofiles(runners)
if err := r.combineCoverprofiles(runners); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
} else {
// Just move them
r.moveCoverprofiles(runners)
}
}
}

for _, runner := range runners {
runner.CleanUp()
}

fmt.Printf("\nGinkgo ran %d %s in %s\n", numSuites, pluralizedWord("suite", "suites", numSuites), time.Since(t))

if runResult.Passed {
Expand Down Expand Up @@ -151,38 +154,41 @@ func (r *SpecRunner) moveCoverprofiles(runners []*testrunner.TestRunner) {
}

// Combines all generated profiles in the specified directory
func (r *SpecRunner) combineCoverprofiles(runners []*testrunner.TestRunner) {
func (r *SpecRunner) combineCoverprofiles(runners []*testrunner.TestRunner) error {

path, _ := filepath.Abs(r.getOutputDir())
if !fileExists(path) {
return fmt.Errorf("Unable to create combined profile, outputdir does not exist: %s", r.getOutputDir())
}

fmt.Println("path is " + path)
os.MkdirAll(path, os.ModePerm)

combined, err := os.OpenFile(filepath.Join(path, r.getCoverprofile()),
os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)

if err != nil {
fmt.Printf("Unable to create combined profile, %v\n", err)
return
return nil // non-fatal error
}

for _, runner := range runners {
contents, err := ioutil.ReadFile(runner.CoverageFile)

if err != nil {
fmt.Printf("Unable to read coverage file %s to combine, %v\n", runner.CoverageFile, err)
return
return nil // non-fatal error
}

_, err = combined.Write(contents)

if err != nil {
fmt.Printf("Unable to append to coverprofile, %v\n", err)
return
return nil // non-fatal error
}
}

fmt.Println("All profiles combined")
return nil
}

func (r *SpecRunner) isInCoverageMode() bool {
Expand Down
13 changes: 4 additions & 9 deletions integration/coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,12 @@ var _ = Describe("Coverage Specs", func() {
os.RemoveAll("./_fixtures/combined_coverage_fixture/coverage.txt")
})

It("Creates directories in path if they don't exist", func() {
It("Fails with an error if output dir and coverprofile were set, but the output dir did not exist", func() {
session := startGinkgo("./_fixtures/combined_coverage_fixture", "-outputdir=./all/profiles/here", "-r", "-cover", "-coverprofile=coverage.txt")

defer os.RemoveAll("./_fixtures/combined_coverage_fixture/all")
defer os.RemoveAll("./_fixtures/combined_coverage_fixture/coverage.txt")

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

_, err := os.Stat("./_fixtures/combined_coverage_fixture/all/profiles/here/coverage.txt")

Ω(err).ShouldNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
output := session.Out.Contents()
Ω(string(output)).Should(ContainSubstring("Unable to create combined profile, outputdir does not exist: ./all/profiles/here"))
})

It("Moves coverages if only output dir was set", func() {
Expand Down

0 comments on commit c0b857d

Please sign in to comment.