From f257a4210ed7329d9da24a11cd2fdedf8cd22373 Mon Sep 17 00:00:00 2001 From: itchyny Date: Sun, 8 May 2022 10:09:00 +0900 Subject: [PATCH 1/4] Use t.Cleanup to remove temporally directories for tests --- cmd_create_test.go | 1 - cmd_get_test.go | 1 - cmd_list_test.go | 12 ++---------- cmd_root_test.go | 1 - commands_test.go | 2 -- helpers_test.go | 2 ++ local_repository_test.go | 24 ++++-------------------- vcs_test.go | 3 --- 8 files changed, 8 insertions(+), 38 deletions(-) diff --git a/cmd_create_test.go b/cmd_create_test.go index 3ea9ed14..e12b0556 100644 --- a/cmd_create_test.go +++ b/cmd_create_test.go @@ -27,7 +27,6 @@ func TestDoCreate(t *testing.T) { _home = "" homeOnce = &sync.Once{} tmpd := newTempDir(t) - defer os.RemoveAll(tmpd) defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) defer tmpEnv(envGhqRoot, tmpd)() _localRepositoryRoots = nil diff --git a/cmd_get_test.go b/cmd_get_test.go index 2b0bb28a..0f35a49d 100644 --- a/cmd_get_test.go +++ b/cmd_get_test.go @@ -173,7 +173,6 @@ func TestCommandGet(t *testing.T) { name: "ghq..root", scenario: func(t *testing.T, tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) { tmpd := newTempDir(t) - defer os.RemoveAll(tmpd) defer gitconfig.WithConfig(t, fmt.Sprintf(` [ghq "https://github.com/motemen"] root = "%s" diff --git a/cmd_list_test.go b/cmd_list_test.go index 05de9b64..e2cc759e 100644 --- a/cmd_list_test.go +++ b/cmd_list_test.go @@ -192,9 +192,7 @@ func TestDoList_unique(t *testing.T) { defer func(orig string) { os.Setenv(envGhqRoot, orig) }(os.Getenv(envGhqRoot)) tmp1 := newTempDir(t) - defer os.RemoveAll(tmp1) tmp2 := newTempDir(t) - defer os.RemoveAll(tmp2) _localRepositoryRoots = nil localRepoOnce = &sync.Once{} @@ -229,10 +227,7 @@ func TestDoList_notPermittedRoot(t *testing.T) { } defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) tmpdir := newTempDir(t) - defer func(dir string) { - os.Chmod(dir, 0755) - os.RemoveAll(dir) - }(tmpdir) + defer os.Chmod(tmpdir, 0755) defer tmpEnv(envGhqRoot, tmpdir)() _localRepositoryRoots = nil @@ -253,10 +248,7 @@ func TestDoList_withSystemHiddenDir(t *testing.T) { tmpdir := newTempDir(t) systemHidden := filepath.Join(tmpdir, ".system") os.MkdirAll(systemHidden, 0000) - defer func(dir string) { - os.Chmod(systemHidden, 0755) - os.RemoveAll(dir) - }(tmpdir) + defer os.Chmod(systemHidden, 0755) defer tmpEnv(envGhqRoot, tmpdir)() _localRepositoryRoots = nil diff --git a/cmd_root_test.go b/cmd_root_test.go index 24d9afbd..d6d20903 100644 --- a/cmd_root_test.go +++ b/cmd_root_test.go @@ -83,7 +83,6 @@ func TestDoRoot(t *testing.T) { restore3 := tmpEnv("HOME", "/path/to/ghqhome") return func() { - os.RemoveAll(tmpd) restore1() restore2() restore3() diff --git a/commands_test.go b/commands_test.go index 7b356584..06d9bb89 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2,7 +2,6 @@ package main import ( "net/url" - "os" "path/filepath" "sync" "testing" @@ -23,7 +22,6 @@ type _updateArgs struct { func withFakeGitBackend(t *testing.T, block func(*testing.T, string, *_cloneArgs, *_updateArgs)) { tmpRoot := newTempDir(t) - defer os.RemoveAll(tmpRoot) defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) _localRepositoryRoots = []string{tmpRoot} diff --git a/helpers_test.go b/helpers_test.go index e9861713..6c86b05b 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -89,6 +89,8 @@ func newTempDir(t *testing.T) string { if err != nil { t.Fatal(err) } + t.Cleanup(func() { os.RemoveAll(tmpdir) }) + // Resolve /var/folders/.../T/... to /private/var/... in OSX wd, err := os.Getwd() if err != nil { diff --git a/local_repository_test.go b/local_repository_test.go index 2d0f1f47..820ae804 100644 --- a/local_repository_test.go +++ b/local_repository_test.go @@ -26,7 +26,6 @@ func samePathSlice(lhss, rhss []string) bool { func TestLocalRepositoryFromFullPath(t *testing.T) { defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) tmproot := newTempDir(t) - defer os.RemoveAll(tmproot) _localRepositoryRoots = []string{tmproot} testCases := []struct { @@ -63,7 +62,6 @@ func TestLocalRepositoryFromFullPath(t *testing.T) { func TestNewLocalRepository(t *testing.T) { defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) tmproot := newTempDir(t) - defer os.RemoveAll(tmproot) _localRepositoryRoots = []string{tmproot} testCases := []struct { @@ -167,10 +165,7 @@ func TestList_Symlink(t *testing.T) { t.SkipNow() } root := newTempDir(t) - defer os.RemoveAll(root) - symDir := newTempDir(t) - defer os.RemoveAll(symDir) origLocalRepositryRoots := _localRepositoryRoots _localRepositoryRoots = []string{root} @@ -203,10 +198,7 @@ func TestList_Symlink_In_Same_Directory(t *testing.T) { t.SkipNow() } root := newTempDir(t) - defer os.RemoveAll(root) - symDir := newTempDir(t) - defer os.RemoveAll(symDir) origLocalRepositryRoots := _localRepositoryRoots _localRepositoryRoots = []string{root} @@ -248,9 +240,7 @@ func TestFindVCSBackend(t *testing.T) { setup: func(t *testing.T) (string, string, func()) { dir := newTempDir(t) os.MkdirAll(filepath.Join(dir, ".git"), 0755) - return dir, "", func() { - os.RemoveAll(dir) - } + return dir, "", func() {} }, expect: GitBackend, }, { @@ -258,9 +248,7 @@ func TestFindVCSBackend(t *testing.T) { setup: func(t *testing.T) (string, string, func()) { dir := newTempDir(t) os.MkdirAll(filepath.Join(dir, ".git", "svn"), 0755) - return dir, "", func() { - os.RemoveAll(dir) - } + return dir, "", func() {} }, expect: GitBackend, }, { @@ -268,9 +256,7 @@ func TestFindVCSBackend(t *testing.T) { setup: func(t *testing.T) (string, string, func()) { dir := newTempDir(t) os.MkdirAll(filepath.Join(dir, ".git"), 0755) - return dir, "git", func() { - os.RemoveAll(dir) - } + return dir, "git", func() {} }, expect: GitBackend, }, { @@ -278,9 +264,7 @@ func TestFindVCSBackend(t *testing.T) { setup: func(t *testing.T) (string, string, func()) { dir := newTempDir(t) os.MkdirAll(filepath.Join(dir, ".git"), 0755) - return dir, "mercurial", func() { - os.RemoveAll(dir) - } + return dir, "mercurial", func() {} }, expect: nil, }} diff --git a/vcs_test.go b/vcs_test.go index de99ad10..1d9e0685 100644 --- a/vcs_test.go +++ b/vcs_test.go @@ -28,7 +28,6 @@ Last Changed Date: 2019-08-16 15:16:45 +0900 (Fri, 16 Aug 2019) func TestVCSBackend(t *testing.T) { tempDir := newTempDir(t) - defer os.RemoveAll(tempDir) localDir := filepath.Join(tempDir, "repo") _commands := []*exec.Cmd{} lastCommand := func() *exec.Cmd { return _commands[len(_commands)-1] } @@ -416,7 +415,6 @@ func TestVCSBackend(t *testing.T) { func TestCvsDummyBackend(t *testing.T) { tempDir := newTempDir(t) - defer os.RemoveAll(tempDir) localDir := filepath.Join(tempDir, "repo") if err := cvsDummyBackend.Clone(&vcsGetOption{ @@ -443,7 +441,6 @@ func TestCvsDummyBackend(t *testing.T) { func TestBranchOptionIgnoredErrors(t *testing.T) { tempDir := newTempDir(t) - defer os.RemoveAll(tempDir) localDir := filepath.Join(tempDir, "repo") if err := DarcsBackend.Clone(&vcsGetOption{ From d46d183a769e4dd96cda8df20983fb387a42af00 Mon Sep 17 00:00:00 2001 From: itchyny Date: Sun, 8 May 2022 10:17:14 +0900 Subject: [PATCH 2/4] Use t.Cleanup in test setup functions --- cmd_create_test.go | 17 ++++++----------- cmd_root_test.go | 33 ++++++++++++--------------------- local_repository_test.go | 21 ++++++++++----------- url_test.go | 14 +++++++------- 4 files changed, 35 insertions(+), 50 deletions(-) diff --git a/cmd_create_test.go b/cmd_create_test.go index e12b0556..7a45b73d 100644 --- a/cmd_create_test.go +++ b/cmd_create_test.go @@ -38,7 +38,7 @@ func TestDoCreate(t *testing.T) { want []string wantDir string errStr string - setup func() func() + setup func(t *testing.T) cmdRun func(cmd *exec.Cmd) error skipOnWin bool }{{ @@ -50,9 +50,8 @@ func TestDoCreate(t *testing.T) { name: "empty directory exists", input: []string{"create", "motemen/ghqqq"}, want: []string{"git", "init"}, - setup: func() func() { + setup: func(t *testing.T) { os.MkdirAll(filepath.Join(tmpd, "github.com/motemen/ghqqq"), 0755) - return func() {} }, wantDir: filepath.Join(tmpd, "github.com/motemen/ghqqq"), }, { @@ -90,22 +89,19 @@ func TestDoCreate(t *testing.T) { }, { name: "not permitted", input: []string{"create", "motemen/ghq-notpermitted"}, - setup: func() func() { + setup: func(t *testing.T) { f := filepath.Join(tmpd, "github.com/motemen/ghq-notpermitted") os.MkdirAll(f, 0) - return func() { - os.Chmod(f, 0755) - } + t.Cleanup(func() { os.Chmod(f, 0755) }) }, errStr: "permission denied", skipOnWin: true, }, { name: "not empty", input: []string{"create", "motemen/ghq-notempty"}, - setup: func() func() { + setup: func(t *testing.T) { f := filepath.Join(tmpd, "github.com/motemen/ghq-notempty", "dummy") os.MkdirAll(f, 0755) - return func() {} }, errStr: "already exists and not empty", }} @@ -117,8 +113,7 @@ func TestDoCreate(t *testing.T) { } lastCmd = nil if tc.setup != nil { - teardown := tc.setup() - defer teardown() + tc.setup(t) } cmdutil.CommandRunner = commandRunner diff --git a/cmd_root_test.go b/cmd_root_test.go index d6d20903..74816d83 100644 --- a/cmd_root_test.go +++ b/cmd_root_test.go @@ -38,38 +38,35 @@ func samePaths(lhs, rhs string) bool { func TestDoRoot(t *testing.T) { testCases := []struct { name string - setup func() func() + setup func(t *testing.T) expect, allExpect string }{{ name: "env", - setup: func() func() { + setup: func(t *testing.T) { orig := os.Getenv(envGhqRoot) os.Setenv(envGhqRoot, "/path/to/ghqroot1"+string(os.PathListSeparator)+"/path/to/ghqroot2") - return func() { os.Setenv(envGhqRoot, orig) } + t.Cleanup(func() { os.Setenv(envGhqRoot, orig) }) }, expect: "/path/to/ghqroot1\n", allExpect: "/path/to/ghqroot1\n/path/to/ghqroot2\n", }, { name: "gitconfig", - setup: func() func() { + setup: func(t *testing.T) { orig := os.Getenv(envGhqRoot) os.Setenv(envGhqRoot, "") - teardown := gitconfig.WithConfig(t, ` + t.Cleanup(gitconfig.WithConfig(t, ` [ghq] root = /path/to/ghqroot12 root = /path/to/ghqroot12 root = /path/to/ghqroot11 -`) - return func() { - os.Setenv(envGhqRoot, orig) - teardown() - } +`)) + t.Cleanup(func() { os.Setenv(envGhqRoot, orig) }) }, expect: "/path/to/ghqroot11\n", allExpect: "/path/to/ghqroot11\n/path/to/ghqroot12\n", }, { name: "default home", - setup: func() func() { + setup: func(t *testing.T) { tmpd := newTempDir(t) fpath := filepath.Join(tmpd, "unknown-ghq-dummy") f, err := os.Create(fpath) @@ -78,15 +75,9 @@ func TestDoRoot(t *testing.T) { } f.Close() - restore1 := tmpEnv(envGhqRoot, "") - restore2 := tmpEnv("GIT_CONFIG", fpath) - restore3 := tmpEnv("HOME", "/path/to/ghqhome") - - return func() { - restore1() - restore2() - restore3() - } + t.Cleanup(tmpEnv(envGhqRoot, "")) + t.Cleanup(tmpEnv("GIT_CONFIG", fpath)) + t.Cleanup(tmpEnv("HOME", "/path/to/ghqhome")) }, expect: "/path/to/ghqhome/ghq\n", allExpect: "/path/to/ghqhome/ghq\n", @@ -100,7 +91,7 @@ func TestDoRoot(t *testing.T) { defer func(orig string) { _home = orig }(_home) _home = "" homeOnce = &sync.Once{} - defer tc.setup()() + tc.setup(t) out, _, _ := capture(func() { newApp().Run([]string{"", "root"}) }) diff --git a/local_repository_test.go b/local_repository_test.go index 820ae804..ac72f7f7 100644 --- a/local_repository_test.go +++ b/local_repository_test.go @@ -233,46 +233,45 @@ func TestList_Symlink_In_Same_Directory(t *testing.T) { func TestFindVCSBackend(t *testing.T) { testCases := []struct { name string - setup func(t *testing.T) (string, string, func()) + setup func(t *testing.T) (string, string) expect *VCSBackend }{{ name: "git", - setup: func(t *testing.T) (string, string, func()) { + setup: func(t *testing.T) (string, string) { dir := newTempDir(t) os.MkdirAll(filepath.Join(dir, ".git"), 0755) - return dir, "", func() {} + return dir, "" }, expect: GitBackend, }, { name: "git svn", - setup: func(t *testing.T) (string, string, func()) { + setup: func(t *testing.T) (string, string) { dir := newTempDir(t) os.MkdirAll(filepath.Join(dir, ".git", "svn"), 0755) - return dir, "", func() {} + return dir, "" }, expect: GitBackend, }, { name: "git with matched vcs", - setup: func(t *testing.T) (string, string, func()) { + setup: func(t *testing.T) (string, string) { dir := newTempDir(t) os.MkdirAll(filepath.Join(dir, ".git"), 0755) - return dir, "git", func() {} + return dir, "git" }, expect: GitBackend, }, { name: "git with not matched vcs", - setup: func(t *testing.T) (string, string, func()) { + setup: func(t *testing.T) (string, string) { dir := newTempDir(t) os.MkdirAll(filepath.Join(dir, ".git"), 0755) - return dir, "mercurial", func() {} + return dir, "mercurial" }, expect: nil, }} for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - fpath, vcs, teardown := tc.setup(t) - defer teardown() + fpath, vcs := tc.setup(t) backend := findVCSBackend(fpath, vcs) if backend != tc.expect { t.Errorf("got: %v, expect: %v", backend, tc.expect) diff --git a/url_test.go b/url_test.go index df388433..7741848f 100644 --- a/url_test.go +++ b/url_test.go @@ -14,7 +14,7 @@ import ( func TestNewURL(t *testing.T) { testCases := []struct { name, url, expect, host string - setup func() func() + setup func(t *testing.T) }{{ name: "https", // Does nothing when the URL has scheme part url: "https://github.com/motemen/pusheen-explorer", @@ -52,20 +52,20 @@ func TestNewURL(t *testing.T) { host: "golang.org", }, { name: "fill username", - setup: func() func() { + setup: func(t *testing.T) { key := "GITHUB_USER" orig := os.Getenv(key) os.Setenv(key, "ghq-test") - return func() { os.Setenv(key, orig) } + t.Cleanup(func() { os.Setenv(key, orig) }) }, url: "same-name-ghq", expect: "https://github.com/ghq-test/same-name-ghq", host: "github.com", }, { name: "same name repository", - setup: func() func() { - return gitconfig.WithConfig(t, `[ghq] -completeUser = false`) + setup: func(t *testing.T) { + t.Cleanup(gitconfig.WithConfig(t, `[ghq] +completeUser = false`)) }, url: "peco", expect: "https://github.com/peco/peco", @@ -75,7 +75,7 @@ completeUser = false`) for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { if tc.setup != nil { - defer tc.setup()() + tc.setup(t) } repo, err := newURL(tc.url, false, false) if err != nil { From 24ba4ce422ad569448d7c8931ef886bd27ad952e Mon Sep 17 00:00:00 2001 From: itchyny Date: Sun, 8 May 2022 10:26:06 +0900 Subject: [PATCH 3/4] Use t.Cleanup to set environment variables in tests --- cmd_create_test.go | 2 +- cmd_list_test.go | 9 ++++----- cmd_root_test.go | 14 +++++--------- helpers_test.go | 6 +++--- local_repository_test.go | 8 +++----- url_test.go | 10 +++------- 6 files changed, 19 insertions(+), 30 deletions(-) diff --git a/cmd_create_test.go b/cmd_create_test.go index 7a45b73d..fba24a38 100644 --- a/cmd_create_test.go +++ b/cmd_create_test.go @@ -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{} diff --git a/cmd_list_test.go b/cmd_list_test.go index e2cc759e..3efbeeb7 100644 --- a/cmd_list_test.go +++ b/cmd_list_test.go @@ -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{} diff --git a/cmd_root_test.go b/cmd_root_test.go index 74816d83..2066aec0 100644 --- a/cmd_root_test.go +++ b/cmd_root_test.go @@ -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", diff --git a/helpers_test.go b/helpers_test.go index 6c86b05b..d5727932 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -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) } - } + }) } diff --git a/local_repository_test.go b/local_repository_test.go index ac72f7f7..a8346103 100644 --- a/local_repository_test.go +++ b/local_repository_test.go @@ -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 = "" diff --git a/url_test.go b/url_test.go index 7741848f..89ef46eb 100644 --- a/url_test.go +++ b/url_test.go @@ -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) From a1b17bd215f35e9c7f7d88f98ad2d01cac8088e5 Mon Sep 17 00:00:00 2001 From: itchyny Date: Sun, 8 May 2022 10:29:43 +0900 Subject: [PATCH 4/4] Use t.Cleanup for gitconfig.WithConfig --- cmd_get_test.go | 4 ++-- local_repository_test.go | 4 ++-- url_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd_get_test.go b/cmd_get_test.go index 0f35a49d..3e4f14b8 100644 --- a/cmd_get_test.go +++ b/cmd_get_test.go @@ -173,10 +173,10 @@ func TestCommandGet(t *testing.T) { name: "ghq..root", scenario: func(t *testing.T, tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) { tmpd := newTempDir(t) - defer gitconfig.WithConfig(t, fmt.Sprintf(` + t.Cleanup(gitconfig.WithConfig(t, fmt.Sprintf(` [ghq "https://github.com/motemen"] root = "%s" -`, filepath.ToSlash(tmpd)))() +`, filepath.ToSlash(tmpd)))) app.Run([]string{"", "get", "motemen/ghq-test-repo"}) localDir := filepath.Join(tmpd, "github.com", "motemen", "ghq-test-repo") diff --git a/local_repository_test.go b/local_repository_test.go index a8346103..f942cf20 100644 --- a/local_repository_test.go +++ b/local_repository_test.go @@ -334,7 +334,7 @@ func TestLocalRepositoryRoots_URLMatchLocalRepositoryRoots(t *testing.T) { _home = "" homeOnce = &sync.Once{} - defer gitconfig.WithConfig(t, ` + t.Cleanup(gitconfig.WithConfig(t, ` [ghq] root = /hoge [ghq "https://github.com/hatena"] @@ -342,7 +342,7 @@ func TestLocalRepositoryRoots_URLMatchLocalRepositoryRoots(t *testing.T) { root = /backups/hatena [ghq "https://github.com/natureglobal"] root = ~/proj/natureglobal -`)() +`)) want := []string{"/hoge", "/home/tmp/proj/hatena", "/backups/hatena", "/home/tmp/proj/natureglobal"} diff --git a/url_test.go b/url_test.go index 89ef46eb..22207f09 100644 --- a/url_test.go +++ b/url_test.go @@ -125,7 +125,7 @@ func TestNewURL_err(t *testing.T) { if got := fmt.Sprint(err); !strings.Contains(got, wantSub) { t.Errorf("newURL(%q, false, false) error = %q; want substring %q", invalidURL, got, wantSub) } - defer gitconfig.WithConfig(t, `[[[`)() + t.Cleanup(gitconfig.WithConfig(t, `[[[`)) var exitError *exec.ExitError _, err = newURL("peco", false, false)