Skip to content

Commit

Permalink
Merge #11524
Browse files Browse the repository at this point in the history
11524: test: use T.TempDir to create temporary test directory r=Frassle a=Juneezee

<!--- 
Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. -->

A testing cleanup. 

This pull request replaces `ioutil.TempDir` with `t.TempDir`. We can use the `T.TempDir` function from the `testing` package to create temporary 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

```go
func TestFoo(t *testing.T) {
	// before
	tmpDir, err := ioutil.TempDir("", "")
	assert.NoError(t, err)
	defer os.RemoveAll(tmpDir)

	// now
	tmpDir := t.TempDir()
}
```

## Checklist

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [ ] ~~I have added tests that prove my fix is effective or that my feature works~~: Tests refactoring only, no new features added.
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [ ] ~~I have updated the [CHANGELOG-PENDING](https://github.com/pulumi/pulumi/blob/master/CHANGELOG_PENDING.md) file with my change~~: This PR is a non user-facing change.
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Service,
then the service should honor older versions of the CLI where this change would not exist.
You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] ~~Yes, there are changes in this PR that warrants bumping the Pulumi Service API version~~: N/A
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
bors[bot] and Juneezee committed Dec 3, 2022
2 parents 712145c + 48eff46 commit 9ebdb8f
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 129 deletions.
19 changes: 6 additions & 13 deletions pkg/backend/filestate/backend_test.go
Expand Up @@ -3,7 +3,6 @@ package filestate
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/pulumi/convert_test.go
Expand Up @@ -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)
Expand Down
16 changes: 5 additions & 11 deletions pkg/cmd/pulumi/new_smoke_test.go
Expand Up @@ -15,7 +15,6 @@ package main

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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{
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -123,17 +119,15 @@ 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)
t.Setenv("PULUMI_CONFIG_PASSPHRASE", "how now brown cow")
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)

Expand Down
47 changes: 15 additions & 32 deletions pkg/cmd/pulumi/new_test.go
Expand Up @@ -17,8 +17,6 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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"

Expand All @@ -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"

Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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.
Expand All @@ -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.
Expand Down
5 changes: 1 addition & 4 deletions pkg/cmd/pulumi/policy_new_smoke_test.go
Expand Up @@ -16,8 +16,6 @@ package main

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
Expand All @@ -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{
Expand Down
12 changes: 3 additions & 9 deletions pkg/cmd/pulumi/policy_new_test.go
Expand Up @@ -18,8 +18,6 @@ package main

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

Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down
6 changes: 1 addition & 5 deletions pkg/testing/integration/program.go
Expand Up @@ -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))
}

Expand Down
6 changes: 1 addition & 5 deletions pkg/testing/integration/program_test.go
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions sdk/go/auto/git_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit 9ebdb8f

Please sign in to comment.