Skip to content

Commit

Permalink
Add support for include-regex option.
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony-sc committed Aug 16, 2023
1 parent 8abb702 commit 15d6920
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
14 changes: 14 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ You can use the `showconfig` command to see the config mockery injects. The outp
??? note "performance characteristics"
The performance when using `#!yaml recursive: true` may be worse than manually specifying all packages statically in the yaml file. This is because of the fact that mockery has to recursively walk the filesystem path that contains the package in question. It may unnecessarily walk down unrelated paths (for example, a Python virtual environment that is in the same path as your package). For this reason, it is recommended _not_ to use `#!yaml recursive: true` if it can be avoided.

### Regex matching

You can filter matched interfaces using the `include-regex` option. To generate mocks only for interfaces ending in `Client` we can use the following configuration:

```yaml
packages:
github.com/user/project:
config:
recursive: true
include-regex: ".*Client"
```

??? note "all: true"
Using `all: true` will override `include-regex` and issue a warning.

Mock Constructors
-----------------
Expand Down
31 changes: 23 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"reflect"
"regexp"
"strings"

"github.com/chigopher/pathlib"
Expand Down Expand Up @@ -43,6 +44,7 @@ type Config struct {
Exported bool `mapstructure:"exported"`
FileName string `mapstructure:"filename"`
IncludeAutoGenerated bool `mapstructure:"include-auto-generated"`
IncludeRegex string `mapstructure:"include-regex"`
InPackage bool `mapstructure:"inpackage"`
InPackageSuffix bool `mapstructure:"inpackage-suffix"`
KeepTree bool `mapstructure:"keeptree"`
Expand Down Expand Up @@ -83,13 +85,13 @@ func NewConfigFromViper(v *viper.Viper) (*Config, error) {
Config: v.ConfigFileUsed(),
}

packages, err := c.GetPackages(context.Background())
packageList, err := c.GetPackages(context.Background())
if err != nil {
return c, fmt.Errorf("failed to get packages: %w", err)
}

// Set defaults
if len(packages) == 0 {
if len(packageList) == 0 {
v.SetDefault("case", "camel")
v.SetDefault("dir", ".")
v.SetDefault("output", "./mocks")
Expand Down Expand Up @@ -185,11 +187,11 @@ func (c *Config) GetPackages(ctx context.Context) ([]string, error) {
log.Error().Msg(msg)
return []string{}, fmt.Errorf(msg)
}
packages := []string{}
packageList := []string{}
for key := range packageSection {
packages = append(packages, key)
packageList = append(packageList, key)
}
return packages, nil
return packageList, nil
}

func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (map[string]any, error) {
Expand Down Expand Up @@ -268,7 +270,20 @@ func (c *Config) ShouldGenerateInterface(ctx context.Context, packageName, inter
return false, err
}
_, interfaceExists := interfacesSection[interfaceName]
return pkgConfig.All || interfaceExists, nil

var matchedByRegex bool
if pkgConfig.IncludeRegex != "" {
if pkgConfig.All {
log := zerolog.Ctx(ctx)
log.Warn().Msg("interface config has both `all` and `include-regex` set. `include-regex` will be ignored")
} else {
matchedByRegex, err = regexp.MatchString(pkgConfig.IncludeRegex, interfaceName)
if err != nil {
return false, err
}
}
}
return pkgConfig.All || interfaceExists || matchedByRegex, nil
}

func (c *Config) getInterfacesSection(ctx context.Context, packageName string) (map[string]any, error) {
Expand Down Expand Up @@ -552,11 +567,11 @@ func (c *Config) subPackages(
func (c *Config) discoverRecursivePackages(ctx context.Context) error {
log := zerolog.Ctx(ctx)
recursivePackages := map[string]*Config{}
packages, err := c.GetPackages(ctx)
packageList, err := c.GetPackages(ctx)
if err != nil {
return fmt.Errorf("failed to get packages: %w", err)
}
for _, pkg := range packages {
for _, pkg := range packageList {
pkgConfig, err := c.GetPackageConfig(ctx, pkg)
if err != nil {
return fmt.Errorf("failed to get package config: %w", err)
Expand Down
40 changes: 40 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,46 @@ func TestConfig_ShouldGenerateInterface(t *testing.T) {
},
want: true,
},
{
name: "should generate using included-regex",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{
"config": map[string]interface{}{
"include-regex": ".*Interface",
},
},
},
},
want: true,
},
{
name: "should generate when using all and included-regex doesn't match",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{
"config": map[string]interface{}{
"all": true,
"include-regex": ".*XInterface",
},
},
},
},
want: true,
},
{
name: "should not generate when included-regex doesn't match",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{
"config": map[string]interface{}{
"include-regex": ".*XInterface",
},
},
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 15d6920

Please sign in to comment.