pulumi/pkg/codegen/go/gen_program_test.go

537 lines
14 KiB
Go
Raw Normal View History

package gen
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/hashicorp/hcl/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model/format"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/test"
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/utils"
)
var testdataPath = filepath.Join("..", "testing", "test", "testdata")
func TestGenerateProgramVersionSelection(t *testing.T) {
t.Parallel()
expectedVersion := map[string]test.PkgVersionInfo{
"aws-resource-options-4.26": {
Pkg: "github.com/pulumi/pulumi-aws/sdk/v4",
OpAndVersion: "v4.26.0",
},
"aws-resource-options-5.16.2": {
Pkg: "github.com/pulumi/pulumi-aws/sdk/v5",
OpAndVersion: "v5.16.2",
},
2022-10-10 23:01:53 +00:00
"modpath": {
Pkg: "git.example.org/thirdparty/sdk",
OpAndVersion: "v0.1.0",
},
}
test.TestProgramCodegen(t,
test.ProgramCodegenOptions{
Language: "go",
Extension: "go",
OutputFile: "main.go",
Check: func(t *testing.T, path string, dependencies codegen.StringSet) {
Check(t, path, dependencies, "../../../../../../../sdk")
},
GenProgram: func(program *pcl.Program) (map[string][]byte, hcl.Diagnostics, error) {
// Prevent tests from interfering with each other
return GenerateProgramWithOptions(program, GenerateProgramOptions{ExternalCache: NewCache()})
},
TestCases: []test.ProgramTest{
{
Directory: "aws-resource-options-4.26",
Description: "Resource Options",
},
{
Directory: "aws-resource-options-5.16.2",
Description: "Resource Options",
},
2022-10-10 23:01:53 +00:00
{
Directory: "modpath",
Description: "Check that modpath is respected",
MockPluginVersions: map[string]string{
"other": "0.1.0",
},
// We don't compile because the test relies on the `other` package,
// which does not exist.
SkipCompile: codegen.NewStringSet("go"),
},
},
IsGenProject: true,
GenProject: GenerateProject,
ExpectedVersion: expectedVersion,
DependencyFile: "go.mod",
})
}
2020-05-21 17:23:33 +00:00
func TestCollectImports(t *testing.T) {
t.Parallel()
g := newTestGenerator(t, filepath.Join("aws-s3-logging-pp", "aws-s3-logging.pp"))
programgen(go): Handle conflicting names in imported packages This fixes how programgen generates import statements to handle conflicting imports when two imported packages have the same name, e.g. github.com/pulumi/pulumi-aws/sdk/v5/go/aws/ecs github.com/pulumi/pulumi-awsx/sdk/go/awsx/ecs To do this, we add a fileImporter type that tracks these imports. It prefers unnamed imports for packages unless one of the following is true: - the name of the package has already been used by another import - the name of the package does not match the last component of the import path (e.g., `example.com/foo-go` with `package foo`). If the name has already been used by another import, it attempts the following in-order: - Combine the last two path components of the import path into an identifier and use that if available. e.g., `awsxs3` from `sdk/go/awsx/s3`. - Append a number to the package name and increment it until an unused name is found. e.g. `ecs2`, `ecs3`, and so on. There's a change in how this information is tracked as well. Previously, this was a pull approach: various calls returned programImports objects which all got merged together. This change switches to a push approach: as code is generated and imports are requested, they're submitted to the fileImporter which keeps track of them until the next `Reset()` call. The above also has a nice side effect of dropping a parameter. Another change worth explicitly calling out: Previously, getModOrAlias partially duplicated some of the logic implemented in getPulumiImport, and used `mod`, `originalMod` in a non-obvious way. This generated incorrect imports like the following (note the two `/aws` at the end): github.com/pulumi/pulumi-aws/sdk/v5/go/aws/aws This change replicates more of the logic of getPulumiImport (now called addPulumiImport) into this function, and addresses the discrepancy in codegen caused by `mod`/`originalMod`. The result leaves most existing code unchanged, except in a couple existing cases where the resulting changes make sense given the logic for named imports outlined above. Resolves #11176
2023-06-23 21:11:00 +00:00
g.collectImports(g.program)
programgen(go): Handle conflicting names in imported packages This fixes how programgen generates import statements to handle conflicting imports when two imported packages have the same name, e.g. github.com/pulumi/pulumi-aws/sdk/v5/go/aws/ecs github.com/pulumi/pulumi-awsx/sdk/go/awsx/ecs To do this, we add a fileImporter type that tracks these imports. It prefers unnamed imports for packages unless one of the following is true: - the name of the package has already been used by another import - the name of the package does not match the last component of the import path (e.g., `example.com/foo-go` with `package foo`). If the name has already been used by another import, it attempts the following in-order: - Combine the last two path components of the import path into an identifier and use that if available. e.g., `awsxs3` from `sdk/go/awsx/s3`. - Append a number to the package name and increment it until an unused name is found. e.g. `ecs2`, `ecs3`, and so on. There's a change in how this information is tracked as well. Previously, this was a pull approach: various calls returned programImports objects which all got merged together. This change switches to a push approach: as code is generated and imports are requested, they're submitted to the fileImporter which keeps track of them until the next `Reset()` call. The above also has a nice side effect of dropping a parameter. Another change worth explicitly calling out: Previously, getModOrAlias partially duplicated some of the logic implemented in getPulumiImport, and used `mod`, `originalMod` in a non-obvious way. This generated incorrect imports like the following (note the two `/aws` at the end): github.com/pulumi/pulumi-aws/sdk/v5/go/aws/aws This change replicates more of the logic of getPulumiImport (now called addPulumiImport) into this function, and addresses the discrepancy in codegen caused by `mod`/`originalMod`. The result leaves most existing code unchanged, except in a couple existing cases where the resulting changes make sense given the logic for named imports outlined above. Resolves #11176
2023-06-23 21:11:00 +00:00
var allImports []string
for _, group := range g.importer.ImportGroups() {
allImports = append(allImports, group...)
}
assert.Equal(t, []string{
`"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/s3"`,
}, allImports)
}
func TestFileImporter(t *testing.T) {
t.Parallel()
// importCall is a single call to fileImporter.Import.
type importCall struct {
// Import path to import.
importPath string
// Name of the package at the import path.
name string
// Expected name used to refer to the imported package.
want string
}
tests := []struct {
desc string
// List of Import method invocations made in-order.
imports []importCall
// List of import groups generated from the collective effect
// of all import calls in this test case.
wantGroups [][]string
}{
{desc: "no imports"},
{
desc: "single import/std",
imports: []importCall{
{importPath: "fmt", name: "fmt", want: "fmt"},
},
wantGroups: [][]string{
{`"fmt"`},
},
},
{
desc: "single import/pulumi",
imports: []importCall{
{importPath: "github.com/pulumi/pulumi/sdk/v3/go/pulumi", name: "pulumi", want: "pulumi"},
},
wantGroups: [][]string{
{`"github.com/pulumi/pulumi/sdk/v3/go/pulumi"`},
},
},
{
desc: "std and pulumi/no conflict",
imports: []importCall{
{importPath: "fmt", name: "fmt", want: "fmt"},
{importPath: "github.com/pulumi/pulumi/sdk/v3/go/pulumi", name: "pulumi", want: "pulumi"},
},
wantGroups: [][]string{
{`"fmt"`},
{`"github.com/pulumi/pulumi/sdk/v3/go/pulumi"`},
},
},
{
desc: "std and pulumi many imports, no conflict",
imports: []importCall{
{importPath: "fmt", name: "fmt", want: "fmt"},
{importPath: "github.com/pulumi/pulumi/sdk/v3/go/pulumi", name: "pulumi", want: "pulumi"},
{importPath: "github.com/pulumi/pulumi/sdk/v3/go/pulumi/config", name: "config", want: "config"},
{importPath: "encoding/json", name: "json", want: "json"},
{importPath: "github.com/pulumi/pulumi-aws/sdk/v5/go/aws/s3", name: "s3", want: "s3"},
{importPath: "io", name: "io", want: "io"},
{importPath: "github.com/pulumi/pulumi-awsx/sdk/v5/go/awsx", name: "awsx", want: "awsx"},
},
wantGroups: [][]string{
{
`"encoding/json"`,
`"fmt"`,
`"io"`,
},
{
`"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/s3"`,
`"github.com/pulumi/pulumi-awsx/sdk/v5/go/awsx"`,
`"github.com/pulumi/pulumi/sdk/v3/go/pulumi"`,
`"github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"`,
},
},
},
{
desc: "std and pulumi/conflict",
imports: []importCall{
{importPath: "encoding/json", name: "json", want: "json"},
{
// This doesn't actually exist yet,
// but it's conceivable that it might.
importPath: "github.com/pulumi/pulumi-std/sdk/go/std/encoding/json",
name: "json",
want: "encodingjson",
},
},
wantGroups: [][]string{
{`"encoding/json"`},
{`encodingjson "github.com/pulumi/pulumi-std/sdk/go/std/encoding/json"`},
},
},
{
desc: "std and pulumi/conflict repeated",
imports: []importCall{
{importPath: "encoding/json", name: "json", want: "json"},
{
importPath: "github.com/pulumi/pulumi-std/sdk/go/std/encoding/json",
name: "json",
want: "encodingjson",
},
{importPath: "encoding/json/v2", name: "json", want: "jsonv2"},
{
importPath: "github.com/pulumi/pulumi-std/sdk/v2/go/std/encoding/json",
name: "json",
want: "json2",
},
},
wantGroups: [][]string{
{
`"encoding/json"`,
`jsonv2 "encoding/json/v2"`,
},
{
`encodingjson "github.com/pulumi/pulumi-std/sdk/go/std/encoding/json"`,
`json2 "github.com/pulumi/pulumi-std/sdk/v2/go/std/encoding/json"`,
},
},
},
{
desc: "std and pulumi/conflict reverse",
imports: []importCall{
{
// This doesn't actually exist yet,
// but it's conceivable that it might.
importPath: "github.com/pulumi/pulumi-std/sdk/go/std/encoding/json",
name: "json",
want: "json",
},
{importPath: "encoding/json", name: "json", want: "json2"},
},
wantGroups: [][]string{
{`json2 "encoding/json"`},
{`"github.com/pulumi/pulumi-std/sdk/go/std/encoding/json"`},
},
},
{
desc: "pulumi aws awsx conflict",
imports: []importCall{
{importPath: "github.com/pulumi/pulumi-aws/sdk/v5/go/aws/ecs", name: "ecs", want: "ecs"},
{importPath: "github.com/pulumi/pulumi-awsx/sdk/go/awsx/ecs", name: "ecs", want: "awsxecs"},
},
wantGroups: [][]string{
{
`"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/ecs"`,
`awsxecs "github.com/pulumi/pulumi-awsx/sdk/go/awsx/ecs"`,
},
},
},
{
desc: "basename mismatch/std",
imports: []importCall{
{importPath: "math/rand/v2", name: "rand", want: "rand"},
},
wantGroups: [][]string{
{`rand "math/rand/v2"`},
},
},
{
desc: "basename mismatch/pulumi",
imports: []importCall{
{importPath: "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/core/v1", name: "corev1", want: "corev1"},
},
wantGroups: [][]string{
{`corev1 "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/core/v1"`},
},
},
{
desc: "basename mismatch/third party",
imports: []importCall{
{importPath: "gopkg.in/yaml.v3", name: "yaml", want: "yaml"},
},
wantGroups: [][]string{
{`yaml "gopkg.in/yaml.v3"`},
},
},
{
desc: "already imported",
imports: []importCall{
{importPath: "example.com/foo/bar", name: "bar", want: "bar"},
{importPath: "example.com/baz/bar", name: "bar", want: "bazbar"},
// Reimport should get existing resolved names.
{importPath: "example.com/foo/bar", name: "bar", want: "bar"},
{importPath: "example.com/baz/bar", name: "bar", want: "bazbar"},
},
wantGroups: [][]string{
{
// Note:
// Imports are sorted by import path,
// not the order they were imported.
`bazbar "example.com/baz/bar"`,
`"example.com/foo/bar"`,
},
},
},
{
desc: "many conflicts",
imports: []importCall{
{importPath: "example.com/foo/bar", name: "bar", want: "bar"},
{importPath: "example.com/baz/bar", name: "bar", want: "bazbar"},
{importPath: "example.com/qux/bar", name: "bar", want: "quxbar"},
{importPath: "example.com/quux/bar", name: "bar", want: "quuxbar"},
},
wantGroups: [][]string{
{
// Note:
// Imports are sorted by import path,
// not the order they were imported.
`bazbar "example.com/baz/bar"`,
`"example.com/foo/bar"`,
`quuxbar "example.com/quux/bar"`,
`quxbar "example.com/qux/bar"`,
},
},
},
{
desc: "conflict with special characters",
imports: []importCall{
{importPath: "example.com/foo/bar-go", name: "bar", want: "bar"},
{importPath: "example.com/foo/bar.go", name: "bar", want: "foobargo"},
{importPath: "example.com/bar", name: "bar", want: "bar2"}, // nothing to join with
{importPath: "example.com/f-o-o/bar", name: "bar", want: "foobar"},
},
wantGroups: [][]string{
{
`bar2 "example.com/bar"`,
`foobar "example.com/f-o-o/bar"`,
`bar "example.com/foo/bar-go"`,
`foobargo "example.com/foo/bar.go"`,
},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
fimp := newFileImporter()
for _, imp := range tt.imports {
gotName := fimp.Import(imp.importPath, imp.name)
assert.Equal(t, imp.want, gotName, "Import(%q, %q)", imp.importPath, imp.name)
}
assert.Equal(t, tt.wantGroups, fimp.ImportGroups())
})
}
}
func TestFileImporter_Reset(t *testing.T) {
t.Parallel()
fimp := newFileImporter()
assert.Empty(t, fimp.ImportGroups())
// Add imports.
assert.Equal(t, "bar", fimp.Import("example.com/foo/bar", "bar"))
assert.Equal(t, "bazbar", fimp.Import("example.com/baz/bar", "bar"))
assert.NotEmpty(t, fimp.ImportGroups()) // sanity check
// Reset and check that imports are gone.
fimp.Reset()
assert.Empty(t, fimp.ImportGroups())
// Import again in reverse order.
// Prior state shouldn't affect result.
assert.Equal(t, "bar", fimp.Import("example.com/baz/bar", "bar"),
"should not get alias after reset")
assert.Equal(t, "foobar", fimp.Import("example.com/foo/bar", "bar"))
}
func TestToIdentifier(t *testing.T) {
t.Parallel()
tests := []struct {
give string
want string
ok bool
}{
{"foo", "foo", true},
{"foo-bar", "foobar", true},
{"foo_bar", "foo_bar", true},
{"foo.bar", "foobar", true},
{"foo/123", "foo123", true},
{"foo.123/bar", "foo123bar", true},
{"123", "", false},
{"1/foo", "", false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.give, func(t *testing.T) {
t.Parallel()
got, ok := toIdentifier(tt.give)
require.Equal(t, tt.ok, ok)
if ok {
assert.Equal(t, tt.want, got)
}
})
}
}
func TestSecondLastIndex(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
haystack string
needle string
want int
}{
{
desc: "empty",
haystack: "",
needle: "foo",
want: -1,
},
{
desc: "no match",
haystack: "foo",
needle: "bar",
want: -1,
},
{
desc: "one match",
haystack: "a/b",
needle: "/",
want: -1,
},
{
desc: "two matches",
haystack: "a/b/c",
needle: "/",
want: 1,
},
{
desc: "three matches",
haystack: "a/b/c/d",
needle: "/",
want: 3,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
got := secondLastIndex(tt.haystack, tt.needle)
assert.Equal(t, tt.want, got)
})
}
2020-05-21 17:23:33 +00:00
}
func newTestGenerator(t *testing.T, testFile string) *generator {
path := filepath.Join(testdataPath, testFile)
contents, err := os.ReadFile(path)
require.NoErrorf(t, err, "could not read %v: %v", path, err)
parser := syntax.NewParser()
err = parser.ParseFile(bytes.NewReader(contents), filepath.Base(path))
2020-05-21 17:23:33 +00:00
if err != nil {
t.Fatalf("could not read %v: %v", path, err)
}
if parser.Diagnostics.HasErrors() {
t.Fatalf("failed to parse files: %v", parser.Diagnostics)
2020-05-21 17:23:33 +00:00
}
program, diags, err := pcl.BindProgram(parser.Files, pcl.PluginHost(utils.NewHost(testdataPath)))
if err != nil {
t.Fatalf("could not bind program: %v", err)
}
if diags.HasErrors() {
t.Fatalf("failed to bind program: %v", diags)
}
2020-05-21 17:23:33 +00:00
g := &generator{
program: program,
jsonTempSpiller: &jsonSpiller{},
ternaryTempSpiller: &tempSpiller{},
readDirTempSpiller: &readDirSpiller{},
splatSpiller: &splatSpiller{},
optionalSpiller: &optionalSpiller{},
[go/program-gen] Fix using inline invoke expressions inside resources, objects and arrays (#14484) # Description This PR fixes an issue in Go program where if users are writing invoke expressions inline inside other expressions, then that invoke is extracted into a temporary variable and then later referenced the same way it was used. This is because non-output-versioned invokes cannot be used directly since they return a tuple (InvokeResult, err) so we need to check for the error before proceeding. Fixes #14064 ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, 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 Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-03 14:22:43 +00:00
inlineInvokeSpiller: &inlineInvokeSpiller{},
scopeTraversalRoots: codegen.NewStringSet(),
arrayHelpers: make(map[string]*promptToInputArrayHelper),
programgen(go): Handle conflicting names in imported packages This fixes how programgen generates import statements to handle conflicting imports when two imported packages have the same name, e.g. github.com/pulumi/pulumi-aws/sdk/v5/go/aws/ecs github.com/pulumi/pulumi-awsx/sdk/go/awsx/ecs To do this, we add a fileImporter type that tracks these imports. It prefers unnamed imports for packages unless one of the following is true: - the name of the package has already been used by another import - the name of the package does not match the last component of the import path (e.g., `example.com/foo-go` with `package foo`). If the name has already been used by another import, it attempts the following in-order: - Combine the last two path components of the import path into an identifier and use that if available. e.g., `awsxs3` from `sdk/go/awsx/s3`. - Append a number to the package name and increment it until an unused name is found. e.g. `ecs2`, `ecs3`, and so on. There's a change in how this information is tracked as well. Previously, this was a pull approach: various calls returned programImports objects which all got merged together. This change switches to a push approach: as code is generated and imports are requested, they're submitted to the fileImporter which keeps track of them until the next `Reset()` call. The above also has a nice side effect of dropping a parameter. Another change worth explicitly calling out: Previously, getModOrAlias partially duplicated some of the logic implemented in getPulumiImport, and used `mod`, `originalMod` in a non-obvious way. This generated incorrect imports like the following (note the two `/aws` at the end): github.com/pulumi/pulumi-aws/sdk/v5/go/aws/aws This change replicates more of the logic of getPulumiImport (now called addPulumiImport) into this function, and addresses the discrepancy in codegen caused by `mod`/`originalMod`. The result leaves most existing code unchanged, except in a couple existing cases where the resulting changes make sense given the logic for named imports outlined above. Resolves #11176
2023-06-23 21:11:00 +00:00
importer: newFileImporter(),
}
g.Formatter = format.NewFormatter(g)
return g
}
func parseAndBindProgram(t *testing.T,
text string,
name string,
options ...pcl.BindOption,
) (*pcl.Program, hcl.Diagnostics, error) {
parser := syntax.NewParser()
err := parser.ParseFile(strings.NewReader(text), name)
if err != nil {
t.Fatalf("could not read %v: %v", name, err)
}
if parser.Diagnostics.HasErrors() {
t.Fatalf("failed to parse files: %v", parser.Diagnostics)
}
options = append(options, pcl.PluginHost(utils.NewHost(testdataPath)))
return pcl.BindProgram(parser.Files, options...)
}
func TestGenerateProjectDoesNotPanicWhenMissingVersion(t *testing.T) {
t.Parallel()
source := `
resource main "auto-deploy:index:AutoDeployer" {
project = "example"
}`
program, diags, err := parseAndBindProgram(t, source, "main.pp")
require.NoError(t, err)
require.False(t, diags.HasErrors())
files, diags, err := GenerateProjectFiles(workspace.Project{}, program)
assert.NotNil(t, files, "Files were generated")
2023-07-13 23:47:25 +00:00
require.NoError(t, err)
require.False(t, diags.HasErrors())
}