Skip to content

Commit

Permalink
test: use T.TempDir to create temporary test directory (gopasspw#2377)
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]>

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored Oct 4, 2022
1 parent 6957589 commit d2f6445
Show file tree
Hide file tree
Showing 27 changed files with 53 additions and 274 deletions.
11 changes: 3 additions & 8 deletions internal/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,20 @@ func TestAction(t *testing.T) {
func TestNew(t *testing.T) {
t.Parallel()

td, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(td)
}()

td := t.TempDir()
cfg := config.New()
sv := semver.Version{}

t.Run("init a new store", func(t *testing.T) { //nolint:paralleltest
_, err = New(cfg, sv)
_, err := New(cfg, sv)
require.NoError(t, err)
})

t.Run("init an existing plain store", func(t *testing.T) { //nolint:paralleltest
cfg.Path = filepath.Join(td, "store")
assert.NoError(t, os.MkdirAll(cfg.Path, 0o700))
assert.NoError(t, os.WriteFile(filepath.Join(cfg.Path, plain.IDFile), []byte("foobar"), 0o600))
_, err = New(cfg, sv)
_, err := New(cfg, sv)
assert.NoError(t, err)
})
}
8 changes: 0 additions & 8 deletions internal/backend/crypto/plain/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@ package plain

import (
"context"
"os"
"testing"

"github.com/blang/semver/v4"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPlain(t *testing.T) {
t.Parallel()

td, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(td)
}()

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)

Expand Down
8 changes: 1 addition & 7 deletions internal/backend/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ func TestDetectCrypto(t *testing.T) { //nolint:paralleltest
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()

td, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(td)
}()

fsDir := filepath.Join(td, "fs")
fsDir := filepath.Join(t.TempDir(), "fs")
_ = os.RemoveAll(fsDir)
assert.NoError(t, os.MkdirAll(fsDir, 0o700))
assert.NoError(t, os.WriteFile(filepath.Join(fsDir, tc.file), []byte("foo"), 0o600))
Expand Down
12 changes: 2 additions & 10 deletions internal/backend/rcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ func TestClone(t *testing.T) {

ctx := context.Background()

td, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(td)
}()
td := t.TempDir()

repo := filepath.Join(td, "repo")
require.NoError(t, os.MkdirAll(repo, 0o700))
Expand All @@ -43,11 +39,7 @@ func TestInitRCS(t *testing.T) {

ctx := context.Background()

td, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(td)
}()
td := t.TempDir()

buf := &bytes.Buffer{}
out.Stdout = buf
Expand Down
3 changes: 1 addition & 2 deletions internal/backend/storage/fs/fsck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func TestFsck(t *testing.T) {
ctx := context.Background()
ctx = ctxutil.WithHidden(ctx, true)

path, cleanup := newTempDir(t)
defer cleanup()
path := t.TempDir()

l := &loader{}
s, err := l.Init(ctx, path)
Expand Down
3 changes: 1 addition & 2 deletions internal/backend/storage/fs/rcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ func TestRCS(t *testing.T) {
t.Parallel()

ctx := context.Background()
path, cleanup := newTempDir(t)
defer cleanup()
path := t.TempDir()

g := New(path)
// the fs backend does not support the RCS operations
Expand Down
25 changes: 4 additions & 21 deletions internal/backend/storage/fs/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func TestSetAndGet(t *testing.T) {
otherContent := []byte(`other file content`)
ctx := context.Background()

path, cleanup := newTempDir(t)
defer cleanup()
path := t.TempDir()

s := &Store{path}

Expand Down Expand Up @@ -51,8 +50,7 @@ func TestMove(t *testing.T) {
otherContent := []byte(`other file content`)
ctx := context.Background()

path, cleanup := newTempDir(t)
defer cleanup()
path := t.TempDir()

s := &Store{path}

Expand Down Expand Up @@ -143,8 +141,7 @@ func TestRemoveEmptyParentDirectories(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

td, cleanup := newTempDir(t)
defer cleanup()
td := t.TempDir()

path := filepath.Join(append([]string{td}, test.storeRoot...)...)
subdir := filepath.Join(append([]string{path}, test.subdirs...)...)
Expand Down Expand Up @@ -227,8 +224,7 @@ func TestDelete(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

path, cleanup := newTempDir(t)
defer cleanup()
path := t.TempDir()

subdir := filepath.Join(append([]string{path}, test.location...)...)
if err := os.MkdirAll(subdir, 0o777); err != nil {
Expand Down Expand Up @@ -262,16 +258,3 @@ func TestDelete(t *testing.T) {
})
}
}

func newTempDir(t *testing.T) (string, func()) {
t.Helper()

td, err := os.MkdirTemp("", "gopass-")
if err != nil {
t.Error(err)
}

return td, func() {
_ = os.RemoveAll(td)
}
}
8 changes: 1 addition & 7 deletions internal/backend/storage/gitfs/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ import (
)

func TestGitConfig(t *testing.T) { //nolint:paralleltest
td, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(td)
}()

gitdir := filepath.Join(td, "git")
gitdir := filepath.Join(t.TempDir(), "git")
require.NoError(t, os.Mkdir(gitdir, 0o755))

ctx := context.Background()
Expand Down
6 changes: 1 addition & 5 deletions internal/backend/storage/gitfs/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import (
)

func TestGit(t *testing.T) { //nolint:paralleltest
td, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(td)
}()
td := t.TempDir()

gitdir := filepath.Join(td, "git")
require.NoError(t, os.Mkdir(gitdir, 0o755))
Expand Down
7 changes: 1 addition & 6 deletions internal/backend/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ import (
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDetectStorage(t *testing.T) { //nolint:paralleltest
ctx := context.Background()

td, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(td)
}()
td := t.TempDir()

// all tests involving age should set GOPASS_HOMEDIR
t.Setenv("GOPASS_HOMEDIR", td)
Expand Down
10 changes: 1 addition & 9 deletions internal/cache/disk_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package cache

import (
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestOnDisk(t *testing.T) { //nolint:paralleltest
td, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)

defer func() {
_ = os.RemoveAll(td)
}()

td := t.TempDir()
t.Setenv("GOPASS_HOMEDIR", td)

odc, err := NewOnDisk("test", time.Hour)
Expand Down
9 changes: 1 addition & 8 deletions internal/config/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,7 @@ func TestLoadError(t *testing.T) { //nolint:paralleltest
cfg, err := load(gcfg, false)
assert.Error(t, err)

td, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)

defer func() {
_ = os.RemoveAll(td)
}()

gcfg = filepath.Join(td, "foo", ".gopass.yml")
gcfg = filepath.Join(t.TempDir(), "foo", ".gopass.yml")
assert.NoError(t, os.Setenv("GOPASS_CONFIG", gcfg))
assert.NoError(t, cfg.Save())
}
Expand Down
6 changes: 1 addition & 5 deletions internal/store/leaf/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ func TestGPG(t *testing.T) {

ctx := context.Background()

tempdir, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempdir)
}()
tempdir := t.TempDir()

obuf := &bytes.Buffer{}
out.Stdout = obuf
Expand Down
7 changes: 1 addition & 6 deletions internal/store/leaf/fsck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/gopass/secrets"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFsck(t *testing.T) {
Expand All @@ -28,8 +27,7 @@ func TestFsck(t *testing.T) {
}()

// common setup
tempdir, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
tempdir := t.TempDir()

s := &Store{
alias: "",
Expand All @@ -47,9 +45,6 @@ func TestFsck(t *testing.T) {

assert.NoError(t, s.Fsck(ctx, ""))
obuf.Reset()

// common tear down
_ = os.RemoveAll(tempdir)
}

func TestCompareStringSlices(t *testing.T) {
Expand Down
8 changes: 1 addition & 7 deletions internal/store/leaf/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@ package leaf

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestInit(t *testing.T) {
t.Parallel()

ctx := context.Background()

tempdir, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempdir)
}()
tempdir := t.TempDir()

s, err := createSubStore(tempdir)
assert.NoError(t, err)
Expand Down
8 changes: 1 addition & 7 deletions internal/store/leaf/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package leaf

import (
"context"
"os"
"testing"

"github.com/gopasspw/gopass/pkg/gopass/secrets"
Expand All @@ -15,12 +14,7 @@ func TestLink(t *testing.T) {

ctx := context.Background()

tempdir, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempdir)
}()

tempdir := t.TempDir()
t.Logf(tempdir)

s, err := createSubStore(tempdir)
Expand Down
4 changes: 1 addition & 3 deletions internal/store/leaf/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,10 @@ func TestList(t *testing.T) {

t.Run(tc.name, func(t *testing.T) {
// common setup
tempdir, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
tempdir := t.TempDir()

defer func() {
obuf.Reset()
_ = os.RemoveAll(tempdir)
}()

s := &Store{
Expand Down
Loading

0 comments on commit d2f6445

Please sign in to comment.