2020-10-08 03:13:10 +02:00
|
|
|
// nolint: lll
|
2020-09-25 21:35:27 +02:00
|
|
|
package nodejs
|
|
|
|
|
|
|
|
import (
|
2021-09-15 18:49:36 +02:00
|
|
|
"bytes"
|
2021-09-20 19:11:44 +02:00
|
|
|
"os"
|
2021-09-15 18:49:36 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-09-25 21:35:27 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-09-15 18:49:36 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-03-17 14:20:05 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/internal/test"
|
2021-07-09 19:23:10 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
2021-09-15 18:49:36 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/executable"
|
2020-09-25 21:35:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGeneratePackage(t *testing.T) {
|
2021-09-22 19:55:20 +02:00
|
|
|
test.TestSDKCodegen(t, &test.SDKCodegenOptions{
|
|
|
|
Language: "nodejs",
|
|
|
|
GenPackage: GeneratePackage,
|
|
|
|
Checks: map[string]test.CodegenCheck{
|
|
|
|
"nodejs/compile": typeCheckGeneratedPackage,
|
|
|
|
},
|
|
|
|
})
|
2021-09-15 18:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func typeCheckGeneratedPackage(t *testing.T, pwd string) {
|
|
|
|
var err error
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmdOptions := integration.ProgramTestOptions{
|
|
|
|
Verbose: true,
|
|
|
|
Stderr: &stderr,
|
|
|
|
Stdout: &stdout,
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
// TODO: previous attempt used npm. It may be more popular and
|
|
|
|
// better target than yarn, however our build uses yarn in
|
|
|
|
// other places at the moment, and yarn does not run into the
|
|
|
|
// ${VERSION} problem; use yarn for now.
|
|
|
|
//
|
|
|
|
// var npm string
|
|
|
|
// npm, err = executable.FindExecutable("npm")
|
|
|
|
// require.NoError(t, err)
|
|
|
|
// // TODO remove when https://github.com/pulumi/pulumi/pull/7938 lands
|
|
|
|
// file := filepath.Join(pwd, "package.json")
|
|
|
|
// oldFile, err := ioutil.ReadFile(file)
|
|
|
|
// require.NoError(t, err)
|
|
|
|
// newFile := strings.ReplaceAll(string(oldFile), "${VERSION}", "0.0.1")
|
|
|
|
// err = ioutil.WriteFile(file, []byte(newFile), 0600)
|
|
|
|
// require.NoError(t, err)
|
|
|
|
// err = integration.RunCommand(t, "npm install", []string{npm, "i"}, pwd, &cmdOptions)
|
|
|
|
// require.NoError(t, err)
|
|
|
|
|
|
|
|
var yarn string
|
|
|
|
yarn, err = executable.FindExecutable("yarn")
|
2021-09-15 18:49:36 +02:00
|
|
|
require.NoError(t, err)
|
2021-09-22 19:55:20 +02:00
|
|
|
|
|
|
|
err = integration.RunCommand(t, "yarn link @pulumi/pulumi",
|
|
|
|
[]string{yarn, "link", "@pulumi/pulumi"}, pwd, &cmdOptions)
|
2021-09-15 18:49:36 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
err = integration.RunCommand(t, "yarn install",
|
|
|
|
[]string{yarn, "install"}, pwd, &cmdOptions)
|
2021-09-15 18:49:36 +02:00
|
|
|
require.NoError(t, err)
|
2021-09-20 19:11:44 +02:00
|
|
|
|
|
|
|
// We increase the amount of memory node can use. We get OOM otherwise.
|
|
|
|
nodeOptions := []string{os.Getenv("NODE_OPTIONS"), "--max_old_space_size=4096"}
|
|
|
|
err = os.Setenv("NODE_OPTIONS", strings.Join(nodeOptions, " "))
|
|
|
|
require.NoError(t, err)
|
2021-09-22 19:55:20 +02:00
|
|
|
|
2021-09-15 18:49:36 +02:00
|
|
|
err = integration.RunCommand(t, "typecheck ts",
|
|
|
|
[]string{filepath.Join(".", "node_modules", ".bin", "tsc"), "--noEmit"}, pwd, &cmdOptions)
|
2021-09-22 19:55:20 +02:00
|
|
|
|
2021-09-15 18:49:36 +02:00
|
|
|
if err != nil {
|
|
|
|
stderr := stderr.String()
|
|
|
|
if len(stderr) > 0 {
|
|
|
|
t.Logf("stderr: %s", stderr)
|
|
|
|
}
|
|
|
|
stdout := stdout.String()
|
|
|
|
if len(stdout) > 0 {
|
|
|
|
t.Logf("stdout: %s", stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
2020-09-25 21:35:27 +02:00
|
|
|
}
|
2021-07-09 19:23:10 +02:00
|
|
|
|
|
|
|
func TestGenerateTypeNames(t *testing.T) {
|
|
|
|
test.TestTypeNameCodegen(t, "nodejs", func(pkg *schema.Package) test.TypeNameGeneratorFunc {
|
2021-07-09 19:41:35 +02:00
|
|
|
modules, info, err := generateModuleContextMap("test", pkg, nil)
|
2021-07-09 19:23:10 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pkg.Language["nodejs"] = info
|
|
|
|
|
|
|
|
root, ok := modules[""]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
return func(t schema.Type) string {
|
|
|
|
return root.typeString(t, false, nil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-09-20 19:11:44 +02:00
|
|
|
|
|
|
|
func TestPascalCases(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "hi",
|
|
|
|
expected: "Hi",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "NothingChanges",
|
|
|
|
expected: "NothingChanges",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "everything-changed",
|
|
|
|
expected: "EverythingChanged",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
result := pascal(tt.input)
|
|
|
|
require.Equal(t, tt.expected, result)
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 00:02:56 +02:00
|
|
|
|
|
|
|
func Test_isStringType(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input schema.Type
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{"string", schema.StringType, true},
|
|
|
|
{"int", schema.IntType, false},
|
|
|
|
{"Input[string]", &schema.InputType{ElementType: schema.StringType}, true},
|
|
|
|
{"Input[int]", &schema.InputType{ElementType: schema.IntType}, false},
|
|
|
|
{"StrictStringEnum", &schema.EnumType{ElementType: schema.StringType}, true},
|
|
|
|
{"StrictIntEnum", &schema.EnumType{ElementType: schema.IntType}, false},
|
|
|
|
{"RelaxedStringEnum", &schema.UnionType{
|
|
|
|
ElementTypes: []schema.Type{&schema.EnumType{ElementType: schema.StringType}, schema.StringType},
|
|
|
|
}, true},
|
|
|
|
{"RelaxedIntEnum", &schema.UnionType{
|
|
|
|
ElementTypes: []schema.Type{&schema.EnumType{ElementType: schema.IntType}, schema.IntType},
|
|
|
|
}, false},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := isStringType(tt.input); got != tt.expected {
|
|
|
|
t.Errorf("isStringType() = %v, want %v", got, tt.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|