Skip to content

Commit

Permalink
Merge pull request #446 from onsi/do-not-create-outputdir
Browse files Browse the repository at this point in the history
Fail fast when the outputdir of combined coverage profile does not exist
  • Loading branch information
blgm committed Apr 6, 2018
2 parents 874520d + a37f4c0 commit b36a6e0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## HEAD

- When using custom reporters register the custom reporters *before* the default reporter. This allows users to see the output of any print statements in their customer reporters. [#365]
- When running a test and calculating the coverage using the `-coverprofile` and `-outputdir` flags, Ginkgo fails with an error if the directory does not exist. This is due to an [issue in go 1.10](https://github.com/golang/go/issues/24588) [#446]

## 1.4.0 7/16/2017

Expand Down
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 b36a6e0

Please sign in to comment.