Skip to content

Commit

Permalink
Merge pull request #209 from dnephin/golden-create-dir
Browse files Browse the repository at this point in the history
golden: create directories when -test.update-golden
  • Loading branch information
dnephin authored Sep 27, 2020
2 parents 96aacb9 + c99d01a commit 568bc57
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
19 changes: 12 additions & 7 deletions golden/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ func Path(filename string) string {
return filepath.Join("testdata", filename)
}

func update(filename string, actual []byte) error {
if *flagUpdate {
return ioutil.WriteFile(Path(filename), actual, 0644)
}
return nil
}

func removeCarriageReturn(in []byte) []byte {
if !NormalizeCRLFToLF {
return in
Expand Down Expand Up @@ -180,3 +173,15 @@ func compare(actual []byte, filename string) (cmp.Result, []byte) {
}
return nil, expected
}

func update(filename string, actual []byte) error {
if dir := filepath.Dir(filename); dir != "." {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
}
if *flagUpdate {
return ioutil.WriteFile(Path(filename), actual, 0644)
}
return nil
}
30 changes: 30 additions & 0 deletions golden/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,33 @@ func TestFlagUpdate(t *testing.T) {
defer undo()
assert.Assert(t, FlagUpdate())
}

func TestUpdate_CreatesPathsAndFile(t *testing.T) {
undo := setUpdateFlag()
defer undo()

dir := fs.NewDir(t, t.Name())

t.Run("creates the file", func(t *testing.T) {
filename := dir.Join("filename")
err := update(filename, nil)
assert.NilError(t, err)

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

t.Run("creates directories", func(t *testing.T) {
filename := dir.Join("one/two/filename")
err := update(filename, nil)
assert.NilError(t, err)

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

t.Run("no error when directory exists", func(t *testing.T) {
err = update(filename, nil)
assert.NilError(t, err)
})
})
}

0 comments on commit 568bc57

Please sign in to comment.