mirror of https://github.com/pulumi/pulumi.git
90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package python
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2"
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/internal/test"
|
|
)
|
|
|
|
var testdataPath = filepath.Join("..", "internal", "test", "testdata")
|
|
|
|
func TestGenProgram(t *testing.T) {
|
|
files, err := ioutil.ReadDir(testdataPath)
|
|
if err != nil {
|
|
t.Fatalf("could not read test data: %v", err)
|
|
}
|
|
|
|
for _, f := range files {
|
|
if filepath.Ext(f.Name()) != ".pp" {
|
|
continue
|
|
}
|
|
|
|
expectNYIDiags := false
|
|
if filepath.Base(f.Name()) == "aws-s3-folder.pp" {
|
|
expectNYIDiags = true
|
|
}
|
|
|
|
t.Run(f.Name(), func(t *testing.T) {
|
|
path := filepath.Join(testdataPath, f.Name())
|
|
contents, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("could not read %v: %v", path, err)
|
|
}
|
|
expected, err := ioutil.ReadFile(path + ".py")
|
|
if err != nil {
|
|
t.Fatalf("could not read %v: %v", path+".py", err)
|
|
}
|
|
|
|
parser := syntax.NewParser()
|
|
err = parser.ParseFile(bytes.NewReader(contents), f.Name())
|
|
if err != nil {
|
|
t.Fatalf("could not read %v: %v", path, err)
|
|
}
|
|
if parser.Diagnostics.HasErrors() {
|
|
t.Fatalf("failed to parse files: %v", parser.Diagnostics)
|
|
}
|
|
|
|
program, diags, err := hcl2.BindProgram(parser.Files, hcl2.PluginHost(test.NewHost(testdataPath)))
|
|
if err != nil {
|
|
t.Fatalf("could not bind program: %v", err)
|
|
}
|
|
if diags.HasErrors() {
|
|
t.Fatalf("failed to bind program: %v", diags)
|
|
}
|
|
|
|
files, diags, err := GenerateProgram(program)
|
|
assert.NoError(t, err)
|
|
if expectNYIDiags {
|
|
var tmpDiags hcl.Diagnostics
|
|
for _, d := range diags {
|
|
if !strings.HasPrefix(d.Summary, "not yet implemented") {
|
|
tmpDiags = append(tmpDiags, d)
|
|
}
|
|
}
|
|
diags = tmpDiags
|
|
}
|
|
if diags.HasErrors() {
|
|
t.Fatalf("failed to generate program: %v", diags)
|
|
}
|
|
|
|
if os.Getenv("PULUMI_ACCEPT") != "" {
|
|
err := ioutil.WriteFile(path+".py", files["__main__.py"], 0600)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
assert.Equal(t, string(expected), string(files["__main__.py"]))
|
|
})
|
|
}
|
|
}
|