This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from hypnoglow/fix-mockgen-custom-import-paths
Fix mockgen for package name not matching import path
- Loading branch information
Showing
8 changed files
with
287 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"testing" | ||
) | ||
|
||
func TestFileParser_ParseFile(t *testing.T) { | ||
fs := token.NewFileSet() | ||
file, err := parser.ParseFile(fs, "tests/custom_package_name/greeter/greeter.go", nil, 0) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
p := fileParser{ | ||
fileSet: fs, | ||
imports: make(map[string]string), | ||
importedInterfaces: make(map[string]map[string]*ast.InterfaceType), | ||
} | ||
|
||
pkg, err := p.parseFile(file) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
checkGreeterImports(t, p.imports) | ||
|
||
expectedName := "greeter" | ||
if pkg.Name != expectedName { | ||
t.Fatalf("Expected name to be %v but got %v", expectedName, pkg.Name) | ||
} | ||
|
||
expectedInterfaceName := "InputMaker" | ||
if pkg.Interfaces[0].Name != expectedInterfaceName { | ||
t.Fatalf("Expected interface name to be %v but got %v", expectedInterfaceName, pkg.Interfaces[0].Name) | ||
} | ||
} | ||
|
||
func TestFileParser_ParsePackage(t *testing.T) { | ||
fs := token.NewFileSet() | ||
_, err := parser.ParseFile(fs, "tests/custom_package_name/greeter/greeter.go", nil, 0) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
p := fileParser{ | ||
fileSet: fs, | ||
imports: make(map[string]string), | ||
importedInterfaces: make(map[string]map[string]*ast.InterfaceType), | ||
} | ||
|
||
err = p.parsePackage("github.com/golang/mock/mockgen/tests/custom_package_name/greeter") | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
checkGreeterImports(t, p.imports) | ||
} | ||
|
||
func TestImportsOfFile(t *testing.T) { | ||
fs := token.NewFileSet() | ||
file, err := parser.ParseFile(fs, "tests/custom_package_name/greeter/greeter.go", nil, 0) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
imports := importsOfFile(file) | ||
checkGreeterImports(t, imports) | ||
} | ||
|
||
func checkGreeterImports(t *testing.T, imports map[string]string) { | ||
// check that imports have stdlib package "fmt" | ||
if fmtPackage, ok := imports["fmt"]; !ok { | ||
t.Errorf("Expected imports to have key \"fmt\"") | ||
} else { | ||
expectedFmtPackage := "fmt" | ||
if fmtPackage != expectedFmtPackage { | ||
t.Errorf("Expected fmt key to have value %s but got %s", expectedFmtPackage, fmtPackage) | ||
} | ||
} | ||
|
||
// check that imports have package named "validator" | ||
if validatorPackage, ok := imports["validator"]; !ok { | ||
t.Errorf("Expected imports to have key \"fmt\"") | ||
} else { | ||
expectedValidatorPackage := "github.com/golang/mock/mockgen/tests/custom_package_name/validator" | ||
if validatorPackage != expectedValidatorPackage { | ||
t.Errorf("Expected validator key to have value %s but got %s", expectedValidatorPackage, validatorPackage) | ||
} | ||
} | ||
|
||
// check that imports have package named "client" | ||
if clientPackage, ok := imports["client"]; !ok { | ||
t.Errorf("Expected imports to have key \"client\"") | ||
} else { | ||
expectedClientPackage := "github.com/golang/mock/mockgen/tests/custom_package_name/client/v1" | ||
if clientPackage != expectedClientPackage { | ||
t.Errorf("Expected client key to have value %s but got %s", expectedClientPackage, clientPackage) | ||
} | ||
} | ||
|
||
// check that imports don't have package named "v1" | ||
if _, ok := imports["v1"]; ok { | ||
t.Errorf("Expected import not to have key \"v1\"") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Tests for custom package names | ||
|
||
This directory contains test for mockgen generating mocks when imported package | ||
name does not match import path suffix. For example, package with name "client" | ||
is located under import path "github.com/golang/mock/mockgen/tests/custom_package_name/client/v1". | ||
|
||
Prior to this patch: | ||
|
||
$ go generate greeter/greeter.go | ||
2018/03/05 22:44:52 Loading input failed: greeter.go:17:11: failed parsing returns: greeter.go:17:14: unknown package "client" | ||
greeter/greeter.go:1: running "mockgen": exit status 1 | ||
|
||
This can be fixed by manually providing `-imports` flag, like `-imports client=github.com/golang/mock/mockgen/tests/custom_package_name/client/v1`. | ||
But, mockgen should be able to automatically resolve package names in such situations. | ||
|
||
With this patch applied: | ||
|
||
$ go generate greeter/greeter.go | ||
$ echo $? | ||
0 | ||
|
||
Mockgen runs successfully, produced output is equal to [greeter_mock_test.go](greeter/greeter_mock_test.go) content. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package client | ||
|
||
import "fmt" | ||
|
||
type Client struct{} | ||
|
||
func (c *Client) Greet(in GreetInput) string { | ||
return fmt.Sprintf("Hello, %s!", in.Name) | ||
} | ||
|
||
type GreetInput struct { | ||
Name string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:generate mockgen -source greeter.go -destination greeter_mock_test.go -package greeter | ||
|
||
package greeter | ||
|
||
import ( | ||
// stdlib import | ||
"fmt" | ||
|
||
// non-matching import suffix and package name | ||
"github.com/golang/mock/mockgen/tests/custom_package_name/client/v1" | ||
|
||
// matching import suffix and package name | ||
"github.com/golang/mock/mockgen/tests/custom_package_name/validator" | ||
) | ||
|
||
type InputMaker interface { | ||
MakeInput() client.GreetInput | ||
} | ||
|
||
type Greeter struct { | ||
InputMaker InputMaker | ||
Client *client.Client | ||
} | ||
|
||
func (g *Greeter) Greet() (string, error) { | ||
in := g.InputMaker.MakeInput() | ||
if err := validator.Validate(in.Name); err != nil { | ||
return "", fmt.Errorf("validation failed: %v", err) | ||
} | ||
return g.Client.Greet(in), nil | ||
} |
46 changes: 46 additions & 0 deletions
46
mockgen/tests/custom_package_name/greeter/greeter_mock_test.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package greeter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/golang/mock/mockgen/tests/custom_package_name/client/v1" | ||
) | ||
|
||
func TestGreeter_Greet(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
input := client.GreetInput{ | ||
Name: "Foo", | ||
} | ||
|
||
inputMaker := NewMockInputMaker(ctrl) | ||
inputMaker.EXPECT(). | ||
MakeInput(). | ||
Return(input) | ||
|
||
g := &Greeter{ | ||
InputMaker: inputMaker, | ||
Client: &client.Client{}, | ||
} | ||
|
||
greeting, err := g.Greet() | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
expected := "Hello, Foo!" | ||
if greeting != expected { | ||
t.Fatalf("Expected greeting to be %v but got %v", expected, greeting) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package validator | ||
|
||
func Validate(s string) error { | ||
return nil | ||
} |