pulumi/pkg/codegen/dotnet/gen_test.go

119 lines
2.8 KiB
Go
Raw Permalink Normal View History

package dotnet
import (
"os"
"path/filepath"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/test"
)
func TestGeneratePackage(t *testing.T) {
t.Parallel()
test.TestSDKCodegen(t, &test.SDKCodegenOptions{
Fixes to pass dotnet conformance (#15987) <!--- 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. --> This isn't quite enough for dotnet conformance to pass on everything yet, but it's a start and gets the L1 programs passing. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] 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-04-19 10:21:09 +00:00
Language: "dotnet",
GenPackage: func(t string, p *schema.Package, e map[string][]byte) (map[string][]byte, error) {
return GeneratePackage(t, p, e, nil)
},
Checks: map[string]test.CodegenCheck{
"dotnet/compile": typeCheckGeneratedPackage,
"dotnet/test": testGeneratedPackage,
},
TestCases: test.PulumiPulumiSDKTests,
})
}
var buildMutex sync.Mutex
func typeCheckGeneratedPackage(t *testing.T, pwd string) {
versionPath := filepath.Join(pwd, "version.txt")
if _, err := os.Stat(versionPath); os.IsNotExist(err) {
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
err := os.WriteFile(versionPath, []byte("0.0.0\n"), 0o600)
require.NoError(t, err)
} else if err != nil {
require.NoError(t, err)
}
// dotnet build requires exclusive access to shared nuget package:
// https://github.com/pulumi/pulumi/runs/5436354735?check_suite_focus=true#step:36:277
buildMutex.Lock()
defer buildMutex.Unlock()
test.RunCommand(t, "dotnet build", pwd, "dotnet", "build")
}
func testGeneratedPackage(t *testing.T, pwd string) {
test.RunCommand(t, "dotnet build", pwd, "dotnet", "test")
}
func TestGenerateType(t *testing.T) {
t.Parallel()
cases := []struct {
typ schema.Type
expected string
}{
{
&schema.InputType{
ElementType: &schema.ArrayType{
ElementType: &schema.InputType{
ElementType: &schema.ArrayType{
ElementType: &schema.InputType{
ElementType: schema.NumberType,
},
},
},
},
},
"InputList<ImmutableArray<double>>",
},
{
&schema.InputType{
ElementType: &schema.MapType{
ElementType: &schema.InputType{
ElementType: &schema.ArrayType{
ElementType: &schema.InputType{
ElementType: schema.NumberType,
},
},
},
},
},
"InputMap<ImmutableArray<double>>",
},
}
mod := &modContext{mod: "main"}
//nolint:paralleltest // false positive because range var isn't used directly in t.Run(name) arg
for _, c := range cases {
c := c
t.Run(c.typ.String(), func(t *testing.T) {
t.Parallel()
typeString := mod.typeString(c.typ, "", true, false, false)
assert.Equal(t, c.expected, typeString)
})
}
}
func TestGenerateTypeNames(t *testing.T) {
t.Parallel()
test.TestTypeNameCodegen(t, "dotnet", func(pkg *schema.Package) test.TypeNameGeneratorFunc {
modules, _, err := generateModuleContextMap("test", pkg)
require.NoError(t, err)
root, ok := modules[""]
require.True(t, ok)
return func(t schema.Type) string {
return root.typeString(t, "", false, false, false)
}
})
}