Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add -build-tag flag. #215

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
6 changes: 5 additions & 1 deletion internal/template/template.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/template/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type userFlags struct {
outFile string
pkgName string
formatter string
buildTag string
stubImpl bool
skipEnsure bool
withResets bool
Expand All @@ -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 [...]]]`)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/moq/moq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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() {
Expand Down
16 changes: 16 additions & 0 deletions pkg/moq/moq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down