Skip to content

Commit d164a16

Browse files
authored
Add gob mode to support custom package loading techniques in place of --exec_only (#214)
Change #207 replaced reflect mode with package mode. In doing so, we no longer had to create separate reflect programs that would encode the resulting package into a gob for the rest of mockgen to read from. Because of that, we removed the `--exec_only` flag. However, this flag was actually used to pass in programs that created gob encodings in other ways, allowing customization of the package loading and encoding portion of mockgen. As an alternative, this adds a new flag to mockgen `--model_gob`, which allows mockgen users to pass a path to a gob encoding of a `model.Package`, so that custom programs for encoding package information can still be used in conjunction with mockgen.
1 parent d01ed30 commit d164a16

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

mockgen/gob.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"encoding/gob"
5+
"os"
6+
7+
"go.uber.org/mock/mockgen/model"
8+
)
9+
10+
func gobMode(path string) (*model.Package, error) {
11+
in, err := os.Open(path)
12+
if err != nil {
13+
return nil, err
14+
}
15+
defer in.Close()
16+
var pkg model.Package
17+
if err := gob.NewDecoder(in).Decode(&pkg); err != nil {
18+
return nil, err
19+
}
20+
return &pkg, nil
21+
}

mockgen/gob_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"encoding/gob"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestGobMode(t *testing.T) {
14+
15+
// Encode a package to a temporary gob.
16+
parser := packageModeParser{}
17+
want, err := parser.parsePackage(
18+
"go.uber.org/mock/mockgen/internal/tests/package_mode" /* package name */,
19+
[]string{ "Human", "Earth" } /* ifaces */,
20+
)
21+
path := filepath.Join(t.TempDir(), "model.gob")
22+
outfile, err := os.Create(path)
23+
require.NoError(t, err)
24+
require.NoError(t, gob.NewEncoder(outfile).Encode(want))
25+
outfile.Close()
26+
27+
// Ensure gobMode loads it correctly.
28+
got, err := gobMode(path)
29+
require.NoError(t, err)
30+
assert.Equal(t, want, got)
31+
}

mockgen/mockgen.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ var (
6969
imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
7070
auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.")
7171
excludeInterfaces = flag.String("exclude_interfaces", "", "(source mode) Comma-separated names of interfaces to be excluded")
72+
modelGob = flag.String("model_gob", "", "Skip package/source loading entirely and use the gob encoded model.Package at the given path")
7273

7374
debugParser = flag.Bool("debug_parser", false, "Print out parser results only.")
7475
showVersion = flag.Bool("version", false, "Print version.")
@@ -88,7 +89,9 @@ func main() {
8889
var pkg *model.Package
8990
var err error
9091
var packageName string
91-
if *source != "" {
92+
if *modelGob != "" {
93+
pkg, err = gobMode(*modelGob)
94+
} else if *source != "" {
9295
pkg, err = sourceMode(*source)
9396
} else {
9497
if flag.NArg() != 2 {

0 commit comments

Comments
 (0)