From 4d3ac11bb131596d794c777791ddb40d5b493bbb Mon Sep 17 00:00:00 2001 From: Thanabodee Charoenpiriyakij Date: Wed, 2 Jan 2019 15:21:18 +0700 Subject: [PATCH] JUnit: create directory before creating file Close #554 --- reporters/junit_reporter.go | 4 ++++ reporters/junit_reporter_test.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/reporters/junit_reporter.go b/reporters/junit_reporter.go index 2c9f3c7929..9756592706 100644 --- a/reporters/junit_reporter.go +++ b/reporters/junit_reporter.go @@ -13,6 +13,7 @@ import ( "fmt" "math" "os" + "path/filepath" "strings" "github.com/onsi/ginkgo/config" @@ -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()) diff --git a/reporters/junit_reporter_test.go b/reporters/junit_reporter_test.go index 9b75dc0066..73e7364b5e 100644 --- a/reporters/junit_reporter_test.go +++ b/reporters/junit_reporter_test.go @@ -255,4 +255,18 @@ var _ = Describe("JUnit Reporter", func() { }) }) } + + When("output directory doesn't exist", func() { + It("should create before open file", func() { + output := "/tmp/not/exists/report.xml" + reporter := reporters.NewJUnitReporter(output) + reporter.SpecSuiteDidEnd(&types.SuiteSummary{ + NumberOfSpecsThatWillBeRun: 1, + NumberOfFailedSpecs: 0, + RunTime: testSuiteTime, + }) + defer os.RemoveAll("/tmp/not") + Ω(output).Should(BeAnExistingFile()) + }) + }) })