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"
|
|
|
|
"io/ioutil"
|
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-15 18:49:36 +02:00
|
|
|
test.TestSDKCodegen(t, "nodejs", GeneratePackage, typeCheckGeneratedPackage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func typeCheckGeneratedPackage(t *testing.T, pwd string) {
|
|
|
|
var err error
|
|
|
|
var npm string
|
|
|
|
npm, err = executable.FindExecutable("npm")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmdOptions := integration.ProgramTestOptions{
|
|
|
|
Verbose: true,
|
|
|
|
Stderr: &stderr,
|
|
|
|
Stdout: &stdout,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
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-15 18:49:36 +02:00
|
|
|
err = integration.RunCommand(t, "typecheck ts",
|
|
|
|
[]string{filepath.Join(".", "node_modules", ".bin", "tsc"), "--noEmit"}, pwd, &cmdOptions)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|