Skip to content

Commit

Permalink
test: use T.TempDir to create temporary test directory
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 <[email protected]>
  • Loading branch information
Juneezee authored and praveenkumar committed Nov 3, 2022
1 parent 1425c16 commit fa9e605
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 193 deletions.
20 changes: 6 additions & 14 deletions cmd/crc/cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,42 @@ package cmd

import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/crc-org/crc/pkg/crc/machine/fakemachine"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPlainDelete(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache")
require.NoError(t, err)
defer os.RemoveAll(cacheDir)
cacheDir := t.TempDir()

out := new(bytes.Buffer)
assert.NoError(t, runDelete(out, fakemachine.NewClient(), true, cacheDir, true, true, ""))
assert.Equal(t, "Deleted the instance\n", out.String())

_, err = os.Stat(cacheDir)
_, err := os.Stat(cacheDir)
assert.True(t, os.IsNotExist(err))
}

func TestNonForceDelete(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache")
require.NoError(t, err)
defer os.RemoveAll(cacheDir)
cacheDir := t.TempDir()

out := new(bytes.Buffer)
assert.NoError(t, runDelete(out, fakemachine.NewClient(), true, cacheDir, true, false, ""))
assert.Equal(t, "", out.String())

_, err = os.Stat(cacheDir)
_, err := os.Stat(cacheDir)
assert.NoError(t, err)
}

func TestJSONDelete(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache")
require.NoError(t, err)
defer os.RemoveAll(cacheDir)
cacheDir := t.TempDir()

out := new(bytes.Buffer)
assert.NoError(t, runDelete(out, fakemachine.NewClient(), true, cacheDir, false, true, jsonFormat))
assert.JSONEq(t, `{"success": true}`, out.String())

_, err = os.Stat(cacheDir)
_, err := os.Stat(cacheDir)
assert.True(t, os.IsNotExist(err))
}
25 changes: 6 additions & 19 deletions cmd/crc/cmd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -48,9 +47,7 @@ func setUpFailingClient(t *testing.T) *mocks.Client {
}

func TestPlainStatus(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache")
require.NoError(t, err)
defer os.RemoveAll(cacheDir)
cacheDir := t.TempDir()

client := setUpClient(t)

Expand All @@ -73,9 +70,7 @@ Cache Directory: %s
}

func TestStatusWithoutPodman(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache")
require.NoError(t, err)
defer os.RemoveAll(cacheDir)
cacheDir := t.TempDir()

client := mocks.NewClient(t)
require.NoError(t, ioutil.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))
Expand Down Expand Up @@ -105,9 +100,7 @@ Cache Directory: %s
}

func TestJsonStatus(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache")
require.NoError(t, err)
defer os.RemoveAll(cacheDir)
cacheDir := t.TempDir()

client := setUpClient(t)

Expand Down Expand Up @@ -135,9 +128,7 @@ func TestJsonStatus(t *testing.T) {
}

func TestPlainStatusWithError(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache")
require.NoError(t, err)
defer os.RemoveAll(cacheDir)
cacheDir := t.TempDir()

client := setUpFailingClient(t)

Expand All @@ -151,9 +142,7 @@ func TestPlainStatusWithError(t *testing.T) {
}

func TestJsonStatusWithError(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache")
require.NoError(t, err)
defer os.RemoveAll(cacheDir)
cacheDir := t.TempDir()

client := setUpFailingClient(t)

Expand All @@ -174,9 +163,7 @@ func TestJsonStatusWithError(t *testing.T) {
}

func TestStatusWithMemoryPodman(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache")
require.NoError(t, err)
defer os.RemoveAll(cacheDir)
cacheDir := t.TempDir()

client := mocks.NewClient(t)
require.NoError(t, ioutil.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))
Expand Down
4 changes: 1 addition & 3 deletions pkg/compress/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func testCompress(t *testing.T, baseDir string) {
require.NoError(t, Compress(baseDir, testArchiveName))
defer os.Remove(testArchiveName)

destDir, err := ioutil.TempDir("", "testdata-extracted")
require.NoError(t, err)
defer os.RemoveAll(destDir)
destDir := t.TempDir()

fileList, err := extract.Uncompress(testArchiveName, destDir, false)
require.NoError(t, err)
Expand Down
5 changes: 1 addition & 4 deletions pkg/crc/api/api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -263,9 +262,7 @@ func TestTelemetry(t *testing.T) {
}

func TestPullSecret(t *testing.T) {
dir, err := ioutil.TempDir("", "test-pull-secret")
assert.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()

fakeMachine := fakemachine.NewClient()
config := setupNewInMemoryConfig()
Expand Down
7 changes: 2 additions & 5 deletions pkg/crc/cluster/pullsecret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cluster

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

Expand All @@ -21,9 +20,7 @@ const (
func TestLoadPullSecret(t *testing.T) {
keyring.MockInit()

dir, err := ioutil.TempDir("", "pull-secret")
assert.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()

cfg := config.New(config.NewEmptyInMemoryStorage(), config.NewEmptyInMemorySecretStorage())
config.RegisterSettings(cfg)
Expand All @@ -33,7 +30,7 @@ func TestLoadPullSecret(t *testing.T) {
path: filepath.Join(dir, "file1"),
}

_, err = loader.Value()
_, err := loader.Value()
assert.Error(t, err)

assert.NoError(t, StoreInKeyring(secret3))
Expand Down
49 changes: 12 additions & 37 deletions pkg/crc/config/viper_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

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

Expand Down Expand Up @@ -34,9 +33,7 @@ func newTestConfig(configFile, envPrefix string) (*Config, error) {
}

func TestViperConfigUnknown(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")

config, err := newTestConfig(configFile, "CRC")
Expand All @@ -48,9 +45,7 @@ func TestViperConfigUnknown(t *testing.T) {
}

func TestViperConfigSetAndGet(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")

config, err := newTestConfig(configFile, "CRC")
Expand All @@ -70,9 +65,7 @@ func TestViperConfigSetAndGet(t *testing.T) {
}

func TestViperConfigUnsetAndGet(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")
assert.NoError(t, ioutil.WriteFile(configFile, []byte("{\"cpus\": 5}"), 0600))

Expand All @@ -93,9 +86,7 @@ func TestViperConfigUnsetAndGet(t *testing.T) {
}

func TestViperConfigSetReloadAndGet(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")

config, err := newTestConfig(configFile, "CRC")
Expand All @@ -114,9 +105,7 @@ func TestViperConfigSetReloadAndGet(t *testing.T) {
}

func TestViperConfigLoadDefaultValue(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")

config, err := newTestConfig(configFile, "CRC")
Expand Down Expand Up @@ -149,9 +138,7 @@ func TestViperConfigLoadDefaultValue(t *testing.T) {
}

func TestViperConfigBindFlagSet(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")

validateCPUs := func(value interface{}) (bool, string) {
Expand Down Expand Up @@ -195,9 +182,7 @@ func TestViperConfigBindFlagSet(t *testing.T) {
}

func TestViperConfigCastSet(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")

config, err := newTestConfig(configFile, "CRC")
Expand All @@ -220,9 +205,7 @@ func TestViperConfigCastSet(t *testing.T) {
}

func TestCannotSetWithWrongType(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")

config, err := newTestConfig(configFile, "CRC")
Expand All @@ -233,9 +216,7 @@ func TestCannotSetWithWrongType(t *testing.T) {
}

func TestCannotGetWithWrongType(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")
assert.NoError(t, ioutil.WriteFile(configFile, []byte("{\"cpus\": \"hello\"}"), 0600))

Expand All @@ -246,9 +227,7 @@ func TestCannotGetWithWrongType(t *testing.T) {
}

func TestTwoInstancesSharingSameConfiguration(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")

config1, err := newTestConfig(configFile, "CRC")
Expand All @@ -275,9 +254,7 @@ func TestTwoInstancesSharingSameConfiguration(t *testing.T) {
}

func TestTwoInstancesWriteSameConfiguration(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")

config1, err := newTestConfig(configFile, "CRC")
Expand All @@ -304,9 +281,7 @@ func TestTwoInstancesWriteSameConfiguration(t *testing.T) {
}

func TestTwoInstancesSetAndUnsetSameConfiguration(t *testing.T) {
dir, err := ioutil.TempDir("", "cfg")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
configFile := filepath.Join(dir, "crc.json")

config1, err := newTestConfig(configFile, "CRC")
Expand Down
9 changes: 2 additions & 7 deletions pkg/crc/machine/bundle/copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bundle
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -16,16 +15,12 @@ func TestGenerateBundle(t *testing.T) {
var b CrcBundleInfo
assert.NoError(t, json.Unmarshal([]byte(jsonForBundle("crc_4.7.1")), &b))

tmpBundleDir, err := ioutil.TempDir("", "bundle_data")
assert.NoError(t, err)
tmpBundleDir := t.TempDir()
b.cachedPath = filepath.Join(tmpBundleDir, b.Name)
defer os.RemoveAll(tmpBundleDir)

createDummyBundleFiles(t, &b)

srcDir, err := ioutil.TempDir("", "testdata")
assert.NoError(t, err)
defer os.RemoveAll(srcDir)
srcDir := t.TempDir()

customBundleName := "custom_bundle"
copier, err := NewCopier(&b, srcDir, customBundleName)
Expand Down
Loading

0 comments on commit fa9e605

Please sign in to comment.