pulumi/pkg/codegen/internal/test/sdk_driver.go
Pat Gavlin 4d1ca1596b
[codegen/schema] YAML {un,}marshaling support. (#7509)
These changes add support for unmarshaling and marshaling package
schemas using YAML instead of JSON. Language-specific data is
canonically JSON. Users of the `*Spec` types will need to update the
types of the the their `Language` values to use the new
`schema.RawMessage` type instead of `json.RawMessage`: the former has
support for YAML while the latter does not.
2021-07-13 16:41:40 -07:00

116 lines
3 KiB
Go

package test
import (
"os"
"path/filepath"
"testing"
"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/stretchr/testify/assert"
)
type sdkTest struct {
Directory string
Description string
Skip codegen.StringSet
}
var sdkTests = []sdkTest{
{
Directory: "external-resource-schema",
Description: "External resource schema",
},
{
Directory: "nested-module",
Description: "Nested module",
},
{
Directory: "nested-module-thirdparty",
Description: "Third-party nested module",
},
{
Directory: "plain-schema-gh6957",
Description: "Repro for #6957",
},
{
Directory: "resource-args-python",
Description: "Resource args with same named resource and type",
},
{
Directory: "simple-enum-schema",
Description: "Simple schema with enum types",
},
{
Directory: "simple-plain-schema",
Description: "Simple schema with plain properties",
},
{
Directory: "simple-plain-schema-with-root-package",
Description: "Simple schema with root package set",
},
{
Directory: "simple-resource-schema",
Description: "Simple schema with local resource properties",
},
{
Directory: "simple-resource-schema-custom-pypackage-name",
Description: "Simple schema with local resource properties and custom Python package name",
},
{
Directory: "simple-methods-schema",
Description: "Simple schema with methods",
Skip: codegen.NewStringSet("docs", "dotnet"),
},
{
Directory: "simple-yaml-schema",
Description: "Simple schema encoded using YAML",
},
}
// TestSDKCodegen runs the complete set of SDK code generation tests against a particular language's code generator.
//
// An SDK code generation test consists of a schema and a set of expected outputs for each language. Each test is
// structured as a directory that contains that information:
//
// test-directory/
// schema.(json|yaml)
// language-0
// ...
// language-n
//
// The schema is the only piece that must be manually authored. Once the schema has been written, the expected outputs
// can be generated by running `PULUMI_ACCEPT=true go test ./..." from the `pkg/codegen` directory.
func TestSDKCodegen(t *testing.T, language string, genPackage GenPkgSignature) {
testDir := filepath.Join("..", "internal", "test", "testdata")
for _, tt := range sdkTests {
t.Run(tt.Description, func(t *testing.T) {
if tt.Skip.Has(language) {
t.Skip()
return
}
dirPath := filepath.Join(testDir, filepath.FromSlash(tt.Directory))
schemaPath := filepath.Join(dirPath, "schema.json")
if _, err := os.Stat(schemaPath); err != nil && os.IsNotExist(err) {
schemaPath = filepath.Join(dirPath, "schema.yaml")
}
files, err := GeneratePackageFilesFromSchema(schemaPath, genPackage)
assert.NoError(t, err)
dir := filepath.Join(testDir, tt.Directory)
if RewriteFilesWhenPulumiAccept(t, dir, language, files) {
return
}
expectedFiles, err := LoadBaseline(dir, language)
assert.NoError(t, err)
ValidateFileEquality(t, files, expectedFiles)
})
}
}