Skip to content

Commit 7fe928e

Browse files
authored
Merge pull request #223 from seriousben/fix-golden-dir-creation
Stop creating directory outside of testdata
2 parents 10e310b + b0ea9a7 commit 7fe928e

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

golden/golden.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func update(filename string, actual []byte) error {
178178
if !*flagUpdate {
179179
return nil
180180
}
181-
if dir := filepath.Dir(filename); dir != "." {
181+
if dir := filepath.Dir(Path(filename)); dir != "." {
182182
if err := os.MkdirAll(dir, 0755); err != nil {
183183
return err
184184
}

golden/golden_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,43 @@ func TestGoldenAssertInvalidContentUpdate(t *testing.T) {
107107
assert.Assert(t, !fakeT.Failed)
108108
}
109109

110+
func TestGoldenAssertAbsolutePath(t *testing.T) {
111+
file := fs.NewFile(t, "abs-test", fs.WithContent("foo"))
112+
defer file.Remove()
113+
fakeT := new(fakeT)
114+
115+
Assert(fakeT, "foo", file.Path())
116+
assert.Assert(t, !fakeT.Failed)
117+
}
118+
119+
func TestGoldenAssertInDir(t *testing.T) {
120+
filename, clean := setupGoldenFileWithDir(t, "testdatasubdir", "foo")
121+
defer clean()
122+
123+
fakeT := new(fakeT)
124+
125+
Assert(fakeT, "foo", filepath.Join("testdatasubdir", filename))
126+
assert.Assert(t, !fakeT.Failed)
127+
128+
_, err := os.Stat("testdatasubdir")
129+
assert.Assert(t, os.IsNotExist(err), "testdatasubdir should not exist outside of testdata")
130+
}
131+
132+
func TestGoldenAssertInDir_UpdateGolden(t *testing.T) {
133+
filename, clean := setupGoldenFileWithDir(t, "testdatasubdir", "foo")
134+
defer clean()
135+
unsetUpdateFlag := setUpdateFlag()
136+
defer unsetUpdateFlag()
137+
138+
fakeT := new(fakeT)
139+
140+
Assert(fakeT, "foo", filepath.Join("testdatasubdir", filename))
141+
assert.Assert(t, !fakeT.Failed)
142+
143+
_, err := os.Stat("testdatasubdir")
144+
assert.Assert(t, os.IsNotExist(err), "testdatasubdir should not exist outside of testdata")
145+
}
146+
110147
func TestGoldenAssert(t *testing.T) {
111148
filename, clean := setupGoldenFile(t, "foo")
112149
defer clean()
@@ -161,6 +198,22 @@ func setUpdateFlag() func() {
161198
return func() { *flagUpdate = oldFlagUpdate }
162199
}
163200

201+
func setupGoldenFileWithDir(t *testing.T, dirname, content string) (string, func()) {
202+
dirpath := filepath.Join("testdata", dirname)
203+
_ = os.MkdirAll(filepath.Join("testdata", dirname), 0755)
204+
f, err := ioutil.TempFile(dirpath, t.Name()+"-")
205+
assert.NilError(t, err, "fail to create test golden file")
206+
defer f.Close() // nolint: errcheck
207+
208+
_, err = f.Write([]byte(content))
209+
assert.NilError(t, err)
210+
211+
return filepath.Base(f.Name()), func() {
212+
assert.NilError(t, os.Remove(f.Name()))
213+
assert.NilError(t, os.Remove(dirpath))
214+
}
215+
}
216+
164217
func setupGoldenFile(t *testing.T, content string) (string, func()) {
165218
_ = os.Mkdir("testdata", 0755)
166219
f, err := ioutil.TempFile("testdata", t.Name()+"-")

0 commit comments

Comments
 (0)