Skip to content

Commit 1baf2ee

Browse files
authored
Merge pull request #637 from LandonTClipp/issue_611
Add link to documentation for root packages with no go files
2 parents b98dd8b + 7a3849d commit 1baf2ee

File tree

6 files changed

+42
-14
lines changed

6 files changed

+42
-14
lines changed

Taskfile.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tasks:
4141
- go run github.com/golangci/golangci-lint/cmd/golangci-lint run
4242

4343
test.ci:
44-
deps: [test, fmt, mocks, lint]
44+
deps: [fmt, lint, mocks, test]
4545

4646
default:
4747
deps: [test.ci]

cmd/mockery.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ func NewRootCmd() *cobra.Command {
3636
cmd := &cobra.Command{
3737
Use: "mockery",
3838
Short: "Generate mock objects for your Golang interfaces",
39-
RunE: func(cmd *cobra.Command, args []string) error {
39+
Run: func(cmd *cobra.Command, args []string) {
4040
r, err := GetRootAppFromViper(viperCfg)
4141
if err != nil {
4242
printStackTrace(err)
43-
return err
43+
os.Exit(1)
44+
}
45+
if err := r.Run(); err != nil {
46+
printStackTrace(err)
47+
os.Exit(1)
4448
}
45-
return r.Run()
4649
},
4750
}
4851

@@ -104,7 +107,6 @@ func printStackTrace(e error) {
104107
// Execute executes the cobra CLI workflow
105108
func Execute() {
106109
if err := NewRootCmd().Execute(); err != nil {
107-
// printStackTrace(err)
108110
os.Exit(1)
109111
}
110112
}

go.work.sum

+1
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e h1:MZM7FHLqUHYI0Y/mQAt
510510
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc=
511511
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
512512
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
513+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
513514
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
514515
github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM=
515516
github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms=

pkg/config/config.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020
)
2121

2222
var (
23-
ErrNoConfigFile = fmt.Errorf("no config file exists")
24-
ErrPkgNotFound = fmt.Errorf("package not found in config")
23+
ErrNoConfigFile = fmt.Errorf("no config file exists")
24+
ErrNoGoFilesFoundInRoot = fmt.Errorf("no go files found in root search path")
25+
ErrPkgNotFound = fmt.Errorf("package not found in config")
2526
)
2627

2728
type Interface struct {
@@ -427,7 +428,12 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP
427428
return nil
428429
}
429430

430-
func (c *Config) subPackages(ctx context.Context, pkgPath string, pkgConfig *Config) ([]string, error) {
431+
func (c *Config) subPackages(
432+
ctx context.Context,
433+
pkgPath string,
434+
pkgConfig *Config,
435+
currentDepth int,
436+
) ([]string, error) {
431437
log := zerolog.Ctx(ctx)
432438

433439
pkgs, err := packages.Load(&packages.Config{
@@ -438,6 +444,13 @@ func (c *Config) subPackages(ctx context.Context, pkgPath string, pkgConfig *Con
438444
}
439445
pkg := pkgs[0]
440446

447+
if currentDepth == 0 && len(pkg.GoFiles) == 0 {
448+
log.Error().
449+
Err(ErrNoGoFilesFoundInRoot).
450+
Str("documentation", "https://vektra.github.io/mockery/notes/#error-no-go-files-found-in-root-search-path").
451+
Msg("package contains no go files")
452+
return nil, ErrNoGoFilesFoundInRoot
453+
}
441454
representativeFile := pathlib.NewPath(pkg.GoFiles[0])
442455
searchRoot := representativeFile.Parent()
443456
packageRootName := pathlib.NewPath(pkg.PkgPath)
@@ -498,7 +511,7 @@ func (c *Config) subPackages(ctx context.Context, pkgPath string, pkgConfig *Con
498511
return nil
499512
})
500513
if walkErr != nil {
501-
return nil, fmt.Errorf("error occured during filesystem walk: %w", err)
514+
return nil, fmt.Errorf("error occured during filesystem walk: %w", walkErr)
502515
}
503516

504517
// Parse the subdirectories we found into their respective fully qualified
@@ -538,10 +551,10 @@ func (c *Config) discoverRecursivePackages(ctx context.Context) error {
538551
return nil
539552
}
540553
for pkgPath, conf := range recursivePackages {
541-
pkgLog := log.With().Str("package-name", pkgPath).Logger()
554+
pkgLog := log.With().Str("package-path", pkgPath).Logger()
542555
pkgCtx := pkgLog.WithContext(ctx)
543556
pkgLog.Debug().Msg("discovering sub-packages")
544-
subPkgs, err := c.subPackages(pkgCtx, pkgPath, conf)
557+
subPkgs, err := c.subPackages(pkgCtx, pkgPath, conf, 0)
545558
if err != nil {
546559
return fmt.Errorf("failed to get subpackages: %w", err)
547560
}

pkg/config/config_test.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"reflect"
78
"testing"
89

@@ -787,8 +788,18 @@ func TestConfig_Initialize(t *testing.T) {
787788
name string
788789
cfgYaml string
789790
wantCfgMap string
790-
wantErr bool
791+
wantErr error
791792
}{
793+
{
794+
name: "package with no go files",
795+
cfgYaml: `
796+
packages:
797+
github.com/vektra/mockery/v2/pkg/fixtures/pkg_with_no_files:
798+
config:
799+
recursive: True
800+
all: True`,
801+
wantErr: ErrNoGoFilesFoundInRoot,
802+
},
792803
{
793804
name: "test with no subpackages present",
794805
cfgYaml: `
@@ -951,14 +962,14 @@ with-expecter: false
951962
log, err := logging.GetLogger("TRACE")
952963
require.NoError(t, err)
953964

954-
if err := c.Initialize(log.WithContext(context.Background())); (err != nil) != tt.wantErr {
965+
if err := c.Initialize(log.WithContext(context.Background())); !errors.Is(err, tt.wantErr) {
955966
t.Errorf("Config.Initialize() error = %v, wantErr %v", err, tt.wantErr)
956967
}
957968

958969
cfgAsStr, err := yaml.Marshal(c._cfgAsMap)
959970
require.NoError(t, err)
960971

961-
if !reflect.DeepEqual(string(cfgAsStr), tt.wantCfgMap) {
972+
if tt.wantCfgMap != "" && !reflect.DeepEqual(string(cfgAsStr), tt.wantCfgMap) {
962973
t.Errorf(`Config.Initialize resultant config map
963974
got
964975
----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package foo

0 commit comments

Comments
 (0)