From 48eff4676ac84307f17e56f69c3ce19ca82b834d Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Sat, 3 Dec 2022 15:17:08 +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 --- pkg/backend/filestate/backend_test.go | 19 +++----- pkg/cmd/pulumi/convert_test.go | 3 +- pkg/cmd/pulumi/new_smoke_test.go | 16 ++----- pkg/cmd/pulumi/new_test.go | 47 ++++++------------- pkg/cmd/pulumi/policy_new_smoke_test.go | 5 +- pkg/cmd/pulumi/policy_new_test.go | 12 ++--- pkg/testing/integration/program.go | 6 +-- pkg/testing/integration/program_test.go | 6 +-- sdk/go/auto/git_test.go | 5 +- sdk/go/common/resource/asset_test.go | 6 +-- sdk/go/common/util/archive/archive_test.go | 15 ++---- sdk/go/common/workspace/paths_test.go | 10 ++-- .../common/workspace/plugins_install_test.go | 9 +--- sdk/go/common/workspace/plugins_test.go | 2 +- .../cmd/pulumi-language-nodejs/main_test.go | 5 +- sdk/nodejs/npm/npm_test.go | 3 +- sdk/python/python_test.go | 6 +-- tests/integration/integration_go_test.go | 4 +- 18 files changed, 50 insertions(+), 129 deletions(-) diff --git a/pkg/backend/filestate/backend_test.go b/pkg/backend/filestate/backend_test.go index 80a9e36baf66..0a58501e3a17 100644 --- a/pkg/backend/filestate/backend_test.go +++ b/pkg/backend/filestate/backend_test.go @@ -3,7 +3,6 @@ package filestate import ( "context" "encoding/json" - "io/ioutil" "os" "path" "path/filepath" @@ -143,8 +142,7 @@ func makeUntypedDeployment(name tokens.QName, phrase, state string) (*apitype.Un //nolint:paralleltest // mutates environment variables func TestListStacksWithMultiplePassphrases(t *testing.T) { // Login to a temp dir filestate backend - tmpDir, err := ioutil.TempDir("", "filestatebackend") - assert.NoError(t, err) + tmpDir := t.TempDir() b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir)) assert.NoError(t, err) ctx := context.Background() @@ -205,8 +203,7 @@ func TestDrillError(t *testing.T) { t.Parallel() // Login to a temp dir filestate backend - tmpDir, err := ioutil.TempDir("", "filestatebackend") - assert.NoError(t, err) + tmpDir := t.TempDir() b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir)) assert.NoError(t, err) ctx := context.Background() @@ -224,8 +221,7 @@ func TestCancel(t *testing.T) { t.Parallel() // Login to a temp dir filestate backend - tmpDir, err := ioutil.TempDir("", "filestatebackend") - assert.NoError(t, err) + tmpDir := t.TempDir() b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir)) assert.NoError(t, err) ctx := context.Background() @@ -286,8 +282,7 @@ func TestRemoveMakesBackups(t *testing.T) { t.Parallel() // Login to a temp dir filestate backend - tmpDir, err := ioutil.TempDir("", "filestatebackend") - assert.NoError(t, err) + tmpDir := t.TempDir() b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir)) assert.NoError(t, err) ctx := context.Background() @@ -330,8 +325,7 @@ func TestRenameWorks(t *testing.T) { t.Parallel() // Login to a temp dir filestate backend - tmpDir, err := ioutil.TempDir("", "filestatebackend") - assert.NoError(t, err) + tmpDir := t.TempDir() b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir)) assert.NoError(t, err) ctx := context.Background() @@ -445,8 +439,7 @@ func TestHtmlEscaping(t *testing.T) { } // Login to a temp dir filestate backend - tmpDir, err := ioutil.TempDir("", "filestatebackend") - assert.NoError(t, err) + tmpDir := t.TempDir() b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir)) assert.NoError(t, err) ctx := context.Background() diff --git a/pkg/cmd/pulumi/convert_test.go b/pkg/cmd/pulumi/convert_test.go index c424e5f9d648..bb738bc516d2 100644 --- a/pkg/cmd/pulumi/convert_test.go +++ b/pkg/cmd/pulumi/convert_test.go @@ -41,8 +41,7 @@ func TestPclConvert(t *testing.T) { t.Setenv("PULUMI_DEV", "TRUE") // Check that we can run convert from PCL to PCL - tmp, err := os.MkdirTemp("", "pulumi-convert-test") - assert.NoError(t, err) + tmp := t.TempDir() result := runConvert("pcl_convert_testdata", []string{}, "pcl", "pcl", tmp, true) assert.Nil(t, result) diff --git a/pkg/cmd/pulumi/new_smoke_test.go b/pkg/cmd/pulumi/new_smoke_test.go index 0800608638f2..409f35805a05 100644 --- a/pkg/cmd/pulumi/new_smoke_test.go +++ b/pkg/cmd/pulumi/new_smoke_test.go @@ -15,7 +15,6 @@ package main import ( "context" - "io/ioutil" "os" "path/filepath" "strings" @@ -44,8 +43,7 @@ func chdir(t *testing.T, dir string) { func TestCreatingStackWithArgsSpecifiedName(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) var args = newArgs{ @@ -68,8 +66,7 @@ func TestCreatingStackWithArgsSpecifiedName(t *testing.T) { func TestCreatingStackWithPromptedName(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) uniqueProjectName := filepath.Base(tempdir) @@ -91,8 +88,7 @@ func TestCreatingStackWithPromptedName(t *testing.T) { func TestCreatingProjectWithDefaultName(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) defaultProjectName := filepath.Base(tempdir) @@ -123,8 +119,7 @@ func TestCreatingProjectWithPulumiBackendURL(t *testing.T) { require.NoError(t, err) assert.True(t, strings.HasPrefix(b.URL(), "https://app.pulumi.com")) - fileStateDir, _ := ioutil.TempDir("", "local-state-dir") - defer os.RemoveAll(fileStateDir) + fileStateDir := t.TempDir() // Now override to local filesystem backend backendURL := "file://" + filepath.ToSlash(fileStateDir) @@ -132,8 +127,7 @@ func TestCreatingProjectWithPulumiBackendURL(t *testing.T) { t.Setenv(workspace.PulumiBackendURLEnvVar, backendURL) backendInstance = nil - tempdir, _ := ioutil.TempDir("", "test-env-local") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) defaultProjectName := filepath.Base(tempdir) diff --git a/pkg/cmd/pulumi/new_test.go b/pkg/cmd/pulumi/new_test.go index 7177678cd0e5..a7829f4278c0 100644 --- a/pkg/cmd/pulumi/new_test.go +++ b/pkg/cmd/pulumi/new_test.go @@ -17,8 +17,6 @@ package main import ( "context" "fmt" - "io/ioutil" - "os" "path/filepath" "testing" @@ -31,8 +29,7 @@ import ( func TestFailInInteractiveWithoutYes(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) var args = newArgs{ @@ -52,8 +49,7 @@ func TestFailInInteractiveWithoutYes(t *testing.T) { func TestCreatingStackWithArgsSpecifiedOrgName(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) orgStackName := fmt.Sprintf("%s/%s", currentUser(t), stackName) @@ -78,8 +74,7 @@ func TestCreatingStackWithArgsSpecifiedOrgName(t *testing.T) { func TestCreatingStackWithPromptedOrgName(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) uniqueProjectName := filepath.Base(tempdir) @@ -103,8 +98,7 @@ func TestCreatingStackWithPromptedOrgName(t *testing.T) { func TestCreatingStackWithArgsSpecifiedFullNameSucceeds(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) // the project name and the project name in the stack name must match @@ -131,8 +125,7 @@ func TestCreatingStackWithArgsSpecifiedFullNameSucceeds(t *testing.T) { func TestCreatingProjectWithArgsSpecifiedName(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) uniqueProjectName := filepath.Base(tempdir) + "test" @@ -159,8 +152,7 @@ func TestCreatingProjectWithArgsSpecifiedName(t *testing.T) { func TestCreatingProjectWithPromptedName(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) uniqueProjectName := filepath.Base(tempdir) + "test" @@ -184,8 +176,7 @@ func TestCreatingProjectWithPromptedName(t *testing.T) { func TestCreatingProjectWithExistingArgsSpecifiedNameFails(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) backendInstance = &backend.MockBackend{ @@ -212,8 +203,7 @@ func TestCreatingProjectWithExistingArgsSpecifiedNameFails(t *testing.T) { func TestCreatingProjectWithExistingPromptedNameFails(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) backendInstance = &backend.MockBackend{ @@ -238,8 +228,7 @@ func TestCreatingProjectWithExistingPromptedNameFails(t *testing.T) { func TestGeneratingProjectWithExistingArgsSpecifiedNameSucceeds(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) backendInstance = &backend.MockBackend{ @@ -270,8 +259,7 @@ func TestGeneratingProjectWithExistingArgsSpecifiedNameSucceeds(t *testing.T) { func TestGeneratingProjectWithExistingPromptedNameSucceeds(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) backendInstance = &backend.MockBackend{ @@ -300,8 +288,7 @@ func TestGeneratingProjectWithExistingPromptedNameSucceeds(t *testing.T) { func TestGeneratingProjectWithInvalidArgsSpecifiedNameFails(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) backendInstance = &backend.MockBackend{ @@ -330,8 +317,7 @@ func TestGeneratingProjectWithInvalidArgsSpecifiedNameFails(t *testing.T) { func TestGeneratingProjectWithInvalidPromptedNameFails(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) backendInstance = &backend.MockBackend{ @@ -367,8 +353,7 @@ func TestInvalidTemplateName(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) t.Run("NoTemplateSpecified", func(t *testing.T) { - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) var args = newArgs{ @@ -385,8 +370,7 @@ func TestInvalidTemplateName(t *testing.T) { }) t.Run("RemoteTemplateNotFound", func(t *testing.T) { - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) // A template that will never exist. @@ -406,8 +390,7 @@ func TestInvalidTemplateName(t *testing.T) { }) t.Run("LocalTemplateNotFound", func(t *testing.T) { - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) // A template that will never exist remotely. diff --git a/pkg/cmd/pulumi/policy_new_smoke_test.go b/pkg/cmd/pulumi/policy_new_smoke_test.go index f2e362f0bc9f..770428cf6042 100644 --- a/pkg/cmd/pulumi/policy_new_smoke_test.go +++ b/pkg/cmd/pulumi/policy_new_smoke_test.go @@ -16,8 +16,6 @@ package main import ( "context" - "io/ioutil" - "os" "path/filepath" "strings" "testing" @@ -29,8 +27,7 @@ import ( func TestCreatingPolicyPackWithArgsSpecifiedName(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) var args = newPolicyArgs{ diff --git a/pkg/cmd/pulumi/policy_new_test.go b/pkg/cmd/pulumi/policy_new_test.go index 904328c5d4d3..c8d94db0edd7 100644 --- a/pkg/cmd/pulumi/policy_new_test.go +++ b/pkg/cmd/pulumi/policy_new_test.go @@ -18,8 +18,6 @@ package main import ( "context" - "io/ioutil" - "os" "path/filepath" "testing" @@ -30,8 +28,7 @@ import ( func TestCreatingPolicyPackWithPromptedName(t *testing.T) { skipIfShortOrNoPulumiAccessToken(t) - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) var args = newPolicyArgs{ @@ -54,9 +51,7 @@ func TestInvalidPolicyPackTemplateName(t *testing.T) { const nonExistantTemplate = "this-is-not-the-template-youre-looking-for" t.Run("RemoteTemplateNotFound", func(t *testing.T) { - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) - assert.DirExists(t, tempdir) + tempdir := t.TempDir() chdir(t, tempdir) var args = newPolicyArgs{ @@ -71,8 +66,7 @@ func TestInvalidPolicyPackTemplateName(t *testing.T) { }) t.Run("LocalTemplateNotFound", func(t *testing.T) { - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() chdir(t, tempdir) var args = newPolicyArgs{ diff --git a/pkg/testing/integration/program.go b/pkg/testing/integration/program.go index d902e231c32b..261dc4e6fc2f 100644 --- a/pkg/testing/integration/program.go +++ b/pkg/testing/integration/program.go @@ -778,11 +778,7 @@ func newProgramTester(t *testing.T, opts *ProgramTestOptions) *ProgramTester { // MakeTempBackend creates a temporary backend directory which will clean up on test exit. func MakeTempBackend(t *testing.T) string { - tempDir, err := os.MkdirTemp("", "") - if err != nil { - t.Fatalf("Failed to create temporary directory: %v", err) - } - t.Cleanup(func() { os.RemoveAll(tempDir) }) + tempDir := t.TempDir() return fmt.Sprintf("file://%s", filepath.ToSlash(tempDir)) } diff --git a/pkg/testing/integration/program_test.go b/pkg/testing/integration/program_test.go index 90a3fc1c7dd3..868b7067748b 100644 --- a/pkg/testing/integration/program_test.go +++ b/pkg/testing/integration/program_test.go @@ -22,8 +22,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - - "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" ) // Test that RunCommand writes the command's output to a log file. @@ -42,9 +40,7 @@ func TestRunCommandLog(t *testing.T) { Stderr: os.Stderr, } - tempdir, err := ioutil.TempDir("", "test") - contract.AssertNoError(err) - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() args := []string{node, "-e", "console.log('output from node');"} err = RunCommand(t, "node", args, tempdir, opts) diff --git a/sdk/go/auto/git_test.go b/sdk/go/auto/git_test.go index 83f03492048f..19c34362bed5 100644 --- a/sdk/go/auto/git_test.go +++ b/sdk/go/auto/git_test.go @@ -21,10 +21,7 @@ func TestGitClone(t *testing.T) { // This makes a git repo to clone from, so to avoid relying on something at GitHub that could // change or be inaccessible. - tmpDir, err := os.MkdirTemp("", "pulumi-git-test") - assert.NoError(t, err) - assert.True(t, len(tmpDir) > 1) - t.Cleanup(func() { os.RemoveAll(tmpDir) }) + tmpDir := t.TempDir() originDir := filepath.Join(tmpDir, "origin") origin, err := git.PlainInit(originDir, false) diff --git a/sdk/go/common/resource/asset_test.go b/sdk/go/common/resource/asset_test.go index 02657eced118..31a2a8435714 100644 --- a/sdk/go/common/resource/asset_test.go +++ b/sdk/go/common/resource/asset_test.go @@ -444,8 +444,7 @@ func TestNestedArchive(t *testing.T) { t.Parallel() // Create temp dir and place some files. - dirName, err := os.MkdirTemp("", "") - assert.Nil(t, err) + dirName := t.TempDir() assert.NoError(t, os.MkdirAll(filepath.Join(dirName, "foo", "bar"), 0777)) assert.NoError(t, os.WriteFile(filepath.Join(dirName, "foo", "a.txt"), []byte("a"), 0777)) assert.NoError(t, os.WriteFile(filepath.Join(dirName, "foo", "bar", "b.txt"), []byte("b"), 0777)) @@ -487,8 +486,7 @@ func TestFileReferencedThroughMultiplePaths(t *testing.T) { t.Parallel() // Create temp dir and place some files. - dirName, err := os.MkdirTemp("", "") - assert.Nil(t, err) + dirName := t.TempDir() assert.NoError(t, os.MkdirAll(filepath.Join(dirName, "foo", "bar"), 0777)) assert.NoError(t, os.WriteFile(filepath.Join(dirName, "foo", "bar", "b.txt"), []byte("b"), 0777)) diff --git a/sdk/go/common/util/archive/archive_test.go b/sdk/go/common/util/archive/archive_test.go index 187cfe4b86a0..f84d7002ef5d 100644 --- a/sdk/go/common/util/archive/archive_test.go +++ b/sdk/go/common/util/archive/archive_test.go @@ -28,8 +28,6 @@ import ( "strings" "testing" - "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" - "github.com/stretchr/testify/assert" ) @@ -122,7 +120,7 @@ func TestIgnoreNestedGitignore(t *testing.T) { func doArchiveTest(t *testing.T, path string, files ...fileContents) { doTest := func(prefixPathInsideTar, path string) { - tarball, err := archiveContents(prefixPathInsideTar, path, files...) + tarball, err := archiveContents(t, prefixPathInsideTar, path, files...) assert.NoError(t, err) tarReader := bytes.NewReader(tarball) @@ -137,15 +135,8 @@ func doArchiveTest(t *testing.T, path string, files ...fileContents) { } } -func archiveContents(prefixPathInsideTar, path string, files ...fileContents) ([]byte, error) { - dir, err := os.MkdirTemp("", "archive-test") - if err != nil { - return nil, err - } - - defer func() { - contract.IgnoreError(os.RemoveAll(dir)) - }() +func archiveContents(t *testing.T, prefixPathInsideTar, path string, files ...fileContents) ([]byte, error) { + dir := t.TempDir() for _, file := range files { name := file.name diff --git a/sdk/go/common/workspace/paths_test.go b/sdk/go/common/workspace/paths_test.go index be36e18a64c0..db3e417526be 100644 --- a/sdk/go/common/workspace/paths_test.go +++ b/sdk/go/common/workspace/paths_test.go @@ -15,7 +15,6 @@ package workspace import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -28,9 +27,8 @@ import ( // that directory. However DetectProjectAndPath will do symlink resolution, while ioutil.TempDir normally does // not. This can lead to asserts especially on macos where TmpDir will have returned /var/folders/XX, but // after sym link resolution that is /private/var/folders/XX. -func mkTempDir(t *testing.T, pattern string) string { - tmpDir, err := ioutil.TempDir("", pattern) - assert.NoError(t, err) +func mkTempDir(t *testing.T) string { + tmpDir := t.TempDir() result, err := filepath.EvalSymlinks(tmpDir) assert.NoError(t, err) return result @@ -38,7 +36,7 @@ func mkTempDir(t *testing.T, pattern string) string { //nolint:paralleltest // Theses test use and change the current working directory func TestDetectProjectAndPath(t *testing.T) { - tmpDir := mkTempDir(t, "TestDetectProjectAndPath") + tmpDir := mkTempDir(t) cwd, err := os.Getwd() assert.NoError(t, err) defer func() { err := os.Chdir(cwd); assert.NoError(t, err) }() @@ -99,7 +97,7 @@ func TestProjectStackPath(t *testing.T) { for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { - tmpDir := mkTempDir(t, "TestProjectStackPath") + tmpDir := mkTempDir(t) cwd, err := os.Getwd() assert.NoError(t, err) defer func() { err := os.Chdir(cwd); assert.NoError(t, err) }() diff --git a/sdk/go/common/workspace/plugins_install_test.go b/sdk/go/common/workspace/plugins_install_test.go index fb703d40ece2..52d8fa1d8144 100644 --- a/sdk/go/common/workspace/plugins_install_test.go +++ b/sdk/go/common/workspace/plugins_install_test.go @@ -84,8 +84,7 @@ func prepareTestPluginTGZ(t *testing.T, files map[string][]byte) io.ReadCloser { func prepareTestDir(t *testing.T, files map[string][]byte) (string, io.ReadCloser, PluginSpec) { tarball := prepareTestPluginTGZ(t, files) - dir, err := ioutil.TempDir("", "plugins-test-dir") - require.NoError(t, err) + dir := t.TempDir() v1 := semver.MustParse("0.1.0") plugin := PluginSpec{ @@ -163,7 +162,6 @@ func testPluginInstall(t *testing.T, expectedDir string, files map[string][]byte } dir, tarball, plugin := prepareTestDir(t, files) - defer os.RemoveAll(dir) err := plugin.Install(tarball, false) assert.NoError(t, err) @@ -182,7 +180,6 @@ func TestInstallNoDeps(t *testing.T) { content := []byte("hello\n") dir, tarball, plugin := prepareTestDir(t, map[string][]byte{name: content}) - defer os.RemoveAll(dir) err := plugin.Install(tarball, false) require.NoError(t, err) @@ -201,7 +198,6 @@ func TestReinstall(t *testing.T) { content := []byte("hello\n") dir, tarball, plugin := prepareTestDir(t, map[string][]byte{name: content}) - defer os.RemoveAll(dir) err := plugin.Install(tarball, false) require.NoError(t, err) @@ -231,7 +227,6 @@ func TestConcurrentInstalls(t *testing.T) { content := []byte("hello\n") dir, tarball, plugin := prepareTestDir(t, map[string][]byte{name: content}) - defer os.RemoveAll(dir) assertSuccess := func() PluginInfo { pluginInfo := assertPluginInstalled(t, dir, plugin) @@ -266,7 +261,6 @@ func TestConcurrentInstalls(t *testing.T) { func TestInstallCleansOldFiles(t *testing.T) { dir, tarball, plugin := prepareTestDir(t, nil) - defer os.RemoveAll(dir) // Leftover temp dirs. tempDir1, err := ioutil.TempDir(dir, fmt.Sprintf("%s.tmp", plugin.Dir())) @@ -298,7 +292,6 @@ func TestInstallCleansOldFiles(t *testing.T) { func TestGetPluginsSkipsPartial(t *testing.T) { dir, tarball, plugin := prepareTestDir(t, nil) - defer os.RemoveAll(dir) err := plugin.Install(tarball, false) assert.NoError(t, err) diff --git a/sdk/go/common/workspace/plugins_test.go b/sdk/go/common/workspace/plugins_test.go index 51993a79ed80..99b0b8f36ed0 100644 --- a/sdk/go/common/workspace/plugins_test.go +++ b/sdk/go/common/workspace/plugins_test.go @@ -812,7 +812,7 @@ func TestParsePluginDownloadURLOverride(t *testing.T) { //nolint:paralleltest // changes directory for process func TestUnmarshalProjectWithProviderList(t *testing.T) { t.Parallel() - tempdir, _ := ioutil.TempDir("", "test-env") + tempdir := t.TempDir() pyaml := filepath.Join(tempdir, "Pulumi.yaml") //write to pyaml diff --git a/sdk/nodejs/cmd/pulumi-language-nodejs/main_test.go b/sdk/nodejs/cmd/pulumi-language-nodejs/main_test.go index 2cfd35887dfd..39ea506aebaa 100644 --- a/sdk/nodejs/cmd/pulumi-language-nodejs/main_test.go +++ b/sdk/nodejs/cmd/pulumi-language-nodejs/main_test.go @@ -16,7 +16,6 @@ package main import ( "context" - "io/ioutil" "os" "path/filepath" "strings" @@ -123,9 +122,7 @@ func TestCompatibleVersions(t *testing.T) { func TestGetRequiredPlugins(t *testing.T) { t.Parallel() - dir, err := ioutil.TempDir("", "test-dir") - assert.NoError(t, err) - defer os.RemoveAll(dir) + dir := t.TempDir() files := []struct { path string diff --git a/sdk/nodejs/npm/npm_test.go b/sdk/nodejs/npm/npm_test.go index f9fa114233eb..35686be48a36 100644 --- a/sdk/nodejs/npm/npm_test.go +++ b/sdk/nodejs/npm/npm_test.go @@ -57,8 +57,7 @@ func testInstall(t *testing.T, expectedBin string, production bool) { } // Create a new empty test directory and change the current working directory to it. - tempdir, _ := ioutil.TempDir("", "test-env") - t.Cleanup(func() { os.RemoveAll(tempdir) }) + tempdir := t.TempDir() chdir(t, tempdir) // Create a package directory to install dependencies into. diff --git a/sdk/python/python_test.go b/sdk/python/python_test.go index ee5acb7c8cce..001812d577ca 100644 --- a/sdk/python/python_test.go +++ b/sdk/python/python_test.go @@ -30,8 +30,7 @@ func TestIsVirtualEnv(t *testing.T) { t.Parallel() // Create a new empty test directory. - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() // Assert the empty test directory is not a virtual environment. assert.False(t, IsVirtualEnv(tempdir)) @@ -94,8 +93,7 @@ func TestRunningPipInVirtualEnvironment(t *testing.T) { } // Create a new empty test directory. - tempdir, _ := ioutil.TempDir("", "test-env") - defer os.RemoveAll(tempdir) + tempdir := t.TempDir() // Create and run a python command to create a virtual environment. venvDir := filepath.Join(tempdir, "venv") diff --git a/tests/integration/integration_go_test.go b/tests/integration/integration_go_test.go index 8f922c808c82..eecd4cac5059 100644 --- a/tests/integration/integration_go_test.go +++ b/tests/integration/integration_go_test.go @@ -707,9 +707,7 @@ func TestComponentProviderSchemaGo(t *testing.T) { // TestTracePropagationGo checks that --tracing flag lets golang sub-process to emit traces. func TestTracePropagationGo(t *testing.T) { - dir, err := os.MkdirTemp("", "pulumi-tracefiles") - assert.NoError(t, err) - defer os.RemoveAll(dir) + dir := t.TempDir() opts := &integration.ProgramTestOptions{ Dir: filepath.Join("empty", "go"),