pulumi/pkg/codegen/python/gen_program_test.go
Pat Gavlin 2f22c1c59c
HCL2 updates (#4309)
Pulumi HCL2 IR:
- Add support for invokes
- Add support for resource options, incl. ranged resources
- Allow the apply rewriter to ignore promise-typed values
- Add tests for the binder
- Add support functions for TF: entries and range

NodeJS codegen:
- Simplify for expression codegen
- Add support for invoke codegen
- Add support for entries and range functions
- Add tests

Python codegen:
- Implement codegen for most expression types
- Add support for invoke codegen
- Add tests
2020-04-06 19:43:16 -07:00

65 lines
1.6 KiB
Go

package python
import (
"bytes"
"io/ioutil"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/pulumi/pulumi/pkg/codegen/hcl2"
"github.com/pulumi/pulumi/pkg/codegen/hcl2/syntax"
"github.com/pulumi/pulumi/pkg/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
}
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, 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 diags.HasErrors() {
t.Fatalf("failed to bind program: %v", diags)
}
assert.Equal(t, string(expected), string(files["__main__.py"]))
})
}
}