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 /modules/git | |
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 'modules/git')
-rw-r--r-- | modules/git/commit_info_test.go | 17 | ||||
-rw-r--r-- | modules/git/repo_compare_test.go | 8 | ||||
-rw-r--r-- | modules/git/repo_tag_test.go | 8 |
3 files changed, 8 insertions, 25 deletions
diff --git a/modules/git/commit_info_test.go b/modules/git/commit_info_test.go index a12452c404..4bc3596896 100644 --- a/modules/git/commit_info_test.go +++ b/modules/git/commit_info_test.go @@ -6,13 +6,10 @@ package git import ( "context" - "os" "path/filepath" "testing" "time" - "code.gitea.io/gitea/modules/util" - "github.com/stretchr/testify/assert" ) @@ -20,18 +17,14 @@ const ( testReposDir = "tests/repos/" ) -func cloneRepo(url, name string) (string, error) { - repoDir, err := os.MkdirTemp("", name) - if err != nil { - return "", err - } +func cloneRepo(tb testing.TB, url string) (string, error) { + repoDir := tb.TempDir() if err := Clone(DefaultContext, url, repoDir, CloneRepoOptions{ Mirror: false, Bare: false, Quiet: true, Timeout: 5 * time.Minute, }); err != nil { - _ = util.RemoveAll(repoDir) return "", err } return repoDir, nil @@ -118,11 +111,10 @@ func TestEntries_GetCommitsInfo(t *testing.T) { testGetCommitsInfo(t, bareRepo1) - clonedPath, err := cloneRepo(bareRepo1Path, "repo1_TestEntries_GetCommitsInfo") + clonedPath, err := cloneRepo(t, bareRepo1Path) if err != nil { assert.NoError(t, err) } - defer util.RemoveAll(clonedPath) clonedRepo1, err := openRepositoryWithDefaultContext(clonedPath) if err != nil { assert.NoError(t, err) @@ -150,11 +142,10 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) { var commit *Commit var entries Entries var repo *Repository - repoPath, err := cloneRepo(benchmark.url, benchmark.name) + repoPath, err := cloneRepo(b, benchmark.url) if err != nil { b.Fatal(err) } - defer util.RemoveAll(repoPath) if repo, err = openRepositoryWithDefaultContext(repoPath); err != nil { b.Fatal(err) diff --git a/modules/git/repo_compare_test.go b/modules/git/repo_compare_test.go index 245920c2bd..63f7254dea 100644 --- a/modules/git/repo_compare_test.go +++ b/modules/git/repo_compare_test.go @@ -10,19 +10,16 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/modules/util" - "github.com/stretchr/testify/assert" ) func TestGetFormatPatch(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") - clonedPath, err := cloneRepo(bareRepo1Path, "repo1_TestGetFormatPatch") + clonedPath, err := cloneRepo(t, bareRepo1Path) if err != nil { assert.NoError(t, err) return } - defer util.RemoveAll(clonedPath) repo, err := openRepositoryWithDefaultContext(clonedPath) if err != nil { @@ -84,12 +81,11 @@ func TestReadWritePullHead(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") // As we are writing we should clone the repository first - clonedPath, err := cloneRepo(bareRepo1Path, "TestReadWritePullHead") + clonedPath, err := cloneRepo(t, bareRepo1Path) if err != nil { assert.NoError(t, err) return } - defer util.RemoveAll(clonedPath) repo, err := openRepositoryWithDefaultContext(clonedPath) if err != nil { diff --git a/modules/git/repo_tag_test.go b/modules/git/repo_tag_test.go index 9d84672862..6a00473bb7 100644 --- a/modules/git/repo_tag_test.go +++ b/modules/git/repo_tag_test.go @@ -8,8 +8,6 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/modules/util" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -38,12 +36,11 @@ func TestRepository_GetTags(t *testing.T) { func TestRepository_GetTag(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") - clonedPath, err := cloneRepo(bareRepo1Path, "TestRepository_GetTag") + clonedPath, err := cloneRepo(t, bareRepo1Path) if err != nil { assert.NoError(t, err) return } - defer util.RemoveAll(clonedPath) bareRepo1, err := openRepositoryWithDefaultContext(clonedPath) if err != nil { @@ -143,12 +140,11 @@ func TestRepository_GetTag(t *testing.T) { func TestRepository_GetAnnotatedTag(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") - clonedPath, err := cloneRepo(bareRepo1Path, "TestRepository_GetAnnotatedTag") + clonedPath, err := cloneRepo(t, bareRepo1Path) if err != nil { assert.NoError(t, err) return } - defer util.RemoveAll(clonedPath) bareRepo1, err := openRepositoryWithDefaultContext(clonedPath) if err != nil { |