Skip to content

Commit

Permalink
fix 'invalid cross-device link' error
Browse files Browse the repository at this point in the history
Previously cleaning golden files would involve moving them into a tmp dir, which could fail
if the tmp dir and testdata dir are not on the same device per #20

```
❯ go test -update                                                                                                                                                         2021-03-16 15:19:44
--- FAIL: Test_WorstScores_nilResult (0.00s)
    autogold.go:82: rename testdata/nil.golden /tmp/go-golden-074934610/nil.golden: invalid cross-device link
FAIL
exit status 1
FAIL    <pkg name redacted>  0.006s
```

This changes the tmp dir we use to be the same path as the testdata dir, plus a `.autogold.tmp`
extension - so it is always on the same device.

Fixes #20

Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
emidoots committed Dec 21, 2022
1 parent 42d5dc6 commit 978b0bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Additionally, CLI flag behavior has been improved substantially based on experie
* Previously autogold would fail tests when running `-update`, meaning you may need to run `go test -update` many times to get to your desired end-state if updating a lot of test cases. Now we match the behavior of OCaml expect tests in not failing tests by default (you can now specify `-fail-on-update`)
* `-fail-on-update` now uses `t.FailNow()` instead of `t.Fatal()` to allow as many tests as possible to succeed when performing an update operation.
* `autogold.Want` has been deprecated in favor of `autogold.Expect`
* Fixed `invalid cross-device link` errors on some systems.

Finally, please note the renaming of functions before and after:

Expand Down
24 changes: 11 additions & 13 deletions autogold.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func ExpectFile(t *testing.T, got interface{}, opts ...Option) {

if shouldCleanup() {
cleanMu.Lock()
if err := mkTempDir(); err != nil {
if err := mkTempDir(dir); err != nil {
t.Fatal(err)
}
grabLock()
Expand Down Expand Up @@ -231,27 +231,25 @@ func shouldCleanup() bool {
return true
}

func mkTempDir() error {
func mkTempDir(testDataDir string) error {
if cleanDir != "" {
return nil
}

// Try to remove past go-golden temp dirs.
matches, err := filepath.Glob(filepath.Join(os.TempDir(), "go-golden-*"))
if err != nil {
// The tmp dir will be `testdata/foo.autogold.tmp` or `testdata.autogold.tmp` (depending on testdata
// folder name.)
tmpDir := testDataDir + ".autogold.tmp"

// The dir may have been left behind if a past test run exited early, so remove it.
if err := os.RemoveAll(tmpDir); err != nil && !os.IsNotExist(err) {
return err
}
for _, match := range matches {
if err := os.RemoveAll(match); err != nil {
return err
}
}

// Create a temp dir for this run.
cleanDir, err = ioutil.TempDir("", "go-golden-*")
if err != nil {
// Create the tmp dir.
if err := os.Mkdir(tmpDir, os.ModePerm); err != nil {
return err
}
cleanDir = tmpDir
return nil
}

Expand Down

0 comments on commit 978b0bb

Please sign in to comment.