Skip to content

Commit 8adc9de

Browse files
authored
feat: mockgen flag --build_constraint to add //go:build directives (#191)
Resolves #190 Note that `//go:generate` has unusual handling of flags with spaces so the quotes have to include the flag name ([example](https://github.com/ARR4N/mock/blob/36b5e87796c7f82be7275e40bd54965c8605875c/mockgen/internal/tests/build_constraint/input.go#L3)) for complex constraints. This revealed a bug in `--write_generate_directive`, which just prints space-delimited `os.Args`. The workaround of using `--copyright_file` is neither viable (it adds a space between `//` and `go:build`) nor good practice as overloading functionality can result in bugs due to unanticipated usage (e.g. the aforementioned space).
1 parent c50d83c commit 8adc9de

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package empty_interface
2+
3+
//go:generate mockgen -package empty_interface -destination mock.go -source input.go "-build_constraint=(linux && 386) || (darwin && !cgo) || usertag"
4+
5+
type Empty interface{}

mockgen/internal/tests/build_constraint/mock.go

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mockgen/mockgen.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var (
6464
writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (reflect mode) comment if true.")
6565
writeGenerateDirective = flag.Bool("write_generate_directive", false, "Add //go:generate directive to regenerate the mock")
6666
copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header")
67+
buildConstraint = flag.String("build_constraint", "", "If non-empty, added as //go:build <constraint>")
6768
typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function")
6869
imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
6970
auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.")
@@ -143,7 +144,9 @@ func main() {
143144
}
144145
}
145146

146-
g := new(generator)
147+
g := &generator{
148+
buildConstraint: *buildConstraint,
149+
}
147150
if *source != "" {
148151
g.filename = *source
149152
} else {
@@ -251,6 +254,7 @@ type generator struct {
251254
destination string // may be empty
252255
srcPackage, srcInterfaces string // may be empty
253256
copyrightHeader string
257+
buildConstraint string // may be empty
254258

255259
packageMap map[string]string // map from import path to package name
256260
}
@@ -306,6 +310,12 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
306310
g.p("")
307311
}
308312

313+
if g.buildConstraint != "" {
314+
g.p("//go:build %s", g.buildConstraint)
315+
// https://pkg.go.dev/cmd/go#hdr-Build_constraints:~:text=a%20build%20constraint%20should%20be%20followed%20by%20a%20blank%20line
316+
g.p("")
317+
}
318+
309319
g.p("// Code generated by MockGen. DO NOT EDIT.")
310320
if *writeSourceComment {
311321
if g.filename != "" {

0 commit comments

Comments
 (0)