From 8b0aaa5f86e40a4699f278d09d116add63f8e4a0 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Sun, 4 Sep 2022 23:14:53 +0800 Subject: 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 --- modules/indexer/code/bleve_test.go | 10 +--------- modules/indexer/issues/bleve_test.go | 13 ++----------- modules/indexer/issues/indexer_test.go | 9 +-------- 3 files changed, 4 insertions(+), 28 deletions(-) (limited to 'modules/indexer') 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" -- cgit v1.2.3