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

JUnit: create directory before creating output file #555

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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())
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be nice if we could test this error, maybe by providing a super long path name perhaps. May be hard to drive out but would be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be hard to test because SpecSuiteDidEnd doesn't return an error and we used fmt.Printf as a returned message.

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 think in this case we should return immediately after print a message and assert report file should not exist. What do you think?

Copy link
Contributor Author

@wingyplus wingyplus Jan 3, 2019

Choose a reason for hiding this comment

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

I just try to write a test case that create with too long folder:

FIt("should print an error if directory cannot be create", func() {
        dir, err := ioutil.TempDir("", "not-exist")
        Expect(err).ShouldNot(HaveOccurred())

        // make directory name over os limit.
        var name string
        for i := 0; i < 256; i++ {
                name += "a"
        }
        output := filepath.Join(dir, name, "report.xml")
        reporter := reporters.NewJUnitReporter(output)
        reporter.SpecSuiteDidEnd(&types.SuiteSummary{
                NumberOfSpecsThatWillBeRun: 1,
                NumberOfFailedSpecs:        0,
                RunTime:                    testSuiteTime,
        })
        Ω(output).ShouldNot(BeAnExistingFile())
})

As I understand correctly, the test case above should pass but it failed:

Running Suite: Reporters Suite
==============================
Random Seed: 1546490671
Will run 1 of 48 specs

SSSSSSSSSSSSSSSSSFailed to create JUnit report file: /var/folders/x7/tl6gyzn92vzcr35t94xgt6y00000gp/T/not-exist738463321/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/report.xml
	mkdir /var/folders/x7/tl6gyzn92vzcr35t94xgt6y00000gp/T/not-exist738463321/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: file name too longFailed to create JUnit report file: /var/folders/x7/tl6gyzn92vzcr35t94xgt6y00000gp/T/not-exist738463321/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/report.xml
	open /var/folders/x7/tl6gyzn92vzcr35t94xgt6y00000gp/T/not-exist738463321/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/report.xml: file name too longFailed to generate JUnit report
	invalid argument
------------------------------
• Failure [0.001 seconds]
JUnit Reporter
/Users/thanabodee/src/github.com/onsi/ginkgo/reporters/junit_reporter_test.go:18
  when output directory doesn't exist
  /Users/thanabodee/src/github.com/onsi/ginkgo/reporters/junit_reporter_test.go:260
    should print an error if directory cannot be create [It]
    /Users/thanabodee/src/github.com/onsi/ginkgo/reporters/junit_reporter_test.go:276

    stat /var/folders/x7/tl6gyzn92vzcr35t94xgt6y00000gp/T/not-exist738463321/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/report.xml: file name too long

    /Users/thanabodee/src/github.com/onsi/ginkgo/reporters/junit_reporter_test.go:292
------------------------------
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

Summarizing 1 Failure:

[Fail] JUnit Reporter when output directory doesn't exist [It] should print an error if directory cannot be create 
/Users/thanabodee/src/github.com/onsi/ginkgo/reporters/junit_reporter_test.go:292

Ran 1 of 48 Specs in 0.001 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 47 Skipped
--- FAIL: TestReporters (0.00s)
FAIL

Ginkgo ran 1 suite in 985.136553ms
Test Suite Failed

That's happen because Gomega doesn't not check file path is too long at this line. Is it a bug? Should be report to gonega repository?

}
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())
})
})
})