Skip to content

Commit

Permalink
test: use T.Setenv to set env vars in tests (gopasspw#2463)
Browse files Browse the repository at this point in the history
This commit replaces `os.Setenv` with `t.Setenv` in tests. The
environment variable is automatically restored to its original value
when the test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.Setenv
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee authored Dec 11, 2022
1 parent eecb7e1 commit 840fb82
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 188 deletions.
8 changes: 4 additions & 4 deletions internal/backend/crypto/gpg/gpgconf/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gpgconf

import (
"bytes"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,9 +14,10 @@ func TestGpgOpts(t *testing.T) {
"": nil,
"--decrypt --armor --recipient 0xDEADBEEF": {"--decrypt", "--armor", "--recipient", "0xDEADBEEF"},
} {
assert.NoError(t, os.Setenv(vn, in))
assert.Equal(t, out, GPGOpts())
assert.NoError(t, os.Unsetenv(vn))
t.Run(vn, func(t *testing.T) {
t.Setenv(vn, in)
assert.Equal(t, out, GPGOpts())
})
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/config/legacy/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestNewConfig(t *testing.T) {
assert.NoError(t, os.Setenv("GOPASS_CONFIG", filepath.Join(os.TempDir(), ".gopass.yml")))
t.Setenv("GOPASS_CONFIG", filepath.Join(os.TempDir(), ".gopass.yml"))

cfg := legacy.New()
cs := cfg.String()
Expand All @@ -31,7 +31,7 @@ func TestNewConfig(t *testing.T) {
}

func TestSetConfigValue(t *testing.T) {
assert.NoError(t, os.Setenv("GOPASS_CONFIG", filepath.Join(os.TempDir(), ".gopass.yml")))
t.Setenv("GOPASS_CONFIG", filepath.Join(os.TempDir(), ".gopass.yml"))

cfg := legacy.New()
assert.NoError(t, cfg.SetConfigValue("autoclip", "true"))
Expand Down
10 changes: 5 additions & 5 deletions internal/config/legacy/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ func TestLoad(t *testing.T) {
td := os.TempDir()
gcfg := filepath.Join(td, ".gopass.yml")
_ = os.Remove(gcfg)
assert.NoError(t, os.Setenv("GOPASS_CONFIG", gcfg))
assert.NoError(t, os.Setenv("GOPASS_HOMEDIR", td))
t.Setenv("GOPASS_CONFIG", gcfg)
t.Setenv("GOPASS_HOMEDIR", td)

require.NoError(t, os.WriteFile(gcfg, []byte(testConfig), 0o600))

Expand All @@ -391,7 +391,7 @@ func TestLoad(t *testing.T) {

func TestLoadError(t *testing.T) {
gcfg := filepath.Join(os.TempDir(), ".gopass-err.yml")
assert.NoError(t, os.Setenv("GOPASS_CONFIG", gcfg))
t.Setenv("GOPASS_CONFIG", gcfg)

_ = os.Remove(gcfg)

Expand All @@ -409,13 +409,13 @@ func TestLoadError(t *testing.T) {
assert.Error(t, err)

gcfg = filepath.Join(t.TempDir(), "foo", ".gopass.yml")
assert.NoError(t, os.Setenv("GOPASS_CONFIG", gcfg))
t.Setenv("GOPASS_CONFIG", gcfg)
assert.NoError(t, cfg.Save())
}

func TestDecodeError(t *testing.T) {
gcfg := filepath.Join(os.TempDir(), ".gopass-err2.yml")
assert.NoError(t, os.Setenv("GOPASS_CONFIG", gcfg))
t.Setenv("GOPASS_CONFIG", gcfg)

_ = os.Remove(gcfg)
require.NoError(t, os.WriteFile(gcfg, []byte(testConfig+"\nfoobar: zab\n"), 0o600))
Expand Down
60 changes: 34 additions & 26 deletions internal/config/location_xdg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ import (

func TestPwStoreDir(t *testing.T) {
gph := filepath.Join(os.TempDir(), "home")
require.NoError(t, os.Setenv("GOPASS_HOMEDIR", gph))
t.Setenv("GOPASS_HOMEDIR", gph)

assert.Equal(t, filepath.Join(gph, ".local", "share", "gopass", "stores", "root"), PwStoreDir(""))
assert.Equal(t, filepath.Join(gph, ".local", "share", "gopass", "stores", "foo"), PwStoreDir("foo"))

psd := filepath.Join(gph, ".password-store-test")
require.NoError(t, os.Setenv("PASSWORD_STORE_DIR", psd))
t.Setenv("PASSWORD_STORE_DIR", psd)

assert.Equal(t, psd, PwStoreDir(""))
assert.Equal(t, filepath.Join(gph, ".local", "share", "gopass", "stores", "foo"), PwStoreDir("foo"))

// GOPASS_HOMEDIR takes precedence
require.NoError(t, os.Setenv("XDG_DATA_HOME", filepath.Join(os.TempDir(), ".local", "foo")))
assert.Equal(t, psd, PwStoreDir(""))
assert.Equal(t, filepath.Join(gph, ".local", "share", "gopass", "stores", "foo"), PwStoreDir("foo"))
assert.NoError(t, os.Unsetenv("XDG_DATA_HOME"))

// GOPASS_HOMEDIR unset, XDG_DATA_HOME takes precedence
require.NoError(t, os.Unsetenv("GOPASS_HOMEDIR"))
require.NoError(t, os.Setenv("XDG_DATA_HOME", filepath.Join(os.TempDir(), ".local", "foo")))
assert.Equal(t, psd, PwStoreDir(""))
assert.Equal(t, filepath.Join(os.TempDir(), ".local", "foo", "gopass", "stores", "foo"), PwStoreDir("foo"))
assert.NoError(t, os.Unsetenv("XDG_DATA_HOME"))
t.Run("GOPASS_HOMEDIR takes precedence", func(t *testing.T) {
t.Setenv("XDG_DATA_HOME", filepath.Join(os.TempDir(), ".local", "foo"))
assert.Equal(t, psd, PwStoreDir(""))
assert.Equal(t, filepath.Join(gph, ".local", "share", "gopass", "stores", "foo"), PwStoreDir("foo"))
})

t.Run("GOPASS_HOMEDIR unset, XDG_DATA_HOME takes precedence", func(t *testing.T) {
t.Setenv("XDG_DATA_HOME", filepath.Join(os.TempDir(), ".local", "foo"))
require.NoError(t, os.Unsetenv("GOPASS_HOMEDIR"))
assert.Equal(t, psd, PwStoreDir(""))
assert.Equal(t, filepath.Join(os.TempDir(), ".local", "foo", "gopass", "stores", "foo"), PwStoreDir("foo"))
})
}

func TestConfigLocation(t *testing.T) {
Expand All @@ -54,9 +54,10 @@ func TestConfigLocation(t *testing.T) {
}

for k, v := range evs {
assert.NoError(t, os.Setenv(k, v.ev))
assert.Equal(t, v.loc, configLocation())
assert.NoError(t, os.Unsetenv(k))
t.Run(k, func(t *testing.T) {
t.Setenv(k, v.ev)
assert.Equal(t, v.loc, configLocation())
})
}
}

Expand All @@ -69,16 +70,23 @@ func TestConfigLocations(t *testing.T) {
curcfg := filepath.Join(gphome, ".config", "gopass", "config.yml")
oldcfg := filepath.Join(gphome, ".gopass.yml")

assert.NoError(t, os.Setenv("GOPASS_CONFIG", gpcfg))
assert.NoError(t, os.Setenv("GOPASS_HOMEDIR", gphome))
t.Run("GOPASS_CONFIG, GOPASS_HOMEDIR set", func(t *testing.T) {
t.Setenv("GOPASS_CONFIG", gpcfg)
t.Setenv("GOPASS_HOMEDIR", gphome)

assert.Equal(t, []string{gpcfg, curcfg, curcfg, oldcfg}, configLocations())
})

assert.Equal(t, []string{gpcfg, curcfg, curcfg, oldcfg}, configLocations())
t.Run("GOPASS_CONFIG, GOPASS_HOMEDIR, XDG_CONFIG_HOME set", func(t *testing.T) {
t.Setenv("GOPASS_CONFIG", gpcfg)
t.Setenv("GOPASS_HOMEDIR", gphome)
t.Setenv("XDG_CONFIG_HOME", xdghome)

assert.NoError(t, os.Setenv("XDG_CONFIG_HOME", xdghome))
assert.Equal(t, []string{gpcfg, curcfg, curcfg, oldcfg}, configLocations())
assert.Equal(t, []string{gpcfg, curcfg, curcfg, oldcfg}, configLocations())
})

require.NoError(t, os.Setenv("XDG_CONFIG_HOME", xdghome))
require.NoError(t, os.Unsetenv("GOPASS_HOMEDIR"))
require.NoError(t, os.Unsetenv("GOPASS_CONFIG"))
assert.Equal(t, xdgcfg, configLocations()[0])
t.Run("XDG_CONFIG_HOME set only", func(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", xdghome)
assert.Equal(t, xdgcfg, configLocations()[0])
})
}
60 changes: 30 additions & 30 deletions internal/editor/edit_others_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"flag"
"os"
"os/exec"
"runtime"
"testing"

"github.com/gopasspw/gopass/tests/gptest"
Expand Down Expand Up @@ -37,38 +36,39 @@ func TestEditor(t *testing.T) {
func TestGetEditor(t *testing.T) {
app := cli.NewApp()

// --editor=fooed
fs := flag.NewFlagSet("default", flag.ContinueOnError)
sf := cli.StringFlag{
Name: "editor",
Usage: "editor",
}
require.NoError(t, sf.Apply(fs))
require.NoError(t, fs.Parse([]string{"--editor", "fooed"}))
c := cli.NewContext(app, fs, nil)
t.Run("--editor=fooed", func(t *testing.T) {
fs := flag.NewFlagSet("default", flag.ContinueOnError)
sf := cli.StringFlag{
Name: "editor",
Usage: "editor",
}
require.NoError(t, sf.Apply(fs))
require.NoError(t, fs.Parse([]string{"--editor", "fooed"}))
c := cli.NewContext(app, fs, nil)

assert.Equal(t, "fooed", Path(c))
assert.Equal(t, "fooed", Path(c))
})

// EDITOR
fs = flag.NewFlagSet("default", flag.ContinueOnError)
c = cli.NewContext(app, fs, nil)
assert.NoError(t, os.Setenv("EDITOR", "fooenv"))
assert.Equal(t, "fooenv", Path(c))
assert.NoError(t, os.Unsetenv("EDITOR"))
t.Run("/usr/bin/editor", func(t *testing.T) {
fs := flag.NewFlagSet("default", flag.ContinueOnError)
c := cli.NewContext(app, fs, nil)
pathed, err := exec.LookPath("editor")
if err == nil {
assert.Equal(t, pathed, Path(c))
}
})

// editor
pathed, err := exec.LookPath("editor")
if err == nil {
assert.Equal(t, pathed, Path(c))
}
t.Run("EDITOR", func(t *testing.T) {
fs := flag.NewFlagSet("default", flag.ContinueOnError)
c := cli.NewContext(app, fs, nil)
t.Setenv("EDITOR", "fooenv")
assert.Equal(t, "fooenv", Path(c))
})

// vi
op := os.Getenv("PATH")
assert.NoError(t, os.Setenv("PATH", "/tmp"))
if runtime.GOOS == "windows" {
assert.Equal(t, "notepad.exe", Path(c))
} else {
t.Run("vi", func(t *testing.T) {
fs := flag.NewFlagSet("default", flag.ContinueOnError)
c := cli.NewContext(app, fs, nil)
t.Setenv("PATH", "/tmp")
assert.Equal(t, "vi", Path(c))
}
assert.NoError(t, os.Setenv("PATH", op))
})
}
58 changes: 31 additions & 27 deletions internal/editor/edit_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package editor
import (
"context"
"flag"
"os"
"os/exec"
"testing"

Expand Down Expand Up @@ -32,34 +31,39 @@ func TestEditor(t *testing.T) {
func TestGetEditor(t *testing.T) {
app := cli.NewApp()

// --editor=fooed
fs := flag.NewFlagSet("default", flag.ContinueOnError)
sf := cli.StringFlag{
Name: "editor",
Usage: "editor",
}
require.NoError(t, sf.Apply(fs))
require.NoError(t, fs.Parse([]string{"--editor", "fooed"}))
c := cli.NewContext(app, fs, nil)
t.Run("--editor=fooed", func(t *testing.T) {
fs := flag.NewFlagSet("default", flag.ContinueOnError)
sf := cli.StringFlag{
Name: "editor",
Usage: "editor",
}
require.NoError(t, sf.Apply(fs))
require.NoError(t, fs.Parse([]string{"--editor", "fooed"}))
c := cli.NewContext(app, fs, nil)

assert.Equal(t, "fooed", Path(c))
assert.Equal(t, "fooed", Path(c))
})

// EDITOR
fs = flag.NewFlagSet("default", flag.ContinueOnError)
c = cli.NewContext(app, fs, nil)
assert.NoError(t, os.Setenv("EDITOR", "fooenv"))
assert.Equal(t, "fooenv", Path(c))
assert.NoError(t, os.Unsetenv("EDITOR"))
t.Run("/usr/bin/editor", func(t *testing.T) {
fs := flag.NewFlagSet("default", flag.ContinueOnError)
c := cli.NewContext(app, fs, nil)
pathed, err := exec.LookPath("editor")
if err == nil {
assert.Equal(t, pathed, Path(c))
}
})

// editor
pathed, err := exec.LookPath("editor")
if err == nil {
assert.Equal(t, pathed, Path(c))
}
t.Run("EDITOR", func(t *testing.T) {
fs := flag.NewFlagSet("default", flag.ContinueOnError)
c := cli.NewContext(app, fs, nil)
t.Setenv("EDITOR", "fooenv")
assert.Equal(t, "fooenv", Path(c))
})

// notepad
op := os.Getenv("PATH")
assert.NoError(t, os.Setenv("PATH", "/tmp"))
assert.Equal(t, "notepad.exe", Path(c))
assert.NoError(t, os.Setenv("PATH", op))
t.Run("vi", func(t *testing.T) {
fs := flag.NewFlagSet("default", flag.ContinueOnError)
c := cli.NewContext(app, fs, nil)
t.Setenv("PATH", "/tmp")
assert.Equal(t, "notepad.exe", Path(c))
})
}
6 changes: 1 addition & 5 deletions internal/store/leaf/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@ import (
)

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

ctx := context.Background()

tempdir := t.TempDir()

obuf := &bytes.Buffer{}
out.Stdout = obuf
defer func() {
out.Stdout = os.Stdout
}()

s, err := createSubStore(tempdir)
s, err := createSubStore(t)
require.NoError(t, err)

assert.NoError(t, s.ImportMissingPublicKeys(ctx))
Expand Down
6 changes: 1 addition & 5 deletions internal/store/leaf/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ import (
)

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

ctx := context.Background()

tempdir := t.TempDir()

s, err := createSubStore(tempdir)
s, err := createSubStore(t)
assert.NoError(t, err)

assert.Error(t, s.Init(ctx, "", "0xDEADBEEF"))
Expand Down
7 changes: 1 addition & 6 deletions internal/store/leaf/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ import (
)

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

ctx := context.Background()

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

s, err := createSubStore(tempdir)
s, err := createSubStore(t)
require.NoError(t, err)

sec := secrets.NewAKV()
Expand Down
12 changes: 2 additions & 10 deletions internal/store/leaf/rcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ import (
)

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

ctx := context.Background()

tempdir := t.TempDir()

s, err := createSubStore(tempdir)
s, err := createSubStore(t)
require.NoError(t, err)

require.NotNil(t, s.Storage())
Expand All @@ -40,13 +36,9 @@ func TestGit(t *testing.T) {
}

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

ctx := context.Background()

tempdir := t.TempDir()

s, err := createSubStore(tempdir)
s, err := createSubStore(t)
require.NoError(t, err)

require.NotNil(t, s.Storage())
Expand Down
Loading

0 comments on commit 840fb82

Please sign in to comment.