pulumi/pkg/codegen/nodejs/test.go

124 lines
3.3 KiB
Go
Raw Normal View History

2022-03-16 18:42:30 +00:00
package nodejs
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/test"
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
)
func Check(t *testing.T, path string, dependencies codegen.StringSet, linkLocal bool) {
dir := filepath.Dir(path)
removeFile := func(name string) {
path := filepath.Join(dir, name)
if err := os.Remove(path); !os.IsNotExist(err) {
require.NoError(t, err, "Failed to delete '%s'", path)
}
2022-03-16 18:42:30 +00:00
}
// We delete and regenerate package files for each run.
removeFile("yarn.lock")
removeFile("package.json")
removeFile("tsconfig.json")
// Write out package.json
pkgs := nodejsPackages(t, dependencies)
2022-03-16 18:42:30 +00:00
pkgInfo := npmPackage{
Dependencies: map[string]string{
"@pulumi/pulumi": "latest",
},
DevDependencies: map[string]string{
"@types/node": "^17.0.14",
"typescript": "^4.5.5",
},
}
for pkg, v := range pkgs {
pkgInfo.Dependencies[pkg] = v
}
pkgJSON, err := json.MarshalIndent(pkgInfo, "", " ")
require.NoError(t, err)
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
err = os.WriteFile(filepath.Join(dir, "package.json"), pkgJSON, 0o600)
2022-03-16 18:42:30 +00:00
require.NoError(t, err)
tsConfig := map[string]string{}
tsConfigJSON, err := json.MarshalIndent(tsConfig, "", " ")
require.NoError(t, err)
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
err = os.WriteFile(filepath.Join(dir, "tsconfig.json"), tsConfigJSON, 0o600)
require.NoError(t, err)
2022-03-16 18:42:30 +00:00
TypeCheck(t, path, dependencies, linkLocal)
}
func TypeCheck(t *testing.T, path string, _ codegen.StringSet, linkLocal bool) {
dir := filepath.Dir(path)
typeCheckGeneratedPackage(t, dir, linkLocal)
}
func typeCheckGeneratedPackage(t *testing.T, pwd string, linkLocal bool) {
test.RunCommand(t, "npm_install", pwd, "npm", "install")
if linkLocal {
test.RunCommand(t, "yarn_link", pwd, "yarn", "link", "@pulumi/pulumi")
}
tscOptions := &integration.ProgramTestOptions{
// Avoid Out of Memory error on CI:
Env: []string{"NODE_OPTIONS=--max_old_space_size=4096"},
}
test.RunCommandWithOptions(t, tscOptions, "tsc", pwd, filepath.Join(pwd, "node_modules", ".bin", "tsc"),
"--noEmit", "--skipLibCheck", "true", "--skipDefaultLibCheck", "true")
2022-03-16 18:42:30 +00:00
}
// Returns the nodejs equivalent to the hcl2 package names provided.
func nodejsPackages(t *testing.T, deps codegen.StringSet) map[string]string {
result := make(map[string]string, len(deps))
for _, d := range deps.SortedValues() {
Enable perfsprint linter (#14813) <!--- 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. --> Prompted by a comment in another review: https://github.com/pulumi/pulumi/pull/14654#discussion_r1419995945 This lints that we don't use `fmt.Errorf` when `errors.New` will suffice, it also covers a load of other cases where `Sprintf` is sub-optimal. Most of these edits were made by running `perfsprint --fix`. ## Checklist - [x] 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. --> - [ ] 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-12-12 12:19:42 +00:00
pkgName := "@pulumi/" + d
2022-03-16 18:42:30 +00:00
set := func(pkgVersion string) {
result[pkgName] = "^" + pkgVersion
}
switch d {
case "aws":
set(test.AwsSchema)
case "azure-native":
set(test.AzureNativeSchema)
case "azure":
set(test.AzureSchema)
case "kubernetes":
set(test.KubernetesSchema)
case "random":
set(test.RandomSchema)
case "eks":
set(test.EksSchema)
[program-gen] Fix panic when generating programs for MLC packages using external types (#15605) # Description For an MLC package such as `aws-static-website`, it has some types which are referenced from the `aws` package. Program-gen assumes packages are inferred from resources and invokes, not types which caused panics in Go (#15597), dotnet and python (https://github.com/pulumi/pulumi-converter-constructor-syntax/issues/2) This PR fixes those panics. In Go the panic was due to using package name instead of the package reference from the imported type. In dotnet and python was due to assuming no external type references. Now we generate nice code for all these languages. That said, there is still an issue of resolving imports for the packages of these external types. It works in Go, TypeScript doesn't need it but dotnet and python do. That is why the latter are added in `SkipCompile` in the test program. Fixes #15597 Fixes https://github.com/pulumi/pulumi-converter-constructor-syntax/issues/3 Fixes https://github.com/pulumi/pulumi-converter-constructor-syntax/issues/2 ## 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 - [ ] 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. -->
2024-03-10 17:23:15 +00:00
case "aws-static-website":
set(test.AwsStaticWebsiteSchema)
[program-gen/go,dotnet] Fixes emited code for object expressions assigned to properties of type Any (#15770) # Description Fixes #15769 by specifically handling object expressions that are annotated as `Any` and emitting the correct code. - In case of Go, expression `X` is emitted as `pulumi.Any(X)` where the type name here is `map[string]interface{}`. - In case of C#, we generate `Dictionary<string, object?>` expressions (similar to what we do inside `toJSON` calls) - Updates the test aws-native schema from 0.13.0 to 0.99.0 ## 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. -->
2024-03-24 00:06:57 +00:00
case "aws-native":
set(test.AwsNativeSchema)
2022-03-16 18:42:30 +00:00
default:
t.Logf("Unknown package requested: %s", d)
}
}
return result
}
func GenerateProgramBatchTest(t *testing.T, testCases []test.ProgramTest) {
test.TestProgramCodegen(t,
test.ProgramCodegenOptions{
Language: "nodejs",
Extension: "ts",
OutputFile: "index.ts",
Check: func(t *testing.T, path string, dependencies codegen.StringSet) {
Check(t, path, dependencies, true)
},
GenProgram: GenerateProgram,
TestCases: testCases,
})
}