pulumi/pkg/codegen/go/test.go

85 lines
2.6 KiB
Go
Raw Normal View History

2022-03-16 18:42:30 +00:00
package gen
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/hashicorp/hcl/v2"
2022-03-16 18:42:30 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/test"
2022-03-16 18:42:30 +00:00
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/executable"
)
func Check(t *testing.T, path string, deps codegen.StringSet, pulumiSDKPath string) {
2022-03-16 18:42:30 +00:00
dir := filepath.Dir(path)
ex, err := executable.FindExecutable("go")
require.NoError(t, err)
// We remove go.mod to ensure tests are reproducible.
goMod := filepath.Join(dir, "go.mod")
if err = os.Remove(goMod); !os.IsNotExist(err) {
require.NoError(t, err)
}
err = integration.RunCommand(t, "generate go.mod",
[]string{ex, "mod", "init", "main"},
dir, &integration.ProgramTestOptions{})
require.NoError(t, err)
err = integration.RunCommand(t, "go tidy",
[]string{ex, "mod", "tidy"},
2022-03-16 18:42:30 +00:00
dir, &integration.ProgramTestOptions{})
require.NoError(t, err)
if pulumiSDKPath != "" {
err = integration.RunCommand(t, "point towards local Go SDK",
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
[]string{
ex, "mod", "edit",
2022-03-16 18:42:30 +00:00
fmt.Sprintf("--replace=%s=%s",
"github.com/pulumi/pulumi/sdk/v3",
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
pulumiSDKPath),
},
2022-03-16 18:42:30 +00:00
dir, &integration.ProgramTestOptions{})
require.NoError(t, err)
}
TypeCheck(t, path, deps, pulumiSDKPath)
}
func TypeCheck(t *testing.T, path string, deps codegen.StringSet, pulumiSDKPath string) {
dir := filepath.Dir(path)
ex, err := executable.FindExecutable("go")
require.NoError(t, err)
2022-03-16 18:42:30 +00:00
err = integration.RunCommand(t, "go tidy after replace",
[]string{ex, "mod", "tidy"},
2022-03-16 18:42:30 +00:00
dir, &integration.ProgramTestOptions{})
require.NoError(t, err)
err = integration.RunCommand(t, "test build", []string{ex, "build", "-v", "all"},
dir, &integration.ProgramTestOptions{})
require.NoError(t, err)
os.Remove(filepath.Join(dir, "main"))
assert.NoError(t, err)
}
func GenerateProgramBatchTest(t *testing.T, testCases []test.ProgramTest) {
test.TestProgramCodegen(t,
test.ProgramCodegenOptions{
Language: "go",
Extension: "go",
OutputFile: "main.go",
Check: func(t *testing.T, path string, dependencies codegen.StringSet) {
Check(t, path, dependencies, "../../../../../../../sdk")
},
GenProgram: func(program *pcl.Program) (map[string][]byte, hcl.Diagnostics, error) {
// Prevent tests from interfering with each other
return GenerateProgramWithOptions(program, GenerateProgramOptions{ExternalCache: NewCache()})
},
TestCases: testCases,
})
}