Skip to content

Commit

Permalink
Merge #11524 #11586
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

# Description

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

- [ ] ~~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. -->


11586: ci: Fix extraneous alpha draft releases r=AaronFriel a=AaronFriel

The [PR workflow to update Pulumi YAML to 1.0.4](https://github.com/pulumi/pulumi/actions/runs/3642973677) shows that it created a draft release, and this has been the case for all contributors' PRs.

This fixes the extra alpha releases created as a result of pull requests. The job "prepare-release" shouldn't run unless the ci/test label is set, indicating an intent to evaluate the full CI pipeline.


Co-authored-by: Eng Zer Jun <engzerjun@gmail.com>
Co-authored-by: Aaron Friel <mayreply@aaronfriel.com>
  • Loading branch information
3 people committed Dec 8, 2022
3 parents 711a3e9 + 481458a + 72cba44 commit f6d669c
Show file tree
Hide file tree
Showing 17 changed files with 31 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/on-pr.yml
Expand Up @@ -71,7 +71,7 @@ jobs:

prepare-release:
name: prepare
if: |
if: >- # No newlines or trailing newline.
${{
github.event.pull_request.head.repo.full_name == github.repository
&& contains(github.event.pull_request.labels.*.name, 'ci/test')
Expand Down
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 @@ -844,11 +844,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

0 comments on commit f6d669c

Please sign in to comment.