pulumi/pkg/codegen/pcl/binder_test.go

88 lines
2.4 KiB
Go

package pcl_test
import (
"bytes"
"os"
"path/filepath"
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/test"
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/utils"
)
var testdataPath = filepath.Join("..", "testing", "test", "testdata")
func TestBindProgram(t *testing.T) {
t.Parallel()
testdata, err := os.ReadDir(testdataPath)
if err != nil {
t.Fatalf("could not read test data: %v", err)
}
bindOptions := map[string][]pcl.BindOption{}
for _, r := range test.PulumiPulumiProgramTests {
bindOptions[r.Directory+"-pp"] = r.BindOptions
}
//nolint:paralleltest // false positive because range var isn't used directly in t.Run(name) arg
for _, v := range testdata {
v := v
if !v.IsDir() {
continue
}
folderPath := filepath.Join(testdataPath, v.Name())
files, err := os.ReadDir(folderPath)
if err != nil {
t.Fatalf("could not read test data: %v", err)
}
for _, fileName := range files {
fileName := fileName.Name()
if filepath.Ext(fileName) != ".pp" {
continue
}
t.Run(fileName, func(t *testing.T) {
t.Parallel()
path := filepath.Join(folderPath, fileName)
contents, err := os.ReadFile(path)
require.NoErrorf(t, err, "could not read %v", path)
parser := syntax.NewParser()
err = parser.ParseFile(bytes.NewReader(contents), fileName)
require.NoErrorf(t, err, "could not read %v", path)
require.False(t, parser.Diagnostics.HasErrors(), "failed to parse files")
var bindError error
var diags hcl.Diagnostics
loader := pcl.Loader(schema.NewPluginLoader(utils.NewHost(testdataPath)))
absoluteFolderPath, err := filepath.Abs(folderPath)
if err != nil {
t.Fatalf("failed to bind program: unable to find the absolute path of %v", folderPath)
}
options := append(
bindOptions[v.Name()],
loader,
pcl.DirPath(absoluteFolderPath),
pcl.ComponentBinder(pcl.ComponentProgramBinderFromFileSystem()))
// PCL binder options are taken from program_driver.go
program, diags, bindError := pcl.BindProgram(parser.Files, options...)
assert.NoError(t, bindError)
if diags.HasErrors() || program == nil {
t.Fatalf("failed to bind program: %v", diags)
}
})
}
}
}