From cfe4a0ad824b40aa78cfd2670a040c35e2ef5858 Mon Sep 17 00:00:00 2001 From: Nick Jones Date: Wed, 10 Apr 2024 15:38:38 +1000 Subject: [PATCH 1/2] feat: add -build-tag flag. We use moq to build mocks for testing beyond the current package. We'd like those mocks to be available only to tests, not live code, so we've been doing this by catting build tags into the generated mocks. This adds a flag to make that process easier. --- README.md | 2 ++ internal/template/template.go | 6 +++++- internal/template/template_data.go | 1 + main.go | 3 +++ pkg/moq/moq.go | 2 ++ pkg/moq/moq_test.go | 16 ++++++++++++++++ 6 files changed, 29 insertions(+), 1 deletion(-) 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..ba610a2 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" From fe07fb07854ee975a3b7d3115a2eb48b15ce7d08 Mon Sep 17 00:00:00 2001 From: Nick Jones Date: Thu, 11 Apr 2024 13:44:05 +1000 Subject: [PATCH 2/2] Fix flag default <-> usage. --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index ba610a2..0a30a12 100644 --- a/main.go +++ b/main.go @@ -41,7 +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.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 [...]]]`)