2020-05-14 17:43:12 +02:00
|
|
|
package gen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2020-06-16 08:00:02 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/v2/codegen"
|
2020-05-14 17:43:12 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/v2/codegen/hcl2"
|
2020-05-21 19:23:33 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/v2/codegen/hcl2/model/format"
|
2020-05-14 17:43:12 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/v2/codegen/hcl2/syntax"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v2/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
|
|
|
|
}
|
|
|
|
// TODO: include all test files
|
2020-06-06 03:52:00 +02:00
|
|
|
if filepath.Base(f.Name()) != "aws-s3-logging.pp" &&
|
2020-06-16 08:00:02 +02:00
|
|
|
filepath.Base(f.Name()) != "aws-s3-folder.pp" &&
|
2020-06-06 03:52:00 +02:00
|
|
|
filepath.Base(f.Name()) != "aws-fargate.pp" {
|
2020-05-14 17:43:12 +02:00
|
|
|
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 + ".go")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not read %v: %v", path+".go", 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 diags.HasErrors() {
|
2020-06-16 08:00:02 +02:00
|
|
|
t.Fatalf("failed to generate program: %v", diags)
|
2020-05-14 17:43:12 +02:00
|
|
|
}
|
|
|
|
assert.Equal(t, string(expected), string(files["main.go"]))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-05-21 19:23:33 +02:00
|
|
|
|
|
|
|
func TestCollectImports(t *testing.T) {
|
|
|
|
g := newTestGenerator(t, "aws-s3-logging.pp")
|
|
|
|
var index bytes.Buffer
|
2020-06-06 03:52:00 +02:00
|
|
|
stdImports, pulumiImports := g.collectImports(&index, g.program)
|
|
|
|
stdVals := stdImports.SortedValues()
|
|
|
|
pulumiVals := pulumiImports.SortedValues()
|
|
|
|
assert.Equal(t, 0, len(stdVals))
|
|
|
|
assert.Equal(t, 1, len(pulumiVals))
|
|
|
|
assert.Equal(t, "github.com/pulumi/pulumi-aws/sdk/v2/go/aws/s3", pulumiVals[0])
|
2020-05-21 19:23:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestGenerator(t *testing.T, testFile string) *generator {
|
|
|
|
files, err := ioutil.ReadDir(testdataPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not read test data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
if filepath.Base(f.Name()) != testFile {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(testdataPath, f.Name())
|
|
|
|
contents, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not read %v: %v", path, 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
g := &generator{
|
2020-06-16 08:00:02 +02:00
|
|
|
program: program,
|
|
|
|
jsonTempSpiller: &jsonSpiller{},
|
|
|
|
ternaryTempSpiller: &tempSpiller{},
|
|
|
|
readDirTempSpiller: &readDirSpiller{},
|
|
|
|
scopeTraversalRoots: codegen.NewStringSet(),
|
2020-05-21 19:23:33 +02:00
|
|
|
}
|
|
|
|
g.Formatter = format.NewFormatter(g)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
t.Fatalf("test file not found")
|
|
|
|
return nil
|
|
|
|
}
|