github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/nodejs/test.go (about)

     1  package nodejs
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/pulumi/pulumi/pkg/v3/codegen"
    13  	"github.com/pulumi/pulumi/pkg/v3/codegen/testing/test"
    14  	"github.com/pulumi/pulumi/pkg/v3/testing/integration"
    15  )
    16  
    17  func Check(t *testing.T, path string, dependencies codegen.StringSet, linkLocal bool) {
    18  	dir := filepath.Dir(path)
    19  
    20  	removeFile := func(name string) {
    21  		path := filepath.Join(dir, name)
    22  		if err := os.Remove(path); !os.IsNotExist(err) {
    23  			require.NoError(t, err, "Failed to delete '%s'", path)
    24  		}
    25  	}
    26  
    27  	// We delete and regenerate package files for each run.
    28  	removeFile("yarn.lock")
    29  	removeFile("package.json")
    30  	removeFile("tsconfig.json")
    31  
    32  	// Write out package.json
    33  	pkgs := nodejsPackages(t, dependencies)
    34  	pkgInfo := npmPackage{
    35  		Dependencies: map[string]string{
    36  			"@pulumi/pulumi": "latest",
    37  		},
    38  		DevDependencies: map[string]string{
    39  			"@types/node": "^17.0.14",
    40  			"typescript":  "^4.5.5",
    41  		},
    42  	}
    43  	for pkg, v := range pkgs {
    44  		pkgInfo.Dependencies[pkg] = v
    45  	}
    46  	pkgJSON, err := json.MarshalIndent(pkgInfo, "", "    ")
    47  	require.NoError(t, err)
    48  	err = os.WriteFile(filepath.Join(dir, "package.json"), pkgJSON, 0600)
    49  	require.NoError(t, err)
    50  
    51  	tsConfig := map[string]string{}
    52  	tsConfigJSON, err := json.MarshalIndent(tsConfig, "", "    ")
    53  	require.NoError(t, err)
    54  	err = os.WriteFile(filepath.Join(dir, "tsconfig.json"), tsConfigJSON, 0600)
    55  	require.NoError(t, err)
    56  
    57  	TypeCheck(t, path, dependencies, linkLocal)
    58  }
    59  
    60  func TypeCheck(t *testing.T, path string, _ codegen.StringSet, linkLocal bool) {
    61  	dir := filepath.Dir(path)
    62  
    63  	typeCheckGeneratedPackage(t, dir, linkLocal)
    64  }
    65  
    66  func typeCheckGeneratedPackage(t *testing.T, pwd string, linkLocal bool) {
    67  	// NOTE: previous attempt used npm. It may be more popular and
    68  	// better target than yarn, however our build uses yarn in
    69  	// other places at the moment, and yarn does not run into the
    70  	// ${VERSION} problem; use yarn for now.
    71  
    72  	if linkLocal {
    73  		test.RunCommand(t, "yarn_link", pwd, "yarn", "link", "@pulumi/pulumi")
    74  	}
    75  	test.RunCommand(t, "yarn_install", pwd, "yarn", "install")
    76  	tscOptions := &integration.ProgramTestOptions{
    77  		// Avoid Out of Memory error on CI:
    78  		Env: []string{"NODE_OPTIONS=--max_old_space_size=4096"},
    79  	}
    80  	test.RunCommandWithOptions(t, tscOptions, "tsc", pwd, "yarn", "run", "tsc",
    81  		"--noEmit", "--skipLibCheck", "true", "--skipDefaultLibCheck", "true")
    82  }
    83  
    84  // Returns the nodejs equivalent to the hcl2 package names provided.
    85  func nodejsPackages(t *testing.T, deps codegen.StringSet) map[string]string {
    86  	result := make(map[string]string, len(deps))
    87  	for _, d := range deps.SortedValues() {
    88  		pkgName := fmt.Sprintf("@pulumi/%s", d)
    89  		set := func(pkgVersion string) {
    90  			result[pkgName] = "^" + pkgVersion
    91  		}
    92  		switch d {
    93  		case "aws":
    94  			set(test.AwsSchema)
    95  		case "azure-native":
    96  			set(test.AzureNativeSchema)
    97  		case "azure":
    98  			set(test.AzureSchema)
    99  		case "kubernetes":
   100  			set(test.KubernetesSchema)
   101  		case "random":
   102  			set(test.RandomSchema)
   103  		case "eks":
   104  			set(test.EksSchema)
   105  		default:
   106  			t.Logf("Unknown package requested: %s", d)
   107  		}
   108  
   109  	}
   110  	return result
   111  }
   112  
   113  func GenerateProgramBatchTest(t *testing.T, testCases []test.ProgramTest) {
   114  	test.TestProgramCodegen(t,
   115  		test.ProgramCodegenOptions{
   116  			Language:   "nodejs",
   117  			Extension:  "ts",
   118  			OutputFile: "index.ts",
   119  			Check: func(t *testing.T, path string, dependencies codegen.StringSet) {
   120  				Check(t, path, dependencies, true)
   121  			},
   122  			GenProgram: GenerateProgram,
   123  			TestCases:  testCases,
   124  		})
   125  }