Files
gopass-mirror/internal/backend/storage_test.go
Eng Zer Jun d2f64457fb test: use T.TempDir to create temporary test directory (#2377)
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>

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-10-04 21:14:46 +02:00

37 lines
803 B
Go

package backend
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/stretchr/testify/assert"
)
func TestDetectStorage(t *testing.T) { //nolint:paralleltest
ctx := context.Background()
td := t.TempDir()
// all tests involving age should set GOPASS_HOMEDIR
t.Setenv("GOPASS_HOMEDIR", td)
ctx = ctxutil.WithPasswordCallback(ctx, func(_ string, _ bool) ([]byte, error) {
debug.Log("static test password callback")
return []byte("gopass"), nil
})
fsDir := filepath.Join(td, "fs")
assert.NoError(t, os.MkdirAll(fsDir, 0o700))
t.Run("detect fs", func(t *testing.T) {
r, err := DetectStorage(ctx, fsDir)
assert.NoError(t, err)
assert.NotNil(t, r)
assert.Equal(t, "fs", r.Name())
})
}