Skip to content

Commit

Permalink
Merge pull request #693 from mcdoker18/fix/691
Browse files Browse the repository at this point in the history
[Fix] Generate mock build constraints
  • Loading branch information
LandonTClipp authored Jan 12, 2024
2 parents 138d507 + 58b0c4c commit 2502f52
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 4 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
7 changes: 4 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ Parameter Descriptions
| `include-auto-generated` | :fontawesome-solid-x: | `#!yaml true` | Set to `#!yaml false` if you need mockery to skip auto-generated files during its recursive package discovery. When set to `#!yaml true`, mockery includes auto-generated files when determining if a particular directory is an importable package. |
| `include-regex` | :fontawesome-solid-x: | `#!yaml ""` | When set, only interface names that match the expression will be generated. This setting is ignored if `all: True` is specified in the configuration. To further refine the interfaces generated, use `exclude-regex`. |
| `inpackage` | :fontawesome-solid-x: | `#!yaml false` | When generating mocks alongside the original interfaces, you must specify `inpackage: True` to inform mockery that the mock is being placed in the same package as the original interface. |
| `log-level` | :fontawesome-solid-x: | `#!yaml "info"` | Set the level of the logger |
| `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). |
| `mockname` | :fontawesome-solid-check: | `#!yaml "Mock{{.InterfaceName}}"` | The name of the generated mock. |
| `outpkg` | :fontawesome-solid-check: | `#!yaml "{{.PackageName}}"` | Use `outpkg` to specify the package name of the generated mocks. |
| `log-level` | :fontawesome-solid-x: | `#!yaml "info"` | Set the level of the logger |
| [`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. |
| [`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. |
| `tags` | :fontawesome-solid-x: | `#!yaml ""` | A space-separated list of additional build tags to load packages. |
| [`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. |

Layouts
-------
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 2502f52

Please sign in to comment.