Skip to content

Commit

Permalink
Don't remove nested subdirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhicks committed Jan 8, 2024
1 parent 083c801 commit 055364f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion v2/goldie.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (g *Goldie) ensureDir(loc string) error {
// the location does not exist, so make directories to there
return os.MkdirAll(loc, g.dirPerms)

case err == nil && s.IsDir() && *clean && s.ModTime().UnixNano() != ts.UnixNano():
case err == nil && s.IsDir() && *clean && s.ModTime().UnixNano() < ts.UnixNano():
if err := os.RemoveAll(loc); err != nil {
return err
}
Expand Down
22 changes: 17 additions & 5 deletions v2/goldie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,37 @@ Got: Lorem ipsum dolor.`},
}

func TestCleanFunction(t *testing.T) {

savedCleanState := *clean
*clean = false
savedUpdateState := *update
*update = true
t.Cleanup(func() {
*clean = savedCleanState
*update = savedUpdateState
})

ts = time.Now()

sampleData := []byte("sample data")
fixtureDir := "test-fixtures"
fixtureSubDirA := fixtureDir + "/a"
fixtureSubDirB := fixtureDir + "/b"
fixtureNestedDirAC := fixtureSubDirA + "/c"
suffix := ".golden"

t.Cleanup(func() {
err := os.RemoveAll(fixtureDir)
assert.Nil(t, err)
})

// The first time running go test, with -update, without -clean
firstTests := []struct {
fixtureDirWithSub string
filePrefix string
}{
{fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a1"},
{fixtureDirWithSub: fixtureNestedDirAC, filePrefix: "example-ac1"},
{fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a2"},
{fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b1"},
{fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b2"},
Expand Down Expand Up @@ -260,6 +273,7 @@ func TestCleanFunction(t *testing.T) {
filePrefix string
}{
{fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a3"},
{fixtureDirWithSub: fixtureNestedDirAC, filePrefix: "example-ac2"},
{fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a4"},
{fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b3"},
{fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b4"},
Expand All @@ -274,7 +288,10 @@ func TestCleanFunction(t *testing.T) {
t.Run(fmt.Sprint(i), func(t *testing.T) {
g.Assert(t, tt.filePrefix, sampleData)
})
}

// make sure all the output files from the second run now exist
for _, tt := range secondTests {
fullPath := fmt.Sprintf("%s%s",
filepath.Join(tt.fixtureDirWithSub, tt.filePrefix),
suffix,
Expand All @@ -295,9 +312,4 @@ func TestCleanFunction(t *testing.T) {
assert.Error(t, err)
assert.True(t, os.IsNotExist(err))
}

err := os.RemoveAll(fixtureDir)
assert.Nil(t, err)
*clean = savedCleanState
*update = savedUpdateState
}

0 comments on commit 055364f

Please sign in to comment.