diff --git a/mockgen/internal/tests/build_constraint/input.go b/mockgen/internal/tests/build_constraint/input.go new file mode 100644 index 0000000..009ad7c --- /dev/null +++ b/mockgen/internal/tests/build_constraint/input.go @@ -0,0 +1,5 @@ +package empty_interface + +//go:generate mockgen -package empty_interface -destination mock.go -source input.go "-build_constraint=(linux && 386) || (darwin && !cgo) || usertag" + +type Empty interface{} diff --git a/mockgen/internal/tests/build_constraint/mock.go b/mockgen/internal/tests/build_constraint/mock.go new file mode 100644 index 0000000..7d4dba7 --- /dev/null +++ b/mockgen/internal/tests/build_constraint/mock.go @@ -0,0 +1,44 @@ +//go:build (linux && 386) || (darwin && !cgo) || usertag + +// Code generated by MockGen. DO NOT EDIT. +// Source: input.go +// +// Generated by this command: +// +// mockgen -package empty_interface -destination mock.go -source input.go -build_constraint=(linux && 386) || (darwin && !cgo) || usertag +// + +// Package empty_interface is a generated GoMock package. +package empty_interface + +import ( + gomock "go.uber.org/mock/gomock" +) + +// MockEmpty is a mock of Empty interface. +type MockEmpty struct { + ctrl *gomock.Controller + recorder *MockEmptyMockRecorder +} + +// MockEmptyMockRecorder is the mock recorder for MockEmpty. +type MockEmptyMockRecorder struct { + mock *MockEmpty +} + +// NewMockEmpty creates a new mock instance. +func NewMockEmpty(ctrl *gomock.Controller) *MockEmpty { + mock := &MockEmpty{ctrl: ctrl} + mock.recorder = &MockEmptyMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockEmpty) EXPECT() *MockEmptyMockRecorder { + return m.recorder +} + +// ISGOMOCK indicates that this struct is a gomock mock. +func (m *MockEmpty) ISGOMOCK() struct{} { + return struct{}{} +} diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index 6d7634b..7e2fe25 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -64,6 +64,7 @@ var ( writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (reflect mode) comment if true.") writeGenerateDirective = flag.Bool("write_generate_directive", false, "Add //go:generate directive to regenerate the mock") copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header") + buildConstraint = flag.String("build_constraint", "", "If non-empty, added as //go:build ") 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.") @@ -143,7 +144,9 @@ func main() { } } - g := new(generator) + g := &generator{ + buildConstraint: *buildConstraint, + } if *source != "" { g.filename = *source } else { @@ -251,6 +254,7 @@ type generator struct { destination string // may be empty srcPackage, srcInterfaces string // may be empty copyrightHeader string + buildConstraint string // may be empty packageMap map[string]string // map from import path to package name } @@ -306,6 +310,12 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac g.p("") } + if g.buildConstraint != "" { + g.p("//go:build %s", g.buildConstraint) + // https://pkg.go.dev/cmd/go#hdr-Build_constraints:~:text=a%20build%20constraint%20should%20be%20followed%20by%20a%20blank%20line + g.p("") + } + g.p("// Code generated by MockGen. DO NOT EDIT.") if *writeSourceComment { if g.filename != "" {