github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/go/gen_program_test.go (about) 1 package gen 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 7 "path/filepath" 8 "testing" 9 10 "github.com/hashicorp/hcl/v2" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 14 "github.com/pulumi/pulumi/pkg/v3/codegen" 15 "github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model/format" 16 "github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax" 17 "github.com/pulumi/pulumi/pkg/v3/codegen/pcl" 18 "github.com/pulumi/pulumi/pkg/v3/codegen/testing/test" 19 "github.com/pulumi/pulumi/pkg/v3/codegen/testing/utils" 20 ) 21 22 var testdataPath = filepath.Join("..", "testing", "test", "testdata") 23 24 func TestGenerateProgramVersionSelection(t *testing.T) { 25 t.Parallel() 26 27 expectedVersion := map[string]test.PkgVersionInfo{ 28 "aws-resource-options-4.26": { 29 Pkg: "github.com/pulumi/pulumi-aws/sdk/v4", 30 OpAndVersion: "v4.26.0", 31 }, 32 "aws-resource-options-5.16.2": { 33 Pkg: "github.com/pulumi/pulumi-aws/sdk/v5", 34 OpAndVersion: "v5.16.2", 35 }, 36 "modpath": { 37 Pkg: "git.example.org/thirdparty/sdk", 38 OpAndVersion: "v0.1.0", 39 }, 40 } 41 42 test.TestProgramCodegen(t, 43 test.ProgramCodegenOptions{ 44 Language: "go", 45 Extension: "go", 46 OutputFile: "main.go", 47 Check: func(t *testing.T, path string, dependencies codegen.StringSet) { 48 Check(t, path, dependencies, "../../../../../../../sdk") 49 }, 50 GenProgram: func(program *pcl.Program) (map[string][]byte, hcl.Diagnostics, error) { 51 // Prevent tests from interfering with each other 52 return GenerateProgramWithOptions(program, GenerateProgramOptions{ExternalCache: NewCache()}) 53 }, 54 TestCases: []test.ProgramTest{ 55 { 56 Directory: "aws-resource-options-4.26", 57 Description: "Resource Options", 58 }, 59 { 60 Directory: "aws-resource-options-5.16.2", 61 Description: "Resource Options", 62 }, 63 { 64 Directory: "modpath", 65 Description: "Check that modpath is respected", 66 MockPluginVersions: map[string]string{ 67 "other": "0.1.0", 68 }, 69 // We don't compile because the test relies on the `other` package, 70 // which does not exist. 71 SkipCompile: codegen.NewStringSet("go"), 72 }, 73 }, 74 75 IsGenProject: true, 76 GenProject: GenerateProject, 77 ExpectedVersion: expectedVersion, 78 DependencyFile: "go.mod", 79 }) 80 } 81 82 func TestCollectImports(t *testing.T) { 83 t.Parallel() 84 85 g := newTestGenerator(t, filepath.Join("aws-s3-logging-pp", "aws-s3-logging.pp")) 86 pulumiImports := codegen.NewStringSet() 87 stdImports := codegen.NewStringSet() 88 preambleHelperMethods := codegen.NewStringSet() 89 g.collectImports(g.program, stdImports, pulumiImports, preambleHelperMethods) 90 stdVals := stdImports.SortedValues() 91 pulumiVals := pulumiImports.SortedValues() 92 assert.Equal(t, 0, len(stdVals)) 93 assert.Equal(t, 1, len(pulumiVals)) 94 assert.Equal(t, "\"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/s3\"", pulumiVals[0]) 95 } 96 97 func newTestGenerator(t *testing.T, testFile string) *generator { 98 path := filepath.Join(testdataPath, testFile) 99 contents, err := ioutil.ReadFile(path) 100 require.NoErrorf(t, err, "could not read %v: %v", path, err) 101 102 parser := syntax.NewParser() 103 err = parser.ParseFile(bytes.NewReader(contents), filepath.Base(path)) 104 if err != nil { 105 t.Fatalf("could not read %v: %v", path, err) 106 } 107 if parser.Diagnostics.HasErrors() { 108 t.Fatalf("failed to parse files: %v", parser.Diagnostics) 109 } 110 111 program, diags, err := pcl.BindProgram(parser.Files, pcl.PluginHost(utils.NewHost(testdataPath))) 112 if err != nil { 113 t.Fatalf("could not bind program: %v", err) 114 } 115 if diags.HasErrors() { 116 t.Fatalf("failed to bind program: %v", diags) 117 } 118 119 g := &generator{ 120 program: program, 121 jsonTempSpiller: &jsonSpiller{}, 122 ternaryTempSpiller: &tempSpiller{}, 123 readDirTempSpiller: &readDirSpiller{}, 124 splatSpiller: &splatSpiller{}, 125 optionalSpiller: &optionalSpiller{}, 126 scopeTraversalRoots: codegen.NewStringSet(), 127 arrayHelpers: make(map[string]*promptToInputArrayHelper), 128 } 129 g.Formatter = format.NewFormatter(g) 130 return g 131 }