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 /services | |
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 'services')
-rw-r--r-- | services/wiki/wiki_test.go | 12 |
1 files changed, 2 insertions, 10 deletions
diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index 938a7d6acd..cabeecb60a 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -5,7 +5,6 @@ package wiki import ( - "os" "path/filepath" "testing" @@ -13,7 +12,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" "github.com/stretchr/testify/assert" ) @@ -273,15 +271,9 @@ func TestPrepareWikiFileName_FirstPage(t *testing.T) { unittest.PrepareTestEnv(t) // Now create a temporaryDirectory - tmpDir, err := os.MkdirTemp("", "empty-wiki") - assert.NoError(t, err) - defer func() { - if _, err := os.Stat(tmpDir); !os.IsNotExist(err) { - _ = util.RemoveAll(tmpDir) - } - }() + tmpDir := t.TempDir() - err = git.InitRepository(git.DefaultContext, tmpDir, true) + err := git.InitRepository(git.DefaultContext, tmpDir, true) assert.NoError(t, err) gitRepo, err := git.OpenRepository(git.DefaultContext, tmpDir) |