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

Create any missing directories for {json,junit}file #241

Merged
merged 1 commit into from
Apr 9, 2022
Merged
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
3 changes: 3 additions & 0 deletions cmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"os/exec"
"path/filepath"

"github.com/pkg/errors"
"gotest.tools/gotestsum/internal/junitxml"
Expand Down Expand Up @@ -68,6 +69,7 @@ func newEventHandler(opts *options) (*eventHandler, error) {
}
var err error
if opts.jsonFile != "" {
_ = os.MkdirAll(filepath.Dir(opts.jsonFile), 0o755)
handler.jsonFile, err = os.Create(opts.jsonFile)
if err != nil {
return handler, errors.Wrap(err, "failed to open JSON file")
Expand All @@ -80,6 +82,7 @@ func writeJUnitFile(opts *options, execution *testjson.Execution) error {
if opts.junitFile == "" {
return nil
}
_ = os.MkdirAll(filepath.Dir(opts.junitFile), 0o755)
junitFile, err := os.Create(opts.junitFile)
if err != nil {
return fmt.Errorf("failed to open JUnit file: %v", err)
Expand Down
35 changes: 35 additions & 0 deletions cmd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"gotest.tools/gotestsum/testjson"
"gotest.tools/v3/assert"
"gotest.tools/v3/env"
"gotest.tools/v3/fs"
"gotest.tools/v3/golden"
)

Expand Down Expand Up @@ -85,3 +87,36 @@ func TestEventHandler_Event_MaxFails(t *testing.T) {
_, err := testjson.ScanTestOutput(cfg)
assert.Error(t, err, "ending test run because max failures was reached")
}

func TestNewEventHandler_CreatesDirectory(t *testing.T) {
dir := fs.NewDir(t, t.Name())
jsonFile := filepath.Join(dir.Path(), "new-path", "log.json")

opts := &options{
stdout: new(bytes.Buffer),
format: "testname",
jsonFile: jsonFile,
}
_, err := newEventHandler(opts)
assert.NilError(t, err)

_, err = os.Stat(jsonFile)
assert.NilError(t, err)
}

func TestWriteJunitFile_CreatesDirectory(t *testing.T) {
dir := fs.NewDir(t, t.Name())
junitFile := filepath.Join(dir.Path(), "new-path", "junit.xml")

opts := &options{
junitFile: junitFile,
junitTestCaseClassnameFormat: &junitFieldFormatValue{},
junitTestSuiteNameFormat: &junitFieldFormatValue{},
}
exec := &testjson.Execution{}
err := writeJUnitFile(opts, exec)
assert.NilError(t, err)

_, err = os.Stat(junitFile)
assert.NilError(t, err)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ require (
gotest.tools/v3 v3.0.3
)

go 1.11
go 1.13