diff --git a/mockgen/internal/tests/defined_import_local_name/input.go b/mockgen/internal/tests/defined_import_local_name/input.go new file mode 100644 index 0000000..617ba81 --- /dev/null +++ b/mockgen/internal/tests/defined_import_local_name/input.go @@ -0,0 +1,13 @@ +package defined_import_local_name + +import ( + "bytes" + "context" +) + +//go:generate mockgen -package defined_import_local_name -destination mock.go -source input.go -imports b_mock=bytes,c_mock=context + +type WithImports interface { + Method1() bytes.Buffer + Method2() context.Context +} diff --git a/mockgen/internal/tests/defined_import_local_name/mock.go b/mockgen/internal/tests/defined_import_local_name/mock.go new file mode 100644 index 0000000..5b44939 --- /dev/null +++ b/mockgen/internal/tests/defined_import_local_name/mock.go @@ -0,0 +1,68 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./mockgen/internal/tests/defined_import_local_name/input.go +// +// Generated by this command: +// +// C:\Users\snapp\AppData\Local\Temp\go-build3951283790\b001\exe\mockgen.exe -package defined_import_local_name -destination ./mockgen/internal/tests/defined_import_local_name/mock.go -source ./mockgen/internal/tests/defined_import_local_name/input.go -imports b_mock=bytes,c_mock=context +// +// Package defined_import_local_name is a generated GoMock package. +package defined_import_local_name + +import ( + b_mock "bytes" + c_mock "context" + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockWithImports is a mock of WithImports interface. +type MockWithImports struct { + ctrl *gomock.Controller + recorder *MockWithImportsMockRecorder +} + +// MockWithImportsMockRecorder is the mock recorder for MockWithImports. +type MockWithImportsMockRecorder struct { + mock *MockWithImports +} + +// NewMockWithImports creates a new mock instance. +func NewMockWithImports(ctrl *gomock.Controller) *MockWithImports { + mock := &MockWithImports{ctrl: ctrl} + mock.recorder = &MockWithImportsMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockWithImports) EXPECT() *MockWithImportsMockRecorder { + return m.recorder +} + +// Method1 mocks base method. +func (m *MockWithImports) Method1() b_mock.Buffer { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Method1") + ret0, _ := ret[0].(b_mock.Buffer) + return ret0 +} + +// Method1 indicates an expected call of Method1. +func (mr *MockWithImportsMockRecorder) Method1() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Method1", reflect.TypeOf((*MockWithImports)(nil).Method1)) +} + +// Method2 mocks base method. +func (m *MockWithImports) Method2() c_mock.Context { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Method2") + ret0, _ := ret[0].(c_mock.Context) + return ret0 +} + +// Method2 indicates an expected call of Method2. +func (mr *MockWithImportsMockRecorder) Method2() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Method2", reflect.TypeOf((*MockWithImports)(nil).Method2)) +} diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index 4409035..262bf0b 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -63,6 +63,8 @@ var ( writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (reflect mode) comment if true.") copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header") 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.") debugParser = flag.Bool("debug_parser", false, "Print out parser results only.") showVersion = flag.Bool("version", false, "Print version.") @@ -319,6 +321,16 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac packagesName := createPackageMap(sortedPaths) + definedImports := make(map[string]string, len(im)) + if *imports != "" { + for _, kv := range strings.Split(*imports, ",") { + eq := strings.Index(kv, "=") + if k, v := kv[:eq], kv[eq+1:]; k != "." { + definedImports[v] = k + } + } + } + g.packageMap = make(map[string]string, len(im)) localNames := make(map[string]bool, len(im)) for _, pth := range sortedPaths { @@ -330,9 +342,14 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac // Local names for an imported package can usually be the basename of the import path. // A couple of situations don't permit that, such as duplicate local names // (e.g. importing "html/template" and "text/template"), or where the basename is - // a keyword (e.g. "foo/case"). + // a keyword (e.g. "foo/case") or when defining a name for that by using the -imports flag. // try base0, base1, ... pkgName := base + + if _, ok := definedImports[base]; ok { + pkgName = definedImports[base] + } + i := 0 for localNames[pkgName] || token.Lookup(pkgName).IsKeyword() { pkgName = base + strconv.Itoa(i) diff --git a/mockgen/parse.go b/mockgen/parse.go index a397922..bcd65b9 100644 --- a/mockgen/parse.go +++ b/mockgen/parse.go @@ -18,7 +18,6 @@ package main import ( "errors" - "flag" "fmt" "go/ast" "go/build" @@ -36,11 +35,6 @@ import ( "go.uber.org/mock/mockgen/model" ) -var ( - 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.") -) - // sourceMode generates mocks via source file. func sourceMode(source string) (*model.Package, error) { srcDir, err := filepath.Abs(filepath.Dir(source))