Skip to content

Commit

Permalink
use test environments for codegen tests
Browse files Browse the repository at this point in the history
Currently these tests always just use the directory in the repository
tree to run.  This isn't great for a couple of reasons:
- The tests write files into the repo tree (these are .gitignored, but
  still large and aren't cleaned up after the test finished)
- Tests running in parallel can potentially use the same directory.

We have a nice helper that creates new test environments and cleans
them up at the end of the tests.  Use that here for more test
isolation.

Also remove the t.Run() in the inner loop.  For a lot of these tests
it's not possible to run only one bit at a time (which is also why
there is an ordering enforced).  It's better to just make them run in
the outer test loop to avoid confusion when trying to rerun only part
of the test.
  • Loading branch information
tgummerer committed Apr 9, 2024
1 parent 616e911 commit 3a33af2
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions pkg/codegen/testing/test/sdk_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/pulumi/pulumi/pkg/v3/codegen"
ptesting "github.com/pulumi/pulumi/sdk/v3/go/common/testing"
)

// Defines an extra check logic that accepts the directory with the
Expand Down Expand Up @@ -559,11 +560,20 @@ func TestSDKCodegen(t *testing.T, opts *SDKCodegenOptions) { // revive:disable-l

t.Log(tt.Description)

e := ptesting.NewEnvironment(t)
defer e.DeleteIfNotFailed()

// Some tests need the directory to have the right name. Create a subdirectory
// under the test environment to ensure that
os.Mkdir(filepath.Join(e.RootPath, filepath.FromSlash(tt.Directory)), 0o755)

Check failure on line 568 in pkg/codegen/testing/test/sdk_driver.go

View workflow job for this annotation

GitHub Actions / CI / Lint / Lint Go

Error return value of `os.Mkdir` is not checked (errcheck)
e.CWD = filepath.Join(e.RootPath, filepath.FromSlash(tt.Directory))

dirPath := filepath.Join(testDir, filepath.FromSlash(tt.Directory))
e.ImportDirectory(dirPath)

schemaPath := filepath.Join(dirPath, "schema.json")
schemaPath := filepath.Join(e.CWD, "schema.json")
if _, err := os.Stat(schemaPath); err != nil && os.IsNotExist(err) {
schemaPath = filepath.Join(dirPath, "schema.yaml")
schemaPath = filepath.Join(e.CWD, "schema.yaml")
}

if tt.ShouldSkipCodegen(opts.Language) {
Expand All @@ -574,8 +584,8 @@ func TestSDKCodegen(t *testing.T, opts *SDKCodegenOptions) { // revive:disable-l
files, err := GeneratePackageFilesFromSchema(schemaPath, opts.GenPackage)
require.NoError(t, err)

if !RewriteFilesWhenPulumiAccept(t, dirPath, opts.Language, files) {
expectedFiles, err := LoadBaseline(dirPath, opts.Language)
if !RewriteFilesWhenPulumiAccept(t, e.CWD, opts.Language, files) {
expectedFiles, err := LoadBaseline(e.CWD, opts.Language)
require.NoError(t, err)

if !ValidateFileEquality(t, files, expectedFiles) {
Expand All @@ -587,7 +597,7 @@ func TestSDKCodegen(t *testing.T, opts *SDKCodegenOptions) { // revive:disable-l
return
}

CopyExtraFiles(t, dirPath, opts.Language)
CopyExtraFiles(t, e.CWD, opts.Language)

// Merge language-specific global and
// test-specific checks, with test-specific
Expand All @@ -607,19 +617,15 @@ func TestSDKCodegen(t *testing.T, opts *SDKCodegenOptions) { // revive:disable-l
}
sort.Strings(checkOrder)

codeDir := filepath.Join(dirPath, opts.Language)
codeDir := filepath.Join(e.CWD, opts.Language)

// Perform the checks.
//nolint:paralleltest // test functions are ordered
for _, check := range checkOrder {
check := check
t.Run(check, func(t *testing.T) {
if tt.ShouldSkipTest(opts.Language, check) {
t.Skip()
}
checkFun := allChecks[check]
checkFun(t, codeDir)
})
if tt.ShouldSkipTest(opts.Language, check) {
t.Skip()
}
checkFun := allChecks[check]
checkFun(t, codeDir)
}
})
}
Expand Down

0 comments on commit 3a33af2

Please sign in to comment.