test: use `T.TempDir` to create temporary test directory

The directory created by `T.TempDir` is automatically removed when the
test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2022-03-01 13:57:39 +08:00
parent 3c13ae7ba0
commit 83e66e73f2
No known key found for this signature in database
GPG Key ID: DAEBBD2E34C111E6
8 changed files with 38 additions and 214 deletions

View File

@ -58,12 +58,7 @@ func shutdownEtcd(tetcd *testutil.TestEmbeddedEtcd) {
} }
func TestEtcdReset(t *testing.T) { func TestEtcdReset(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -167,12 +162,7 @@ func TestEtcdReset(t *testing.T) {
} }
func TestEtcdResetWalsGap(t *testing.T) { func TestEtcdResetWalsGap(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -295,12 +285,7 @@ func TestEtcdResetWalsGap(t *testing.T) {
} }
func TestConcurrentUpdate(t *testing.T) { func TestConcurrentUpdate(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -388,12 +373,7 @@ func TestConcurrentUpdate(t *testing.T) {
} }
func TestEtcdWalCleaner(t *testing.T) { func TestEtcdWalCleaner(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -464,12 +444,7 @@ func TestEtcdWalCleaner(t *testing.T) {
} }
func TestReadObject(t *testing.T) { func TestReadObject(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -759,12 +734,7 @@ func TestCheckpoint(t *testing.T) {
} }
func testCheckpoint(t *testing.T, basePath string) { func testCheckpoint(t *testing.T, basePath string) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -977,12 +947,7 @@ func testCheckpoint(t *testing.T, basePath string) {
} }
func TestRead(t *testing.T) { func TestRead(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -1093,12 +1058,7 @@ func TestClean(t *testing.T) {
} }
func testClean(t *testing.T, basePath string) { func testClean(t *testing.T, basePath string) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -1213,12 +1173,7 @@ func TestCleanConcurrentCheckpoint(t *testing.T) {
} }
func testCleanConcurrentCheckpoint(t *testing.T, basePath string) { func testCleanConcurrentCheckpoint(t *testing.T, basePath string) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -1344,12 +1299,7 @@ func TestStorageWalCleaner(t *testing.T) {
} }
func testStorageWalCleaner(t *testing.T, basePath string) { func testStorageWalCleaner(t *testing.T, basePath string) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -1486,12 +1436,7 @@ func testStorageWalCleaner(t *testing.T, basePath string) {
} }
func TestExportImport(t *testing.T) { func TestExportImport(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")

View File

@ -17,7 +17,6 @@ package objectstorage
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -47,11 +46,7 @@ func setupS3(t *testing.T, dir string) (*S3Storage, error) {
} }
func TestList(t *testing.T) { func TestList(t *testing.T) {
dir, err := ioutil.TempDir("", "objectstorage") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ps, err := setupPosix(t, dir) ps, err := setupPosix(t, dir)
if err != nil { if err != nil {
@ -302,11 +297,7 @@ func TestList(t *testing.T) {
} }
func TestWriteObject(t *testing.T) { func TestWriteObject(t *testing.T) {
dir, err := ioutil.TempDir("", "objectstorage") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ps, err := setupPosix(t, dir) ps, err := setupPosix(t, dir)
if err != nil { if err != nil {

View File

@ -16,7 +16,6 @@ package objectstorage
import ( import (
"bytes" "bytes"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -25,11 +24,7 @@ import (
func TestPosixDeleteObject(t *testing.T) { func TestPosixDeleteObject(t *testing.T) {
objects := []string{"☺☺☺☺a☺☺☺☺☺☺b☺☺☺☺", "s3/is/nota/fil.fa", "s3/is/not/a/file///system/fi%l%%e01"} objects := []string{"☺☺☺☺a☺☺☺☺☺☺b☺☺☺☺", "s3/is/nota/fil.fa", "s3/is/not/a/file///system/fi%l%%e01"}
dir, err := ioutil.TempDir("", "objectstorage") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
//defer os.RemoveAll(dir)
ls, err := NewPosix(dir) ls, err := NewPosix(dir)
if err != nil { if err != nil {

View File

@ -16,7 +16,6 @@ package objectstorage
import ( import (
"bytes" "bytes"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -82,11 +81,7 @@ func TestEscapeUnescape(t *testing.T) {
func TestPosixFlatDeleteObject(t *testing.T) { func TestPosixFlatDeleteObject(t *testing.T) {
objects := []string{"/", "//", "☺☺☺☺a☺☺☺☺☺☺b☺☺☺☺", "s3/is/nota/fil.fa", "s3/is/not/a/file///system/fi%l%%e01"} objects := []string{"/", "//", "☺☺☺☺a☺☺☺☺☺☺b☺☺☺☺", "s3/is/nota/fil.fa", "s3/is/not/a/file///system/fi%l%%e01"}
dir, err := ioutil.TempDir("", "objectstorage") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
//defer os.RemoveAll(dir)
ls, err := NewPosixFlat(dir) ls, err := NewPosixFlat(dir)
if err != nil { if err != nil {

View File

@ -16,7 +16,6 @@ package config
import ( import (
"io/ioutil" "io/ioutil"
"os"
"path" "path"
"testing" "testing"
@ -229,14 +228,10 @@ gitserver:
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "ParseConfig") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
content := []byte(tt.in) content := []byte(tt.in)
err = ioutil.WriteFile(path.Join(dir, "config.yml"), content, 0644) err := ioutil.WriteFile(path.Join(dir, "config.yml"), content, 0644)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }

View File

@ -20,7 +20,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"os"
"path" "path"
"reflect" "reflect"
"sync" "sync"
@ -124,12 +123,7 @@ func getUsers(ctx context.Context, cs *Configstore) ([]*types.User, error) {
} }
func TestResync(t *testing.T) { func TestResync(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -300,12 +294,7 @@ func TestResync(t *testing.T) {
} }
func TestExportImport(t *testing.T) { func TestExportImport(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
etcdDir, err := ioutil.TempDir(dir, "etcd") etcdDir, err := ioutil.TempDir(dir, "etcd")
@ -549,12 +538,7 @@ func compareUsers(u1, u2 []*types.User) bool {
} }
func TestUser(t *testing.T) { func TestUser(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx := context.Background() ctx := context.Background()
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
@ -617,12 +601,7 @@ func TestUser(t *testing.T) {
} }
func TestProjectGroupsAndProjectsCreate(t *testing.T) { func TestProjectGroupsAndProjectsCreate(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx := context.Background() ctx := context.Background()
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
@ -765,12 +744,7 @@ func TestProjectGroupsAndProjectsCreate(t *testing.T) {
} }
func TestProjectUpdate(t *testing.T) { func TestProjectUpdate(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx := context.Background() ctx := context.Background()
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
@ -842,12 +816,7 @@ func TestProjectUpdate(t *testing.T) {
} }
func TestProjectGroupUpdate(t *testing.T) { func TestProjectGroupUpdate(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx := context.Background() ctx := context.Background()
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
@ -993,12 +962,7 @@ func TestProjectGroupUpdate(t *testing.T) {
} }
func TestProjectGroupDelete(t *testing.T) { func TestProjectGroupDelete(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx := context.Background() ctx := context.Background()
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
@ -1050,12 +1014,7 @@ func TestProjectGroupDelete(t *testing.T) {
} }
func TestProjectGroupDeleteDontSeeOldChildObjects(t *testing.T) { func TestProjectGroupDeleteDontSeeOldChildObjects(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx := context.Background() ctx := context.Background()
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
@ -1186,12 +1145,7 @@ func TestProjectGroupDeleteDontSeeOldChildObjects(t *testing.T) {
} }
func TestOrgMembers(t *testing.T) { func TestOrgMembers(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx := context.Background() ctx := context.Background()
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
@ -1277,12 +1231,7 @@ func TestOrgMembers(t *testing.T) {
} }
func TestRemoteSource(t *testing.T) { func TestRemoteSource(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
tests := []struct { tests := []struct {

View File

@ -16,7 +16,6 @@ package gitserver
import ( import (
"context" "context"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -87,12 +86,7 @@ func TestRepoCleaner(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
log := testutil.NewLogger(t) log := testutil.NewLogger(t)
dir := t.TempDir()
dir, err := ioutil.TempDir("", "agola")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()

View File

@ -369,11 +369,7 @@ func setup(ctx context.Context, t *testing.T, dir string, gitea bool) (*testutil
} }
func TestCreateLinkedAccount(t *testing.T) { func TestCreateLinkedAccount(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -445,11 +441,7 @@ func createLinkedAccount(ctx context.Context, t *testing.T, tgitea *testutil.Tes
} }
func TestCreateProject(t *testing.T) { func TestCreateProject(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -491,11 +483,7 @@ func TestUpdateProject(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -797,11 +785,7 @@ func TestPush(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -988,11 +972,7 @@ func TestDirectRun(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -1136,11 +1116,7 @@ func TestDirectRunVariables(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
if err := ioutil.WriteFile(filepath.Join(dir, "varfile01.yml"), []byte(varfile01), 0644); err != nil { if err := ioutil.WriteFile(filepath.Join(dir, "varfile01.yml"), []byte(varfile01), 0644); err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
@ -1310,11 +1286,7 @@ func TestDirectRunLogs(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -1489,11 +1461,7 @@ func TestPullRequest(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -1828,11 +1796,7 @@ def main(ctx):
config = starlarkConfig config = starlarkConfig
} }
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -1936,11 +1900,7 @@ def main(ctx):
} }
func TestUserOrgs(t *testing.T) { func TestUserOrgs(t *testing.T) {
dir, err := ioutil.TempDir("", "agola") dir := t.TempDir()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()