Skip to content

Commit

Permalink
Merge pull request #637 from LandonTClipp/issue_611
Browse files Browse the repository at this point in the history
Add link to documentation for root packages with no go files
  • Loading branch information
LandonTClipp committed Jun 2, 2023
2 parents b98dd8b + 7a3849d commit 1baf2ee
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Expand Up @@ -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]
10 changes: 6 additions & 4 deletions cmd/mockery.go
Expand Up @@ -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()
},
}

Expand Down Expand Up @@ -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)
}
}
Expand Down
1 change: 1 addition & 0 deletions go.work.sum
Expand Up @@ -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=
Expand Down
25 changes: 19 additions & 6 deletions pkg/config/config.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
17 changes: 14 additions & 3 deletions pkg/config/config_test.go
Expand Up @@ -3,6 +3,7 @@ package config
import (
"bytes"
"context"
"errors"
"reflect"
"testing"

Expand Down Expand Up @@ -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: `
Expand Down Expand Up @@ -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
----
Expand Down
1 change: 1 addition & 0 deletions pkg/fixtures/pkg_with_no_files/subpkg/foo.go
@@ -0,0 +1 @@
package foo

0 comments on commit 1baf2ee

Please sign in to comment.