From 78bb1019ac00be84933bd5140fdae651787a27e2 Mon Sep 17 00:00:00 2001 From: Tulskiy Aleksandr Date: Thu, 7 Sep 2023 18:57:10 +0300 Subject: [PATCH] added ignoring interfaces via -exclude flag (#68) --- mockgen/internal/tests/exclude/interfaces.go | 15 ++++++ mockgen/internal/tests/exclude/mock.go | 52 ++++++++++++++++++++ mockgen/mockgen.go | 15 ++++++ mockgen/parse.go | 8 +++ 4 files changed, 90 insertions(+) create mode 100644 mockgen/internal/tests/exclude/interfaces.go create mode 100644 mockgen/internal/tests/exclude/mock.go diff --git a/mockgen/internal/tests/exclude/interfaces.go b/mockgen/internal/tests/exclude/interfaces.go new file mode 100644 index 0000000..0ba9ecf --- /dev/null +++ b/mockgen/internal/tests/exclude/interfaces.go @@ -0,0 +1,15 @@ +package exclude + +//go:generate mockgen -source=interfaces.go -destination=mock.go -package=ignore -exclude=IgnoreMe,IgnoreMe2 + +type IgnoreMe interface { + A() bool +} + +type IgnoreMe2 interface { + ~int +} + +type GenerateMockForMe interface { + B() int +} diff --git a/mockgen/internal/tests/exclude/mock.go b/mockgen/internal/tests/exclude/mock.go new file mode 100644 index 0000000..08288d7 --- /dev/null +++ b/mockgen/internal/tests/exclude/mock.go @@ -0,0 +1,52 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./internal/tests/exclude/interfaces.go +// +// Generated by this command: +// +// /Users/tulzke/Library/Caches/JetBrains/GoLand2023.1/tmp/GoLand/___go_build_go_uber_org_mock_mockgen -source=./internal/tests/exclude/interfaces.go -destination=./internal/tests/exclude/mock.go -package=exclude -exclude=IgnoreMe,IgnoreMe2 +// +// Package exclude is a generated GoMock package. +package exclude + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockGenerateMockForMe is a mock of GenerateMockForMe interface. +type MockGenerateMockForMe struct { + ctrl *gomock.Controller + recorder *MockGenerateMockForMeMockRecorder +} + +// MockGenerateMockForMeMockRecorder is the mock recorder for MockGenerateMockForMe. +type MockGenerateMockForMeMockRecorder struct { + mock *MockGenerateMockForMe +} + +// NewMockGenerateMockForMe creates a new mock instance. +func NewMockGenerateMockForMe(ctrl *gomock.Controller) *MockGenerateMockForMe { + mock := &MockGenerateMockForMe{ctrl: ctrl} + mock.recorder = &MockGenerateMockForMeMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockGenerateMockForMe) EXPECT() *MockGenerateMockForMeMockRecorder { + return m.recorder +} + +// B mocks base method. +func (m *MockGenerateMockForMe) B() int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "B") + ret0, _ := ret[0].(int) + return ret0 +} + +// B indicates an expected call of B. +func (mr *MockGenerateMockForMeMockRecorder) B() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "B", reflect.TypeOf((*MockGenerateMockForMe)(nil).B)) +} diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index ce57b75..0a069f2 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -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.") + exclude = flag.String("exclude", "", "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.") @@ -200,6 +201,20 @@ func parseMockNames(names string) map[string]string { return mocksMap } +func parseExclude(names string) map[string]struct{} { + splitNames := strings.Split(names, ",") + if len(splitNames) == 0 { + return nil + } + + namesSet := make(map[string]struct{}, len(splitNames)) + for _, name := range splitNames { + namesSet[name] = struct{}{} + } + + return namesSet +} + func usage() { _, _ = io.WriteString(os.Stderr, usageText) flag.PrintDefaults() diff --git a/mockgen/parse.go b/mockgen/parse.go index 8379fa3..112c99e 100644 --- a/mockgen/parse.go +++ b/mockgen/parse.go @@ -75,6 +75,10 @@ func sourceMode(source string) (*model.Package, error) { } } + if *exclude != "" { + p.excludeNamesSet = parseExclude(*exclude) + } + // Handle -aux_files. if err := p.parseAuxFiles(*auxFiles); err != nil { return nil, err @@ -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 { @@ -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