Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use T.TempDir to create temporary test directory #11524

Merged
merged 2 commits into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
6 changes: 2 additions & 4 deletions sdk/go/common/resource/asset_test.go
Expand Up @@ -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))
Expand Down Expand Up @@ -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))

Expand Down
15 changes: 3 additions & 12 deletions sdk/go/common/util/archive/archive_test.go
Expand Up @@ -28,8 +28,6 @@ import (
"strings"
"testing"

"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
10 changes: 4 additions & 6 deletions sdk/go/common/workspace/paths_test.go
Expand Up @@ -15,7 +15,6 @@
package workspace

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -28,17 +27,16 @@ 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
}

//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) }()
Expand Down Expand Up @@ -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) }()
Expand Down
9 changes: 1 addition & 8 deletions sdk/go/common/workspace/plugins_install_test.go
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sdk/go/common/workspace/plugins_test.go
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions sdk/nodejs/cmd/pulumi-language-nodejs/main_test.go
Expand Up @@ -16,7 +16,6 @@ package main

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions sdk/nodejs/npm/npm_test.go
Expand Up @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions sdk/python/python_test.go
Expand Up @@ -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))
Expand Down Expand Up @@ -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")
Expand Down