Skip to content

Commit 1e73a68

Browse files
author
nieml
committed
init
0 parents  commit 1e73a68

File tree

76 files changed

+4540
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+4540
-0
lines changed

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/ssoor/implgen
2+
3+
go 1.13
4+
5+
require (
6+
github.com/golang/mock v1.4.3
7+
golang.org/x/mod v0.3.0
8+
golang.org/x/tools v0.0.0-20200612220849-54c614fe050c
9+
)

go.sum

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
github.com/golang/mock v1.4.3 h1:GV+pQPG/EUUbkh47niozDcADz6go/dUwhVzdUQHIVRw=
2+
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
3+
github.com/yuin/goldmark v1.1.27 h1:nqDD4MMMQA0lmWq03Z2/myGPYLQoXtmi0rGVs95ntbo=
4+
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
5+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
6+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
7+
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
8+
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
9+
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
10+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
11+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
12+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
13+
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
14+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
15+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
16+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
17+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
18+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
19+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
20+
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
21+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
22+
golang.org/x/tools v0.0.0-20200612220849-54c614fe050c h1:g6oFfz6Cmw68izP3xsdud3Oxu145IPkeFzyRg58AKHM=
23+
golang.org/x/tools v0.0.0-20200612220849-54c614fe050c/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
24+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
25+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
26+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
27+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
28+
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
29+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Embedded Interfaces in aux_files
2+
3+
Embedded interfaces in `aux_files` generate `unknown embedded interface XXX` errors.
4+
See below for example of the problem:
5+
6+
```go
7+
// source
8+
import (
9+
alias "some.org/package/imported"
10+
)
11+
12+
type Source interface {
13+
alias.Foreign
14+
}
15+
```
16+
17+
```go
18+
// some.org/package/imported
19+
type Foreign interface {
20+
Embedded
21+
}
22+
23+
type Embedded interface {}
24+
```
25+
26+
Attempting to generate a mock will result in an `unknown embedded interface Embedded`.
27+
The issue is that the `fileParser` stores `auxInterfaces` underneath the package name
28+
explicitly specified in the `aux_files` flag.
29+
30+
In the `parseInterface` method, there is an incorrect assumption about an embedded interface
31+
always being in the source file.
32+
33+
```go
34+
case *ast.Ident:
35+
// Embedded interface in this package.
36+
ei := p.auxInterfaces[""][v.String()]
37+
if ei == nil {
38+
return nil, p.errorf(v.Pos(), "unknown embedded interface %s", v.String())
39+
}
40+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:generate mockgen -aux_files faux=faux/faux.go -destination bugreport_mock.go -package bugreport -source=bugreport.go Example
2+
3+
package bugreport
4+
5+
import (
6+
"log"
7+
8+
"github.com/ssoor/implgen/internal/tests/aux_imports_embedded_interface/faux"
9+
)
10+
11+
// Source is an interface w/ an embedded foreign interface
12+
type Source interface {
13+
faux.Foreign
14+
}
15+
16+
func CallForeignMethod(s Source) {
17+
log.Println(s.Method())
18+
}

internal/tests/aux_imports_embedded_interface/bugreport_mock.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package bugreport
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golang/mock/gomock"
7+
)
8+
9+
// TestValidInterface assesses whether or not the generated mock is valid
10+
func TestValidInterface(t *testing.T) {
11+
ctrl := gomock.NewController(t)
12+
defer ctrl.Finish()
13+
14+
s := NewMockSource(ctrl)
15+
s.EXPECT().Method().Return("")
16+
17+
CallForeignMethod(s)
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package faux
2+
3+
type Foreign interface {
4+
Method() Return
5+
Embedded
6+
}
7+
8+
type Embedded interface{}
9+
10+
type Return interface{}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:generate mockgen -package empty_interface -destination mock.go -source input.go -copyright_file=mock_copyright_header
2+
3+
package empty_interface
4+
5+
type Empty interface{}

internal/tests/copyright_file/mock.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is a mock copyright header.
2+
3+
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
4+
sed do eiusmod tempor incididunt ut labore et dolore magna
5+
aliqua. Velit ut tortor pretium viverra suspendisse potenti.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Tests for custom package names
2+
3+
This directory contains test for mockgen generating mocks when imported package
4+
name does not match import path suffix. For example, package with name "client"
5+
is located under import path "github.com/ssoor/implgen/internal/tests/custom_package_name/client/v1".
6+
7+
Prior to this patch:
8+
9+
```bash
10+
$ go generate greeter/greeter.go
11+
2018/03/05 22:44:52 Loading input failed: greeter.go:17:11: failed parsing returns: greeter.go:17:14: unknown package "client"
12+
greeter/greeter.go:1: running "mockgen": exit status 1
13+
```
14+
15+
This can be fixed by manually providing `-imports` flag, like `-imports client=github.com/ssoor/implgen/internal/tests/custom_package_name/client/v1`.
16+
But, mockgen should be able to automatically resolve package names in such situations.
17+
18+
With this patch applied:
19+
20+
```bash
21+
$ go generate greeter/greeter.go
22+
$ echo $?
23+
0
24+
```
25+
26+
Mockgen runs successfully, produced output is equal to [greeter_mock_test.go](greeter/greeter_mock_test.go) content.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package client
2+
3+
import "fmt"
4+
5+
type Client struct{}
6+
7+
func (c *Client) Greet(in GreetInput) string {
8+
return fmt.Sprintf("Hello, %s!", in.Name)
9+
}
10+
11+
type GreetInput struct {
12+
Name string
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:generate mockgen -source greeter.go -destination greeter_mock_test.go -package greeter
2+
3+
package greeter
4+
5+
import (
6+
// stdlib import
7+
"fmt"
8+
9+
// non-matching import suffix and package name
10+
"github.com/ssoor/implgen/internal/tests/custom_package_name/client/v1"
11+
12+
// matching import suffix and package name
13+
"github.com/ssoor/implgen/internal/tests/custom_package_name/validator"
14+
)
15+
16+
type InputMaker interface {
17+
MakeInput() client.GreetInput
18+
}
19+
20+
type Greeter struct {
21+
InputMaker InputMaker
22+
Client *client.Client
23+
}
24+
25+
func (g *Greeter) Greet() (string, error) {
26+
in := g.InputMaker.MakeInput()
27+
if err := validator.Validate(in.Name); err != nil {
28+
return "", fmt.Errorf("validation failed: %v", err)
29+
}
30+
return g.Client.Greet(in), nil
31+
}

internal/tests/custom_package_name/greeter/greeter_mock_test.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package greeter
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golang/mock/gomock"
7+
"github.com/ssoor/implgen/internal/tests/custom_package_name/client/v1"
8+
)
9+
10+
func TestGreeter_Greet(t *testing.T) {
11+
ctrl := gomock.NewController(t)
12+
defer ctrl.Finish()
13+
14+
input := client.GreetInput{
15+
Name: "Foo",
16+
}
17+
18+
inputMaker := NewMockInputMaker(ctrl)
19+
inputMaker.EXPECT().
20+
MakeInput().
21+
Return(input)
22+
23+
g := &Greeter{
24+
InputMaker: inputMaker,
25+
Client: &client.Client{},
26+
}
27+
28+
greeting, err := g.Greet()
29+
if err != nil {
30+
t.Fatalf("Unexpected error: %v", err)
31+
}
32+
33+
expected := "Hello, Foo!"
34+
if greeting != expected {
35+
t.Fatalf("Expected greeting to be %v but got %v", expected, greeting)
36+
}
37+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package validator
2+
3+
func Validate(s string) error {
4+
return nil
5+
}

0 commit comments

Comments
 (0)