From 055364f9ff07d5ac3c11a2e4355674b167632e7b Mon Sep 17 00:00:00 2001 From: Adam Hicks Date: Mon, 8 Jan 2024 14:31:19 +0000 Subject: [PATCH] Don't remove nested subdirectories --- v2/goldie.go | 2 +- v2/goldie_test.go | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/v2/goldie.go b/v2/goldie.go index f9a4c15..10051a8 100644 --- a/v2/goldie.go +++ b/v2/goldie.go @@ -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 } diff --git a/v2/goldie_test.go b/v2/goldie_test.go index 599dfb3..be40103 100644 --- a/v2/goldie_test.go +++ b/v2/goldie_test.go @@ -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"}, @@ -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"}, @@ -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, @@ -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 }