Skip to content

Commit

Permalink
fixup! Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Oct 18, 2022
1 parent 15f9858 commit 7785f31
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
13 changes: 8 additions & 5 deletions pkg/codegen/report/report.go
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
32 changes: 27 additions & 5 deletions pkg/codegen/report/report_test.go
Expand Up @@ -2,18 +2,22 @@ package report_test

import (
"bytes"
"path/filepath"
"testing"

"github.com/pulumi/pulumi/pkg/v3/codegen/dotnet"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
"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()

Expand All @@ -26,29 +30,47 @@ 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)
}
}

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,
Expand Down

0 comments on commit 7785f31

Please sign in to comment.