2020-04-03 02:59:08 +00:00
|
|
|
package gen
|
|
|
|
|
|
|
|
import (
|
2022-04-19 16:39:23 +00:00
|
|
|
"bytes"
|
2021-10-07 18:56:44 +00:00
|
|
|
"encoding/json"
|
2021-08-23 20:46:09 +00:00
|
|
|
"fmt"
|
2023-01-06 22:39:16 +00:00
|
|
|
"os"
|
2021-08-23 20:46:09 +00:00
|
|
|
"path/filepath"
|
2022-04-19 16:39:23 +00:00
|
|
|
"regexp"
|
2021-10-07 18:56:44 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-04-03 02:59:08 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-12-12 13:26:07 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
2021-07-07 23:25:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
2022-02-07 11:10:04 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/test"
|
2023-03-23 22:16:19 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/utils"
|
2023-06-28 16:02:04 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/slice"
|
2021-09-15 16:49:36 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/executable"
|
2020-04-03 02:59:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInputUsage(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-03-31 05:23:04 +00:00
|
|
|
pkg := &pkgContext{}
|
|
|
|
arrayUsage := pkg.getInputUsage("FooArray")
|
2020-04-03 02:59:08 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"FooArrayInput is an input type that accepts FooArray and FooArrayOutput values.\nYou can construct a "+
|
|
|
|
"concrete instance of `FooArrayInput` via:\n\n\t\t FooArray{ FooArgs{...} }\n ",
|
|
|
|
arrayUsage)
|
|
|
|
|
2021-03-31 05:23:04 +00:00
|
|
|
mapUsage := pkg.getInputUsage("FooMap")
|
2020-04-03 02:59:08 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"FooMapInput is an input type that accepts FooMap and FooMapOutput values.\nYou can construct a concrete"+
|
|
|
|
" instance of `FooMapInput` via:\n\n\t\t FooMap{ \"key\": FooArgs{...} }\n ",
|
|
|
|
mapUsage)
|
|
|
|
|
2021-03-31 05:23:04 +00:00
|
|
|
ptrUsage := pkg.getInputUsage("FooPtr")
|
2020-04-03 02:59:08 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"FooPtrInput is an input type that accepts FooArgs, FooPtr and FooPtrOutput values.\nYou can construct a "+
|
|
|
|
"concrete instance of `FooPtrInput` via:\n\n\t\t FooArgs{...}\n\n or:\n\n\t\t nil\n ",
|
|
|
|
ptrUsage)
|
|
|
|
|
2021-03-31 05:23:04 +00:00
|
|
|
usage := pkg.getInputUsage("Foo")
|
2020-04-03 02:59:08 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"FooInput is an input type that accepts FooArgs and FooOutput values.\nYou can construct a concrete instance"+
|
|
|
|
" of `FooInput` via:\n\n\t\t FooArgs{...}\n ",
|
|
|
|
usage)
|
|
|
|
}
|
2020-09-15 11:56:58 +00:00
|
|
|
|
|
|
|
func TestGoPackageName(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-09-15 11:56:58 +00:00
|
|
|
assert.Equal(t, "aws", goPackage("aws"))
|
2021-10-11 22:28:11 +00:00
|
|
|
assert.Equal(t, "azurenextgen", goPackage("azure-nextgen"))
|
|
|
|
assert.Equal(t, "plantprovider", goPackage("plant-provider"))
|
2020-09-15 11:56:58 +00:00
|
|
|
assert.Equal(t, "", goPackage(""))
|
|
|
|
}
|
2020-11-09 18:55:53 +00:00
|
|
|
|
|
|
|
func TestGeneratePackage(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-07-06 22:40:53 +00:00
|
|
|
generatePackage := func(tool string, pkg *schema.Package, files map[string][]byte) (map[string][]byte, error) {
|
2021-09-22 17:55:20 +00:00
|
|
|
for f := range files {
|
|
|
|
t.Logf("Ignoring extraFile %s", f)
|
|
|
|
}
|
|
|
|
|
2021-07-06 22:40:53 +00:00
|
|
|
return GeneratePackage(tool, pkg)
|
2020-11-09 18:55:53 +00:00
|
|
|
}
|
2023-11-21 16:43:51 +00:00
|
|
|
|
2021-09-22 17:55:20 +00:00
|
|
|
test.TestSDKCodegen(t, &test.SDKCodegenOptions{
|
|
|
|
Language: "go",
|
|
|
|
GenPackage: generatePackage,
|
|
|
|
Checks: map[string]test.CodegenCheck{
|
|
|
|
"go/compile": typeCheckGeneratedPackage,
|
|
|
|
"go/test": testGeneratedPackage,
|
|
|
|
},
|
2022-02-07 11:10:04 +00:00
|
|
|
TestCases: test.PulumiPulumiSDKTests,
|
2021-09-22 17:55:20 +00:00
|
|
|
})
|
2021-09-15 16:49:36 +00:00
|
|
|
}
|
|
|
|
|
Support returning plain values from methods (#13592)
Support returning plain values from methods.
Implements Node, Python and Go support.
Remaining:
- [x] test receiving unknowns
- [x] acceptance tests written and passing locally for Node, Python, Go
clients against a Go server
- [x] acceptance tests passing in CI
- [x] tickets filed for remaining languages
- [x] https://github.com/pulumi/pulumi-yaml/issues/499
- [x] https://github.com/pulumi/pulumi-java/issues/1193
- [x] https://github.com/pulumi/pulumi-dotnet/issues/170
Known limitations:
- this is technically a breaking change in case there is code out there
that already uses methods that return Plain: true
- struct-wrapping limitation: the provider for the component resource
needs to still wrap the plain-returning Method response with a 1-arg
struct; by convention the field is named "res", and this is how it
travels through the plumbing
- resources cannot return plain values yet
- the provider for the component resource cannot have unknown
configuration, if it does, the methods will not be called
- Per Luke https://github.com/pulumi/pulumi/issues/11520 this might not
be supported/realizable yet
<!---
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. -->
Fixes https://github.com/pulumi/pulumi/issues/12709
## Checklist
- [ ] I have run `make tidy` to update any new dependencies
- [ ] I have run `make lint` to verify my code passes the lint check
- [ ] I have formatted my code using `gofumpt`
<!--- 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
<!---
User-facing changes require a CHANGELOG entry.
-->
- [ ] 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-18 06:02:06 +00:00
|
|
|
func readGoPackageInfo(schemaPath string) (*GoPackageInfo, error) {
|
|
|
|
f, err := os.Open(schemaPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
type language struct {
|
|
|
|
Go GoPackageInfo `json:"go"`
|
|
|
|
}
|
|
|
|
type model struct {
|
|
|
|
Language language `json:"language"`
|
|
|
|
}
|
|
|
|
var m model
|
|
|
|
if err := json.NewDecoder(f).Decode(&m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &m.Language.Go, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decide the name of the Go module for a generated test.
|
|
|
|
//
|
|
|
|
// For example for this path:
|
|
|
|
//
|
|
|
|
// codeDir = "../testing/test/testdata/external-resource-schema/go/"
|
|
|
|
//
|
|
|
|
// We will generate "$codeDir/go.mod" using `external-resource-schema` as the module name so that it can compile
|
|
|
|
// independently.
|
|
|
|
//
|
|
|
|
// This can be overwritten by setting ModulePath in GoPackageInfo in
|
|
|
|
//
|
|
|
|
// jq .language.go.modulePath ${codeDir}../schema.json
|
2021-09-22 17:55:20 +00:00
|
|
|
func inferModuleName(codeDir string) string {
|
Support returning plain values from methods (#13592)
Support returning plain values from methods.
Implements Node, Python and Go support.
Remaining:
- [x] test receiving unknowns
- [x] acceptance tests written and passing locally for Node, Python, Go
clients against a Go server
- [x] acceptance tests passing in CI
- [x] tickets filed for remaining languages
- [x] https://github.com/pulumi/pulumi-yaml/issues/499
- [x] https://github.com/pulumi/pulumi-java/issues/1193
- [x] https://github.com/pulumi/pulumi-dotnet/issues/170
Known limitations:
- this is technically a breaking change in case there is code out there
that already uses methods that return Plain: true
- struct-wrapping limitation: the provider for the component resource
needs to still wrap the plain-returning Method response with a 1-arg
struct; by convention the field is named "res", and this is how it
travels through the plumbing
- resources cannot return plain values yet
- the provider for the component resource cannot have unknown
configuration, if it does, the methods will not be called
- Per Luke https://github.com/pulumi/pulumi/issues/11520 this might not
be supported/realizable yet
<!---
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. -->
Fixes https://github.com/pulumi/pulumi/issues/12709
## Checklist
- [ ] I have run `make tidy` to update any new dependencies
- [ ] I have run `make lint` to verify my code passes the lint check
- [ ] I have formatted my code using `gofumpt`
<!--- 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
<!---
User-facing changes require a CHANGELOG entry.
-->
- [ ] 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-18 06:02:06 +00:00
|
|
|
schemaPath := filepath.Join(filepath.Dir(codeDir), "schema.json")
|
|
|
|
if gotSchema, err := test.PathExists(schemaPath); err == nil && gotSchema {
|
|
|
|
if info, err := readGoPackageInfo(schemaPath); err == nil {
|
|
|
|
if info.ModulePath != "" {
|
|
|
|
return info.ModulePath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 17:55:20 +00:00
|
|
|
return filepath.Base(filepath.Dir(codeDir))
|
|
|
|
}
|
|
|
|
|
|
|
|
func typeCheckGeneratedPackage(t *testing.T, codeDir string) {
|
|
|
|
sdk, err := filepath.Abs(filepath.Join("..", "..", "..", "sdk"))
|
2021-09-15 16:49:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-22 17:55:20 +00:00
|
|
|
goExe, err := executable.FindExecutable("go")
|
2021-09-15 16:49:36 +00:00
|
|
|
require.NoError(t, err)
|
2021-09-22 17:55:20 +00:00
|
|
|
|
|
|
|
goMod := filepath.Join(codeDir, "go.mod")
|
|
|
|
alreadyHaveGoMod, err := test.PathExists(goMod)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if alreadyHaveGoMod {
|
|
|
|
t.Logf("Found an existing go.mod, leaving as is")
|
|
|
|
} else {
|
2021-09-23 17:42:20 +00:00
|
|
|
test.RunCommand(t, "go_mod_init", codeDir, goExe, "mod", "init", inferModuleName(codeDir))
|
2023-12-12 12:19:42 +00:00
|
|
|
replacement := "github.com/pulumi/pulumi/sdk/v3=" + sdk
|
2021-09-23 17:42:20 +00:00
|
|
|
test.RunCommand(t, "go_mod_edit", codeDir, goExe, "mod", "edit", "-replace", replacement)
|
2021-09-15 16:49:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 17:41:25 +00:00
|
|
|
test.RunCommand(t, "go_mod_tidy", codeDir, goExe, "mod", "tidy")
|
2021-09-23 17:42:20 +00:00
|
|
|
test.RunCommand(t, "go_build", codeDir, goExe, "build", "-v", "all")
|
2021-09-22 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testGeneratedPackage(t *testing.T, codeDir string) {
|
|
|
|
goExe, err := executable.FindExecutable("go")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-12 12:19:42 +00:00
|
|
|
test.RunCommand(t, "go-test", codeDir, goExe, "test", inferModuleName(codeDir)+"/...")
|
2020-11-09 18:55:53 +00:00
|
|
|
}
|
2020-11-05 00:56:39 +00:00
|
|
|
|
2021-07-09 17:23:10 +00:00
|
|
|
func TestGenerateTypeNames(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-07-09 17:23:10 +00:00
|
|
|
test.TestTypeNameCodegen(t, "go", func(pkg *schema.Package) test.TypeNameGeneratorFunc {
|
|
|
|
err := pkg.ImportLanguages(map[string]schema.Language{"go": Importer})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var goPkgInfo GoPackageInfo
|
|
|
|
if goInfo, ok := pkg.Language["go"].(GoPackageInfo); ok {
|
|
|
|
goPkgInfo = goInfo
|
|
|
|
}
|
2022-12-08 10:45:46 +00:00
|
|
|
packages, err := generatePackageContextMap("test", pkg.Reference(), goPkgInfo, nil)
|
|
|
|
require.NoError(t, err)
|
2021-07-09 17:23:10 +00:00
|
|
|
|
|
|
|
root, ok := packages[""]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
return func(t schema.Type) string {
|
|
|
|
return root.typeString(t)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-10-07 18:56:44 +00:00
|
|
|
|
|
|
|
func readSchemaFile(file string) *schema.Package {
|
|
|
|
// Read in, decode, and import the schema.
|
2023-01-06 22:39:16 +00:00
|
|
|
schemaBytes, err := os.ReadFile(filepath.Join("..", "testing", "test", "testdata", file))
|
2021-10-07 18:56:44 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var pkgSpec schema.PackageSpec
|
|
|
|
if err = json.Unmarshal(schemaBytes, &pkgSpec); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-12-12 13:26:07 +00:00
|
|
|
loader := schema.NewPluginLoader(utils.NewHost(testdataPath))
|
|
|
|
pkg, diags, err := schema.BindSpec(pkgSpec, loader)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diags.HasErrors() {
|
|
|
|
panic(diags.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkg
|
|
|
|
}
|
|
|
|
|
|
|
|
func readYamlSchemaFile(file string) *schema.Package {
|
|
|
|
// Read in, decode, and import the schema.
|
|
|
|
schemaBytes, err := os.ReadFile(filepath.Join("..", "testing", "test", "testdata", file))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var pkgSpec schema.PackageSpec
|
|
|
|
if err = yaml.Unmarshal(schemaBytes, &pkgSpec); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
loader := schema.NewPluginLoader(utils.NewHost(testdataPath))
|
|
|
|
pkg, diags, err := schema.BindSpec(pkgSpec, loader)
|
2021-10-07 18:56:44 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-12-12 13:26:07 +00:00
|
|
|
if diags.HasErrors() {
|
|
|
|
panic(diags.Error())
|
|
|
|
}
|
|
|
|
|
2021-10-07 18:56:44 +00:00
|
|
|
return pkg
|
|
|
|
}
|
|
|
|
|
2023-12-12 13:26:07 +00:00
|
|
|
func TestLanguageResources(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
for _, test := range test.PulumiPulumiSDKTests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.Directory, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
var pkg *schema.Package
|
|
|
|
if test.Directory == "simple-yaml-schema" || test.Directory == "cyclic-types" {
|
|
|
|
pkg = readYamlSchemaFile(filepath.Join(test.Directory, "schema.yaml"))
|
|
|
|
} else {
|
|
|
|
pkg = readSchemaFile(filepath.Join(test.Directory, "schema.json"))
|
|
|
|
}
|
|
|
|
|
|
|
|
resources, err := LanguageResources("test", pkg)
|
|
|
|
for token, resource := range resources {
|
|
|
|
assert.Equal(t, tokenToName(token), resource.Name)
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 18:56:44 +00:00
|
|
|
// We test the naming/module structure of generated packages.
|
|
|
|
func TestPackageNaming(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-10-07 18:56:44 +00:00
|
|
|
testCases := []struct {
|
|
|
|
importBasePath string
|
|
|
|
rootPackageName string
|
|
|
|
name string
|
|
|
|
expectedRoot string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
importBasePath: "github.com/pulumi/pulumi-azure-quickstart-acr-geo-replication/sdk/go/acr",
|
|
|
|
expectedRoot: "acr",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
importBasePath: "github.com/ihave/animport",
|
|
|
|
rootPackageName: "root",
|
|
|
|
expectedRoot: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "named-package",
|
2021-10-11 22:28:11 +00:00
|
|
|
expectedRoot: "namedpackage",
|
2021-10-07 18:56:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range testCases {
|
2022-03-04 08:17:41 +00:00
|
|
|
tt := tt
|
2021-10-07 18:56:44 +00:00
|
|
|
t.Run(tt.expectedRoot, func(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-10-07 18:56:44 +00:00
|
|
|
// This schema is arbitrary. We just needed a filled out schema. All
|
|
|
|
// path decisions should be made based off of the Name and
|
|
|
|
// Language[go] fields (which we set after import).
|
|
|
|
schema := readSchemaFile(filepath.Join("schema", "good-enum-1.json"))
|
|
|
|
if tt.name != "" {
|
|
|
|
// We want there to be a name, so if one isn't provided we
|
|
|
|
// default to the schema.
|
|
|
|
schema.Name = tt.name
|
|
|
|
}
|
|
|
|
schema.Language = map[string]interface{}{
|
|
|
|
"go": GoPackageInfo{
|
|
|
|
ImportBasePath: tt.importBasePath,
|
|
|
|
RootPackageName: tt.rootPackageName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
files, err := GeneratePackage("test", schema)
|
|
|
|
require.NoError(t, err)
|
2023-06-28 16:02:04 +00:00
|
|
|
ordering := slice.Prealloc[string](len(files))
|
2021-10-07 18:56:44 +00:00
|
|
|
for k := range files {
|
2023-01-13 18:23:56 +00:00
|
|
|
ordering = append(ordering, k)
|
2021-10-07 18:56:44 +00:00
|
|
|
}
|
2023-01-13 18:23:56 +00:00
|
|
|
sort.Strings(ordering)
|
2021-10-07 18:56:44 +00:00
|
|
|
require.NotEmpty(t, files, "This test only works when files are generated")
|
|
|
|
for _, k := range ordering {
|
|
|
|
root := strings.Split(k, "/")[0]
|
|
|
|
if tt.expectedRoot != "" {
|
|
|
|
require.Equal(t, tt.expectedRoot, root, "Root should precede all cases. Got file %s", k)
|
|
|
|
}
|
|
|
|
// We should work on a way to assert this is one level higher then it otherwise would be.
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 15:43:05 +00:00
|
|
|
|
|
|
|
func TestTokenToType(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-02-03 15:43:05 +00:00
|
|
|
const awsImportBasePath = "github.com/pulumi/pulumi-aws/sdk/v4/go/aws"
|
|
|
|
awsSpec := schema.PackageSpec{
|
|
|
|
Name: "aws",
|
|
|
|
Meta: &schema.MetadataSpec{
|
|
|
|
ModuleFormat: "(.*)(?:/[^/]*)",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const googleNativeImportBasePath = "github.com/pulumi/pulumi-google-native/sdk/go/google"
|
|
|
|
googleNativeSpec := schema.PackageSpec{
|
|
|
|
Name: "google-native",
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pkg *pkgContext
|
|
|
|
token string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pkg: &pkgContext{
|
2022-12-08 10:45:46 +00:00
|
|
|
pkg: importSpec(t, awsSpec).Reference(),
|
2022-02-03 15:43:05 +00:00
|
|
|
importBasePath: awsImportBasePath,
|
|
|
|
},
|
|
|
|
token: "aws:s3/BucketWebsite:BucketWebsite",
|
|
|
|
expected: "s3.BucketWebsite",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pkg: &pkgContext{
|
2022-12-08 10:45:46 +00:00
|
|
|
pkg: importSpec(t, awsSpec).Reference(),
|
2022-02-03 15:43:05 +00:00
|
|
|
importBasePath: awsImportBasePath,
|
|
|
|
pkgImportAliases: map[string]string{
|
|
|
|
"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/s3": "awss3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
token: "aws:s3/BucketWebsite:BucketWebsite",
|
|
|
|
expected: "awss3.BucketWebsite",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pkg: &pkgContext{
|
2022-12-08 10:45:46 +00:00
|
|
|
pkg: importSpec(t, googleNativeSpec).Reference(),
|
2022-02-03 15:43:05 +00:00
|
|
|
importBasePath: googleNativeImportBasePath,
|
|
|
|
pkgImportAliases: map[string]string{
|
|
|
|
"github.com/pulumi/pulumi-google-native/sdk/go/google/dns/v1": "dns",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
token: "google-native:dns/v1:DnsKeySpec",
|
|
|
|
expected: "dns.DnsKeySpec",
|
|
|
|
},
|
|
|
|
}
|
2022-03-04 08:17:41 +00:00
|
|
|
//nolint:paralleltest // false positive because range var isn't used directly in t.Run(name) arg
|
2022-02-03 15:43:05 +00:00
|
|
|
for _, tt := range tests {
|
2022-03-04 08:17:41 +00:00
|
|
|
tt := tt
|
2022-02-03 15:43:05 +00:00
|
|
|
t.Run(tt.token+"=>"+tt.expected, func(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-02-03 15:43:05 +00:00
|
|
|
actual := tt.pkg.tokenToType(tt.token)
|
|
|
|
assert.Equal(t, tt.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTokenToResource(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-02-03 15:43:05 +00:00
|
|
|
const awsImportBasePath = "github.com/pulumi/pulumi-aws/sdk/v4/go/aws"
|
|
|
|
awsSpec := schema.PackageSpec{
|
|
|
|
Name: "aws",
|
|
|
|
Meta: &schema.MetadataSpec{
|
|
|
|
ModuleFormat: "(.*)(?:/[^/]*)",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const googleNativeImportBasePath = "github.com/pulumi/pulumi-google-native/sdk/go/google"
|
|
|
|
googleNativeSpec := schema.PackageSpec{
|
|
|
|
Name: "google-native",
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pkg *pkgContext
|
|
|
|
token string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pkg: &pkgContext{
|
2022-12-08 10:45:46 +00:00
|
|
|
pkg: importSpec(t, awsSpec).Reference(),
|
2022-02-03 15:43:05 +00:00
|
|
|
importBasePath: awsImportBasePath,
|
|
|
|
},
|
|
|
|
token: "aws:s3/Bucket:Bucket",
|
|
|
|
expected: "s3.Bucket",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pkg: &pkgContext{
|
2022-12-08 10:45:46 +00:00
|
|
|
pkg: importSpec(t, awsSpec).Reference(),
|
2022-02-03 15:43:05 +00:00
|
|
|
importBasePath: awsImportBasePath,
|
|
|
|
pkgImportAliases: map[string]string{
|
|
|
|
"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/s3": "awss3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
token: "aws:s3/Bucket:Bucket",
|
|
|
|
expected: "awss3.Bucket",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pkg: &pkgContext{
|
2022-12-08 10:45:46 +00:00
|
|
|
pkg: importSpec(t, googleNativeSpec).Reference(),
|
2022-02-03 15:43:05 +00:00
|
|
|
importBasePath: googleNativeImportBasePath,
|
|
|
|
pkgImportAliases: map[string]string{
|
|
|
|
"github.com/pulumi/pulumi-google-native/sdk/go/google/dns/v1": "dns",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
token: "google-native:dns/v1:Policy",
|
|
|
|
expected: "dns.Policy",
|
|
|
|
},
|
|
|
|
}
|
2022-03-04 08:17:41 +00:00
|
|
|
//nolint:paralleltest // false positive because range var isn't used directly in t.Run(name) arg
|
2022-02-03 15:43:05 +00:00
|
|
|
for _, tt := range tests {
|
2022-03-04 08:17:41 +00:00
|
|
|
tt := tt
|
2022-02-03 15:43:05 +00:00
|
|
|
t.Run(tt.token+"=>"+tt.expected, func(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-02-03 15:43:05 +00:00
|
|
|
actual := tt.pkg.tokenToResource(tt.token)
|
|
|
|
assert.Equal(t, tt.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func importSpec(t *testing.T, spec schema.PackageSpec) *schema.Package {
|
|
|
|
importedPkg, err := schema.ImportSpec(spec, map[string]schema.Language{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
return importedPkg
|
|
|
|
}
|
2022-04-19 16:39:23 +00:00
|
|
|
|
|
|
|
func TestGenHeader(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
pkg := &pkgContext{
|
|
|
|
tool: "a tool",
|
2022-12-08 10:45:46 +00:00
|
|
|
pkg: (&schema.Package{Name: "test-pkg"}).Reference(),
|
2022-04-19 16:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := func() string {
|
|
|
|
b := &bytes.Buffer{}
|
2023-07-06 20:20:04 +00:00
|
|
|
pkg.genHeader(b, []string{"pkg1", "example.com/foo/123-foo"}, nil, false /* isUtil */)
|
2022-04-19 16:39:23 +00:00
|
|
|
return b.String()
|
|
|
|
}()
|
|
|
|
assert.Equal(t, `// Code generated by a tool DO NOT EDIT.
|
|
|
|
// *** WARNING: Do not edit by hand unless you're certain you know what you are doing! ***
|
|
|
|
|
|
|
|
package testpkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"pkg1"
|
|
|
|
"example.com/foo/123-foo"
|
|
|
|
)
|
|
|
|
|
|
|
|
`, s)
|
|
|
|
|
|
|
|
// Compliance is defined by https://pkg.go.dev/cmd/go#hdr-Generate_Go_files_by_processing_source
|
|
|
|
autogenerated := regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`)
|
|
|
|
found := false
|
gosimple: Simplify loops
This replaces for loops and slice appends reported by gosimple
with simpler variants.
Specifically,
for _, x := range src {
dst = append(dst, x)
}
// can be replaced with
dst = append(dst, src...)
And,
for i, x := range src {
dst[i] = x
}
// can be replaced with
copy(dst, src)
And,
for true { ... }
// can be replaced with
for { ... }
And, given a string `s`,
for _, r := range []rune(s) { .. }
// can be replaced with
for _, r := range s { .. }
Lastly, this fixes in ineffective break statement
also reported by the linter.
Inside a switch block,
`break` affects the current `case` only.
The outer loop needs a label.
2023-01-11 15:58:17 +00:00
|
|
|
loop:
|
2022-04-19 16:39:23 +00:00
|
|
|
for _, l := range strings.Split(s, "\n") {
|
|
|
|
switch {
|
|
|
|
case autogenerated.Match([]byte(l)):
|
|
|
|
found = true
|
gosimple: Simplify loops
This replaces for loops and slice appends reported by gosimple
with simpler variants.
Specifically,
for _, x := range src {
dst = append(dst, x)
}
// can be replaced with
dst = append(dst, src...)
And,
for i, x := range src {
dst[i] = x
}
// can be replaced with
copy(dst, src)
And,
for true { ... }
// can be replaced with
for { ... }
And, given a string `s`,
for _, r := range []rune(s) { .. }
// can be replaced with
for _, r := range s { .. }
Lastly, this fixes in ineffective break statement
also reported by the linter.
Inside a switch block,
`break` affects the current `case` only.
The outer loop needs a label.
2023-01-11 15:58:17 +00:00
|
|
|
break loop
|
2022-04-19 16:39:23 +00:00
|
|
|
case l == "" || strings.HasPrefix(l, "//"):
|
|
|
|
default:
|
golangci-lint: Enable staticcheck
Remove staticcheck from the list of disabled linters.
It's enabled by default in golangci-lint.
This also fixes minor remaining staticcheck issues
that don't merit their own pull requests,
or opts out of those that cannot be fixed yet.
Notably, we're opting out of:
- Resource.Name is deprecated (#9469)
- github.com/golang/protobuf is deprecated (#11869)
- strings.Title has been deprecated (#11870)
Besides that, other issues addressed in this change are:
```
// all issues are in pkg
codegen/schema/docs_parser.go:103:4: SA4006: this value of `text` is never used (staticcheck)
codegen/schema/loader.go:253:3: SA9003: empty branch (staticcheck)
resource/deploy/step_executor.go:328:12: SA9003: empty branch (staticcheck)
resource/deploy/step_generator.go:141:10: SA9003: empty branch (staticcheck)
codegen/pcl/invoke.go:97:10: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_const.go:57:2: SA9003: empty branch (staticcheck)
codegen/hcl2/model/type_enum.go:99:9: SA4001: &*x will be simplified to x. It will not copy x. (staticcheck)
codegen/go/gen_test.go:399:19: SA4017: HasPrefix is a pure function but its return value is ignored (staticcheck)
```
Depends on #11857, #11858, #11859, #11860, #11862, #11865, #11866, #11867, #11868
Resolves #11808
2023-01-11 19:53:41 +00:00
|
|
|
break loop
|
2022-04-19 16:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Truef(t, found, `Didn't find a line that complies with "%v"`, autogenerated)
|
|
|
|
}
|
2023-03-03 16:36:39 +00:00
|
|
|
|
2022-10-17 16:37:07 +00:00
|
|
|
func TestTitle(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
assert.Equal("", Title(""))
|
|
|
|
assert.Equal("Plugh", Title("plugh"))
|
|
|
|
assert.Equal("WaldoThudFred", Title("WaldoThudFred"))
|
|
|
|
assert.Equal("WaldoThudFred", Title("waldoThudFred"))
|
|
|
|
assert.Equal("WaldoThudFred", Title("waldo-Thud-Fred"))
|
|
|
|
assert.Equal("WaldoThudFred", Title("waldo-ThudFred"))
|
|
|
|
assert.Equal("WaldoThud_Fred", Title("waldo-Thud_Fred"))
|
|
|
|
assert.Equal("WaldoThud_Fred", Title("waldo-thud_Fred"))
|
2023-12-20 13:16:37 +00:00
|
|
|
assert.Equal("WaldoThud_Fred", Title("$waldo-thud_Fred"))
|
2022-10-17 16:37:07 +00:00
|
|
|
}
|
2023-03-23 22:16:19 +00:00
|
|
|
|
|
|
|
func TestRegressTypeDuplicatesInChunking(t *testing.T) {
|
2023-03-23 22:53:34 +00:00
|
|
|
t.Parallel()
|
2023-03-23 22:16:19 +00:00
|
|
|
pkgSpec := schema.PackageSpec{
|
|
|
|
Name: "test",
|
|
|
|
Version: "0.0.1",
|
|
|
|
Resources: make(map[string]schema.ResourceSpec),
|
|
|
|
Types: map[string]schema.ComplexTypeSpec{
|
|
|
|
"test:index:PolicyStatusAutogenRules": {
|
|
|
|
ObjectTypeSpec: schema.ObjectTypeSpec{
|
|
|
|
Type: "object",
|
|
|
|
Properties: map[string]schema.PropertySpec{
|
|
|
|
"imageExtractors": {
|
|
|
|
TypeSpec: schema.TypeSpec{
|
|
|
|
Type: "object",
|
|
|
|
AdditionalProperties: &schema.TypeSpec{
|
|
|
|
Type: "array",
|
|
|
|
Items: &schema.TypeSpec{
|
|
|
|
Type: "object",
|
|
|
|
Ref: "#/types/test:index:Im",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"test:index:Im": {
|
|
|
|
ObjectTypeSpec: schema.ObjectTypeSpec{
|
|
|
|
Type: "object",
|
|
|
|
Properties: map[string]schema.PropertySpec{
|
|
|
|
"name": {TypeSpec: schema.TypeSpec{Type: "string"}},
|
|
|
|
"path": {TypeSpec: schema.TypeSpec{Type: "string"}},
|
|
|
|
},
|
|
|
|
Required: []string{"path"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to ref PolicyStatusAutogenRules in input position to trigger the code path.
|
|
|
|
pkgSpec.Resources["test:index:Res"] = schema.ResourceSpec{
|
|
|
|
InputProperties: map[string]schema.PropertySpec{
|
|
|
|
"a": {
|
|
|
|
TypeSpec: schema.TypeSpec{
|
|
|
|
Ref: "#/types/test:index:PolicyStatusAutogenRules",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to have N>500 but N<1000 to obtain 2 chunks.
|
|
|
|
for i := 0; i < 750; i++ {
|
|
|
|
ttok := fmt.Sprintf("test:index:Typ%d", i)
|
|
|
|
pkgSpec.Types[ttok] = schema.ComplexTypeSpec{
|
|
|
|
ObjectTypeSpec: schema.ObjectTypeSpec{
|
|
|
|
Type: "object",
|
|
|
|
Required: []string{"x"},
|
|
|
|
Properties: map[string]schema.PropertySpec{
|
|
|
|
"x": {TypeSpec: schema.TypeSpec{Type: "string"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loader := schema.NewPluginLoader(utils.NewHost(testdataPath))
|
|
|
|
pkg, diags, err := schema.BindSpec(pkgSpec, loader)
|
|
|
|
require.NoError(t, err)
|
2023-12-20 15:54:06 +00:00
|
|
|
t.Logf("%v", diags)
|
2023-03-23 22:16:19 +00:00
|
|
|
require.False(t, diags.HasErrors())
|
|
|
|
|
|
|
|
fs, err := GeneratePackage("tests", pkg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for f := range fs {
|
|
|
|
t.Logf("Generated %v", f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect to see two chunked files (chunking at n=500).
|
|
|
|
assert.Contains(t, fs, "test/pulumiTypes.go")
|
|
|
|
assert.Contains(t, fs, "test/pulumiTypes1.go")
|
|
|
|
assert.NotContains(t, fs, "test/pulumiTypes2.go")
|
|
|
|
|
|
|
|
// The types defined in the chunks should be mutually exclusive.
|
|
|
|
typedefs := func(s string) []string {
|
|
|
|
var types []string
|
|
|
|
for _, line := range strings.Split(s, "\n") {
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if strings.HasPrefix(line, "type") {
|
|
|
|
types = append(types, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(types)
|
|
|
|
return types
|
|
|
|
}
|
|
|
|
|
|
|
|
typedefs1 := typedefs(string(fs["test/pulumiTypes.go"]))
|
|
|
|
typedefs2 := typedefs(string(fs["test/pulumiTypes1.go"]))
|
|
|
|
|
|
|
|
for _, typ := range typedefs1 {
|
|
|
|
assert.NotContains(t, typedefs2, typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, typ := range typedefs2 {
|
|
|
|
assert.NotContains(t, typedefs1, typ)
|
|
|
|
}
|
|
|
|
}
|