Skip to content

Commit

Permalink
Merge pull request #596 from amirbenun/exclude-subdirs
Browse files Browse the repository at this point in the history
Exclude subdirectories and files with --exclude
  • Loading branch information
LandonTClipp committed Apr 10, 2023
2 parents 3eb9040 + 1f0d9a5 commit e63962d
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func NewRootCmd() *cobra.Command {
pFlags.String("packageprefix", "", "prefix for the generated package name, it is ignored if outpkg is also specified.")
pFlags.String("dir", "", "directory to search for interfaces")
pFlags.BoolP("recursive", "r", false, "recurse search into sub-directories")
pFlags.StringArray("exclude", nil, "prefixes of subdirectories and files to exclude from search")
pFlags.Bool("all", false, "generates mocks for all found interfaces in all sub-directories")
pFlags.Bool("inpackage", false, "generate a mock that goes inside the original package")
pFlags.Bool("inpackage-suffix", false, "use filename '_mock' suffix instead of 'mock_' prefix for InPackage mocks")
Expand Down
11 changes: 11 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"reflect"
"strings"

"github.com/chigopher/pathlib"
"github.com/jinzhu/copier"
Expand Down Expand Up @@ -58,6 +59,7 @@ type Config struct {
Profile string `mapstructure:"profile"`
Quiet bool `mapstructure:"quiet"`
Recursive bool `mapstructure:"recursive"`
Exclude []string `mapstructure:"exclude"`
SrcPkg string `mapstructure:"srcpkg"`
BoilerplateFile string `mapstructure:"boilerplate-file"`
// StructName overrides the name given to the mock struct and should only be nonempty
Expand Down Expand Up @@ -227,6 +229,15 @@ func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Con
return pkgConfigTyped, nil
}

func (c *Config) ExcludePath(path string) bool {
for _, ex := range c.Exclude {
if strings.HasPrefix(path, ex) {
return true
}
}
return false
}

func (c *Config) ShouldGenerateInterface(ctx context.Context, packageName, interfaceName string) (bool, error) {
pkgConfig, err := c.GetPackageConfig(ctx, packageName)
if err != nil {
Expand Down
129 changes: 129 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,135 @@ func TestConfig_GetPackages(t *testing.T) {
}
}

func TestConfig_ShouldGenerateInterface(t *testing.T) {
tests := []struct {
name string
c *Config
want bool
wantErr bool
}{
{
name: "no packages return error",
c: &Config{
Packages: map[string]interface{}{},
},
want: false,
wantErr: true,
},
{
name: "should generate all interfaces",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{},
},
All: true,
},
want: true,
},
{
name: "should generate this package",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{
"config": map[string]interface{}{
"all": true,
},
},
},
},
want: true,
},
{
name: "should generate this interface",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{
"interfaces": map[string]interface{}{
"SomeInterface": struct{}{},
},
},
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.c.Config = writeConfigFile(t, tt.c)

got, err := tt.c.ShouldGenerateInterface(context.Background(), "some_package", "SomeInterface")
if (err != nil) != tt.wantErr {
t.Errorf("Config.ShouldGenerateInterface() error = %v, wantErr %v", err, tt.wantErr)
return
}

if got != tt.want {
t.Errorf("Config.ShouldGenerateInterface() = %v, want %v", got, tt.want)
}
})
}
}

func TestConfig_ExcludePath(t *testing.T) {
tests := []struct {
name string
file string
c *Config
want bool
}{
{
name: "should not exclude",
file: "some_foo.go",
c: &Config{
Exclude: []string{"foo"},
},
want: false,
},
{
name: "should not exclude both",
file: "some_foo.go",
c: &Config{
Exclude: []string{"foo", "bar"},
},
want: false,
},
{
name: "should exclude",
file: "foo/some_foo.go",
c: &Config{
Exclude: []string{"foo"},
},
want: true,
},
{
name: "should exclude specific file",
file: "foo/some_foo.go",
c: &Config{
Exclude: []string{"foo/some_foo.go"},
},
want: true,
},
{
name: "should exclude both paths",
file: "foo/bar/some_foo.go",
c: &Config{
Exclude: []string{"foo", "foo/bar"},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.c.Config = writeConfigFile(t, tt.c)

got := tt.c.ExcludePath(tt.file)
if got != tt.want {
t.Errorf("Config.ExcludePath() = %v, want %v", got, tt.want)
}
})
}
}

func TestNewConfigFromViper(t *testing.T) {
tests := []struct {
name string
Expand Down
8 changes: 8 additions & 0 deletions pkg/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (w *Walker) Walk(ctx context.Context, visitor WalkerVisitor) (generated boo
continue
}

if w.ExcludePath(iface.FileName) {
continue
}

if !w.Filter.MatchString(iface.Name) {
continue
}
Expand Down Expand Up @@ -81,6 +85,10 @@ func (w *Walker) doWalk(ctx context.Context, parser *Parser, dir string) (genera
}

path := filepath.Join(dir, file.Name())
if w.ExcludePath(path) {
continue
}

if file.IsDir() {
if w.Recursive {
generated = w.doWalk(ctx, parser, path) || generated
Expand Down
30 changes: 30 additions & 0 deletions pkg/walker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vektra/mockery/v2/pkg/config"
)

type GatheringVisitor struct {
Expand Down Expand Up @@ -119,3 +120,32 @@ func TestPackagePrefix(t *testing.T) {
w.Walk(context.Background(), visitor)
assert.Regexp(t, regexp.MustCompile("package prefix_test_test"), bufferedProvider.String())
}

func TestWalkerExclude(t *testing.T) {
if testing.Short() {
t.Skip("skipping recursive walker test")
}

wd, err := os.Getwd()
assert.NoError(t, err)

w := Walker{
BaseDir: wd,
Recursive: true,
LimitOne: false,
Config: config.Config{
Exclude: []string{
getFixturePath("requester"),
getFixturePath("generic.go")},
},
Filter: regexp.MustCompile(".*"),
}

gv := NewGatheringVisitor()

w.Walk(context.Background(), gv)
for _, iface := range gv.Interfaces {
assert.NotContains(t, iface.Name, "Requester",
"Interface %s should have been excluded but found in file: %s", iface.Name, iface.FileName)
}
}

0 comments on commit e63962d

Please sign in to comment.