diff --git a/pkg/codegen/report/report.go b/pkg/codegen/report/report.go index b6f01575e63d..820d96b3b964 100644 --- a/pkg/codegen/report/report.go +++ b/pkg/codegen/report/report.go @@ -38,7 +38,7 @@ type GenerateProgramFn func(*hcl2.Program) (map[string][]byte, hcl.Diagnostics, type Reporter interface { io.Closer // Report a call to GenerateProgram. - Report(title, language string, files []*syntax.File, fn GenerateProgramFn) GenerateProgramFn + Report(title, language string, files []*syntax.File, diags hcl.Diagnostics, err error) Summary() Summary } @@ -108,17 +108,17 @@ func (r *reporter) getLanguage(lang string) *Language { return l } -// Report a new call to GenerateProgram. Every call should be reported. -func (r *reporter) Report(title, language string, files []*syntax.File, f GenerateProgramFn) GenerateProgramFn { +func WrapGen(reporter Reporter, title, language string, files []*syntax.File, f GenerateProgramFn) GenerateProgramFn { return func(p *hcl2.Program) (m map[string][]byte, diags hcl.Diagnostics, err error) { defer func() { - r.report(title, language, files, diags, err) + reporter.Report(title, language, files, diags, err) }() m, diags, err = f(p) return m, diags, err } } -func (r *reporter) report(title, language string, files []*syntax.File, diags hcl.Diagnostics, err error) { + +func (r *reporter) Report(title, language string, files []*syntax.File, diags hcl.Diagnostics, err error) { r.m.Lock() defer r.m.Unlock() if panicErr := recover(); panicErr != nil { @@ -148,6 +148,9 @@ func (r *reporter) report(title, language string, files []*syntax.File, diags hc } if err != nil { err := fmt.Sprintf("error: %v", err) + if lang.GoErrors == nil { + lang.GoErrors = map[string]string{} + } lang.GoErrors[title] = err } diff --git a/pkg/codegen/report/report_test.go b/pkg/codegen/report/report_test.go index 03e89930eb29..f26ced8cb185 100644 --- a/pkg/codegen/report/report_test.go +++ b/pkg/codegen/report/report_test.go @@ -2,6 +2,7 @@ package report_test import ( "bytes" + "path/filepath" "testing" "github.com/pulumi/pulumi/pkg/v3/codegen/dotnet" @@ -9,11 +10,14 @@ import ( "github.com/pulumi/pulumi/pkg/v3/codegen/nodejs" "github.com/pulumi/pulumi/pkg/v3/codegen/pcl" "github.com/pulumi/pulumi/pkg/v3/codegen/report" + "github.com/pulumi/pulumi/pkg/v3/codegen/testing/utils" "github.com/pulumi/pulumi/pkg/v3/version" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +var testdataPath = filepath.Join("..", "testing", "test", "testdata") + func TestReportExample(t *testing.T) { t.Parallel() @@ -26,18 +30,21 @@ func TestReportExample(t *testing.T) { }{ {"Our basic bucket", `resource bucket "aws:s3:BucketV2" { }`}, {"A resource group", `resource group "azure:core:ResourceGroup" { location: "westus2" }`}, + {"Might not bind", `resource foo "not:a:Resource" { foo: "bar" }`}, } for _, example := range examples { parser := syntax.NewParser() err := parser.ParseFile(bytes.NewReader([]byte(example.body)), example.title) require.NoError(t, err, "parse failed") - program, diags, err := pcl.BindProgram(parser.Files) - require.NoError(t, err) - require.False(t, diags.HasErrors(), diags) + program, diags, err := pcl.BindProgram(parser.Files, pcl.PluginHost(utils.NewHost(testdataPath))) + if err != nil || diags.HasErrors() { + reporter.Report(example.title, "", parser.Files, diags, err) + continue + } langs := []string{"dotnet", "nodejs"} for i, genFn := range []report.GenerateProgramFn{dotnet.GenerateProgram, nodejs.GenerateProgram} { - program, diags, err := reporter.Report(example.title, langs[i], parser.Files, genFn)(program) + program, diags, err := report.WrapGen(reporter, example.title, langs[i], parser.Files, genFn)(program) handleAsNormal(program, diags, err) } } @@ -45,10 +52,25 @@ func TestReportExample(t *testing.T) { assert.Equal(t, report.Summary{ Name: "example", Stats: report.Stats{ - NumConversions: 4, + NumConversions: 5, Successes: 4, }, Languages: map[string]*report.Language{ + "": { + Stats: report.Stats{ + NumConversions: 1, + Successes: 0, + }, + GoErrors: map[string]string{ + "Might not bind": "error: could not locate a compatible plugin in " + + "deploytest, the makefile and & constructor of the plugin host " + + "must define the location of the schema: failed " + + "to locate compatible plugin", + }, + Files: map[string][]report.File{ + "Might not bind": {{Name: "Might not bind", Body: "resource foo \"not:a:Resource\" { foo: \"bar\" }"}}, + }, + }, "dotnet": { Stats: report.Stats{ NumConversions: 2,