test: use `T.TempDir` to create temporary test directory
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `os.MkdirTemp` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
Eng Zer Jun committed
Aug 10, 2022 at 22:04 UTC
7f7a5ab1b9c3229b3ab52429a7581a9ed196f44e
5 files changed
+15
-54
fuse/node/mount_test.go
+3
-7
@@ -4,6 +4,7 @@
4
package node
5
6
import (
7
+ "context"
8
"os"
9
"strings"
10
"testing"
@@ -11,8 +12,6 @@ import (
12
13
"bazil.org/fuse"
14
14
- "context"
15
-
15
core "github.com/ipfs/kubo/core"
16
ipns "github.com/ipfs/kubo/fuse/ipns"
17
mount "github.com/ipfs/kubo/fuse/mount"
@@ -52,11 +51,8 @@ func TestExternalUnmount(t *testing.T) {
51
t.Fatal(err)
52
}
53
55
- // get the test dir paths (/tmp/fusetestXXXX)
56
- dir, err := os.MkdirTemp("", "fusetest")
57
- if err != nil {
58
- t.Fatal(err)
59
- }
54
+ // get the test dir paths (/tmp/TestExternalUnmount)
55
+ dir := t.TempDir()
56
57
ipfsDir := dir + "/ipfs"
58
ipnsDir := dir + "/ipns"
repo/fsrepo/config_test.go
+4
-21
@@ -2,7 +2,6 @@ package fsrepo_test
2
3
import (
4
"encoding/json"
5
- "os"
5
"reflect"
6
"testing"
7
@@ -88,11 +87,7 @@ func TestDefaultDatastoreConfig(t *testing.T) {
87
t.Fatal(err)
88
}
89
91
- dir, err := os.MkdirTemp("", "ipfs-datastore-config-test")
92
- if err != nil {
93
- t.Fatal(err)
94
- }
95
- defer os.RemoveAll(dir) // clean up
90
+ dir := t.TempDir()
91
92
config := new(config.Datastore)
93
err = json.Unmarshal(defaultConfig, config)
@@ -126,11 +121,7 @@ func TestLevelDbConfig(t *testing.T) {
121
if err != nil {
122
t.Fatal(err)
123
}
129
- dir, err := os.MkdirTemp("", "ipfs-datastore-config-test")
130
- if err != nil {
131
- t.Fatal(err)
132
- }
133
- defer os.RemoveAll(dir) // clean up
124
+ dir := t.TempDir()
125
126
spec := make(map[string]interface{})
127
err = json.Unmarshal(leveldbConfig, &spec)
@@ -164,11 +155,7 @@ func TestFlatfsConfig(t *testing.T) {
155
if err != nil {
156
t.Fatal(err)
157
}
167
- dir, err := os.MkdirTemp("", "ipfs-datastore-config-test")
168
- if err != nil {
169
- t.Fatal(err)
170
- }
171
- defer os.RemoveAll(dir) // clean up
158
+ dir := t.TempDir()
159
160
spec := make(map[string]interface{})
161
err = json.Unmarshal(flatfsConfig, &spec)
@@ -202,11 +189,7 @@ func TestMeasureConfig(t *testing.T) {
189
if err != nil {
190
t.Fatal(err)
191
}
205
- dir, err := os.MkdirTemp("", "ipfs-datastore-config-test")
206
- if err != nil {
207
- t.Fatal(err)
208
- }
209
- defer os.RemoveAll(dir) // clean up
192
+ dir := t.TempDir()
193
194
spec := make(map[string]interface{})
195
err = json.Unmarshal(measureConfig, &spec)
repo/fsrepo/fsrepo_test.go
+6
-15
@@ -13,18 +13,9 @@ import (
13
config "github.com/ipfs/kubo/config"
14
)
15
16
-// swap arg order
17
-func testRepoPath(p string, t *testing.T) string {
18
- name, err := os.MkdirTemp("", p)
19
- if err != nil {
20
- t.Fatal(err)
21
- }
22
- return name
23
-}
24
-
16
func TestInitIdempotence(t *testing.T) {
17
t.Parallel()
27
- path := testRepoPath("", t)
18
+ path := t.TempDir()
19
for i := 0; i < 10; i++ {
20
assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t, "multiple calls to init should succeed")
21
}
@@ -37,8 +28,8 @@ func Remove(repoPath string) error {
28
29
func TestCanManageReposIndependently(t *testing.T) {
30
t.Parallel()
40
- pathA := testRepoPath("a", t)
41
- pathB := testRepoPath("b", t)
31
+ pathA := t.TempDir()
32
+ pathB := t.TempDir()
33
34
t.Log("initialize two repos")
35
assert.Nil(Init(pathA, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t, "a", "should initialize successfully")
@@ -65,7 +56,7 @@ func TestCanManageReposIndependently(t *testing.T) {
56
57
func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
58
t.Parallel()
68
- path := testRepoPath("test", t)
59
+ path := t.TempDir()
60
61
assert.True(!IsInitialized(path), t, "should NOT be initialized")
62
assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t, "should initialize successfully")
@@ -83,7 +74,7 @@ func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
74
75
func TestDatastorePersistsFromRepoToRepo(t *testing.T) {
76
t.Parallel()
86
- path := testRepoPath("test", t)
77
+ path := t.TempDir()
78
79
assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t)
80
r1, err := Open(path)
@@ -104,7 +95,7 @@ func TestDatastorePersistsFromRepoToRepo(t *testing.T) {
95
96
func TestOpenMoreThanOnceInSameProcess(t *testing.T) {
97
t.Parallel()
107
- path := testRepoPath("", t)
98
+ path := t.TempDir()
99
assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t)
100
101
r1, err := Open(path)
test/bench/bench_cli_ipfs_add/main.go
+1
-6
@@ -45,12 +45,7 @@ func benchmarkAdd(amount int64) (*testing.BenchmarkResult, error) {
45
b.SetBytes(amount)
46
for i := 0; i < b.N; i++ {
47
b.StopTimer()
48
- tmpDir, err := os.MkdirTemp("", "")
49
- if err != nil {
50
- benchmarkError = err
51
- b.Fatal(err)
52
- }
53
- defer os.RemoveAll(tmpDir)
48
+ tmpDir := b.TempDir()
49
50
env := append(
51
[]string{fmt.Sprintf("%s=%s", config.EnvDir, path.Join(tmpDir, config.DefaultPathName))}, // first in order to override
test/bench/offline_add/main.go
+1
-5
@@ -37,11 +37,7 @@ func benchmarkAdd(amount int64) (*testing.BenchmarkResult, error) {
37
b.SetBytes(amount)
38
for i := 0; i < b.N; i++ {
39
b.StopTimer()
40
- tmpDir, err := os.MkdirTemp("", "")
41
- if err != nil {
42
- b.Fatal(err)
43
- }
44
- defer os.RemoveAll(tmpDir)
40
+ tmpDir := b.TempDir()
41
42
env := append(os.Environ(), fmt.Sprintf("%s=%s", config.EnvDir, path.Join(tmpDir, config.DefaultPathName)))
43
setupCmd := func(cmd *exec.Cmd) {