Skip to content

Commit

Permalink
JUnit: create directory before creating output file
Browse files Browse the repository at this point in the history
Close #554
  • Loading branch information
thanabodee-c committed Jan 3, 2019
1 parent 505cc35 commit 349a1e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions reporters/junit_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"math"
"os"
"path/filepath"
"strings"

"github.com/onsi/ginkgo/config"
Expand Down Expand Up @@ -124,6 +125,9 @@ func (reporter *JUnitReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
reporter.suite.Time = math.Trunc(summary.RunTime.Seconds()*1000) / 1000
reporter.suite.Failures = summary.NumberOfFailedSpecs
reporter.suite.Errors = 0
if err := os.MkdirAll(filepath.Dir(reporter.filename), 0755); err != nil {
fmt.Printf("Failed to create JUnit report file: %s\n\t%s", reporter.filename, err.Error())
}
file, err := os.Create(reporter.filename)
if err != nil {
fmt.Printf("Failed to create JUnit report file: %s\n\t%s", reporter.filename, err.Error())
Expand Down
18 changes: 18 additions & 0 deletions reporters/junit_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/xml"
"io/ioutil"
"os"
"path/filepath"
"time"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -255,4 +256,21 @@ var _ = Describe("JUnit Reporter", func() {
})
})
}

When("output directory doesn't exist", func() {
It("should create before open file", func() {
dir, err := ioutil.TempDir("", "not-exist")
Expect(err).ShouldNot(HaveOccurred())
defer os.RemoveAll(dir)

output := filepath.Join(dir, "not", "exist", "report.xml")
reporter := reporters.NewJUnitReporter(output)
reporter.SpecSuiteDidEnd(&types.SuiteSummary{
NumberOfSpecsThatWillBeRun: 1,
NumberOfFailedSpecs: 0,
RunTime: testSuiteTime,
})
Ω(output).Should(BeAnExistingFile())
})
})
})

0 comments on commit 349a1e0

Please sign in to comment.