Skip to content

Commit

Permalink
test: use T.TempDir to create temporary test directory (#1004)
Browse files Browse the repository at this point in the history
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee committed Aug 1, 2022
1 parent 6ac352f commit 43e4324
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 45 deletions.
6 changes: 3 additions & 3 deletions alpha/action/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestMigrate(t *testing.T) {
err := generateSqliteFile(dbFile, sqliteBundles)
require.NoError(t, err)

reg, err := newMigrateRegistry(sqliteBundles)
reg, err := newMigrateRegistry(t, sqliteBundles)
require.NoError(t, err)

specs := []spec{
Expand Down Expand Up @@ -118,8 +118,8 @@ func TestMigrate(t *testing.T) {
}
}

func newMigrateRegistry(imageMap map[image.Reference]string) (image.Registry, error) {
subSqliteImage, err := generateSqliteFS(imageMap)
func newMigrateRegistry(t *testing.T, imageMap map[image.Reference]string) (image.Registry, error) {
subSqliteImage, err := generateSqliteFS(t, imageMap)
if err != nil {
return nil, err
}
Expand Down
16 changes: 6 additions & 10 deletions alpha/action/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestRender(t *testing.T) {
assertion require.ErrorAssertionFunc
}

reg, err := newRegistry()
reg, err := newRegistry(t)
require.NoError(t, err)
foov1csv, err := bundleImageV1.ReadFile("testdata/foo-bundle-v0.1.0/manifests/foo.v0.1.0.csv.yaml")
require.NoError(t, err)
Expand Down Expand Up @@ -563,7 +563,7 @@ func TestAllowRefMask(t *testing.T) {
expectErr error
}

reg, err := newRegistry()
reg, err := newRegistry(t)
require.NoError(t, err)

dir := t.TempDir()
Expand Down Expand Up @@ -766,13 +766,13 @@ var bundleImageV2NoCSVRelatedImages embed.FS
//go:embed testdata/foo-index-v0.2.0-declcfg/foo/*
var declcfgImage embed.FS

func newRegistry() (image.Registry, error) {
func newRegistry(t *testing.T) (image.Registry, error) {
imageMap := map[image.Reference]string{
image.SimpleReference("test.registry/foo-operator/foo-bundle:v0.1.0"): "testdata/foo-bundle-v0.1.0",
image.SimpleReference("test.registry/foo-operator/foo-bundle:v0.2.0"): "testdata/foo-bundle-v0.2.0",
}

subSqliteImage, err := generateSqliteFS(imageMap)
subSqliteImage, err := generateSqliteFS(t, imageMap)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -828,12 +828,8 @@ func newRegistry() (image.Registry, error) {
}, nil
}

func generateSqliteFS(imageMap map[image.Reference]string) (fs.FS, error) {
dir, err := os.MkdirTemp("", "opm-render-test-")
if err != nil {
return nil, err
}
defer os.RemoveAll(dir)
func generateSqliteFS(t *testing.T, imageMap map[image.Reference]string) (fs.FS, error) {
dir := t.TempDir()

dbFile := filepath.Join(dir, "index.db")
if err := generateSqliteFile(dbFile, imageMap); err != nil {
Expand Down
4 changes: 1 addition & 3 deletions alpha/property/property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ func TestFile_GetData(t *testing.T) {

for _, s := range specs {
t.Run(s.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "operator-registry-test-file-")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()

if s.createFile != nil {
require.NoError(t, s.createFile(dir))
Expand Down
7 changes: 2 additions & 5 deletions pkg/image/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package image

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -16,9 +15,7 @@ func TestMockRegistry(t *testing.T) {
dne := SimpleReference("dne")
ctx := context.Background()

tmpDir, err := ioutil.TempDir("", "reg-test-mock-")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()

r := MockRegistry{
RemoteImages: map[Reference]*MockImage{
Expand All @@ -44,7 +41,7 @@ func TestMockRegistry(t *testing.T) {

// Test unpack and labels of unpulled ref
require.Error(t, r.Unpack(ctx, exists, tmpDir))
_, err = r.Labels(ctx, exists)
_, err := r.Labels(ctx, exists)
require.Error(t, err)

// Test pull of existing ref
Expand Down
6 changes: 2 additions & 4 deletions pkg/lib/bundle/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ COPY x/y/z /metadata/
}

func TestCopyYamlOutput(t *testing.T) {
testOutputDir, _ := ioutil.TempDir("./", "test-generate")
defer os.RemoveAll(testOutputDir)
testOutputDir := t.TempDir()

testContent := []byte{0, 1, 0, 0}
testManifestDir := "./testdata/generate/manifests"
Expand Down Expand Up @@ -224,8 +223,7 @@ func TestCopyYamlOutput_NoOutputDir(t *testing.T) {
}

func TestCopyYamlOutput_NestedCopy(t *testing.T) {
testOutputDir, _ := ioutil.TempDir("./", "test-generate")
defer os.RemoveAll(testOutputDir)
testOutputDir := t.TempDir()

testContent := []byte{0, 1, 0, 0}
testManifestDir := "./testdata/generate/nested_manifests"
Expand Down
3 changes: 1 addition & 2 deletions pkg/lib/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,7 @@ func TestCheckForBundles(t *testing.T) {

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
tmpdir, err := os.MkdirTemp(".", "tmpdir-*")
defer os.RemoveAll(tmpdir)
tmpdir := t.TempDir()
db, cleanup := CreateTestDb(t)
defer cleanup()
load, err := sqlite.NewSQLLiteLoader(db)
Expand Down
4 changes: 1 addition & 3 deletions pkg/registry/populator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,9 +1184,7 @@ func TestDeprecatePackage(t *testing.T) {
}

func TestAddAfterDeprecate(t *testing.T) {
tmpdir, err := os.MkdirTemp(".", "add-after-deprecate-*")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
tmpdir := t.TempDir()

/*
(0.1) 0.1.2 <- 0.1.1 <- 0.1.0
Expand Down
8 changes: 1 addition & 7 deletions pkg/sqlite/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package sqlite

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -12,11 +10,7 @@ import (
)

func TestToModel(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "server_test-")
if err != nil {
logrus.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")

db, err := Open(dbPath)
Expand Down
9 changes: 1 addition & 8 deletions pkg/sqlite/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sqlite

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -38,13 +37,7 @@ func TestDirectoryLoaderWithBadPackageData(t *testing.T) {
require.NoError(t, store.Migrate(context.TODO()))

// Copy golden manifests to a temp dir
dir, err := ioutil.TempDir("testdata", "manifests-")
require.NoError(t, err)
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}()
dir := t.TempDir()
require.NoError(t, copy.Copy("./testdata/loader_data", dir))

// Point the first channel at a CSV that doesn't exist
Expand Down

0 comments on commit 43e4324

Please sign in to comment.