github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/go/test.go (about) 1 package gen 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/hashicorp/hcl/v2" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/pulumi/pulumi/pkg/v3/codegen" 14 "github.com/pulumi/pulumi/pkg/v3/codegen/pcl" 15 "github.com/pulumi/pulumi/pkg/v3/codegen/testing/test" 16 "github.com/pulumi/pulumi/pkg/v3/testing/integration" 17 "github.com/pulumi/pulumi/sdk/v3/go/common/util/executable" 18 ) 19 20 func Check(t *testing.T, path string, deps codegen.StringSet, pulumiSDKPath string) { 21 dir := filepath.Dir(path) 22 ex, err := executable.FindExecutable("go") 23 require.NoError(t, err) 24 25 // We remove go.mod to ensure tests are reproducible. 26 goMod := filepath.Join(dir, "go.mod") 27 if err = os.Remove(goMod); !os.IsNotExist(err) { 28 require.NoError(t, err) 29 } 30 err = integration.RunCommand(t, "generate go.mod", 31 []string{ex, "mod", "init", "main"}, 32 dir, &integration.ProgramTestOptions{}) 33 require.NoError(t, err) 34 err = integration.RunCommand(t, "go tidy", 35 []string{ex, "mod", "tidy"}, 36 dir, &integration.ProgramTestOptions{}) 37 require.NoError(t, err) 38 if pulumiSDKPath != "" { 39 err = integration.RunCommand(t, "point towards local Go SDK", 40 []string{ex, "mod", "edit", 41 fmt.Sprintf("--replace=%s=%s", 42 "github.com/pulumi/pulumi/sdk/v3", 43 pulumiSDKPath)}, 44 dir, &integration.ProgramTestOptions{}) 45 require.NoError(t, err) 46 } 47 TypeCheck(t, path, deps, pulumiSDKPath) 48 } 49 50 func TypeCheck(t *testing.T, path string, deps codegen.StringSet, pulumiSDKPath string) { 51 dir := filepath.Dir(path) 52 ex, err := executable.FindExecutable("go") 53 require.NoError(t, err) 54 55 err = integration.RunCommand(t, "go tidy after replace", 56 []string{ex, "mod", "tidy"}, 57 dir, &integration.ProgramTestOptions{}) 58 require.NoError(t, err) 59 60 err = integration.RunCommand(t, "test build", []string{ex, "build", "-v", "all"}, 61 dir, &integration.ProgramTestOptions{}) 62 require.NoError(t, err) 63 os.Remove(filepath.Join(dir, "main")) 64 assert.NoError(t, err) 65 } 66 67 func GenerateProgramBatchTest(t *testing.T, testCases []test.ProgramTest) { 68 test.TestProgramCodegen(t, 69 test.ProgramCodegenOptions{ 70 Language: "go", 71 Extension: "go", 72 OutputFile: "main.go", 73 Check: func(t *testing.T, path string, dependencies codegen.StringSet) { 74 Check(t, path, dependencies, "../../../../../../../sdk") 75 }, 76 GenProgram: func(program *pcl.Program) (map[string][]byte, hcl.Diagnostics, error) { 77 // Prevent tests from interfering with each other 78 return GenerateProgramWithOptions(program, GenerateProgramOptions{ExternalCache: NewCache()}) 79 }, 80 TestCases: testCases, 81 }) 82 }