Skip to content

Commit

Permalink
Merge pull request #223 from seriousben/fix-golden-dir-creation
Browse files Browse the repository at this point in the history
Stop creating directory outside of testdata
  • Loading branch information
dnephin committed Jan 4, 2022
2 parents 10e310b + b0ea9a7 commit 7fe928e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion golden/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func update(filename string, actual []byte) error {
if !*flagUpdate {
return nil
}
if dir := filepath.Dir(filename); dir != "." {
if dir := filepath.Dir(Path(filename)); dir != "." {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
Expand Down
53 changes: 53 additions & 0 deletions golden/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,43 @@ func TestGoldenAssertInvalidContentUpdate(t *testing.T) {
assert.Assert(t, !fakeT.Failed)
}

func TestGoldenAssertAbsolutePath(t *testing.T) {
file := fs.NewFile(t, "abs-test", fs.WithContent("foo"))
defer file.Remove()
fakeT := new(fakeT)

Assert(fakeT, "foo", file.Path())
assert.Assert(t, !fakeT.Failed)
}

func TestGoldenAssertInDir(t *testing.T) {
filename, clean := setupGoldenFileWithDir(t, "testdatasubdir", "foo")
defer clean()

fakeT := new(fakeT)

Assert(fakeT, "foo", filepath.Join("testdatasubdir", filename))
assert.Assert(t, !fakeT.Failed)

_, err := os.Stat("testdatasubdir")
assert.Assert(t, os.IsNotExist(err), "testdatasubdir should not exist outside of testdata")
}

func TestGoldenAssertInDir_UpdateGolden(t *testing.T) {
filename, clean := setupGoldenFileWithDir(t, "testdatasubdir", "foo")
defer clean()
unsetUpdateFlag := setUpdateFlag()
defer unsetUpdateFlag()

fakeT := new(fakeT)

Assert(fakeT, "foo", filepath.Join("testdatasubdir", filename))
assert.Assert(t, !fakeT.Failed)

_, err := os.Stat("testdatasubdir")
assert.Assert(t, os.IsNotExist(err), "testdatasubdir should not exist outside of testdata")
}

func TestGoldenAssert(t *testing.T) {
filename, clean := setupGoldenFile(t, "foo")
defer clean()
Expand Down Expand Up @@ -161,6 +198,22 @@ func setUpdateFlag() func() {
return func() { *flagUpdate = oldFlagUpdate }
}

func setupGoldenFileWithDir(t *testing.T, dirname, content string) (string, func()) {
dirpath := filepath.Join("testdata", dirname)
_ = os.MkdirAll(filepath.Join("testdata", dirname), 0755)
f, err := ioutil.TempFile(dirpath, t.Name()+"-")
assert.NilError(t, err, "fail to create test golden file")
defer f.Close() // nolint: errcheck

_, err = f.Write([]byte(content))
assert.NilError(t, err)

return filepath.Base(f.Name()), func() {
assert.NilError(t, os.Remove(f.Name()))
assert.NilError(t, os.Remove(dirpath))
}
}

func setupGoldenFile(t *testing.T, content string) (string, func()) {
_ = os.Mkdir("testdata", 0755)
f, err := ioutil.TempFile("testdata", t.Name()+"-")
Expand Down

0 comments on commit 7fe928e

Please sign in to comment.