commit 7a64e615691ad264256a11f2a0bc229c2a08d787
parent 386b5628d9c63ac398a1fd4749bae2196c5994b4
Author: Sean Enck <sean@ttypty.com>
Date: Thu, 10 Aug 2023 19:02:06 -0400
migrate to testdata
Diffstat:
7 files changed, 52 insertions(+), 18 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,2 +1,2 @@
bin/
-*.kdbx
+testdata/
diff --git a/Makefile b/Makefile
@@ -23,7 +23,8 @@ runs: $(TARGET)
clean:
@rm -rf $(BUILD) tests/bin
- @find internal/ -type f -name "*.kdbx" -delete
+ @find internal/ -type f -wholename "*testdata*" -delete
+ @find internal/ -type d -empty -delete
.runci:
rm -rf .git
diff --git a/internal/app/conv_test.go b/internal/app/conv_test.go
@@ -8,12 +8,13 @@ import (
)
func TestConv(t *testing.T) {
+ fullSetup(t, false)
c := newMockCommand(t)
if err := app.Conv(c); err.Error() != "conv requires a file" {
t.Errorf("invalid error: %v", err)
}
c.buf = bytes.Buffer{}
- c.args = []string{"test.kdbx"}
+ c.args = []string{testFile()}
if err := app.Conv(c); err != nil {
t.Errorf("invalid error: %v", err)
}
diff --git a/internal/app/list_test.go b/internal/app/list_test.go
@@ -2,18 +2,30 @@ package app_test
import (
"os"
+ "path/filepath"
"testing"
"github.com/enckse/lockbox/internal/app"
"github.com/enckse/lockbox/internal/backend"
+ "github.com/enckse/lockbox/internal/platform"
)
+func testFile() string {
+ dir := "testdata"
+ file := filepath.Join(dir, "test.kdbx")
+ if !platform.PathExists(dir) {
+ os.Mkdir(dir, 0o755)
+ }
+ return file
+}
+
func fullSetup(t *testing.T, keep bool) *backend.Transaction {
+ file := testFile()
if !keep {
- os.Remove("test.kdbx")
+ os.Remove(file)
}
os.Setenv("LOCKBOX_READONLY", "no")
- os.Setenv("LOCKBOX_STORE", "test.kdbx")
+ os.Setenv("LOCKBOX_STORE", file)
os.Setenv("LOCKBOX_KEY", "test")
os.Setenv("LOCKBOX_KEYFILE", "")
os.Setenv("LOCKBOX_KEYMODE", "plaintext")
diff --git a/internal/app/totp_test.go b/internal/app/totp_test.go
@@ -39,11 +39,12 @@ func newMock(t *testing.T) (*mockOptions, app.TOTPOptions) {
}
func fullTOTPSetup(t *testing.T, keep bool) *backend.Transaction {
+ file := testFile()
if !keep {
- os.Remove("test.kdbx")
+ os.Remove(file)
}
os.Setenv("LOCKBOX_READONLY", "no")
- os.Setenv("LOCKBOX_STORE", "test.kdbx")
+ os.Setenv("LOCKBOX_STORE", file)
os.Setenv("LOCKBOX_KEY", "test")
os.Setenv("LOCKBOX_KEYFILE", "")
os.Setenv("LOCKBOX_KEYMODE", "plaintext")
diff --git a/internal/backend/actions_test.go b/internal/backend/actions_test.go
@@ -3,17 +3,32 @@ package backend_test
import (
"fmt"
"os"
+ "path/filepath"
"testing"
"github.com/enckse/lockbox/internal/backend"
+ "github.com/enckse/lockbox/internal/platform"
)
+const (
+ testDir = "testdata"
+)
+
+func testFile(name string) string {
+ file := filepath.Join(testDir, name)
+ if !platform.PathExists(testDir) {
+ os.Mkdir(testDir, 0o755)
+ }
+ return file
+}
+
func fullSetup(t *testing.T, keep bool) *backend.Transaction {
+ file := testFile("test.kdbx")
if !keep {
- os.Remove("test.kdbx")
+ os.Remove(file)
}
os.Setenv("LOCKBOX_READONLY", "no")
- os.Setenv("LOCKBOX_STORE", "test.kdbx")
+ os.Setenv("LOCKBOX_STORE", file)
os.Setenv("LOCKBOX_KEY", "test")
os.Setenv("LOCKBOX_KEYFILE", "")
os.Setenv("LOCKBOX_KEYMODE", "plaintext")
@@ -29,16 +44,18 @@ func fullSetup(t *testing.T, keep bool) *backend.Transaction {
func TestKeyFile(t *testing.T) {
os.Clearenv()
- os.Remove("keyfile_test.kdbx")
+ file := testFile("keyfile_test.kdbx")
+ keyFile := testFile("file.key")
+ os.Remove(file)
os.Setenv("LOCKBOX_READONLY", "no")
- os.Setenv("LOCKBOX_STORE", "keyfile_test.kdbx")
+ os.Setenv("LOCKBOX_STORE", file)
os.Setenv("LOCKBOX_KEY", "test")
- os.Setenv("LOCKBOX_KEYFILE", "file.key.kdbx")
+ os.Setenv("LOCKBOX_KEYFILE", keyFile)
os.Setenv("LOCKBOX_KEYMODE", "plaintext")
os.Setenv("LOCKBOX_TOTP", "totp")
os.Setenv("LOCKBOX_HOOKDIR", "")
os.Setenv("LOCKBOX_SET_MODTIME", "")
- os.WriteFile("file.key.kdbx", []byte("test"), 0o644)
+ os.WriteFile(keyFile, []byte("test"), 0o644)
tr, err := backend.NewTransaction()
if err != nil {
t.Errorf("failed: %v", err)
@@ -253,15 +270,17 @@ func TestKeyAndOrKeyFile(t *testing.T) {
func keyAndOrKeyFile(t *testing.T, key, keyFile bool) {
os.Clearenv()
- os.Remove("keyorkeyfile_test.kdbx")
- os.Setenv("LOCKBOX_STORE", "keyorkeyfile_test.kdbx")
+ file := testFile("keyorkeyfile.kdbx")
+ os.Remove(file)
+ os.Setenv("LOCKBOX_STORE", file)
if key {
os.Setenv("LOCKBOX_KEY", "test")
os.Setenv("LOCKBOX_KEYMODE", "plaintext")
}
if keyFile {
- os.Setenv("LOCKBOX_KEYFILE", "keyfileor.key.kdbx")
- os.WriteFile("keyfileor.key.kdbx", []byte("test"), 0o644)
+ key := testFile("keyfileor.key")
+ os.Setenv("LOCKBOX_KEYFILE", key)
+ os.WriteFile(key, []byte("test"), 0o644)
}
tr, err := backend.NewTransaction()
if err != nil {
diff --git a/internal/backend/hooks_test.go b/internal/backend/hooks_test.go
@@ -25,7 +25,7 @@ func TestHooks(t *testing.T) {
if _, err := backend.NewHook("b", backend.InsertAction); err.Error() != "hook directory does NOT exist" {
t.Errorf("wrong error: %v", err)
}
- testPath := "hooks.kdbx"
+ testPath := filepath.Join("testdata", "hooks.kdbx")
os.RemoveAll(testPath)
if err := os.MkdirAll(testPath, 0o755); err != nil {
t.Errorf("failed, mkdir: %v", err)