Skip to content

Commit 59b5287

Browse files
committed
use temporary directory for tests
1 parent 76e9512 commit 59b5287

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
/test.db
21
/fs/test

db_test.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,29 @@ func TestMain(m *testing.M) {
3333
}
3434
// Run tests against all file systems.
3535
for _, fsys := range []fs.FileSystem{fs.Mem, fs.OSMMap, fs.OS} {
36-
testFS = fsys
36+
var tmpDir string
37+
if fsys == fs.Mem {
38+
testFS = fsys
39+
} else {
40+
var err error
41+
tmpDir, err = os.MkdirTemp("", "pogreb-test")
42+
if err != nil {
43+
fmt.Printf("failed to create temporary directory: %v", err)
44+
os.Exit(1)
45+
}
46+
testFS = fs.Sub(fsys, tmpDir)
47+
}
3748
if testing.Verbose() {
38-
fmt.Printf("=== SET\tFS=%T\n", fsys)
49+
fmt.Printf("=== SET\tFS=%T\ttmpDir=%s\n", fsys, tmpDir)
3950
}
40-
if exitCode := m.Run(); exitCode != 0 {
51+
exitCode := m.Run()
52+
if tmpDir != "" {
53+
_ = os.RemoveAll(tmpDir)
54+
}
55+
if exitCode != 0 {
4156
fmt.Printf("DEBUG\tFS=%T\n", fsys)
4257
os.Exit(exitCode)
4358
}
44-
_ = cleanDir(testDBName)
45-
_ = cleanDir(testDBBackupName)
4659
}
4760
os.Exit(0)
4861
}
@@ -83,6 +96,9 @@ func TestHeaderSize(t *testing.T) {
8396
func cleanDir(path string) error {
8497
files, err := testFS.ReadDir(path)
8598
if err != nil {
99+
if os.IsNotExist(err) {
100+
return nil
101+
}
86102
return err
87103
}
88104
for _, file := range files {
@@ -214,7 +230,7 @@ func fullTest(t *testing.T, getFunc func(db *DB, key []byte) ([]byte, error)) {
214230
verifyKeysAndClose(6)
215231

216232
// Delete all items
217-
db, err = Open(testDBName, &Options{BackgroundSyncInterval: time.Millisecond})
233+
db, err = Open(testDBName, &Options{BackgroundSyncInterval: time.Millisecond, FileSystem: testFS})
218234
assert.Nil(t, err)
219235
for i = 0; i < n; i++ {
220236
assert.Nil(t, db.Delete([]byte{i}))

fs/os_mmap_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import (
55
)
66

77
func TestOSMMapFS(t *testing.T) {
8-
testFS(t, OSMMap)
8+
testFS(t, Sub(OSMMap, t.TempDir()))
99
}

fs/os_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
)
66

77
func TestOSFS(t *testing.T) {
8-
testFS(t, OS)
8+
testFS(t, Sub(OS, t.TempDir()))
99
}
1010

1111
func TestOSLockFile(t *testing.T) {
12-
testLockFile(t, OS)
12+
testLockFile(t, Sub(OS, t.TempDir()))
1313
}
1414

1515
func TestOSLockAcquireExisting(t *testing.T) {
16-
testLockFileAcquireExisting(t, OS)
16+
testLockFileAcquireExisting(t, Sub(OS, t.TempDir()))
1717
}

fs/sub_test.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)