summaryrefslogtreecommitdiffstats
path: root/modules
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 /modules
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 'modules')
-rw-r--r--modules/git/commit_info_test.go17
-rw-r--r--modules/git/repo_compare_test.go8
-rw-r--r--modules/git/repo_tag_test.go8
-rw-r--r--modules/indexer/code/bleve_test.go10
-rw-r--r--modules/indexer/issues/bleve_test.go13
-rw-r--r--modules/indexer/issues/indexer_test.go9
-rw-r--r--modules/log/file_test.go20
-rw-r--r--modules/queue/queue_disk_channel_test.go10
-rw-r--r--modules/queue/queue_disk_test.go7
9 files changed, 20 insertions, 82 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 {
diff --git a/modules/indexer/code/bleve_test.go b/modules/indexer/code/bleve_test.go
index 37ed2eb9f0..a34d54bc0e 100644
--- a/modules/indexer/code/bleve_test.go
+++ b/modules/indexer/code/bleve_test.go
@@ -5,11 +5,9 @@
package code
import (
- "os"
"testing"
"code.gitea.io/gitea/models/unittest"
- "code.gitea.io/gitea/modules/util"
"github.com/stretchr/testify/assert"
)
@@ -17,13 +15,7 @@ import (
func TestBleveIndexAndSearch(t *testing.T) {
unittest.PrepareTestEnv(t)
- dir, err := os.MkdirTemp("", "bleve.index")
- assert.NoError(t, err)
- if err != nil {
- assert.Fail(t, "Unable to create temporary directory")
- return
- }
- defer util.RemoveAll(dir)
+ dir := t.TempDir()
idx, _, err := NewBleveIndexer(dir)
if err != nil {
diff --git a/modules/indexer/issues/bleve_test.go b/modules/indexer/issues/bleve_test.go
index 68a7831a3f..42d22f06a2 100644
--- a/modules/indexer/issues/bleve_test.go
+++ b/modules/indexer/issues/bleve_test.go
@@ -6,22 +6,13 @@ package issues
import (
"context"
- "os"
"testing"
- "code.gitea.io/gitea/modules/util"
-
"github.com/stretchr/testify/assert"
)
func TestBleveIndexAndSearch(t *testing.T) {
- dir, err := os.MkdirTemp("", "bleve.index")
- assert.NoError(t, err)
- if err != nil {
- assert.Fail(t, "Unable to create temporary directory")
- return
- }
- defer util.RemoveAll(dir)
+ dir := t.TempDir()
indexer := NewBleveIndexer(dir)
defer indexer.Close()
@@ -30,7 +21,7 @@ func TestBleveIndexAndSearch(t *testing.T) {
return
}
- err = indexer.Index([]*IndexerData{
+ err := indexer.Index([]*IndexerData{
{
ID: 1,
RepoID: 2,
diff --git a/modules/indexer/issues/indexer_test.go b/modules/indexer/issues/indexer_test.go
index 6bafcbdf24..e8b9ff8370 100644
--- a/modules/indexer/issues/indexer_test.go
+++ b/modules/indexer/issues/indexer_test.go
@@ -6,7 +6,6 @@ package issues
import (
"context"
- "os"
"path"
"path/filepath"
"testing"
@@ -14,7 +13,6 @@ import (
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"
- "code.gitea.io/gitea/modules/util"
_ "code.gitea.io/gitea/models"
@@ -32,11 +30,7 @@ func TestBleveSearchIssues(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
setting.Cfg = ini.Empty()
- tmpIndexerDir, err := os.MkdirTemp("", "issues-indexer")
- if err != nil {
- assert.Fail(t, "Unable to create temporary directory: %v", err)
- return
- }
+ tmpIndexerDir := t.TempDir()
setting.Cfg.Section("queue.issue_indexer").Key("DATADIR").MustString(path.Join(tmpIndexerDir, "issues.queue"))
@@ -44,7 +38,6 @@ func TestBleveSearchIssues(t *testing.T) {
setting.Indexer.IssuePath = path.Join(tmpIndexerDir, "issues.queue")
defer func() {
setting.Indexer.IssuePath = oldIssuePath
- util.RemoveAll(tmpIndexerDir)
}()
setting.Indexer.IssueType = "bleve"
diff --git a/modules/log/file_test.go b/modules/log/file_test.go
index c3074b69df..cc2b9fe077 100644
--- a/modules/log/file_test.go
+++ b/modules/log/file_test.go
@@ -14,15 +14,11 @@ import (
"testing"
"time"
- "code.gitea.io/gitea/modules/util"
-
"github.com/stretchr/testify/assert"
)
func TestFileLoggerFails(t *testing.T) {
- tmpDir, err := os.MkdirTemp("", "TestFileLogger")
- assert.NoError(t, err)
- defer util.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
prefix := "TestPrefix "
level := INFO
@@ -34,7 +30,7 @@ func TestFileLoggerFails(t *testing.T) {
// assert.True(t, ok)
// Fail if there is bad json
- err = fileLogger.Init("{")
+ err := fileLogger.Init("{")
assert.Error(t, err)
// Fail if there is no filename
@@ -47,9 +43,7 @@ func TestFileLoggerFails(t *testing.T) {
}
func TestFileLogger(t *testing.T) {
- tmpDir, err := os.MkdirTemp("", "TestFileLogger")
- assert.NoError(t, err)
- defer util.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
prefix := "TestPrefix "
level := INFO
@@ -150,9 +144,7 @@ func TestFileLogger(t *testing.T) {
}
func TestCompressFileLogger(t *testing.T) {
- tmpDir, err := os.MkdirTemp("", "TestFileLogger")
- assert.NoError(t, err)
- defer util.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
prefix := "TestPrefix "
level := INFO
@@ -210,9 +202,7 @@ func TestCompressFileLogger(t *testing.T) {
}
func TestCompressOldFile(t *testing.T) {
- tmpDir, err := os.MkdirTemp("", "TestFileLogger")
- assert.NoError(t, err)
- defer util.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
fname := filepath.Join(tmpDir, "test")
nonGzip := filepath.Join(tmpDir, "test-nonGzip")
diff --git a/modules/queue/queue_disk_channel_test.go b/modules/queue/queue_disk_channel_test.go
index 22b4f0f452..b1820e73ac 100644
--- a/modules/queue/queue_disk_channel_test.go
+++ b/modules/queue/queue_disk_channel_test.go
@@ -5,13 +5,11 @@
package queue
import (
- "os"
"sync"
"testing"
"time"
"code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/util"
"github.com/stretchr/testify/assert"
)
@@ -33,9 +31,7 @@ func TestPersistableChannelQueue(t *testing.T) {
queueShutdown := []func(){}
queueTerminate := []func(){}
- tmpDir, err := os.MkdirTemp("", "persistable-channel-queue-test-data")
- assert.NoError(t, err)
- defer util.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
queue, err := NewPersistableChannelQueue(handle, PersistableChannelQueueConfiguration{
DataDir: tmpDir,
@@ -223,9 +219,7 @@ func TestPersistableChannelQueue_Pause(t *testing.T) {
queueTerminate := []func(){}
terminated := make(chan struct{})
- tmpDir, err := os.MkdirTemp("", "persistable-channel-queue-pause-test-data")
- assert.NoError(t, err)
- defer util.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
queue, err = NewPersistableChannelQueue(handle, PersistableChannelQueueConfiguration{
DataDir: tmpDir,
diff --git a/modules/queue/queue_disk_test.go b/modules/queue/queue_disk_test.go
index d2d8e135cb..b0835c896f 100644
--- a/modules/queue/queue_disk_test.go
+++ b/modules/queue/queue_disk_test.go
@@ -5,13 +5,10 @@
package queue
import (
- "os"
"sync"
"testing"
"time"
- "code.gitea.io/gitea/modules/util"
-
"github.com/stretchr/testify/assert"
)
@@ -30,9 +27,7 @@ func TestLevelQueue(t *testing.T) {
queueShutdown := []func(){}
queueTerminate := []func(){}
- tmpDir, err := os.MkdirTemp("", "level-queue-test-data")
- assert.NoError(t, err)
- defer util.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
queue, err := NewLevelQueue(handle, LevelQueueConfiguration{
ByteFIFOQueueConfiguration: ByteFIFOQueueConfiguration{