From 28644b714d8ca7fb6d2f8721e7adc14874cfec77 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Fri, 22 Jul 2022 00:06:03 +0800 Subject: [PATCH] test: use `T.TempDir` to create temporary test directory 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 --- x/registration/internal/keeper/genesis_test.go | 13 +++---------- x/registration/internal/keeper/keeper_test.go | 18 +++++------------- x/registration/internal/keeper/querier_test.go | 5 +---- 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/x/registration/internal/keeper/genesis_test.go b/x/registration/internal/keeper/genesis_test.go index 99fd4dca9..834e24997 100644 --- a/x/registration/internal/keeper/genesis_test.go +++ b/x/registration/internal/keeper/genesis_test.go @@ -2,7 +2,6 @@ package keeper import ( "io/ioutil" - "os" "testing" "github.com/enigmampc/SecretNetwork/x/registration/internal/types" @@ -11,9 +10,7 @@ import ( ) func TestInitGenesisNoMaster(t *testing.T) { - tempDir, err := ioutil.TempDir("", "wasm") - require.NoError(t, err) - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() ctx, keeper := CreateTestInput(t, false, tempDir, true) // //cert, err := ioutil.ReadFile("../../testdata/attestation_cert") @@ -29,9 +26,7 @@ func TestInitGenesisNoMaster(t *testing.T) { } func TestInitGenesis(t *testing.T) { - tempDir, err := ioutil.TempDir("", "wasm") - require.NoError(t, err) - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() ctx, keeper := CreateTestInput(t, false, tempDir, true) cert, err := ioutil.ReadFile("../../testdata/attestation_cert_sw") @@ -47,9 +42,7 @@ func TestInitGenesis(t *testing.T) { } func TestExportGenesis(t *testing.T) { - tempDir, err := ioutil.TempDir("", "wasm") - require.NoError(t, err) - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() ctx, keeper := CreateTestInput(t, false, tempDir, true) cert, err := ioutil.ReadFile("../../testdata/attestation_cert_sw") diff --git a/x/registration/internal/keeper/keeper_test.go b/x/registration/internal/keeper/keeper_test.go index 8ed51f22c..ada672760 100644 --- a/x/registration/internal/keeper/keeper_test.go +++ b/x/registration/internal/keeper/keeper_test.go @@ -22,21 +22,17 @@ func init() { } func TestNewKeeper(t *testing.T) { - tempDir, err := ioutil.TempDir("", "reg") - require.NoError(t, err) - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() _, regKeeper := CreateTestInput(t, false, tempDir, true) require.NotNil(t, regKeeper) } func TestNewKeeper_Node(t *testing.T) { - tempDir, err := ioutil.TempDir("", "reg") - require.NoError(t, err) - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() seedPath := filepath.Join(tempDir, types.SecretNodeCfgFolder, types.SecretNodeSeedConfig) - err = os.MkdirAll(filepath.Join(tempDir, types.SecretNodeCfgFolder), 0o700) + err := os.MkdirAll(filepath.Join(tempDir, types.SecretNodeCfgFolder), 0o700) require.NoError(t, err) err = ioutil.WriteFile(seedPath, CreateTestSeedConfig(t), 0o700) @@ -47,9 +43,7 @@ func TestNewKeeper_Node(t *testing.T) { } func TestKeeper_RegisterationStore(t *testing.T) { - tempDir, err := ioutil.TempDir("", "wasm") - require.NoError(t, err) - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() ctx, regKeeper := CreateTestInput(t, false, tempDir, true) cert, err := ioutil.ReadFile("../../testdata/attestation_cert_sw") @@ -73,9 +67,7 @@ func TestKeeper_RegisterationStore(t *testing.T) { } func TestKeeper_RegisterNode(t *testing.T) { - tempDir, err := ioutil.TempDir("", "wasm") - require.NoError(t, err) - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() ctx, regKeeper := CreateTestInput(t, false, tempDir, true) cert, err := ioutil.ReadFile("../../testdata/attestation_cert_sw") diff --git a/x/registration/internal/keeper/querier_test.go b/x/registration/internal/keeper/querier_test.go index 13034f152..52cc6f30a 100644 --- a/x/registration/internal/keeper/querier_test.go +++ b/x/registration/internal/keeper/querier_test.go @@ -6,7 +6,6 @@ import ( "encoding/hex" "encoding/json" "io/ioutil" - "os" "testing" sdkErrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -18,9 +17,7 @@ import ( ) func TestNewQuerier(t *testing.T) { - tempDir, err := ioutil.TempDir("", "wasm") - require.NoError(t, err) - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() ctx, keeper := CreateTestInput(t, false, tempDir, true) nodeIdInvalid := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"