Skip to content

Commit

Permalink
Generate mock build constraints
Browse files Browse the repository at this point in the history
Supported only the new format of build tags
https://pkg.go.dev/cmd/go#hdr-Build_constraints

Fixes #691
  • Loading branch information
mcdoker18 committed Jan 10, 2024
1 parent b9df18e commit 1c18f44
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ with-expecter: True
mockname: "{{.InterfaceName}}"
filename: "{{.MockName}}.go"
outpkg: mocks
tags: "custom2"
packages:
github.com/vektra/mockery/v2/pkg/fixtures/buildtag/comment:
config:
mock-build-tags: "custom3 && (!windows || !darwin || !freebsd)"
disable-version-string: true
interfaces:
IfaceWithCustomBuildTagInComment:
github.com/vektra/mockery/v2/pkg:
interfaces:
TypesPackage:
Expand Down
4 changes: 3 additions & 1 deletion cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func NewRootCmd() *cobra.Command {
pFlags.Bool("version", false, "prints the installed version of mockery")
pFlags.Bool("quiet", false, `suppresses logger output (equivalent to --log-level="")`)
pFlags.Bool("keeptree", false, "keep the tree structure of the original interface files into a different repository. Must be used with XX")
pFlags.String("tags", "", "space-separated list of additional build tags to use")
pFlags.String("tags", "", "space-separated list of additional build tags to load packages")
pFlags.String("mock-build-tags", "", "set the build tags of the generated mocks. Read more about the format: https://pkg.go.dev/cmd/go#hdr-Build_constraints")
pFlags.String("filename", "", "name of generated file (only works with -name and no regex)")
pFlags.String("structname", "", "name of generated struct (only works with -name and no regex)")
pFlags.String("log-level", "info", "Level of logging")
Expand Down Expand Up @@ -376,6 +377,7 @@ func (r *RootApp) Run() error {
InPackage: r.Config.InPackage,
KeepTree: r.Config.KeepTree,
Note: r.Config.Note,
MockBuildTags: r.Config.MockBuildTags,
PackageName: r.Config.Outpkg,
PackageNamePrefix: r.Config.Packageprefix,
StructName: r.Config.StructName,
Expand Down
3 changes: 2 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ Parameter Descriptions
| [`packages`](features.md#packages-configuration) | :fontawesome-solid-x: | `#!yaml null` | A dictionary containing configuration describing the packages and interfaces to generate mocks for. |
| `print` | :fontawesome-solid-x: | `#!yaml false` | Use `print: True` to have the resulting code printed out instead of written to disk. |
| [`recursive`](features.md#recursive-package-discovery) | :fontawesome-solid-x: | `#!yaml false` | When set to `true` on a particular package, mockery will recursively search for all sub-packages and inject those packages into the config map. |
| `tags` | :fontawesome-solid-x: | `#!yaml ""` | Set the build tags of the generated mocks. |
| `tags` | :fontawesome-solid-x: | `#!yaml ""` | A space-separated list of additional build tags to load packages. |
| `mock-build-tags` | :fontawesome-solid-x: | `#!yaml ""` | Set the build tags of the generated mocks. Read more about the [format](https://pkg.go.dev/cmd/go#hdr-Build_constraints). |
| [`with-expecter`](features.md#expecter-structs) | :fontawesome-solid-x: | `#!yaml true` | Use `with-expecter: True` to generate `EXPECT()` methods for your mocks. This is the preferred way to setup your mocks. |
| [`replace-type`](features.md#replace-types) | :fontawesome-solid-x: | `#!yaml null` | Replaces aliases, packages and/or types during generation. |

Expand Down

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

1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Interface struct {

type Config struct {
All bool `mapstructure:"all"`
MockBuildTags string `mapstructure:"mock-build-tags"`
BuildTags string `mapstructure:"tags"`
Case string `mapstructure:"case"`
Config string `mapstructure:"config"`
Expand Down
1 change: 1 addition & 0 deletions pkg/fixtures/buildtag/comment/custom2_iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ package comment

type IfaceWithCustomBuildTagInComment interface {
Sprintf(format string, a ...interface{}) string
Custom2()
}
8 changes: 8 additions & 0 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type GeneratorConfig struct {
InPackage bool
KeepTree bool
Note string
MockBuildTags string
PackageName string
PackageNamePrefix string
StructName string
Expand Down Expand Up @@ -111,6 +112,7 @@ func NewGenerator(ctx context.Context, c GeneratorConfig, iface *Interface, pkg
func (g *Generator) GenerateAll(ctx context.Context) error {
g.GenerateBoilerplate(g.config.Boilerplate)
g.GeneratePrologueNote(g.config.Note)
g.GenerateBuildTags(g.config.MockBuildTags)
g.GeneratePrologue(ctx, g.pkg)
return g.Generate(ctx)
}
Expand Down Expand Up @@ -418,6 +420,12 @@ func (g *Generator) GenerateBoilerplate(boilerplate string) {
}
}

func (g *Generator) GenerateBuildTags(buildTags string) {
if buildTags != "" {
g.printf("//go:build %s\n\n", buildTags)
}
}

// ErrNotInterface is returned when the given type is not an interface
// type.
var ErrNotInterface = errors.New("expression not an interface")
Expand Down
11 changes: 11 additions & 0 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ func (s *GeneratorSuite) TestGeneratorBoilerplate() {
s.Equal(expected, generator.buf.String())
}

func (s *GeneratorSuite) TestGeneratorBuildTags() {
generator := s.getGenerator(testFile, "Requester", false, "")
generator.GenerateBuildTags("custom && (!windows || !linux)")

expected := `//go:build custom && (!windows || !linux)
`

s.Equal(expected, generator.buf.String())
}

func (s *GeneratorSuite) TestGeneratorPrologueNoteNoVersionString() {
generator := s.getGenerator(testFile, "Requester", false, "")
generator.config.DisableVersionString = true
Expand Down
1 change: 1 addition & 0 deletions pkg/outputter.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func (m *Outputter) Generate(ctx context.Context, iface *Interface) error {
InPackage: interfaceConfig.InPackage,
KeepTree: interfaceConfig.KeepTree,
Note: interfaceConfig.Note,
MockBuildTags: interfaceConfig.MockBuildTags,
PackageName: interfaceConfig.Outpkg,
PackageNamePrefix: interfaceConfig.Packageprefix,
StructName: interfaceConfig.MockName,
Expand Down
2 changes: 2 additions & 0 deletions pkg/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ type GeneratorVisitorConfig struct {
InPackage bool
KeepTree bool
Note string
MockBuildTags string
// The name of the output package, if InPackage is false (defaults to "mocks")
PackageName string
PackageNamePrefix string
Expand Down Expand Up @@ -181,6 +182,7 @@ func (v *GeneratorVisitor) VisitWalk(ctx context.Context, iface *Interface) erro
InPackage: v.config.InPackage,
KeepTree: v.config.KeepTree,
Note: v.config.Note,
MockBuildTags: v.config.MockBuildTags,
PackageName: v.config.PackageName,
PackageNamePrefix: v.config.PackageNamePrefix,
StructName: v.config.StructName,
Expand Down

0 comments on commit 1c18f44

Please sign in to comment.