lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 840e52142789fe40ac97f43b0dd90bdd9d622aba
parent 7ad58a74c0119f7a89927aabcb0cf909f5fd92a1
Author: Sean Enck <sean@ttypty.com>
Date:   Thu, 10 Oct 2024 18:00:27 -0400

use t.Setenv and fixup Clearenv calls

Diffstat:
Minternal/app/info_test.go | 9++++-----
Minternal/app/list_test.go | 16++++++++--------
Minternal/app/pwgen_test.go | 50++++++++++++++++++++++++--------------------------
Minternal/app/rekey_test.go | 2++
Minternal/app/totp_test.go | 18+++++++++---------
Minternal/backend/actions_test.go | 60++++++++++++++++++++++++++++++------------------------------
Minternal/backend/hooks_test.go | 9++++-----
Minternal/backend/query_test.go | 15++++++---------
Minternal/config/core_test.go | 60+++++++++++++++++++++++++++++-------------------------------
Minternal/config/key_test.go | 84++++++++++++++++++++++++++++++++++++-------------------------------------------
Minternal/config/vars_test.go | 45+++++++++++++++++++++------------------------
Minternal/platform/clipboard_test.go | 43+++++++++++++++++++++----------------------
Minternal/platform/os_test.go | 11+++++------
13 files changed, 201 insertions(+), 221 deletions(-)

diff --git a/internal/app/info_test.go b/internal/app/info_test.go @@ -54,7 +54,7 @@ func TestEnvInfo(t *testing.T) { if buf.String() != "" { t.Error("nothing written") } - os.Setenv("LOCKBOX_STORE", "1") + t.Setenv("LOCKBOX_STORE", "1") ok, err = app.Info(&buf, "env", []string{}) if !ok || err != nil { t.Errorf("invalid error: %v", err) @@ -71,7 +71,6 @@ func TestEnvInfo(t *testing.T) { } func TestCompletionInfo(t *testing.T) { - defer os.Clearenv() for k, v := range map[string]string{ "zsh": "typeset -A opt_args", "fish": "set -f commands", @@ -80,10 +79,10 @@ func TestCompletionInfo(t *testing.T) { for _, b := range []bool{true, false} { os.Clearenv() sub := []string{k} - os.Setenv("SHELL", "invalid") + t.Setenv("SHELL", "invalid") if b { sub = []string{} - os.Setenv("SHELL", k) + t.Setenv("SHELL", k) } var buf bytes.Buffer ok, err := app.Info(&buf, "completions", sub) @@ -105,7 +104,7 @@ func TestCompletionInfo(t *testing.T) { t.Errorf("invalid error: %v", err) } os.Clearenv() - os.Setenv("SHELL", "bad") + t.Setenv("SHELL", "bad") if _, err := app.Info(&buf, "completions", []string{}); err.Error() != "unknown completion type: bad" { t.Errorf("invalid error: %v", err) } diff --git a/internal/app/list_test.go b/internal/app/list_test.go @@ -24,14 +24,14 @@ func fullSetup(t *testing.T, keep bool) *backend.Transaction { if !keep { os.Remove(file) } - os.Setenv("LOCKBOX_READONLY", "no") - os.Setenv("LOCKBOX_STORE", file) - os.Setenv("LOCKBOX_KEY", "test") - os.Setenv("LOCKBOX_KEYFILE", "") - os.Setenv("LOCKBOX_KEYMODE", "plaintext") - os.Setenv("LOCKBOX_TOTP", "totp") - os.Setenv("LOCKBOX_HOOKDIR", "") - os.Setenv("LOCKBOX_SET_MODTIME", "") + t.Setenv("LOCKBOX_READONLY", "no") + t.Setenv("LOCKBOX_STORE", file) + t.Setenv("LOCKBOX_KEY", "test") + t.Setenv("LOCKBOX_KEYFILE", "") + t.Setenv("LOCKBOX_KEYMODE", "plaintext") + t.Setenv("LOCKBOX_TOTP", "totp") + t.Setenv("LOCKBOX_HOOKDIR", "") + t.Setenv("LOCKBOX_SET_MODTIME", "") tr, err := backend.NewTransaction() if err != nil { t.Errorf("failed: %v", err) diff --git a/internal/app/pwgen_test.go b/internal/app/pwgen_test.go @@ -23,34 +23,33 @@ done } func TestGenerateError(t *testing.T) { - defer os.Clearenv() m := newMockCommand(t) pwgenPath := setupGenScript() - os.Setenv("LOCKBOX_PWGEN_COUNT", "0") + t.Setenv("LOCKBOX_PWGEN_COUNT", "0") if err := app.GeneratePassword(m); err == nil || err.Error() != "word count must be > 0" { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_PWGEN_COUNT", "1") + t.Setenv("LOCKBOX_PWGEN_COUNT", "1") if err := app.GeneratePassword(m); err == nil || err.Error() != "word list command must set" { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_PWGEN_WORDLIST", "1 x") + t.Setenv("LOCKBOX_PWGEN_WORDLIST", "1 x") if err := app.GeneratePassword(m); err == nil || !strings.Contains(err.Error(), "exec: \"1\":") { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_PWGEN_WORDLIST", pwgenPath) + t.Setenv("LOCKBOX_PWGEN_WORDLIST", pwgenPath) if err := app.GeneratePassword(m); err == nil || err.Error() != "no sources given" { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s 1", pwgenPath)) + t.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s 1", pwgenPath)) if err := app.GeneratePassword(m); err != nil { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s aloj 1", pwgenPath)) + t.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s aloj 1", pwgenPath)) if err := app.GeneratePassword(m); err != nil { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_NOPWGEN", "yes") + t.Setenv("LOCKBOX_NOPWGEN", "yes") if err := app.GeneratePassword(m); err == nil || err.Error() != "password generation is disabled" { t.Errorf("invalid error: %v", err) } @@ -68,35 +67,34 @@ func testPasswordGen(t *testing.T, expect string) { } func TestGenerate(t *testing.T) { - defer os.Clearenv() pwgenPath := setupGenScript() - os.Setenv("LOCKBOX_PWGEN_COUNT", "1") - os.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s 1", pwgenPath)) + t.Setenv("LOCKBOX_PWGEN_COUNT", "1") + t.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s 1", pwgenPath)) testPasswordGen(t, "1") - os.Setenv("LOCKBOX_PWGEN_COUNT", "10") - os.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s 1 1 1 1 1 1 1 1 1 1 1 1", pwgenPath)) + t.Setenv("LOCKBOX_PWGEN_COUNT", "10") + t.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s 1 1 1 1 1 1 1 1 1 1 1 1", pwgenPath)) testPasswordGen(t, "1-1-1-1-1-1-1-1-1-1") - os.Setenv("LOCKBOX_PWGEN_COUNT", "4") - os.Setenv("LOCKBOX_PWGEN_TITLE", "yes") - os.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s a a a a a a a a a a a a a a a", pwgenPath)) + t.Setenv("LOCKBOX_PWGEN_COUNT", "4") + t.Setenv("LOCKBOX_PWGEN_TITLE", "yes") + t.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s a a a a a a a a a a a a a a a", pwgenPath)) testPasswordGen(t, "A-A-A-A") - os.Setenv("LOCKBOX_PWGEN_CHARS", "bc") - os.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s abc abc abc abc abc aaa aaa aa a", pwgenPath)) + t.Setenv("LOCKBOX_PWGEN_CHARS", "bc") + t.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s abc abc abc abc abc aaa aaa aa a", pwgenPath)) testPasswordGen(t, "Bc-Bc-Bc-Bc") os.Unsetenv("LOCKBOX_PWGEN_CHARS") - os.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s a a a a a a a a a a a a a a a", pwgenPath)) - os.Setenv("LOCKBOX_PWGEN_TITLE", "no") - os.Setenv("LOCKBOX_PWGEN_TITLE", "no") + t.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s a a a a a a a a a a a a a a a", pwgenPath)) + t.Setenv("LOCKBOX_PWGEN_TITLE", "no") + t.Setenv("LOCKBOX_PWGEN_TITLE", "no") testPasswordGen(t, "a-a-a-a") // NOTE: this allows templating below in golang - os.Setenv("DOLLAR", "$") - os.Setenv("LOCKBOX_PWGEN_TEMPLATE", "{{range ${DOLLAR}idx, ${DOLLAR}val := .}}{{if lt ${DOLLAR}idx 5}}-{{end}}{{ ${DOLLAR}val.Text }}{{ ${DOLLAR}val.Position.Start }}{{ ${DOLLAR}val.Position.End }}{{end}}") + t.Setenv("DOLLAR", "$") + t.Setenv("LOCKBOX_PWGEN_TEMPLATE", "{{range ${DOLLAR}idx, ${DOLLAR}val := .}}{{if lt ${DOLLAR}idx 5}}-{{end}}{{ ${DOLLAR}val.Text }}{{ ${DOLLAR}val.Position.Start }}{{ ${DOLLAR}val.Position.End }}{{end}}") testPasswordGen(t, "-a01-a12-a23-a34") - os.Setenv("LOCKBOX_PWGEN_TEMPLATE", "{{range [%]idx, [%]val := .}}{{if lt [%]idx 5}}-{{end}}{{ [%]val.Text }}{{end}}") + t.Setenv("LOCKBOX_PWGEN_TEMPLATE", "{{range [%]idx, [%]val := .}}{{if lt [%]idx 5}}-{{end}}{{ [%]val.Text }}{{end}}") testPasswordGen(t, "-a-a-a-a") os.Unsetenv("LOCKBOX_PWGEN_TEMPLATE") - os.Setenv("LOCKBOX_PWGEN_TITLE", "yes") - os.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s abc axy axY aZZZ aoijafea aoiajfoea afaeoa", pwgenPath)) + t.Setenv("LOCKBOX_PWGEN_TITLE", "yes") + t.Setenv("LOCKBOX_PWGEN_WORDLIST", fmt.Sprintf("%s abc axy axY aZZZ aoijafea aoiajfoea afaeoa", pwgenPath)) m := newMockCommand(t) if err := app.GeneratePassword(m); err != nil { t.Errorf("invalid error: %v", err) diff --git a/internal/app/rekey_test.go b/internal/app/rekey_test.go @@ -47,6 +47,7 @@ func (m *mockKeyer) Writer() io.Writer { func TestReKey(t *testing.T) { newMockCommand(t) mock := &mockKeyer{} + mock.t = t if err := app.ReKey(mock); err != nil { t.Errorf("invalid error: %v", err) } @@ -64,6 +65,7 @@ func TestReKey(t *testing.T) { func TestReKeyPipe(t *testing.T) { newMockCommand(t) mock := &mockKeyer{} + mock.t = t mock.pipe = true if err := app.ReKey(mock); err == nil || err.Error() != "key and/or keyfile must be set" { t.Errorf("invalid error: %v", err) diff --git a/internal/app/totp_test.go b/internal/app/totp_test.go @@ -43,15 +43,15 @@ func fullTOTPSetup(t *testing.T, keep bool) *backend.Transaction { if !keep { os.Remove(file) } - os.Setenv("LOCKBOX_READONLY", "no") - os.Setenv("LOCKBOX_STORE", file) - os.Setenv("LOCKBOX_KEY", "test") - os.Setenv("LOCKBOX_KEYFILE", "") - os.Setenv("LOCKBOX_KEYMODE", "plaintext") - os.Setenv("LOCKBOX_TOTP", "totp") - os.Setenv("LOCKBOX_HOOKDIR", "") - os.Setenv("LOCKBOX_SET_MODTIME", "") - os.Setenv("LOCKBOX_TOTP_MAX", "1") + t.Setenv("LOCKBOX_READONLY", "no") + t.Setenv("LOCKBOX_STORE", file) + t.Setenv("LOCKBOX_KEY", "test") + t.Setenv("LOCKBOX_KEYFILE", "") + t.Setenv("LOCKBOX_KEYMODE", "plaintext") + t.Setenv("LOCKBOX_TOTP", "totp") + t.Setenv("LOCKBOX_HOOKDIR", "") + t.Setenv("LOCKBOX_SET_MODTIME", "") + t.Setenv("LOCKBOX_TOTP_MAX", "1") tr, err := backend.NewTransaction() if err != nil { t.Errorf("failed: %v", err) diff --git a/internal/backend/actions_test.go b/internal/backend/actions_test.go @@ -27,14 +27,14 @@ func fullSetup(t *testing.T, keep bool) *backend.Transaction { if !keep { os.Remove(file) } - os.Setenv("LOCKBOX_READONLY", "no") - os.Setenv("LOCKBOX_STORE", file) - os.Setenv("LOCKBOX_KEY", "test") - os.Setenv("LOCKBOX_KEYFILE", "") - os.Setenv("LOCKBOX_KEYMODE", "plaintext") - os.Setenv("LOCKBOX_TOTP", "totp") - os.Setenv("LOCKBOX_HOOKDIR", "") - os.Setenv("LOCKBOX_SET_MODTIME", "") + t.Setenv("LOCKBOX_READONLY", "no") + t.Setenv("LOCKBOX_STORE", file) + t.Setenv("LOCKBOX_KEY", "test") + t.Setenv("LOCKBOX_KEYFILE", "") + t.Setenv("LOCKBOX_KEYMODE", "plaintext") + t.Setenv("LOCKBOX_TOTP", "totp") + t.Setenv("LOCKBOX_HOOKDIR", "") + t.Setenv("LOCKBOX_SET_MODTIME", "") tr, err := backend.NewTransaction() if err != nil { t.Errorf("failed: %v", err) @@ -47,14 +47,14 @@ func TestKeyFile(t *testing.T) { file := testFile("keyfile_test.kdbx") keyFile := testFile("file.key") os.Remove(file) - os.Setenv("LOCKBOX_READONLY", "no") - os.Setenv("LOCKBOX_STORE", file) - os.Setenv("LOCKBOX_KEY", "test") - os.Setenv("LOCKBOX_KEYFILE", keyFile) - os.Setenv("LOCKBOX_KEYMODE", "plaintext") - os.Setenv("LOCKBOX_TOTP", "totp") - os.Setenv("LOCKBOX_HOOKDIR", "") - os.Setenv("LOCKBOX_SET_MODTIME", "") + t.Setenv("LOCKBOX_READONLY", "no") + t.Setenv("LOCKBOX_STORE", file) + t.Setenv("LOCKBOX_KEY", "test") + t.Setenv("LOCKBOX_KEYFILE", keyFile) + t.Setenv("LOCKBOX_KEYMODE", "plaintext") + t.Setenv("LOCKBOX_TOTP", "totp") + t.Setenv("LOCKBOX_HOOKDIR", "") + t.Setenv("LOCKBOX_SET_MODTIME", "") os.WriteFile(keyFile, []byte("test"), 0o644) tr, err := backend.NewTransaction() if err != nil { @@ -71,7 +71,7 @@ func setup(t *testing.T) *backend.Transaction { func TestNoWriteOnRO(t *testing.T) { setup(t) - os.Setenv("LOCKBOX_READONLY", "yes") + t.Setenv("LOCKBOX_READONLY", "yes") tr, _ := backend.NewTransaction() if err := tr.Insert("a/a/a", "a"); err.Error() != "unable to alter database in readonly mode" { t.Errorf("wrong error: %v", err) @@ -80,7 +80,7 @@ func TestNoWriteOnRO(t *testing.T) { func TestBadTOTP(t *testing.T) { tr := setup(t) - os.Setenv("LOCKBOX_TOTP", "Title") + t.Setenv("LOCKBOX_TOTP", "Title") if err := tr.Insert("a/a/a", "a"); err.Error() != "invalid totp field, uses restricted name" { t.Errorf("wrong error: %v", err) } @@ -283,16 +283,16 @@ func keyAndOrKeyFile(t *testing.T, key, keyFile bool) { os.Clearenv() file := testFile("keyorkeyfile.kdbx") os.Remove(file) - os.Setenv("LOCKBOX_STORE", file) + t.Setenv("LOCKBOX_STORE", file) if key { - os.Setenv("LOCKBOX_KEY", "test") - os.Setenv("LOCKBOX_KEYMODE", "plaintext") + t.Setenv("LOCKBOX_KEY", "test") + t.Setenv("LOCKBOX_KEYMODE", "plaintext") } else { - os.Setenv("LOCKBOX_KEYMODE", "none") + t.Setenv("LOCKBOX_KEYMODE", "none") } if keyFile { key := testFile("keyfileor.key") - os.Setenv("LOCKBOX_KEYFILE", key) + t.Setenv("LOCKBOX_KEYFILE", key) os.WriteFile(key, []byte("test"), 0o644) } tr, err := backend.NewTransaction() @@ -317,13 +317,13 @@ func TestReKey(t *testing.T) { f := "rekey_test.kdbx" file := testFile(f) defer os.Remove(filepath.Join(testDir, f)) - os.Setenv("LOCKBOX_READONLY", "no") - os.Setenv("LOCKBOX_STORE", file) - os.Setenv("LOCKBOX_KEY", "test") - os.Setenv("LOCKBOX_KEYMODE", "plaintext") - os.Setenv("LOCKBOX_TOTP", "totp") - os.Setenv("LOCKBOX_HOOKDIR", "") - os.Setenv("LOCKBOX_SET_MODTIME", "") + t.Setenv("LOCKBOX_READONLY", "no") + t.Setenv("LOCKBOX_STORE", file) + t.Setenv("LOCKBOX_KEY", "test") + t.Setenv("LOCKBOX_KEYMODE", "plaintext") + t.Setenv("LOCKBOX_TOTP", "totp") + t.Setenv("LOCKBOX_HOOKDIR", "") + t.Setenv("LOCKBOX_SET_MODTIME", "") tr, err := backend.NewTransaction() if err != nil { t.Errorf("failed: %v", err) diff --git a/internal/backend/hooks_test.go b/internal/backend/hooks_test.go @@ -10,8 +10,7 @@ import ( ) func TestHooks(t *testing.T) { - defer os.Clearenv() - os.Setenv("LOCKBOX_HOOKDIR", "") + t.Setenv("LOCKBOX_HOOKDIR", "") h, err := backend.NewHook("a", backend.InsertAction) if err != nil { t.Errorf("invalid error: %v", err) @@ -22,7 +21,7 @@ func TestHooks(t *testing.T) { if _, err := backend.NewHook("", backend.InsertAction); err.Error() != "empty path is not allowed for hooks" { t.Errorf("wrong error: %v", err) } - os.Setenv("LOCKBOX_HOOKDIR", "is_garbage") + t.Setenv("LOCKBOX_HOOKDIR", "is_garbage") if _, err := backend.NewHook("b", backend.InsertAction); err.Error() != "hook directory does NOT exist" { t.Errorf("wrong error: %v", err) } @@ -31,7 +30,7 @@ func TestHooks(t *testing.T) { if err := os.MkdirAll(testPath, 0o755); err != nil { t.Errorf("failed, mkdir: %v", err) } - os.Setenv("LOCKBOX_HOOKDIR", testPath) + t.Setenv("LOCKBOX_HOOKDIR", testPath) h, err = backend.NewHook("a", backend.InsertAction) if err != nil { t.Errorf("invalid error: %v", err) @@ -60,7 +59,7 @@ func TestHooks(t *testing.T) { if err := h.Run(backend.HookPre); strings.Contains("fork/exec", err.Error()) { t.Errorf("wrong error: %v", err) } - os.Setenv("LOCKBOX_NOHOOKS", "yes") + t.Setenv("LOCKBOX_NOHOOKS", "yes") h, err = backend.NewHook("a", backend.InsertAction) if err != nil { t.Errorf("invalid error: %v", err) diff --git a/internal/backend/query_test.go b/internal/backend/query_test.go @@ -97,8 +97,7 @@ func TestValueModes(t *testing.T) { if len(m.ModTime) < 20 { t.Errorf("invalid date/time") } - os.Setenv("LOCKBOX_JSON_DATA_HASH_LENGTH", "10") - defer os.Clearenv() + t.Setenv("LOCKBOX_JSON_DATA_HASH_LENGTH", "10") q, err = fullSetup(t, true).Get("test/test/abc", backend.JSONValue) if err != nil { t.Errorf("no error: %v", err) @@ -128,8 +127,7 @@ func TestValueModes(t *testing.T) { if len(m.ModTime) < 20 || m.Data == "" { t.Errorf("invalid json: %v", m) } - os.Setenv("LOCKBOX_JSON_DATA", "plAINtExt") - defer os.Clearenv() + t.Setenv("LOCKBOX_JSON_DATA", "plAINtExt") q, err = fullSetup(t, true).Get("test/test/abc", backend.JSONValue) if err != nil { t.Errorf("no error: %v", err) @@ -141,8 +139,7 @@ func TestValueModes(t *testing.T) { if len(m.ModTime) < 20 || m.Data != "tedst" { t.Errorf("invalid json: %v", m) } - os.Setenv("LOCKBOX_JSON_DATA", "emPTY") - defer os.Clearenv() + t.Setenv("LOCKBOX_JSON_DATA", "emPTY") q, err = fullSetup(t, true).Get("test/test/abc", backend.JSONValue) if err != nil { t.Errorf("no error: %v", err) @@ -214,7 +211,7 @@ func TestQueryCallback(t *testing.T) { func TestSetModTime(t *testing.T) { testDateTime := "2022-12-30T12:34:56-05:00" tr := fullSetup(t, false) - os.Setenv("LOCKBOX_SET_MODTIME", testDateTime) + t.Setenv("LOCKBOX_SET_MODTIME", testDateTime) tr.Insert("test/xyz", "test") q, err := fullSetup(t, true).Get("test/xyz", backend.JSONValue) if err != nil { @@ -229,7 +226,7 @@ func TestSetModTime(t *testing.T) { } tr = fullSetup(t, false) - os.Setenv("LOCKBOX_SET_MODTIME", "") + t.Setenv("LOCKBOX_SET_MODTIME", "") tr.Insert("test/xyz", "test") q, err = fullSetup(t, true).Get("test/xyz", backend.JSONValue) if err != nil { @@ -244,7 +241,7 @@ func TestSetModTime(t *testing.T) { } tr = fullSetup(t, false) - os.Setenv("LOCKBOX_SET_MODTIME", "garbage") + t.Setenv("LOCKBOX_SET_MODTIME", "garbage") err = tr.Insert("test/xyz", "test") if err == nil || !strings.Contains(err.Error(), "parsing time") { t.Errorf("invalid error: %v", err) diff --git a/internal/config/core_test.go b/internal/config/core_test.go @@ -31,7 +31,6 @@ func isSet(key string) bool { func TestSet(t *testing.T) { os.Clearenv() - defer os.Clearenv() config.EnvStore.Set("TEST") if config.EnvStore.Get() != "TEST" { t.Errorf("invalid set/get") @@ -54,7 +53,7 @@ func TestKeyValue(t *testing.T) { func TestNewPlatform(t *testing.T) { for _, item := range config.Platforms.List() { - os.Setenv("LOCKBOX_PLATFORM", item) + t.Setenv("LOCKBOX_PLATFORM", item) s, err := config.NewPlatform() if err != nil { t.Errorf("invalid clipboard: %v", err) @@ -66,7 +65,7 @@ func TestNewPlatform(t *testing.T) { } func TestNewPlatformUnknown(t *testing.T) { - os.Setenv("LOCKBOX_PLATFORM", "afleaj") + t.Setenv("LOCKBOX_PLATFORM", "afleaj") _, err := config.NewPlatform() if err == nil || err.Error() != "unknown platform mode" { t.Errorf("error expected for platform: %v", err) @@ -108,35 +107,35 @@ func TestParseWindows(t *testing.T) { func TestNewEnvFiles(t *testing.T) { os.Clearenv() - os.Setenv("LOCKBOX_ENV", "none") + t.Setenv("LOCKBOX_ENV", "none") f, err := config.NewEnvFiles() if len(f) != 0 || err != nil { t.Errorf("invalid files: %v %v", f, err) } - os.Setenv("LOCKBOX_ENV", "test") + t.Setenv("LOCKBOX_ENV", "test") f, err = config.NewEnvFiles() if len(f) != 1 || f[0] != "test" || err != nil { t.Errorf("invalid files: %v %v", f, err) } - os.Setenv("HOME", "test") - os.Setenv("LOCKBOX_ENV", "detect") + t.Setenv("HOME", "test") + t.Setenv("LOCKBOX_ENV", "detect") f, err = config.NewEnvFiles() if len(f) != 2 || err != nil { t.Errorf("invalid files: %v %v", f, err) } - os.Setenv("LOCKBOX_ENV", "detect") - os.Setenv("XDG_CONFIG_HOME", "test") + t.Setenv("LOCKBOX_ENV", "detect") + t.Setenv("XDG_CONFIG_HOME", "test") f, err = config.NewEnvFiles() if len(f) != 4 || err != nil { t.Errorf("invalid files: %v %v", f, err) } - os.Setenv("LOCKBOX_ENV", "detect") + t.Setenv("LOCKBOX_ENV", "detect") os.Unsetenv("HOME") f, err = config.NewEnvFiles() if len(f) != 2 || err != nil { t.Errorf("invalid files: %v %v", f, err) } - os.Setenv("LOCKBOX_ENV", "detect") + t.Setenv("LOCKBOX_ENV", "detect") os.Unsetenv("XDG_CONFIG_HOME") _, err = config.NewEnvFiles() if err == nil || err.Error() != "unable to initialize default config locations" { @@ -158,8 +157,7 @@ func TestIsUnset(t *testing.T) { if err != nil || o { t.Error("was set") } - os.Setenv("UNSET_TEST", "abc") - defer os.Clearenv() + t.Setenv("UNSET_TEST", "abc") config.IsUnset("UNSET_TEST", "") if isSet("UNSET_TEST") { t.Error("found unset var") @@ -172,10 +170,10 @@ func TestEnviron(t *testing.T) { if len(e) != 0 { t.Error("invalid environ") } - os.Setenv("LOCKBOX_STORE", "1") - os.Setenv("LOCKBOX_2", "2") - os.Setenv("LOCKBOX_KEY", "2") - os.Setenv("LOCKBOX_ENV", "2") + t.Setenv("LOCKBOX_STORE", "1") + t.Setenv("LOCKBOX_2", "2") + t.Setenv("LOCKBOX_KEY", "2") + t.Setenv("LOCKBOX_ENV", "2") e = config.Environ() if len(e) != 2 || fmt.Sprintf("%v", e) != "[LOCKBOX_KEY=2 LOCKBOX_STORE=1]" { t.Errorf("invalid environ: %v", e) @@ -184,8 +182,8 @@ func TestEnviron(t *testing.T) { func TestExpandParsed(t *testing.T) { os.Clearenv() - os.Setenv("TEST_ABC", "1") - os.Setenv("LOCKBOX_ENV_EXPANDS", "a") + t.Setenv("TEST_ABC", "1") + t.Setenv("LOCKBOX_ENV_EXPANDS", "a") _, err := config.ExpandParsed(nil) if err == nil || err.Error() != "invalid input variables" { t.Errorf("invalid error: %v", err) @@ -194,7 +192,7 @@ func TestExpandParsed(t *testing.T) { if err != nil || len(r) != 0 { t.Errorf("invalid expand") } - os.Setenv("LOCKBOX_ENV_EXPANDS", "a") + t.Setenv("LOCKBOX_ENV_EXPANDS", "a") ins := make(map[string]string) ins["TEST"] = "$TEST_ABC" _, err = config.ExpandParsed(ins) @@ -207,33 +205,33 @@ func TestExpandParsed(t *testing.T) { t.Errorf("invalid expand: %v", r) } delete(ins, "LOCKBOX_ENV_EXPANDS") - os.Setenv("LOCKBOX_ENV_EXPANDS", "2") + t.Setenv("LOCKBOX_ENV_EXPANDS", "2") r, err = config.ExpandParsed(ins) if err != nil || len(r) != 1 || r["TEST"] != "1" { t.Errorf("invalid expand: %v", r) } - os.Setenv("LOCKBOX_ENV_EXPANDS", "2") + t.Setenv("LOCKBOX_ENV_EXPANDS", "2") r, err = config.ExpandParsed(ins) if err != nil || len(r) != 1 || r["TEST"] != "1" { t.Errorf("invalid expand: %v", r) } - os.Setenv("TEST_ABC", "$OTHER_TEST") - os.Setenv("OTHER_TEST", "$ANOTHER_TEST") - os.Setenv("ANOTHER_TEST", "2") - os.Setenv("LOCKBOX_ENV_EXPANDS", "1") + t.Setenv("TEST_ABC", "$OTHER_TEST") + t.Setenv("OTHER_TEST", "$ANOTHER_TEST") + t.Setenv("ANOTHER_TEST", "2") + t.Setenv("LOCKBOX_ENV_EXPANDS", "1") if _, err = config.ExpandParsed(ins); err == nil || err.Error() != "reached maximum expand cycle count" { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_ENV_EXPANDS", "2") + t.Setenv("LOCKBOX_ENV_EXPANDS", "2") ins["OTHER_FIRST"] = "2" ins["OTHER_OTHER"] = "$ANOTHER_TEST|$TEST_ABC|$OTHER_TEST" ins["OTHER"] = "$OTHER_OTHER|$OTHER_FIRST" - os.Setenv("LOCKBOX_ENV_EXPANDS", "20") + t.Setenv("LOCKBOX_ENV_EXPANDS", "20") r, err = config.ExpandParsed(ins) if err != nil || len(r) != 4 || r["TEST"] != "2" || r["OTHER"] != "2|2|2|2" || r["OTHER_OTHER"] != "2|2|2" { t.Errorf("invalid expand: %v", r) } - os.Setenv("LOCKBOX_ENV_EXPANDS", "0") + t.Setenv("LOCKBOX_ENV_EXPANDS", "0") delete(ins, "OTHER_FIRST") delete(ins, "OTHER") delete(ins, "OTHER_OTHER") @@ -252,12 +250,12 @@ func TestExpandParsed(t *testing.T) { } ins["LOCKBOX_ENV_EXPANDS"] = "5" ins["TEST"] = "\"abc\"" - os.Setenv("LOCKBOX_ENV_QUOTED", "yes") + t.Setenv("LOCKBOX_ENV_QUOTED", "yes") r, err = config.ExpandParsed(ins) if err != nil || len(r) != 2 || r["TEST"] != "abc" { t.Errorf("invalid expand: %v", r) } - os.Setenv("LOCKBOX_ENV_QUOTED", "no") + t.Setenv("LOCKBOX_ENV_QUOTED", "no") ins["TEST"] = "\"abc\"" r, err = config.ExpandParsed(ins) if err != nil || len(r) != 2 || r["TEST"] != "\"abc\"" { diff --git a/internal/config/key_test.go b/internal/config/key_test.go @@ -2,7 +2,6 @@ package config_test import ( "errors" - "os" "strings" "testing" @@ -10,75 +9,72 @@ import ( ) func TestDefaultKey(t *testing.T) { - defer os.Clearenv() - os.Setenv("LOCKBOX_KEYMODE", "") - os.Setenv("LOCKBOX_KEY", "test") + t.Setenv("LOCKBOX_KEYMODE", "") + t.Setenv("LOCKBOX_KEY", "test") if _, err := config.NewKey(config.IgnoreKeyMode); err != nil { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_KEYMODE", "") - os.Setenv("LOCKBOX_KEY", "") + t.Setenv("LOCKBOX_KEYMODE", "") + t.Setenv("LOCKBOX_KEY", "") if _, err := config.NewKey(config.DefaultKeyMode); err == nil || err.Error() != "key MUST be set in this key mode" { t.Errorf("invalid error: %v", err) } } func TestNewKeyErrors(t *testing.T) { - defer os.Clearenv() - os.Setenv("LOCKBOX_KEYMODE", "invalid") + t.Setenv("LOCKBOX_KEYMODE", "invalid") if _, err := config.NewKey(config.IgnoreKeyMode); err == nil || err.Error() != "unknown key mode: invalid" { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_KEYMODE", "none") - os.Setenv("LOCKBOX_KEY", " test") + t.Setenv("LOCKBOX_KEYMODE", "none") + t.Setenv("LOCKBOX_KEY", " test") if _, err := config.NewKey(config.IgnoreKeyMode); err == nil || err.Error() != "key can NOT be set in this key mode" { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_KEYMODE", "ask") - os.Setenv("LOCKBOX_KEY", "test") + t.Setenv("LOCKBOX_KEYMODE", "ask") + t.Setenv("LOCKBOX_KEY", "test") if _, err := config.NewKey(config.IgnoreKeyMode); err == nil || err.Error() != "key can NOT be set in this key mode" { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_KEYMODE", "command") - os.Setenv("LOCKBOX_KEY", " ") + t.Setenv("LOCKBOX_KEYMODE", "command") + t.Setenv("LOCKBOX_KEY", " ") if _, err := config.NewKey(config.IgnoreKeyMode); err == nil || err.Error() != "key MUST be set in this key mode" { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_KEYMODE", "plaintext") - os.Setenv("LOCKBOX_KEY", "") + t.Setenv("LOCKBOX_KEYMODE", "plaintext") + t.Setenv("LOCKBOX_KEY", "") if _, err := config.NewKey(config.IgnoreKeyMode); err == nil || err.Error() != "key MUST be set in this key mode" { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_INTERACTIVE", "yes") - os.Setenv("LOCKBOX_KEYMODE", "ask") - os.Setenv("LOCKBOX_KEY", "") + t.Setenv("LOCKBOX_INTERACTIVE", "yes") + t.Setenv("LOCKBOX_KEYMODE", "ask") + t.Setenv("LOCKBOX_KEY", "") if _, err := config.NewKey(config.IgnoreKeyMode); err != nil { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_INTERACTIVE", "no") + t.Setenv("LOCKBOX_INTERACTIVE", "no") if _, err := config.NewKey(config.IgnoreKeyMode); err == nil || err.Error() != "ask key mode requested in non-interactive mode" { t.Errorf("invalid error: %v", err) } } func TestAskKey(t *testing.T) { - defer os.Clearenv() - os.Setenv("LOCKBOX_KEYMODE", "") - os.Setenv("LOCKBOX_KEY", "test") + t.Setenv("LOCKBOX_KEYMODE", "") + t.Setenv("LOCKBOX_KEY", "test") k, _ := config.NewKey(config.IgnoreKeyMode) if k.Ask() { t.Error("invalid ask key") } - os.Setenv("LOCKBOX_KEYMODE", "ask") - os.Setenv("LOCKBOX_KEY", "") - os.Setenv("LOCKBOX_INTERACTIVE", "no") + t.Setenv("LOCKBOX_KEYMODE", "ask") + t.Setenv("LOCKBOX_KEY", "") + t.Setenv("LOCKBOX_INTERACTIVE", "no") k, _ = config.NewKey(config.IgnoreKeyMode) if k.Ask() { t.Error("invalid ask key") } - os.Setenv("LOCKBOX_KEYMODE", "ask") - os.Setenv("LOCKBOX_KEY", "") - os.Setenv("LOCKBOX_INTERACTIVE", "yes") + t.Setenv("LOCKBOX_KEYMODE", "ask") + t.Setenv("LOCKBOX_KEY", "") + t.Setenv("LOCKBOX_INTERACTIVE", "yes") k, _ = config.NewKey(config.IgnoreKeyMode) if !k.Ask() { t.Error("invalid ask key") @@ -107,14 +103,13 @@ func TestAskKey(t *testing.T) { } func TestIgnoreKey(t *testing.T) { - defer os.Clearenv() - os.Setenv("LOCKBOX_KEYMODE", "ignore") - os.Setenv("LOCKBOX_KEY", "test") + t.Setenv("LOCKBOX_KEYMODE", "ignore") + t.Setenv("LOCKBOX_KEY", "test") if _, err := config.NewKey(config.IgnoreKeyMode); err != nil { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_KEYMODE", "ignore") - os.Setenv("LOCKBOX_KEY", "") + t.Setenv("LOCKBOX_KEYMODE", "ignore") + t.Setenv("LOCKBOX_KEY", "") if _, err := config.NewKey(config.IgnoreKeyMode); err != nil { t.Errorf("invalid error: %v", err) } @@ -134,9 +129,8 @@ func TestReadErrors(t *testing.T) { } func TestPlainKey(t *testing.T) { - defer os.Clearenv() - os.Setenv("LOCKBOX_KEYMODE", "plaintext") - os.Setenv("LOCKBOX_KEY", " test ") + t.Setenv("LOCKBOX_KEYMODE", "plaintext") + t.Setenv("LOCKBOX_KEY", " test ") k, err := config.NewKey(config.IgnoreKeyMode) if err != nil { t.Errorf("invalid error: %v", err) @@ -151,9 +145,8 @@ func TestPlainKey(t *testing.T) { } func TestReadIgnoreOrNoKey(t *testing.T) { - defer os.Clearenv() - os.Setenv("LOCKBOX_KEYMODE", "ignore") - os.Setenv("LOCKBOX_KEY", "test") + t.Setenv("LOCKBOX_KEYMODE", "ignore") + t.Setenv("LOCKBOX_KEY", "test") k, err := config.NewKey(config.IgnoreKeyMode) if err != nil { t.Errorf("invalid error: %v", err) @@ -165,8 +158,8 @@ func TestReadIgnoreOrNoKey(t *testing.T) { if err != nil || val != "" { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_KEYMODE", "ignore") - os.Setenv("LOCKBOX_KEY", "") + t.Setenv("LOCKBOX_KEYMODE", "ignore") + t.Setenv("LOCKBOX_KEY", "") k, err = config.NewKey(config.IgnoreKeyMode) if err != nil { t.Errorf("invalid error: %v", err) @@ -175,7 +168,7 @@ func TestReadIgnoreOrNoKey(t *testing.T) { if err != nil || val != "" { t.Errorf("invalid error: %v", err) } - os.Setenv("LOCKBOX_KEYMODE", "none") + t.Setenv("LOCKBOX_KEYMODE", "none") k, err = config.NewKey(config.IgnoreKeyMode) if err != nil { t.Errorf("invalid error: %v", err) @@ -187,9 +180,8 @@ func TestReadIgnoreOrNoKey(t *testing.T) { } func TestCommandKey(t *testing.T) { - defer os.Clearenv() - os.Setenv("LOCKBOX_KEYMODE", "command") - os.Setenv("LOCKBOX_KEY", "thisisagarbagekey") + t.Setenv("LOCKBOX_KEYMODE", "command") + t.Setenv("LOCKBOX_KEY", "thisisagarbagekey") k, err := config.NewKey(config.IgnoreKeyMode) if err != nil { t.Errorf("invalid error: %v", err) diff --git a/internal/config/vars_test.go b/internal/config/vars_test.go @@ -10,7 +10,7 @@ import ( ) func checkYesNo(key string, t *testing.T, obj config.EnvironmentBool, onEmpty bool) { - os.Setenv(key, "yes") + t.Setenv(key, "yes") c, err := obj.Get() if err != nil { t.Errorf("invalid error: %v", err) @@ -18,7 +18,7 @@ func checkYesNo(key string, t *testing.T, obj config.EnvironmentBool, onEmpty bo if !c { t.Error("invalid setting") } - os.Setenv(key, "") + t.Setenv(key, "") c, err = obj.Get() if err != nil { t.Errorf("invalid error: %v", err) @@ -26,7 +26,7 @@ func checkYesNo(key string, t *testing.T, obj config.EnvironmentBool, onEmpty bo if c != onEmpty { t.Error("invalid setting") } - os.Setenv(key, "no") + t.Setenv(key, "no") c, err = obj.Get() if err != nil { t.Errorf("invalid error: %v", err) @@ -34,7 +34,7 @@ func checkYesNo(key string, t *testing.T, obj config.EnvironmentBool, onEmpty bo if c { t.Error("invalid setting") } - os.Setenv(key, "afoieae") + t.Setenv(key, "afoieae") _, err = obj.Get() if err == nil || err.Error() != fmt.Sprintf("invalid yes/no env value for %s", key) { t.Errorf("unexpected error: %v", err) @@ -82,11 +82,11 @@ func TestDefaultCompletions(t *testing.T) { } func TestTOTP(t *testing.T) { - os.Setenv("LOCKBOX_TOTP", "abc") + t.Setenv("LOCKBOX_TOTP", "abc") if config.EnvTOTPToken.Get() != "abc" { t.Error("invalid totp token field") } - os.Setenv("LOCKBOX_TOTP", "") + t.Setenv("LOCKBOX_TOTP", "") if config.EnvTOTPToken.Get() != "totp" { t.Error("invalid totp token field") } @@ -139,12 +139,12 @@ func TestFormatTOTP(t *testing.T) { if otp != "otpauth://totp/lbissuer:lbaccount?algorithm=SHA1&digits=6&issuer=lbissuer&period=30&secret=abc" { t.Errorf("invalid totp token: %s", otp) } - os.Setenv("LOCKBOX_TOTP_FORMAT", "test/%s") + t.Setenv("LOCKBOX_TOTP_FORMAT", "test/%s") otp = config.EnvFormatTOTP.Get("abc") if otp != "test/abc" { t.Errorf("invalid totp token: %s", otp) } - os.Setenv("LOCKBOX_TOTP_FORMAT", "") + t.Setenv("LOCKBOX_TOTP_FORMAT", "") otp = config.EnvFormatTOTP.Get("abc") if otp != "otpauth://totp/lbissuer:lbaccount?algorithm=SHA1&digits=6&issuer=lbissuer&period=30&secret=abc" { t.Errorf("invalid totp token: %s", otp) @@ -152,27 +152,26 @@ func TestFormatTOTP(t *testing.T) { } func TestParseJSONMode(t *testing.T) { - defer os.Clearenv() m, err := config.ParseJSONOutput() if m != config.JSONOutputs.Hash || err != nil { t.Error("invalid mode read") } - os.Setenv("LOCKBOX_JSON_DATA", "hAsH ") + t.Setenv("LOCKBOX_JSON_DATA", "hAsH ") m, err = config.ParseJSONOutput() if m != config.JSONOutputs.Hash || err != nil { t.Error("invalid mode read") } - os.Setenv("LOCKBOX_JSON_DATA", "EMPTY") + t.Setenv("LOCKBOX_JSON_DATA", "EMPTY") m, err = config.ParseJSONOutput() if m != config.JSONOutputs.Blank || err != nil { t.Error("invalid mode read") } - os.Setenv("LOCKBOX_JSON_DATA", " PLAINtext ") + t.Setenv("LOCKBOX_JSON_DATA", " PLAINtext ") m, err = config.ParseJSONOutput() if m != config.JSONOutputs.Raw || err != nil { t.Error("invalid mode read") } - os.Setenv("LOCKBOX_JSON_DATA", "a") + t.Setenv("LOCKBOX_JSON_DATA", "a") if _, err = config.ParseJSONOutput(); err == nil || err.Error() != "invalid JSON output mode: a" { t.Errorf("invalid error: %v", err) } @@ -195,18 +194,17 @@ func TestWordCount(t *testing.T) { } func checkInt(e config.EnvironmentInt, key, text string, def int, allowZero bool, t *testing.T) { - os.Setenv(key, "") - defer os.Clearenv() + t.Setenv(key, "") val, err := e.Get() if err != nil || val != def { t.Error("invalid read") } - os.Setenv(key, "1") + t.Setenv(key, "1") val, err = e.Get() if err != nil || val != 1 { t.Error("invalid read") } - os.Setenv(key, "-1") + t.Setenv(key, "-1") zero := "" if allowZero { zero = "=" @@ -214,11 +212,11 @@ func checkInt(e config.EnvironmentInt, key, text string, def int, allowZero bool if _, err := e.Get(); err == nil || err.Error() != fmt.Sprintf("%s must be >%s 0", text, zero) { t.Errorf("invalid err: %v", err) } - os.Setenv(key, "alk;ja") + t.Setenv(key, "alk;ja") if _, err := e.Get(); err == nil || err.Error() != "strconv.Atoi: parsing \"alk;ja\": invalid syntax" { t.Errorf("invalid err: %v", err) } - os.Setenv(key, "0") + t.Setenv(key, "0") if allowZero { val, err = e.Get() if err != nil || val != 0 { @@ -245,7 +243,7 @@ func TestEnvironDefinitions(t *testing.T) { if env == "LOCKBOX_ENV" { continue } - os.Setenv(env, "test") + t.Setenv(env, "test") expect[env] = struct{}{} } if !found { @@ -270,7 +268,6 @@ func TestEnvironDefinitions(t *testing.T) { } func TestCanColor(t *testing.T) { - defer os.Clearenv() os.Clearenv() if can, _ := config.CanColor(); !can { t.Error("should be able to color") @@ -281,17 +278,17 @@ func TestCanColor(t *testing.T) { } { os.Clearenv() key := fmt.Sprintf("LOCKBOX_%s", raw) - os.Setenv(key, "yes") + t.Setenv(key, "yes") if can, _ := config.CanColor(); can != expect { t.Errorf("expect != actual: %s", key) } - os.Setenv(key, "no") + t.Setenv(key, "no") if can, _ := config.CanColor(); can == expect { t.Errorf("expect == actual: %s", key) } } os.Clearenv() - os.Setenv("NO_COLOR", "1") + t.Setenv("NO_COLOR", "1") if can, _ := config.CanColor(); can { t.Error("should NOT be able to color") } diff --git a/internal/platform/clipboard_test.go b/internal/platform/clipboard_test.go @@ -2,7 +2,6 @@ package platform_test import ( "fmt" - "os" "testing" "github.com/seanenck/lockbox/internal/config" @@ -10,9 +9,9 @@ import ( ) func TestNoClipboard(t *testing.T) { - os.Setenv("LOCKBOX_CLIP_OSC52", "no") - os.Setenv("LOCKBOX_CLIP_MAX", "") - os.Setenv("LOCKBOX_NOCLIP", "yes") + t.Setenv("LOCKBOX_CLIP_OSC52", "no") + t.Setenv("LOCKBOX_CLIP_MAX", "") + t.Setenv("LOCKBOX_NOCLIP", "yes") _, err := platform.NewClipboard() if err == nil || err.Error() != "clipboard is off" { t.Errorf("invalid error: %v", err) @@ -20,10 +19,10 @@ func TestNoClipboard(t *testing.T) { } func TestMaxTime(t *testing.T) { - os.Setenv("LOCKBOX_NOCLIP", "no") - os.Setenv("LOCKBOX_CLIP_OSC52", "no") - os.Setenv("LOCKBOX_PLATFORM", string(config.Platforms.LinuxWaylandPlatform)) - os.Setenv("LOCKBOX_CLIP_MAX", "") + t.Setenv("LOCKBOX_NOCLIP", "no") + t.Setenv("LOCKBOX_CLIP_OSC52", "no") + t.Setenv("LOCKBOX_PLATFORM", string(config.Platforms.LinuxWaylandPlatform)) + t.Setenv("LOCKBOX_CLIP_MAX", "") c, err := platform.NewClipboard() if err != nil { t.Errorf("invalid clipboard: %v", err) @@ -31,7 +30,7 @@ func TestMaxTime(t *testing.T) { if c.MaxTime != 45 { t.Error("invalid default") } - os.Setenv("LOCKBOX_CLIP_MAX", "1") + t.Setenv("LOCKBOX_CLIP_MAX", "1") c, err = platform.NewClipboard() if err != nil { t.Errorf("invalid clipboard: %v", err) @@ -39,12 +38,12 @@ func TestMaxTime(t *testing.T) { if c.MaxTime != 1 { t.Error("invalid default") } - os.Setenv("LOCKBOX_CLIP_MAX", "-1") + t.Setenv("LOCKBOX_CLIP_MAX", "-1") _, err = platform.NewClipboard() if err == nil || err.Error() != "clipboard max time must be > 0" { t.Errorf("invalid max time error: %v", err) } - os.Setenv("LOCKBOX_CLIP_MAX", "$&(+") + t.Setenv("LOCKBOX_CLIP_MAX", "$&(+") _, err = platform.NewClipboard() if err == nil || err.Error() != "strconv.Atoi: parsing \"$&(+\": invalid syntax" { t.Errorf("invalid max time error: %v", err) @@ -52,11 +51,11 @@ func TestMaxTime(t *testing.T) { } func TestClipboardInstances(t *testing.T) { - os.Setenv("LOCKBOX_NOCLIP", "no") - os.Setenv("LOCKBOX_CLIP_MAX", "") - os.Setenv("LOCKBOX_CLIP_OSC52", "no") + t.Setenv("LOCKBOX_NOCLIP", "no") + t.Setenv("LOCKBOX_CLIP_MAX", "") + t.Setenv("LOCKBOX_CLIP_OSC52", "no") for _, item := range config.Platforms.List() { - os.Setenv("LOCKBOX_PLATFORM", item) + t.Setenv("LOCKBOX_PLATFORM", item) _, err := platform.NewClipboard() if err != nil { t.Errorf("invalid clipboard: %v", err) @@ -65,7 +64,7 @@ func TestClipboardInstances(t *testing.T) { } func TestOSC52(t *testing.T) { - os.Setenv("LOCKBOX_CLIP_OSC52", "yes") + t.Setenv("LOCKBOX_CLIP_OSC52", "yes") c, _ := platform.NewClipboard() _, _, ok := c.Args(true) if ok { @@ -78,9 +77,9 @@ func TestOSC52(t *testing.T) { } func TestArgsOverride(t *testing.T) { - os.Setenv("LOCKBOX_CLIP_PASTE", "abc xyz 111") - os.Setenv("LOCKBOX_CLIP_OSC52", "no") - os.Setenv("LOCKBOX_PLATFORM", string(config.Platforms.WindowsLinuxPlatform)) + t.Setenv("LOCKBOX_CLIP_PASTE", "abc xyz 111") + t.Setenv("LOCKBOX_CLIP_OSC52", "no") + t.Setenv("LOCKBOX_PLATFORM", string(config.Platforms.WindowsLinuxPlatform)) c, _ := platform.NewClipboard() cmd, args, ok := c.Args(true) if cmd != "clip.exe" || len(args) != 0 || !ok { @@ -90,7 +89,7 @@ func TestArgsOverride(t *testing.T) { if cmd != "abc" || len(args) != 2 || args[0] != "xyz" || args[1] != "111" || !ok { t.Error("invalid parse") } - os.Setenv("LOCKBOX_CLIP_COPY", "zzz lll 123") + t.Setenv("LOCKBOX_CLIP_COPY", "zzz lll 123") c, _ = platform.NewClipboard() cmd, args, ok = c.Args(true) if cmd != "zzz" || len(args) != 2 || args[0] != "lll" || args[1] != "123" || !ok { @@ -100,8 +99,8 @@ func TestArgsOverride(t *testing.T) { if cmd != "abc" || len(args) != 2 || args[0] != "xyz" || args[1] != "111" || !ok { t.Error("invalid parse") } - os.Setenv("LOCKBOX_CLIP_PASTE", "") - os.Setenv("LOCKBOX_CLIP_COPY", "") + t.Setenv("LOCKBOX_CLIP_PASTE", "") + t.Setenv("LOCKBOX_CLIP_COPY", "") c, _ = platform.NewClipboard() cmd, args, ok = c.Args(true) if cmd != "clip.exe" || len(args) != 0 || !ok { diff --git a/internal/platform/os_test.go b/internal/platform/os_test.go @@ -24,7 +24,6 @@ func TestPathExist(t *testing.T) { func TestLoadEnvConfigs(t *testing.T) { os.Clearenv() - defer os.Clearenv() files := []string{filepath.Join("testdata", "xyz"), filepath.Join("testdata", "abc")} if err := platform.LoadEnvConfigs(files...); err != nil { t.Errorf("unexpected error: %v", err) @@ -48,15 +47,15 @@ TEST_3="abc $HOME $X $TEST_X"`), 0o644) } } verify([]string{"TEST_X=1", "TEST_Y=2", "TEST_3=abc 1", "TEST_Z=\"1", "TEST11=1\""}) - os.Setenv("HOME", "a123") + t.Setenv("HOME", "a123") if err := platform.LoadEnvConfigs(files...); err != nil { t.Errorf("unexpected error: %v", err) } env = fmt.Sprintf("%v", os.Environ()) verify([]string{"TEST_X=1", "TEST_Y=2", "TEST_3=abc a123 1", "TEST_Z=\"1", "TEST11=1\""}) - os.Setenv("XYZ", "xyz") - os.Setenv("HOME", "$TEST4") - os.Setenv("TEST4", "$XYZ") + t.Setenv("XYZ", "xyz") + t.Setenv("HOME", "$TEST4") + t.Setenv("TEST4", "$XYZ") if err := platform.LoadEnvConfigs(files...); err != nil { t.Errorf("unexpected error: %v", err) } @@ -64,7 +63,7 @@ TEST_3="abc $HOME $X $TEST_X"`), 0o644) verify([]string{"TEST_X=1", "TEST_Y=2", "TEST_3=abc xyz 1", "TEST_Z=\"1", "TEST11=1\""}) final := filepath.Join("testdata", "zzz") files = append(files, final) - os.Setenv("TEST4", "a") + t.Setenv("TEST4", "a") os.WriteFile(final, []byte(` TEST_X=1 TEST_Y=2