diff --git a/README.md b/README.md index 34ce210..127da88 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ moq [flags] source-dir interface [interface2 [interface3 [...]]] show the version for moq -with-resets generate functions to facilitate resetting calls made to a mock + -build-tag + specify and optional go:build tag for the generated files, e.g. `-build-tag test`. Specifying an alias for the mock is also supported with the format 'interface:alias' diff --git a/internal/template/template.go b/internal/template/template.go index 9e2672c..6d6c6a7 100644 --- a/internal/template/template.go +++ b/internal/template/template.go @@ -32,7 +32,11 @@ func (t Template) Execute(w io.Writer, data Data) error { // moqTemplate is the template for mocked code. // language=GoTemplate -var moqTemplate = `// Code generated by moq; DO NOT EDIT. +var moqTemplate = `{{ with .BuildTag }} +//go:build {{ . }} + +{{ end -}} +// Code generated by moq; DO NOT EDIT. // github.com/matryer/moq package {{.PkgName}} diff --git a/internal/template/template_data.go b/internal/template/template_data.go index 2a3caeb..b9c18e6 100644 --- a/internal/template/template_data.go +++ b/internal/template/template_data.go @@ -17,6 +17,7 @@ type Data struct { StubImpl bool SkipEnsure bool WithResets bool + BuildTag string } // MocksSomeMethod returns true of any one of the Mocks has at least 1 diff --git a/main.go b/main.go index 89adb3d..0a30a12 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ type userFlags struct { outFile string pkgName string formatter string + buildTag string stubImpl bool skipEnsure bool withResets bool @@ -40,6 +41,7 @@ func main() { flag.BoolVar(&flags.remove, "rm", false, "first remove output file, if it exists") flag.BoolVar(&flags.withResets, "with-resets", false, "generate functions to facilitate resetting calls made to a mock") + flag.StringVar(&flags.buildTag, "build-tag", "", "go:build tag to inject into the mock, e.g. `test`") flag.Usage = func() { fmt.Println(`moq [flags] source-dir interface [interface2 [interface3 [...]]]`) @@ -90,6 +92,7 @@ func run(flags userFlags) error { StubImpl: flags.stubImpl, SkipEnsure: flags.skipEnsure, WithResets: flags.withResets, + BuildTag: flags.buildTag, }) if err != nil { return err diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index e8a2975..b97fc32 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -29,6 +29,7 @@ type Config struct { StubImpl bool SkipEnsure bool WithResets bool + BuildTag string } // New makes a new Mocker for the specified package directory. @@ -83,6 +84,7 @@ func (m *Mocker) Mock(w io.Writer, namePairs ...string) error { StubImpl: m.cfg.StubImpl, SkipEnsure: m.cfg.SkipEnsure, WithResets: m.cfg.WithResets, + BuildTag: m.cfg.BuildTag, } if data.MocksSomeMethod() { diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 2813238..fed2590 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -615,6 +615,22 @@ func TestEmptyInterface(t *testing.T) { } } +func TestEmptyInterfaceWithBuildTag(t *testing.T) { + m, err := New(Config{SrcDir: "testpackages/emptyinterface", BuildTag: "test"}) + if err != nil { + t.Fatalf("moq.New: %s", err) + } + var buf bytes.Buffer + err = m.Mock(&buf, "Empty") + if err != nil { + t.Errorf("mock error: %s", err) + } + s := buf.String() + if !strings.HasPrefix(s, "//go:build test\n\n") { + t.Error("missing `//go:build test` prefix on:", s) + } +} + func TestGoGenerateVendoredPackages(t *testing.T) { cmd := exec.Command("go", "generate", "./...") cmd.Dir = "testpackages/gogenvendoring"