|
5 | 5 | "errors" |
6 | 6 | "io" |
7 | 7 | "os" |
| 8 | + "path/filepath" |
8 | 9 | "runtime" |
9 | 10 | "sort" |
10 | 11 | "strings" |
@@ -42,17 +43,31 @@ func TestNewCIDFileWhenFileAlreadyExists(t *testing.T) { |
42 | 43 | } |
43 | 44 |
|
44 | 45 | func TestCIDFileCloseWithNoWrite(t *testing.T) { |
45 | | - tempdir := fs.NewDir(t, "test-cid-file") |
46 | | - defer tempdir.Remove() |
| 46 | + // Closing should remove the file if it was not written to. |
| 47 | + t.Run("closing should remove file", func(t *testing.T) { |
| 48 | + filename := filepath.Join(t.TempDir(), "cidfile-1") |
| 49 | + file, err := newCIDFile(filename) |
| 50 | + assert.NilError(t, err) |
| 51 | + assert.Check(t, is.Equal(file.path, filename)) |
47 | 52 |
|
48 | | - path := tempdir.Join("cidfile") |
49 | | - file, err := newCIDFile(path) |
50 | | - assert.NilError(t, err) |
51 | | - assert.Check(t, is.Equal(file.path, path)) |
| 53 | + assert.NilError(t, file.Close()) |
| 54 | + _, err = os.Stat(filename) |
| 55 | + assert.Check(t, os.IsNotExist(err)) |
| 56 | + }) |
52 | 57 |
|
53 | | - assert.NilError(t, file.Close()) |
54 | | - _, err = os.Stat(path) |
55 | | - assert.Check(t, os.IsNotExist(err)) |
| 58 | + // Closing (and removing) the file should not produce an error if the file no longer exists. |
| 59 | + t.Run("close should remove file", func(t *testing.T) { |
| 60 | + filename := filepath.Join(t.TempDir(), "cidfile-2") |
| 61 | + file, err := newCIDFile(filename) |
| 62 | + assert.NilError(t, err) |
| 63 | + assert.Check(t, is.Equal(file.path, filename)) |
| 64 | + |
| 65 | + assert.NilError(t, os.Remove(filename)) |
| 66 | + _, err = os.Stat(filename) |
| 67 | + assert.Check(t, os.IsNotExist(err)) |
| 68 | + |
| 69 | + assert.NilError(t, file.Close()) |
| 70 | + }) |
56 | 71 | } |
57 | 72 |
|
58 | 73 | func TestCIDFileCloseWithWrite(t *testing.T) { |
|
0 commit comments