2020-05-19 10:18:38 +02:00
|
|
|
package dotnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2020-06-10 19:21:53 +02:00
|
|
|
"strings"
|
2020-05-19 10:18:38 +02:00
|
|
|
"testing"
|
|
|
|
|
2020-06-10 19:21:53 +02:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2020-05-19 10:18:38 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-03-17 14:20:05 +01:00
|
|
|
"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"
|
2020-05-19 10:18:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:21:53 +02:00
|
|
|
expectNYIDiags := false
|
|
|
|
if filepath.Base(f.Name()) == "aws-s3-folder.pp" {
|
|
|
|
expectNYIDiags = true
|
|
|
|
}
|
|
|
|
|
2020-05-19 10:18:38 +02:00
|
|
|
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 + ".cs")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not read %v: %v", path+".cs", 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)
|
2020-06-10 19:21:53 +02:00
|
|
|
|
|
|
|
if expectNYIDiags {
|
|
|
|
var tmpDiags hcl.Diagnostics
|
|
|
|
for _, d := range diags {
|
|
|
|
if !strings.HasPrefix(d.Summary, "not yet implemented") {
|
|
|
|
tmpDiags = append(tmpDiags, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
diags = tmpDiags
|
|
|
|
}
|
2020-05-19 10:18:38 +02:00
|
|
|
if diags.HasErrors() {
|
2020-06-10 19:21:53 +02:00
|
|
|
t.Fatalf("failed to generate program: %v", diags)
|
2020-05-19 10:18:38 +02:00
|
|
|
}
|
|
|
|
assert.Equal(t, string(expected), string(files["MyStack.cs"]))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|