pulumi/pkg/codegen/go/test.go

87 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) {
Move codegen testdata (#15549) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> It appears that Go copies testdata into every GOMODCACHE of a project that depends on pulumi/pkg; the schemas in codegen testdata add 300MB of weight to the GOMODCACHE needed for download. What if we moved the testdata out from under the tree. The move looks like this: ``` from=pkg/codegen/testing/test/testdata to=tests/testdata/codegen/ git mv "$from" "$to" (cd pkg/codegen/testing/test && ln -s ../../../../tests/testdata/codegen ./testdata) git add "$from" ``` The previous location is symlinked to the new location. Evidence of `GOMODCACHE` pressure reduction: https://gist.github.com/t0yv0/05dd8be5880171045aed01e123ae2b09 ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-03-06 20:36:50 +00:00
pulumiSdkPath, err := filepath.Abs(filepath.Join("..", "..", "..", "sdk"))
require.NoError(t, err)
test.TestProgramCodegen(t,
test.ProgramCodegenOptions{
Language: "go",
Extension: "go",
OutputFile: "main.go",
Check: func(t *testing.T, path string, dependencies codegen.StringSet) {
Move codegen testdata (#15549) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> It appears that Go copies testdata into every GOMODCACHE of a project that depends on pulumi/pkg; the schemas in codegen testdata add 300MB of weight to the GOMODCACHE needed for download. What if we moved the testdata out from under the tree. The move looks like this: ``` from=pkg/codegen/testing/test/testdata to=tests/testdata/codegen/ git mv "$from" "$to" (cd pkg/codegen/testing/test && ln -s ../../../../tests/testdata/codegen ./testdata) git add "$from" ``` The previous location is symlinked to the new location. Evidence of `GOMODCACHE` pressure reduction: https://gist.github.com/t0yv0/05dd8be5880171045aed01e123ae2b09 ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-03-06 20:36:50 +00:00
Check(t, path, dependencies, pulumiSdkPath)
},
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,
})
}