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

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

merged 2 commits into from Dec 8, 2022

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Dec 3, 2022

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

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.
  • I have updated the CHANGELOG-PENDING file with my change: This PR is a non user-facing change.
  • Yes, there are changes in this PR that warrants bumping the Pulumi Service API version: N/A

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 <engzerjun@gmail.com>
@github-actions
Copy link

github-actions bot commented Dec 3, 2022

PR is now waiting for a maintainer to take action.

Note for the maintainer: Commands available:

  • /run-acceptance-tests - used to test run the acceptance tests for the project
  • /run-codegen - used to test the Pull Request against downstream codegen
  • /run-docs-gen - used to test the Pull Request against documentation generation

@pulumi-bot
Copy link
Contributor

Changelog

[uncommitted] (2022-12-03)

@Juneezee Juneezee marked this pull request as ready for review December 3, 2022 07:20
@Frassle Frassle added the impact/no-changelog-required This issue doesn't require a CHANGELOG update label Dec 3, 2022
@Frassle
Copy link
Member

Frassle commented Dec 3, 2022

bors merge

bors bot added a commit that referenced this pull request Dec 3, 2022
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>
@bors
Copy link
Contributor

bors bot commented Dec 3, 2022

Build failed:

@Juneezee
Copy link
Contributor Author

Juneezee commented Dec 3, 2022

@Frassle The failing test (logs 1) may indicate a potential bug in writing / reading the content of Pulumi.yaml.

=== FAIL: cmd/pulumi TestCreatingStackWithArgsSpecifiedName (1.17s)
Logging in using access token from PULUMI_ACCESS_TOKEN
Created project '001'

    new_smoke_test.go:59: 
        	Error Trace:	/tmp/TestCreatingStackWithArgsSpecifiedName663333332/001/new_smoke_test.go:59
        	Error:      	Received unexpected error:
        	            	failed to load Pulumi project located at "/tmp/TestCreatingStackWithArgsSpecifiedName663333332/001/Pulumi.yaml": could not validate '/tmp/TestCreatingStackWithArgsSpecifiedName663333332/001/Pulumi.yaml': project is missing a non-empty string 'name' attribute
        	Test:       	TestCreatingStackWithArgsSpecifiedName
    new_smoke_test.go:192: 
        	Error Trace:	/tmp/TestCreatingStackWithArgsSpecifiedName663333332/001/new_smoke_test.go:192
        	            				/tmp/TestCreatingStackWithArgsSpecifiedName663333332/001/new_smoke_test.go:61
        	Error:      	Received unexpected error:
        	            	could not validate '/tmp/TestCreatingStackWithArgsSpecifiedName663333332/001/Pulumi.yaml': project is missing a non-empty string 'name' attribute
        	Test:       	TestCreatingStackWithArgsSpecifiedName

The format of the directory name returned by t.TempDir() is /tmp/<test-name><random-number>/<sequence-number>, e.g. /tmp/TestCreatingStackWithArgsSpecifiedName663333332/001. We didn't specify the project name in TestCreatingStackWithArgsSpecifiedName, so the default project name will be "001".

However, when we write the project name "001" into Pulumi.yaml, the string "001" is not quoted. This is the content of the file Pulumi.yaml.
image

When we read the project, the project name 001 is unmarshalled as a number, not a string. I have attached two debugging screenshots below.

marshaller, err := marshallerForPath(path)
if err != nil {
return nil, fmt.Errorf("can not read '%s': %w", path, err)
}
b, err := readFileStripUTF8BOM(path)
if err != nil {
return nil, fmt.Errorf("could not read '%s': %w", path, err)
}
var raw interface{}
err = marshaller.Unmarshal(b, &raw)
if err != nil {
return nil, fmt.Errorf("could not unmarshal '%s': %w", path, err)
}
err = ValidateProject(raw)
if err != nil {
return nil, fmt.Errorf("could not validate '%s': %w", path, err)
}

This could be a bug because "001" is a valid project name, according to this regular expression:

// Naming rules are backend-specific. However, we provide baseline sanitization for project names
// in this file. Though the backend may enforce stronger restrictions for a project name or description
// further down the line.
var (
validProjectNameRegexp = regexp.MustCompile("^[A-Za-z0-9_.-]{1,100}$")
)


Debugging screenshots:

image

image

Footnotes

  1. https://github.com/pulumi/pulumi/actions/runs/3607842843/jobs/6080197819#step:36:43

Reference: #11524 (comment)
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@Juneezee
Copy link
Contributor Author

Juneezee commented Dec 3, 2022

Reverted pkg/cmd/pulumi/new_smoke_test.go and pkg/cmd/pulumi/new_test.go because of #11524 (comment)

@Juneezee Juneezee requested a review from Frassle December 3, 2022 11:59
@Frassle
Copy link
Member

Frassle commented Dec 3, 2022

Sigh YAML
Whatever is writing out the YAML with unquoted numbers is what's wrong here, something to dig into but I'm fine with us merging this now to fix up all the other use cases.

@Frassle
Copy link
Member

Frassle commented Dec 8, 2022

bors merge

@bors
Copy link
Contributor

bors bot commented Dec 8, 2022

Build succeeded:

@bors bors bot merged commit f6d669c into pulumi:master Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
impact/no-changelog-required This issue doesn't require a CHANGELOG update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants