diff options
author | Eng Zer Jun <engzerjun@gmail.com> | 2022-09-04 23:14:53 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-04 16:14:53 +0100 |
commit | 8b0aaa5f86e40a4699f278d09d116add63f8e4a0 (patch) | |
tree | 8133c4362e102b84c1d69df0ee09dd5a6b86b5f5 /tests | |
parent | c722a26e7efa69370804ac04590e9e467decc093 (diff) | |
download | gitea-8b0aaa5f86e40a4699f278d09d116add63f8e4a0.tar.gz gitea-8b0aaa5f86e40a4699f278d09d116add63f8e4a0.zip |
test: use `T.TempDir` to create temporary test directory (#21043)
A testing cleanup.
This pull request replaces `os.MkdirTemp` with `t.TempDir`. We can use the `T.TempDir` function from the `testing` package to create temporary directory. The directory created by `T.TempDir` is automatically removed when the test and all its subtests complete.
This saves us at least 2 lines (error check, and cleanup) on every instance, or in some cases adds cleanup that we forgot.
Reference: https://pkg.go.dev/testing#T.TempDir
```go
func TestFoo(t *testing.T) {
// before
tmpDir, err := os.MkdirTemp("", "")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
// now
tmpDir := t.TempDir()
}
```
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/integration/api_repo_file_get_test.go | 10 | ||||
-rw-r--r-- | tests/integration/api_repo_test.go | 8 | ||||
-rw-r--r-- | tests/integration/create_no_session_test.go | 9 | ||||
-rw-r--r-- | tests/integration/git_clone_wiki_test.go | 3 | ||||
-rw-r--r-- | tests/integration/git_helper_for_declarative_test.go | 10 | ||||
-rw-r--r-- | tests/integration/git_test.go | 21 | ||||
-rw-r--r-- | tests/integration/gpg_git_test.go | 7 | ||||
-rw-r--r-- | tests/integration/lfs_local_endpoint_test.go | 9 | ||||
-rw-r--r-- | tests/integration/migrate_test.go | 11 | ||||
-rw-r--r-- | tests/integration/repo_tag_test.go | 8 | ||||
-rw-r--r-- | tests/integration/ssh_key_test.go | 25 |
11 files changed, 32 insertions, 89 deletions
diff --git a/tests/integration/api_repo_file_get_test.go b/tests/integration/api_repo_file_get_test.go index 2a7a5fa634..cca72c2b3e 100644 --- a/tests/integration/api_repo_file_get_test.go +++ b/tests/integration/api_repo_file_get_test.go @@ -7,11 +7,9 @@ package integration import ( "net/http" "net/url" - "os" "testing" api "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" @@ -30,18 +28,14 @@ func TestAPIGetRawFileOrLFS(t *testing.T) { httpContext := NewAPITestContext(t, "user2", "repo-lfs-test") doAPICreateRepository(httpContext, false, func(t *testing.T, repository api.Repository) { u.Path = httpContext.GitPath() - dstPath, err := os.MkdirTemp("", httpContext.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstPath) + dstPath := t.TempDir() u.Path = httpContext.GitPath() u.User = url.UserPassword("user2", userPassword) t.Run("Clone", doGitClone(dstPath, u)) - dstPath2, err := os.MkdirTemp("", httpContext.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstPath2) + dstPath2 := t.TempDir() t.Run("Partial Clone", doPartialGitClone(dstPath2, u)) diff --git a/tests/integration/api_repo_test.go b/tests/integration/api_repo_test.go index 483503ccbb..bfe0c0aa9c 100644 --- a/tests/integration/api_repo_test.go +++ b/tests/integration/api_repo_test.go @@ -8,7 +8,6 @@ import ( "fmt" "net/http" "net/url" - "os" "testing" "code.gitea.io/gitea/models" @@ -19,7 +18,6 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" @@ -389,9 +387,6 @@ func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) { httpContext := baseAPITestContext httpContext.Reponame = "repo-tmp-17" - dstPath, err := os.MkdirTemp("", httpContext.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstPath) t.Run("CreateRepo", doAPICreateRepository(httpContext, false)) user, err := user_model.GetUserByName(db.DefaultContext, httpContext.Username) @@ -473,9 +468,6 @@ func testAPIRepoCreateConflict(t *testing.T, u *url.URL) { httpContext := baseAPITestContext httpContext.Reponame = "repo-tmp-17" - dstPath, err := os.MkdirTemp("", httpContext.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstPath) t.Run("CreateRepo", doAPICreateRepository(httpContext, false)) req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos?token="+httpContext.Token, diff --git a/tests/integration/create_no_session_test.go b/tests/integration/create_no_session_test.go index c9b90974d7..c617b1828a 100644 --- a/tests/integration/create_no_session_test.go +++ b/tests/integration/create_no_session_test.go @@ -14,7 +14,6 @@ import ( "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers" "code.gitea.io/gitea/tests" @@ -70,13 +69,7 @@ func TestSessionFileCreation(t *testing.T) { config.Provider = "file" // Now create a temporaryDirectory - tmpDir, err := os.MkdirTemp("", "sessions") - assert.NoError(t, err) - defer func() { - if _, err := os.Stat(tmpDir); !os.IsNotExist(err) { - _ = util.RemoveAll(tmpDir) - } - }() + tmpDir := t.TempDir() config.ProviderConfig = tmpDir newConfigBytes, err := json.Marshal(config) diff --git a/tests/integration/git_clone_wiki_test.go b/tests/integration/git_clone_wiki_test.go index 4bdbc9b7c3..3e10b17d40 100644 --- a/tests/integration/git_clone_wiki_test.go +++ b/tests/integration/git_clone_wiki_test.go @@ -35,8 +35,7 @@ func TestRepoCloneWiki(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { defer tests.PrepareTestEnv(t)() - dstPath, err := os.MkdirTemp("", "clone_wiki") - assert.NoError(t, err) + dstPath := t.TempDir() r := fmt.Sprintf("%suser2/repo1.wiki.git", u.String()) u, _ = url.Parse(r) diff --git a/tests/integration/git_helper_for_declarative_test.go b/tests/integration/git_helper_for_declarative_test.go index 666f9f6fe9..4f2e540883 100644 --- a/tests/integration/git_helper_for_declarative_test.go +++ b/tests/integration/git_helper_for_declarative_test.go @@ -27,11 +27,9 @@ import ( ) func withKeyFile(t *testing.T, keyname string, callback func(string)) { - tmpDir, err := os.MkdirTemp("", "key-file") - assert.NoError(t, err) - defer util.RemoveAll(tmpDir) + tmpDir := t.TempDir() - err = os.Chmod(tmpDir, 0o700) + err := os.Chmod(tmpDir, 0o700) assert.NoError(t, err) keyFile := filepath.Join(tmpDir, keyname) @@ -120,9 +118,7 @@ func doPartialGitClone(dstLocalPath string, u *url.URL) func(*testing.T) { func doGitCloneFail(u *url.URL) func(*testing.T) { return func(t *testing.T) { - tmpDir, err := os.MkdirTemp("", "doGitCloneFail") - assert.NoError(t, err) - defer util.RemoveAll(tmpDir) + tmpDir := t.TempDir() assert.Error(t, git.Clone(git.DefaultContext, u.String(), tmpDir, git.CloneRepoOptions{})) exist, err := util.IsExist(filepath.Join(tmpDir, "README.md")) assert.NoError(t, err) diff --git a/tests/integration/git_test.go b/tests/integration/git_test.go index caeb5db8b3..bf5e809dab 100644 --- a/tests/integration/git_test.go +++ b/tests/integration/git_test.go @@ -27,7 +27,6 @@ import ( "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" @@ -57,9 +56,7 @@ func testGit(t *testing.T, u *url.URL) { httpContext.Reponame = "repo-tmp-17" forkedUserCtx.Reponame = httpContext.Reponame - dstPath, err := os.MkdirTemp("", httpContext.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstPath) + dstPath := t.TempDir() t.Run("CreateRepoInDifferentUser", doAPICreateRepository(forkedUserCtx, false)) t.Run("AddUserAsCollaborator", doAPIAddCollaborator(forkedUserCtx, httpContext.Username, perm.AccessModeRead)) @@ -71,9 +68,7 @@ func testGit(t *testing.T, u *url.URL) { t.Run("Clone", doGitClone(dstPath, u)) - dstPath2, err := os.MkdirTemp("", httpContext.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstPath2) + dstPath2 := t.TempDir() t.Run("Partial Clone", doPartialGitClone(dstPath2, u)) @@ -114,9 +109,7 @@ func testGit(t *testing.T, u *url.URL) { sshURL := createSSHUrl(sshContext.GitPath(), u) // Setup clone folder - dstPath, err := os.MkdirTemp("", sshContext.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstPath) + dstPath := t.TempDir() t.Run("Clone", doGitClone(dstPath, sshURL)) @@ -140,9 +133,7 @@ func testGit(t *testing.T, u *url.URL) { } func ensureAnonymousClone(t *testing.T, u *url.URL) { - dstLocalPath, err := os.MkdirTemp("", "repo1") - assert.NoError(t, err) - defer util.RemoveAll(dstLocalPath) + dstLocalPath := t.TempDir() t.Run("CloneAnonymous", doGitClone(dstLocalPath, u)) } @@ -560,9 +551,7 @@ func doPushCreate(ctx APITestContext, u *url.URL) func(t *testing.T) { u.Path = ctx.GitPath() // Create a temporary directory - tmpDir, err := os.MkdirTemp("", ctx.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(tmpDir) + tmpDir := t.TempDir() // Now create local repository to push as our test and set its origin t.Run("InitTestRepository", doGitInitTestRepository(tmpDir)) diff --git a/tests/integration/gpg_git_test.go b/tests/integration/gpg_git_test.go index 2e16d150c8..80f484926d 100644 --- a/tests/integration/gpg_git_test.go +++ b/tests/integration/gpg_git_test.go @@ -16,7 +16,6 @@ import ( "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" @@ -29,11 +28,9 @@ func TestGPGGit(t *testing.T) { username := "user2" // OK Set a new GPG home - tmpDir, err := os.MkdirTemp("", "temp-gpg") - assert.NoError(t, err) - defer util.RemoveAll(tmpDir) + tmpDir := t.TempDir() - err = os.Chmod(tmpDir, 0o700) + err := os.Chmod(tmpDir, 0o700) assert.NoError(t, err) oldGNUPGHome := os.Getenv("GNUPGHOME") diff --git a/tests/integration/lfs_local_endpoint_test.go b/tests/integration/lfs_local_endpoint_test.go index 88c08c63db..4042dc95b3 100644 --- a/tests/integration/lfs_local_endpoint_test.go +++ b/tests/integration/lfs_local_endpoint_test.go @@ -25,15 +25,12 @@ func str2url(raw string) *url.URL { func TestDetermineLocalEndpoint(t *testing.T) { defer tests.PrepareTestEnv(t)() - root, _ := os.MkdirTemp("", "lfs_test") - defer os.RemoveAll(root) + root := t.TempDir() - rootdotgit, _ := os.MkdirTemp("", "lfs_test") - defer os.RemoveAll(rootdotgit) + rootdotgit := t.TempDir() os.Mkdir(filepath.Join(rootdotgit, ".git"), 0o700) - lfsroot, _ := os.MkdirTemp("", "lfs_test") - defer os.RemoveAll(lfsroot) + lfsroot := t.TempDir() // Test cases cases := []struct { diff --git a/tests/integration/migrate_test.go b/tests/integration/migrate_test.go index 0fe4014344..99d5d6c8dd 100644 --- a/tests/integration/migrate_test.go +++ b/tests/integration/migrate_test.go @@ -9,6 +9,7 @@ import ( "net/http" "net/url" "os" + "path/filepath" "testing" repo_model "code.gitea.io/gitea/models/repo" @@ -29,16 +30,18 @@ func TestMigrateLocalPath(t *testing.T) { old := setting.ImportLocalPaths setting.ImportLocalPaths = true - lowercasePath, err := os.MkdirTemp("", "lowercase") // may not be lowercase because MkdirTemp creates a random directory name which may be mixedcase + basePath := t.TempDir() + + lowercasePath := filepath.Join(basePath, "lowercase") + err := os.Mkdir(lowercasePath, 0o700) assert.NoError(t, err) - defer os.RemoveAll(lowercasePath) err = migrations.IsMigrateURLAllowed(lowercasePath, adminUser) assert.NoError(t, err, "case lowercase path") - mixedcasePath, err := os.MkdirTemp("", "mIxeDCaSe") + mixedcasePath := filepath.Join(basePath, "mIxeDCaSe") + err = os.Mkdir(mixedcasePath, 0o700) assert.NoError(t, err) - defer os.RemoveAll(mixedcasePath) err = migrations.IsMigrateURLAllowed(mixedcasePath, adminUser) assert.NoError(t, err, "case mixedcase path") diff --git a/tests/integration/repo_tag_test.go b/tests/integration/repo_tag_test.go index a91f1fb209..fb08895e21 100644 --- a/tests/integration/repo_tag_test.go +++ b/tests/integration/repo_tag_test.go @@ -6,7 +6,6 @@ package integration import ( "net/url" - "os" "testing" "code.gitea.io/gitea/models" @@ -15,7 +14,6 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/release" "code.gitea.io/gitea/tests" @@ -59,16 +57,14 @@ func TestCreateNewTagProtected(t *testing.T) { username := "user2" httpContext := NewAPITestContext(t, username, "repo1") - dstPath, err := os.MkdirTemp("", httpContext.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstPath) + dstPath := t.TempDir() u.Path = httpContext.GitPath() u.User = url.UserPassword(username, userPassword) doGitClone(dstPath, u)(t) - _, _, err = git.NewCommand(git.DefaultContext, "tag", "v-2").RunStdString(&git.RunOpts{Dir: dstPath}) + _, _, err := git.NewCommand(git.DefaultContext, "tag", "v-2").RunStdString(&git.RunOpts{Dir: dstPath}) assert.NoError(t, err) _, _, err = git.NewCommand(git.DefaultContext, "push", "--tags").RunStdString(&git.RunOpts{Dir: dstPath}) diff --git a/tests/integration/ssh_key_test.go b/tests/integration/ssh_key_test.go index 65d9b84404..fd98af5125 100644 --- a/tests/integration/ssh_key_test.go +++ b/tests/integration/ssh_key_test.go @@ -15,7 +15,6 @@ import ( "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) @@ -61,9 +60,7 @@ func testPushDeployKeyOnEmptyRepo(t *testing.T, u *url.URL) { t.Run("CreatePushDeployKey", doAPICreateDeployKey(ctx, keyname, keyFile, false)) // Setup the testing repository - dstPath, err := os.MkdirTemp("", "repo-tmp-deploy-key-empty-repo-1") - assert.NoError(t, err) - defer util.RemoveAll(dstPath) + dstPath := t.TempDir() t.Run("InitTestRepository", doGitInitTestRepository(dstPath)) @@ -107,9 +104,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) { withKeyFile(t, keyname, func(keyFile string) { var userKeyPublicKeyID int64 t.Run("KeyCanOnlyBeUser", func(t *testing.T) { - dstPath, err := os.MkdirTemp("", ctx.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstPath) + dstPath := t.TempDir() sshURL := createSSHUrl(ctx.GitPath(), u) @@ -133,9 +128,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) { }) t.Run("KeyCanBeAnyDeployButNotUserAswell", func(t *testing.T) { - dstPath, err := os.MkdirTemp("", ctx.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstPath) + dstPath := t.TempDir() sshURL := createSSHUrl(ctx.GitPath(), u) @@ -151,9 +144,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) { t.Run("FailToPush", doGitPushTestRepositoryFail(dstPath, "origin", "master")) otherSSHURL := createSSHUrl(otherCtx.GitPath(), u) - dstOtherPath, err := os.MkdirTemp("", otherCtx.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstOtherPath) + dstOtherPath := t.TempDir() t.Run("AddWriterDeployKeyToOther", doAPICreateDeployKey(otherCtx, keyname, keyFile, false)) @@ -168,9 +159,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) { t.Run("DeleteRepositoryShouldReleaseKey", func(t *testing.T) { otherSSHURL := createSSHUrl(otherCtx.GitPath(), u) - dstOtherPath, err := os.MkdirTemp("", otherCtx.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstOtherPath) + dstOtherPath := t.TempDir() t.Run("DeleteRepository", doAPIDeleteRepository(ctx)) @@ -190,9 +179,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) { userKeyPublicKeyID = publicKey.ID })) - dstPath, err := os.MkdirTemp("", ctx.Reponame) - assert.NoError(t, err) - defer util.RemoveAll(dstPath) + dstPath := t.TempDir() sshURL := createSSHUrl(ctx.GitPath(), u) |