Skip to content

Commit

Permalink
Fix Clue Mock Gen package name conflict (#259)
Browse files Browse the repository at this point in the history
- Fix a bug where an alias is not used for the mocked package if it
  conflicts with one of the other intial imports (this was discovered
  with a package named `testing`, but it looks like it could also come
  up if it was named `mock` which would conflict with
  `goa.design/clue/mock`.
- Bump version so it will be correct for a new release.
  • Loading branch information
douglaswth authored Jul 21, 2023
1 parent 0005d44 commit b014793
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 13 deletions.
7 changes: 7 additions & 0 deletions mock/cmd/cmg/pkg/generate/_tests/conflicts/conflicts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package testing

type (
Conflicts interface {
Simple(c *Conflicts) *Conflicts
}
)
52 changes: 52 additions & 0 deletions mock/cmd/cmg/pkg/generate/_tests/conflicts/mocks/conflicts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions mock/cmd/cmg/pkg/generate/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,20 @@ var (

func NewMocks(prefix string, p parse.Package, interfaces []parse.Interface, toolVersionFunc ToolVersionFunc) Mocks {
var (
m = &mocks{
pkgName: prefix + p.Name(),
pkgPath: p.PkgPath(),
pkgImport: newImport(p.PkgPath(), p.Name()),
toolVersionFunc: toolVersionFunc,
}
stdImports = importMap{"testing": newImport("testing")}
extImports = importMap{"mock": newImport("goa.design/clue/mock")}
intImports = make(importMap)
modPath = p.ModPath()
typeNames = make(typeMap)
typeZeros = make(typeMap)
m = &mocks{
pkgName: prefix + p.Name(),
pkgPath: p.PkgPath(),
pkgImport: addImport(newImport(p.PkgPath(), p.Name()), stdImports, extImports, intImports, modPath),
toolVersionFunc: toolVersionFunc,
}
typeNames = make(typeMap)
typeZeros = make(typeMap)
)

addImport(m.pkgImport, stdImports, extImports, intImports, modPath)

for _, i := range interfaces {
m.interfaces = append(m.interfaces, newInterface(i, typeNames, typeZeros, stdImports, extImports, intImports, modPath))
}
Expand Down
4 changes: 2 additions & 2 deletions mock/cmd/cmg/pkg/generate/mocks.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ type (
{{ end }}
func {{ .Constructor }}{{ .TypeParameters }}(t *testing.T) *{{ .Name }}{{ .TypeParameterVars }} {
var (
{{ .Var | printf ($import.PkgName | .ConstructorFmt) }} = &{{ .Name }}{{ .TypeParameterVars }}{mock.New(), t}
_ {{ $import.PkgName }}.{{ .Name }}{{ .TypeParameterVars }} = m
{{ .Var | printf ($import.AliasOrPkgName | .ConstructorFmt) }} = &{{ .Name }}{{ .TypeParameterVars }}{mock.New(), t}
_ {{ $import.AliasOrPkgName }}.{{ .Name }}{{ .TypeParameterVars }} = m
)
return {{ .Var }}
}
Expand Down
5 changes: 5 additions & 0 deletions mock/cmd/cmg/pkg/generate/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func TestMocks_Render(t *testing.T) {
Pattern: "./extensive",
ExpectedFiles: []string{"extensive.go"},
},
{
Name: "conflicts",
Pattern: "./conflicts",
ExpectedFiles: []string{"conflicts.go"},
},
}

for _, tc := range cases {
Expand Down
2 changes: 1 addition & 1 deletion mock/cmd/cmg/pkg/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const (
// Major version number
Major = 0
// Minor version number
Minor = 17
Minor = 18
// Build number
Build = 1
// Suffix - set to empty string in release tag commits.
Expand Down

0 comments on commit b014793

Please sign in to comment.