From 7a3849d5f55418416d7c13317e3f620438618276 Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Fri, 2 Jun 2023 12:16:07 -0400 Subject: [PATCH] Add link to docs for empty package directory error Add test --- Taskfile.yml | 2 +- cmd/mockery.go | 10 ++++---- go.work.sum | 1 + pkg/config/config.go | 25 +++++++++++++++----- pkg/config/config_test.go | 17 ++++++++++--- pkg/fixtures/pkg_with_no_files/subpkg/foo.go | 1 + 6 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 pkg/fixtures/pkg_with_no_files/subpkg/foo.go diff --git a/Taskfile.yml b/Taskfile.yml index ffe75897..eb171399 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -41,7 +41,7 @@ tasks: - go run github.com/golangci/golangci-lint/cmd/golangci-lint run test.ci: - deps: [test, fmt, mocks, lint] + deps: [fmt, lint, mocks, test] default: deps: [test.ci] \ No newline at end of file diff --git a/cmd/mockery.go b/cmd/mockery.go index 7b4579d9..a530f419 100644 --- a/cmd/mockery.go +++ b/cmd/mockery.go @@ -36,13 +36,16 @@ func NewRootCmd() *cobra.Command { cmd := &cobra.Command{ Use: "mockery", Short: "Generate mock objects for your Golang interfaces", - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { r, err := GetRootAppFromViper(viperCfg) if err != nil { printStackTrace(err) - return err + os.Exit(1) + } + if err := r.Run(); err != nil { + printStackTrace(err) + os.Exit(1) } - return r.Run() }, } @@ -104,7 +107,6 @@ func printStackTrace(e error) { // Execute executes the cobra CLI workflow func Execute() { if err := NewRootCmd().Execute(); err != nil { - // printStackTrace(err) os.Exit(1) } } diff --git a/go.work.sum b/go.work.sum index 1ec33e6d..de451346 100644 --- a/go.work.sum +++ b/go.work.sum @@ -510,6 +510,7 @@ github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e h1:MZM7FHLqUHYI0Y/mQAt github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM= github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms= diff --git a/pkg/config/config.go b/pkg/config/config.go index faff61d7..1acc2dbe 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -20,8 +20,9 @@ import ( ) var ( - ErrNoConfigFile = fmt.Errorf("no config file exists") - ErrPkgNotFound = fmt.Errorf("package not found in config") + ErrNoConfigFile = fmt.Errorf("no config file exists") + ErrNoGoFilesFoundInRoot = fmt.Errorf("no go files found in root search path") + ErrPkgNotFound = fmt.Errorf("package not found in config") ) type Interface struct { @@ -427,7 +428,12 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP return nil } -func (c *Config) subPackages(ctx context.Context, pkgPath string, pkgConfig *Config) ([]string, error) { +func (c *Config) subPackages( + ctx context.Context, + pkgPath string, + pkgConfig *Config, + currentDepth int, +) ([]string, error) { log := zerolog.Ctx(ctx) pkgs, err := packages.Load(&packages.Config{ @@ -438,6 +444,13 @@ func (c *Config) subPackages(ctx context.Context, pkgPath string, pkgConfig *Con } pkg := pkgs[0] + if currentDepth == 0 && len(pkg.GoFiles) == 0 { + log.Error(). + Err(ErrNoGoFilesFoundInRoot). + Str("documentation", "https://vektra.github.io/mockery/notes/#error-no-go-files-found-in-root-search-path"). + Msg("package contains no go files") + return nil, ErrNoGoFilesFoundInRoot + } representativeFile := pathlib.NewPath(pkg.GoFiles[0]) searchRoot := representativeFile.Parent() packageRootName := pathlib.NewPath(pkg.PkgPath) @@ -498,7 +511,7 @@ func (c *Config) subPackages(ctx context.Context, pkgPath string, pkgConfig *Con return nil }) if walkErr != nil { - return nil, fmt.Errorf("error occured during filesystem walk: %w", err) + return nil, fmt.Errorf("error occured during filesystem walk: %w", walkErr) } // Parse the subdirectories we found into their respective fully qualified @@ -538,10 +551,10 @@ func (c *Config) discoverRecursivePackages(ctx context.Context) error { return nil } for pkgPath, conf := range recursivePackages { - pkgLog := log.With().Str("package-name", pkgPath).Logger() + pkgLog := log.With().Str("package-path", pkgPath).Logger() pkgCtx := pkgLog.WithContext(ctx) pkgLog.Debug().Msg("discovering sub-packages") - subPkgs, err := c.subPackages(pkgCtx, pkgPath, conf) + subPkgs, err := c.subPackages(pkgCtx, pkgPath, conf, 0) if err != nil { return fmt.Errorf("failed to get subpackages: %w", err) } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index dfd2bb6b..8f34a4b8 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -3,6 +3,7 @@ package config import ( "bytes" "context" + "errors" "reflect" "testing" @@ -787,8 +788,18 @@ func TestConfig_Initialize(t *testing.T) { name string cfgYaml string wantCfgMap string - wantErr bool + wantErr error }{ + { + name: "package with no go files", + cfgYaml: ` +packages: + github.com/vektra/mockery/v2/pkg/fixtures/pkg_with_no_files: + config: + recursive: True + all: True`, + wantErr: ErrNoGoFilesFoundInRoot, + }, { name: "test with no subpackages present", cfgYaml: ` @@ -951,14 +962,14 @@ with-expecter: false log, err := logging.GetLogger("TRACE") require.NoError(t, err) - if err := c.Initialize(log.WithContext(context.Background())); (err != nil) != tt.wantErr { + if err := c.Initialize(log.WithContext(context.Background())); !errors.Is(err, tt.wantErr) { t.Errorf("Config.Initialize() error = %v, wantErr %v", err, tt.wantErr) } cfgAsStr, err := yaml.Marshal(c._cfgAsMap) require.NoError(t, err) - if !reflect.DeepEqual(string(cfgAsStr), tt.wantCfgMap) { + if tt.wantCfgMap != "" && !reflect.DeepEqual(string(cfgAsStr), tt.wantCfgMap) { t.Errorf(`Config.Initialize resultant config map got ---- diff --git a/pkg/fixtures/pkg_with_no_files/subpkg/foo.go b/pkg/fixtures/pkg_with_no_files/subpkg/foo.go new file mode 100644 index 00000000..f52652b1 --- /dev/null +++ b/pkg/fixtures/pkg_with_no_files/subpkg/foo.go @@ -0,0 +1 @@ +package foo