aboutsummaryrefslogtreecommitdiffstats
path: root/routers/web
diff options
context:
space:
mode:
authorEng Zer Jun <engzerjun@gmail.com>2022-09-04 23:14:53 +0800
committerGitHub <noreply@github.com>2022-09-04 16:14:53 +0100
commit8b0aaa5f86e40a4699f278d09d116add63f8e4a0 (patch)
tree8133c4362e102b84c1d69df0ee09dd5a6b86b5f5 /routers/web
parentc722a26e7efa69370804ac04590e9e467decc093 (diff)
downloadgitea-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 'routers/web')
-rw-r--r--routers/web/repo/settings_test.go9
1 files changed, 1 insertions, 8 deletions
diff --git a/routers/web/repo/settings_test.go b/routers/web/repo/settings_test.go
index 946220b4fc..4acd7df5b8 100644
--- a/routers/web/repo/settings_test.go
+++ b/routers/web/repo/settings_test.go
@@ -6,7 +6,6 @@ package repo
import (
"net/http"
- "os"
"testing"
"code.gitea.io/gitea/models"
@@ -19,7 +18,6 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
- "code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/services/forms"
@@ -27,18 +25,13 @@ import (
)
func createSSHAuthorizedKeysTmpPath(t *testing.T) func() {
- tmpDir, err := os.MkdirTemp("", "tmp-ssh")
- if err != nil {
- assert.Fail(t, "Unable to create temporary directory: %v", err)
- return nil
- }
+ tmpDir := t.TempDir()
oldPath := setting.SSH.RootPath
setting.SSH.RootPath = tmpDir
return func() {
setting.SSH.RootPath = oldPath
- util.RemoveAll(tmpDir)
}
}