Use importBasePath before name if specified (#8159)

* Use importBasePath before name if specified

This is a go specific code change. We should clarify in the docs how
`name`, `importBasePath` and `rootPackageName` interact.

* Update CHANGELOG_PENDING.md

* Test package naming

* Explain test and remove debugging print

* Comply with linter
This commit is contained in:
Ian Wahbe 2021-10-07 11:56:44 -07:00 committed by GitHub
parent 32695f7022
commit e1f0976667
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 4 deletions

View file

@ -9,3 +9,6 @@
[#8142](https://github.com/pulumi/pulumi/pull/8142)
### Bug Fixes
- [codegen/go] - Use `importBasePath` before `name` if specified
[#8159](https://github.com/pulumi/pulumi/pull/8159)

View file

@ -2974,12 +2974,17 @@ func GeneratePackage(tool string, pkg *schema.Package) (map[string][]byte, error
if name == "" {
name = goPackage(pkg.Name)
}
var pathPrefix string
if goPkgInfo.RootPackageName == "" {
if goPkgInfo.ImportBasePath != "" {
pathPrefix = path.Base(goPkgInfo.ImportBasePath)
} else {
pathPrefix = goPackage(pkg.Name)
}
}
files := map[string][]byte{}
setFile := func(relPath, contents string) {
if goPkgInfo.RootPackageName == "" {
relPath = path.Join(goPackage(name), relPath)
}
relPath = path.Join(pathPrefix, relPath)
if _, ok := files[relPath]; ok {
panic(errors.Errorf("duplicate file: %s", relPath))
}

View file

@ -1,8 +1,12 @@
package gen
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@ -130,3 +134,81 @@ func TestGenerateTypeNames(t *testing.T) {
}
})
}
func readSchemaFile(file string) *schema.Package {
// Read in, decode, and import the schema.
schemaBytes, err := ioutil.ReadFile(filepath.Join("..", "internal", "test", "testdata", file))
if err != nil {
panic(err)
}
var pkgSpec schema.PackageSpec
if err = json.Unmarshal(schemaBytes, &pkgSpec); err != nil {
panic(err)
}
pkg, err := schema.ImportSpec(pkgSpec, map[string]schema.Language{"go": Importer})
if err != nil {
panic(err)
}
return pkg
}
// We test the naming/module structure of generated packages.
func TestPackageNaming(t *testing.T) {
testCases := []struct {
importBasePath string
rootPackageName string
name string
expectedRoot string
}{
{
importBasePath: "github.com/pulumi/pulumi-azure-quickstart-acr-geo-replication/sdk/go/acr",
expectedRoot: "acr",
},
{
importBasePath: "github.com/ihave/animport",
rootPackageName: "root",
expectedRoot: "",
},
{
name: "named-package",
expectedRoot: "named",
},
}
for _, tt := range testCases {
t.Run(tt.expectedRoot, func(t *testing.T) {
// This schema is arbitrary. We just needed a filled out schema. All
// path decisions should be made based off of the Name and
// Language[go] fields (which we set after import).
schema := readSchemaFile(filepath.Join("schema", "good-enum-1.json"))
if tt.name != "" {
// We want there to be a name, so if one isn't provided we
// default to the schema.
schema.Name = tt.name
}
schema.Language = map[string]interface{}{
"go": GoPackageInfo{
ImportBasePath: tt.importBasePath,
RootPackageName: tt.rootPackageName,
},
}
files, err := GeneratePackage("test", schema)
require.NoError(t, err)
ordering := make([]string, len(files))
var i int
for k := range files {
ordering[i] = k
i++
}
ordering = sort.StringSlice(ordering)
require.NotEmpty(t, files, "This test only works when files are generated")
for _, k := range ordering {
root := strings.Split(k, "/")[0]
if tt.expectedRoot != "" {
require.Equal(t, tt.expectedRoot, root, "Root should precede all cases. Got file %s", k)
}
// We should work on a way to assert this is one level higher then it otherwise would be.
}
})
}
}