Skip to content

Commit

Permalink
added ignoring interfaces via -exclude_interfaces flag (#68) (#72)
Browse files Browse the repository at this point in the history
The problem is described in the issue. This is one of the solutions to
this problem.
  • Loading branch information
tulzke authored Sep 15, 2023
1 parent 7826b9c commit dad3840
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
15 changes: 15 additions & 0 deletions mockgen/internal/tests/exclude/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package exclude

//go:generate mockgen -source=interfaces.go -destination=mock.go -package=ignore -exclude_interfaces=IgnoreMe,IgnoreMe2

type IgnoreMe interface {
A() bool
}

type IgnoreMe2 interface {
~int
}

type GenerateMockForMe interface {
B() int
}
52 changes: 52 additions & 0 deletions mockgen/internal/tests/exclude/mock.go

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

19 changes: 19 additions & 0 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var (
typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function")
imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.")
excludeInterfaces = flag.String("exclude_interfaces", "", "Comma-separated names of interfaces to be excluded")

debugParser = flag.Bool("debug_parser", false, "Print out parser results only.")
showVersion = flag.Bool("version", false, "Print version.")
Expand Down Expand Up @@ -200,6 +201,24 @@ func parseMockNames(names string) map[string]string {
return mocksMap
}

func parseExcludeInterfaces(names string) map[string]struct{} {
splitNames := strings.Split(names, ",")
namesSet := make(map[string]struct{}, len(splitNames))
for _, name := range splitNames {
if name == "" {
continue
}

namesSet[name] = struct{}{}
}

if len(namesSet) == 0 {
return nil
}

return namesSet
}

func usage() {
_, _ = io.WriteString(os.Stderr, usageText)
flag.PrintDefaults()
Expand Down
54 changes: 54 additions & 0 deletions mockgen/mockgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,57 @@ func TestParsePackageImport_FallbackMultiGoPath(t *testing.T) {
t.Errorf("expect %s, got %s", expectedPkgPath, pkgPath)
}
}

func TestParseExcludeInterfaces(t *testing.T) {
testCases := []struct {
name string
arg string
expected map[string]struct{}
}{
{
name: "empty string",
arg: "",
expected: nil,
},
{
name: "string without a comma",
arg: "arg1",
expected: map[string]struct{}{"arg1": {}},
},
{
name: "two names",
arg: "arg1,arg2",
expected: map[string]struct{}{"arg1": {}, "arg2": {}},
},
{
name: "two names with a comma at the end",
arg: "arg1,arg2,",
expected: map[string]struct{}{"arg1": {}, "arg2": {}},
},
{
name: "two names with a comma at the beginning",
arg: ",arg1,arg2",
expected: map[string]struct{}{"arg1": {}, "arg2": {}},
},
{
name: "commas only",
arg: ",,,,",
expected: nil,
},
{
name: "duplicates",
arg: "arg1,arg2,arg1",
expected: map[string]struct{}{"arg1": {}, "arg2": {}},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
actual := parseExcludeInterfaces(tt.arg)

if !reflect.DeepEqual(actual, tt.expected) {
t.Errorf("expected %v, actual %v", tt.expected, actual)
}
})
}
}
8 changes: 8 additions & 0 deletions mockgen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func sourceMode(source string) (*model.Package, error) {
}
}

if *excludeInterfaces != "" {
p.excludeNamesSet = parseExcludeInterfaces(*excludeInterfaces)
}

// Handle -aux_files.
if err := p.parseAuxFiles(*auxFiles); err != nil {
return nil, err
Expand Down Expand Up @@ -163,6 +167,7 @@ type fileParser struct {
auxFiles []*ast.File
auxInterfaces *interfaceCache
srcDir string
excludeNamesSet map[string]struct{}
}

func (p *fileParser) errorf(pos token.Pos, format string, args ...any) error {
Expand Down Expand Up @@ -223,6 +228,9 @@ func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Packag

var is []*model.Interface
for ni := range iterInterfaces(file) {
if _, ok := p.excludeNamesSet[ni.name.String()]; ok {
continue
}
i, err := p.parseInterface(ni.name.String(), importPath, ni)
if err != nil {
return nil, err
Expand Down

0 comments on commit dad3840

Please sign in to comment.