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/integration/ssh_key_test.go | |
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/integration/ssh_key_test.go')
-rw-r--r-- | tests/integration/ssh_key_test.go | 25 |
1 files changed, 6 insertions, 19 deletions
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) |