Skip to content

Commit

Permalink
test: use T.TempDir to create temporary test directory
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Juneezee authored and galargh committed Mar 20, 2023
1 parent d001ed7 commit 5f76661
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 54 deletions.
10 changes: 3 additions & 7 deletions fuse/node/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
package node

import (
"context"
"os"
"strings"
"testing"
"time"

"bazil.org/fuse"

"context"

core "github.com/ipfs/kubo/core"
ipns "github.com/ipfs/kubo/fuse/ipns"
mount "github.com/ipfs/kubo/fuse/mount"
Expand Down Expand Up @@ -52,11 +51,8 @@ func TestExternalUnmount(t *testing.T) {
t.Fatal(err)
}

// get the test dir paths (/tmp/fusetestXXXX)
dir, err := os.MkdirTemp("", "fusetest")
if err != nil {
t.Fatal(err)
}
// get the test dir paths (/tmp/TestExternalUnmount)
dir := t.TempDir()

ipfsDir := dir + "/ipfs"
ipnsDir := dir + "/ipns"
Expand Down
25 changes: 4 additions & 21 deletions repo/fsrepo/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fsrepo_test

import (
"encoding/json"
"os"
"reflect"
"testing"

Expand Down Expand Up @@ -88,11 +87,7 @@ func TestDefaultDatastoreConfig(t *testing.T) {
t.Fatal(err)
}

dir, err := os.MkdirTemp("", "ipfs-datastore-config-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
dir := t.TempDir()

config := new(config.Datastore)
err = json.Unmarshal(defaultConfig, config)
Expand Down Expand Up @@ -126,11 +121,7 @@ func TestLevelDbConfig(t *testing.T) {
if err != nil {
t.Fatal(err)
}
dir, err := os.MkdirTemp("", "ipfs-datastore-config-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
dir := t.TempDir()

spec := make(map[string]interface{})
err = json.Unmarshal(leveldbConfig, &spec)
Expand Down Expand Up @@ -164,11 +155,7 @@ func TestFlatfsConfig(t *testing.T) {
if err != nil {
t.Fatal(err)
}
dir, err := os.MkdirTemp("", "ipfs-datastore-config-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
dir := t.TempDir()

spec := make(map[string]interface{})
err = json.Unmarshal(flatfsConfig, &spec)
Expand Down Expand Up @@ -202,11 +189,7 @@ func TestMeasureConfig(t *testing.T) {
if err != nil {
t.Fatal(err)
}
dir, err := os.MkdirTemp("", "ipfs-datastore-config-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
dir := t.TempDir()

spec := make(map[string]interface{})
err = json.Unmarshal(measureConfig, &spec)
Expand Down
21 changes: 6 additions & 15 deletions repo/fsrepo/fsrepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,9 @@ import (
config "github.com/ipfs/kubo/config"
)

// swap arg order
func testRepoPath(p string, t *testing.T) string {
name, err := os.MkdirTemp("", p)
if err != nil {
t.Fatal(err)
}
return name
}

func TestInitIdempotence(t *testing.T) {
t.Parallel()
path := testRepoPath("", t)
path := t.TempDir()
for i := 0; i < 10; i++ {
assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t, "multiple calls to init should succeed")
}
Expand All @@ -37,8 +28,8 @@ func Remove(repoPath string) error {

func TestCanManageReposIndependently(t *testing.T) {
t.Parallel()
pathA := testRepoPath("a", t)
pathB := testRepoPath("b", t)
pathA := t.TempDir()
pathB := t.TempDir()

t.Log("initialize two repos")
assert.Nil(Init(pathA, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t, "a", "should initialize successfully")
Expand All @@ -65,7 +56,7 @@ func TestCanManageReposIndependently(t *testing.T) {

func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
t.Parallel()
path := testRepoPath("test", t)
path := t.TempDir()

assert.True(!IsInitialized(path), t, "should NOT be initialized")
assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t, "should initialize successfully")
Expand All @@ -83,7 +74,7 @@ func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {

func TestDatastorePersistsFromRepoToRepo(t *testing.T) {
t.Parallel()
path := testRepoPath("test", t)
path := t.TempDir()

assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t)
r1, err := Open(path)
Expand All @@ -104,7 +95,7 @@ func TestDatastorePersistsFromRepoToRepo(t *testing.T) {

func TestOpenMoreThanOnceInSameProcess(t *testing.T) {
t.Parallel()
path := testRepoPath("", t)
path := t.TempDir()
assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t)

r1, err := Open(path)
Expand Down
7 changes: 1 addition & 6 deletions test/bench/bench_cli_ipfs_add/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ func benchmarkAdd(amount int64) (*testing.BenchmarkResult, error) {
b.SetBytes(amount)
for i := 0; i < b.N; i++ {
b.StopTimer()
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
benchmarkError = err
b.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tmpDir := b.TempDir()

env := append(
[]string{fmt.Sprintf("%s=%s", config.EnvDir, path.Join(tmpDir, config.DefaultPathName))}, // first in order to override
Expand Down
6 changes: 1 addition & 5 deletions test/bench/offline_add/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ func benchmarkAdd(amount int64) (*testing.BenchmarkResult, error) {
b.SetBytes(amount)
for i := 0; i < b.N; i++ {
b.StopTimer()
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tmpDir := b.TempDir()

env := append(os.Environ(), fmt.Sprintf("%s=%s", config.EnvDir, path.Join(tmpDir, config.DefaultPathName)))
setupCmd := func(cmd *exec.Cmd) {
Expand Down

0 comments on commit 5f76661

Please sign in to comment.