Use t.Cleanup to set environment variables in tests

This commit is contained in:
itchyny 2022-05-08 10:26:06 +09:00
parent d46d183a76
commit 24ba4ce422
6 changed files with 19 additions and 30 deletions

View file

@ -28,7 +28,7 @@ func TestDoCreate(t *testing.T) {
homeOnce = &sync.Once{}
tmpd := newTempDir(t)
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
defer tmpEnv(envGhqRoot, tmpd)()
setEnv(t, envGhqRoot, tmpd)
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}

View file

@ -189,7 +189,6 @@ func TestDoList_query(t *testing.T) {
func TestDoList_unique(t *testing.T) {
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
defer func(orig string) { os.Setenv(envGhqRoot, orig) }(os.Getenv(envGhqRoot))
tmp1 := newTempDir(t)
tmp2 := newTempDir(t)
@ -197,7 +196,7 @@ func TestDoList_unique(t *testing.T) {
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}
rootPaths := []string{tmp1, tmp2}
os.Setenv(envGhqRoot, strings.Join(rootPaths, string(os.PathListSeparator)))
setEnv(t, envGhqRoot, strings.Join(rootPaths, string(os.PathListSeparator)))
for _, rootPath := range rootPaths {
os.MkdirAll(filepath.Join(rootPath, "github.com/motemen/ghq/.git"), 0755)
}
@ -211,7 +210,7 @@ func TestDoList_unique(t *testing.T) {
func TestDoList_unknownRoot(t *testing.T) {
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
defer tmpEnv(envGhqRoot, "/path/to/unknown-ghq")()
setEnv(t, envGhqRoot, "/path/to/unknown-ghq")
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}
@ -228,7 +227,7 @@ func TestDoList_notPermittedRoot(t *testing.T) {
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
tmpdir := newTempDir(t)
defer os.Chmod(tmpdir, 0755)
defer tmpEnv(envGhqRoot, tmpdir)()
setEnv(t, envGhqRoot, tmpdir)
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}
@ -249,7 +248,7 @@ func TestDoList_withSystemHiddenDir(t *testing.T) {
systemHidden := filepath.Join(tmpdir, ".system")
os.MkdirAll(systemHidden, 0000)
defer os.Chmod(systemHidden, 0755)
defer tmpEnv(envGhqRoot, tmpdir)()
setEnv(t, envGhqRoot, tmpdir)
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}

View file

@ -43,24 +43,20 @@ func TestDoRoot(t *testing.T) {
}{{
name: "env",
setup: func(t *testing.T) {
orig := os.Getenv(envGhqRoot)
os.Setenv(envGhqRoot, "/path/to/ghqroot1"+string(os.PathListSeparator)+"/path/to/ghqroot2")
t.Cleanup(func() { os.Setenv(envGhqRoot, orig) })
setEnv(t, envGhqRoot, "/path/to/ghqroot1"+string(os.PathListSeparator)+"/path/to/ghqroot2")
},
expect: "/path/to/ghqroot1\n",
allExpect: "/path/to/ghqroot1\n/path/to/ghqroot2\n",
}, {
name: "gitconfig",
setup: func(t *testing.T) {
orig := os.Getenv(envGhqRoot)
os.Setenv(envGhqRoot, "")
setEnv(t, envGhqRoot, "")
t.Cleanup(gitconfig.WithConfig(t, `
[ghq]
root = /path/to/ghqroot12
root = /path/to/ghqroot12
root = /path/to/ghqroot11
`))
t.Cleanup(func() { os.Setenv(envGhqRoot, orig) })
},
expect: "/path/to/ghqroot11\n",
allExpect: "/path/to/ghqroot11\n/path/to/ghqroot12\n",
@ -75,9 +71,9 @@ func TestDoRoot(t *testing.T) {
}
f.Close()
t.Cleanup(tmpEnv(envGhqRoot, ""))
t.Cleanup(tmpEnv("GIT_CONFIG", fpath))
t.Cleanup(tmpEnv("HOME", "/path/to/ghqhome"))
setEnv(t, envGhqRoot, "")
setEnv(t, "GIT_CONFIG", fpath)
setEnv(t, "HOME", "/path/to/ghqhome")
},
expect: "/path/to/ghqhome/ghq\n",
allExpect: "/path/to/ghqhome/ghq\n",

View file

@ -121,15 +121,15 @@ func newTempDir(t *testing.T) string {
return s
}
func tmpEnv(key, val string) func() {
func setEnv(t *testing.T, key, val string) {
orig, ok := os.LookupEnv(key)
os.Setenv(key, val)
return func() {
t.Cleanup(func() {
if ok {
os.Setenv(key, orig)
} else {
os.Unsetenv(key)
}
}
})
}

View file

@ -122,7 +122,6 @@ func TestNewLocalRepository(t *testing.T) {
func TestLocalRepositoryRoots(t *testing.T) {
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
defer func(orig string) { os.Setenv(envGhqRoot, orig) }(os.Getenv(envGhqRoot))
wd, err := os.Getwd()
if err != nil {
@ -147,7 +146,7 @@ func TestLocalRepositoryRoots(t *testing.T) {
t.Run(tc.root, func(t *testing.T) {
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}
os.Setenv(envGhqRoot, tc.root)
setEnv(t, envGhqRoot, tc.root)
got, err := localRepositoryRoots(true)
if err != nil {
t.Errorf("error should be nil, but: %s", err)
@ -282,12 +281,11 @@ func TestFindVCSBackend(t *testing.T) {
func TestLocalRepository_VCS(t *testing.T) {
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
defer func(orig string) { os.Setenv(envGhqRoot, orig) }(os.Getenv(envGhqRoot))
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}
tmpdir := newTempDir(t)
os.Setenv(envGhqRoot, tmpdir)
setEnv(t, envGhqRoot, tmpdir)
pkg := filepath.Join(tmpdir, "github.com", "motemen", "ghq")
subpkg := filepath.Join(pkg, "logger")
@ -331,7 +329,7 @@ func TestLocalRepositoryRoots_URLMatchLocalRepositoryRoots(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
}
defer tmpEnv("HOME", "/home/tmp")()
setEnv(t, "HOME", "/home/tmp")
defer func(orig string) { _home = orig }(_home)
_home = ""

View file

@ -3,7 +3,6 @@ package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"testing"
@ -53,10 +52,7 @@ func TestNewURL(t *testing.T) {
}, {
name: "fill username",
setup: func(t *testing.T) {
key := "GITHUB_USER"
orig := os.Getenv(key)
os.Setenv(key, "ghq-test")
t.Cleanup(func() { os.Setenv(key, orig) })
setEnv(t, "GITHUB_USER", "ghq-test")
},
url: "same-name-ghq",
expect: "https://github.com/ghq-test/same-name-ghq",
@ -140,9 +136,9 @@ func TestNewURL_err(t *testing.T) {
func TestFillUsernameToPath_err(t *testing.T) {
for _, envStr := range []string{"GITHUB_USER", "GITHUB_TOKEN", "USER", "USERNAME"} {
defer tmpEnv(envStr, "")()
setEnv(t, envStr, "")
}
defer tmpEnv("XDG_CONFIG_HOME", "/dummy/dummy")()
setEnv(t, "XDG_CONFIG_HOME", "/dummy/dummy")
usr, err := fillUsernameToPath("peco", false)
t.Log(usr)