mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-04 22:00:39 +00:00
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>
This commit is contained in:
parent
c722a26e7e
commit
8b0aaa5f86
24 changed files with 57 additions and 194 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue