Skip to content

Commit

Permalink
Add recursive option for packages feature
Browse files Browse the repository at this point in the history
This commit allows you to specify `recursive: true` for the `packages`
config feature. This will recursively search for all packages on-disk
so that you don't have to manually specify them in the mockery config.

Fixes #594

clarify showconfig functionality

Add debug logs for generator

Fix sub-package config merging

updates

Add tests for discoverRecursivePackages

simplify test

call Initialize for test instead

fix linting issue

add test for showconfig
  • Loading branch information
LandonTClipp committed Apr 17, 2023
1 parent 7dad3a3 commit 8a6a6bb
Show file tree
Hide file tree
Showing 15 changed files with 664 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -5,4 +5,5 @@ docs/ve
ve
.cache
coverage.txt
site/
site/
.task/
3 changes: 0 additions & 3 deletions .mockery.yaml
Expand Up @@ -23,6 +23,3 @@ packages:
- mockname: RequesterVariadic
Expecter:
RequesterReturnElided:



24 changes: 23 additions & 1 deletion Taskfile.yml
Expand Up @@ -4,22 +4,44 @@ tasks:
test:
cmds:
- go test -v -coverprofile=coverage.txt ./...
desc: run unit tests
sources:
- "**/*.go"
generates:
- coverage.txt

coverage:
deps: [test]
desc: run unit tests and create HTML coverage file
cmds:
- go tool cover -html=coverage.txt

fmt:
desc: auto-format all go files
sources:
- "**/*.go"
cmds:
- go fmt ./...

mocks:
desc: generate mockery mocks
cmds:
- go run .

docker:
desc: build the mockery docker image
cmds:
- docker build -t vektra/mockery .

lint:
desc: run all the defined linters
sources:
- "**/*.go"
cmds:
- go run github.com/golangci/golangci-lint/cmd/golangci-lint run

test.ci:
deps: [test, fmt, mocks, lint]
deps: [test, fmt, mocks, lint]

default:
deps: [test.ci]
6 changes: 5 additions & 1 deletion cmd/mockery.go
Expand Up @@ -208,6 +208,10 @@ func (r *RootApp) Run() error {
log.Info().Msgf("Using config: %s", r.Config.Config)
ctx := log.WithContext(context.Background())

if err := r.Config.Initialize(ctx); err != nil {
return err
}

Check warning on line 213 in cmd/mockery.go

View check run for this annotation

Codecov / codecov/patch

cmd/mockery.go#L212-L213

Added lines #L212 - L213 were not covered by tests

if r.Config.Version {
fmt.Println(logging.GetSemverInfo())
return nil
Expand Down Expand Up @@ -290,7 +294,7 @@ func (r *RootApp) Run() error {
} else if (r.Config.FileName != "" || r.Config.StructName != "") && r.Config.All {
log.Fatal().Msgf("Cannot specify --filename or --structname with --all")
} else if r.Config.Dir != "" && r.Config.Dir != "." && r.Config.SrcPkg != "" {
log.Fatal().Msgf("Specify -dir or -srcpkg, but not both")
log.Fatal().Msgf("Specify --dir or --srcpkg, but not both")

Check warning on line 297 in cmd/mockery.go

View check run for this annotation

Codecov / codecov/patch

cmd/mockery.go#L297

Added line #L297 was not covered by tests
} else if r.Config.Name != "" {
recursive = r.Config.Recursive
if strings.ContainsAny(r.Config.Name, regexMetadataChars) {
Expand Down
63 changes: 41 additions & 22 deletions cmd/showconfig.go
@@ -1,10 +1,14 @@
package cmd

import (
"context"
"fmt"
"io"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/vektra/mockery/v2/pkg/config"
"github.com/vektra/mockery/v2/pkg/logging"
"gopkg.in/yaml.v2"
Expand All @@ -13,28 +17,43 @@ import (
func NewShowConfigCmd() *cobra.Command {
return &cobra.Command{
Use: "showconfig",
Short: "Show the merged config",
Long: `Print out a yaml representation of the merged config.
This initializes viper and prints out the merged configuration between
config files, environment variables, and CLI flags.`,
RunE: func(cmd *cobra.Command, args []string) error {

config := &config.Config{}
if err := viperCfg.UnmarshalExact(config); err != nil {
return errors.Wrapf(err, "failed to unmarshal config")
}
out, err := yaml.Marshal(config)
if err != nil {
return errors.Wrapf(err, "Failed to marshal yaml")
}
log, err := logging.GetLogger(config.LogLevel)
if err != nil {
panic(err)
}
log.Info().Msgf("Using config: %s", config.Config)
Short: "Show the yaml config",
Long: `Print out a yaml representation of the yaml config file. This does not show config from exterior sources like CLI, environment etc.`,
RunE: func(cmd *cobra.Command, args []string) error { return showConfig(cmd, args, viperCfg, os.Stdout) },
}
}

fmt.Printf("%s", string(out))
return nil
},
func showConfig(
cmd *cobra.Command,
args []string,
v *viper.Viper,
outputter io.Writer,
) error {
if v == nil {
v = viperCfg
}

Check warning on line 34 in cmd/showconfig.go

View check run for this annotation

Codecov / codecov/patch

cmd/showconfig.go#L33-L34

Added lines #L33 - L34 were not covered by tests
ctx := context.Background()
config, err := config.NewConfigFromViper(v)
if err != nil {
return errors.Wrapf(err, "failed to unmarshal config")
}

Check warning on line 39 in cmd/showconfig.go

View check run for this annotation

Codecov / codecov/patch

cmd/showconfig.go#L38-L39

Added lines #L38 - L39 were not covered by tests
if err := config.Initialize(ctx); err != nil {
return err

Check warning on line 41 in cmd/showconfig.go

View check run for this annotation

Codecov / codecov/patch

cmd/showconfig.go#L41

Added line #L41 was not covered by tests
}
cfgMap, err := config.CfgAsMap(ctx)
if err != nil {
panic(err)

Check warning on line 45 in cmd/showconfig.go

View check run for this annotation

Codecov / codecov/patch

cmd/showconfig.go#L45

Added line #L45 was not covered by tests
}
out, err := yaml.Marshal(cfgMap)
if err != nil {
return errors.Wrapf(err, "Failed to marshal yaml")
}

Check warning on line 50 in cmd/showconfig.go

View check run for this annotation

Codecov / codecov/patch

cmd/showconfig.go#L49-L50

Added lines #L49 - L50 were not covered by tests
log, err := logging.GetLogger(config.LogLevel)
if err != nil {
panic(err)

Check warning on line 53 in cmd/showconfig.go

View check run for this annotation

Codecov / codecov/patch

cmd/showconfig.go#L53

Added line #L53 was not covered by tests
}
log.Info().Msgf("Using config: %s", config.Config)

fmt.Fprintf(outputter, "%s", string(out))
return nil
}
28 changes: 28 additions & 0 deletions cmd/showconfig_test.go
@@ -1,12 +1,40 @@
package cmd

import (
"bytes"
"testing"

"github.com/chigopher/pathlib"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestNewShowConfigCmd(t *testing.T) {
cmd := NewShowConfigCmd()
assert.Equal(t, "showconfig", cmd.Name())
}

func TestShowCfg(t *testing.T) {
v := viper.New()
v.Set("with-expecter", true)
cfgFile := pathlib.NewPath(t.TempDir()).Join("config.yaml")
err := cfgFile.WriteFile([]byte(`
with-expecter: true
all: true
packages:
github.com/vektra/mockery/v2/pkg:
config:
all: true`))
assert.NoError(t, err)
v.Set("config", cfgFile.String())
buf := new(bytes.Buffer)
assert.NoError(t, showConfig(nil, nil, v, buf))
assert.Equal(t, `all: true
packages:
github.com/vektra/mockery/v2/pkg:
config:
all: true
with-expecter: true
with-expecter: true
`, buf.String())
}
8 changes: 4 additions & 4 deletions go.mod
Expand Up @@ -3,7 +3,9 @@ module github.com/vektra/mockery/v2
go 1.19

require (
github.com/chigopher/pathlib v0.12.0
github.com/chigopher/pathlib v0.13.0
github.com/go-task/task/v3 v3.24.0
github.com/golangci/golangci-lint v1.52.2
github.com/iancoleman/strcase v0.2.0
github.com/jinzhu/copier v0.3.5
github.com/mitchellh/go-homedir v1.1.0
Expand All @@ -29,7 +31,7 @@ require (
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 // indirect
github.com/GaijinEntertainment/go-exhaustruct/v2 v2.3.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/OpenPeeDeeP/depguard v1.1.1 // indirect
github.com/alexkohler/prealloc v1.0.0 // indirect
github.com/alingse/asasalint v0.0.11 // indirect
Expand Down Expand Up @@ -58,7 +60,6 @@ require (
github.com/fzipp/gocyclo v0.6.0 // indirect
github.com/go-critic/go-critic v0.7.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/go-task/task/v3 v3.23.0 // indirect
github.com/go-toolsmith/astcast v1.1.0 // indirect
github.com/go-toolsmith/astcopy v1.1.0 // indirect
github.com/go-toolsmith/astequal v1.1.0 // indirect
Expand All @@ -75,7 +76,6 @@ require (
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect
github.com/golangci/go-misc v0.0.0-20220329215616-d24fe342adfe // indirect
github.com/golangci/gofmt v0.0.0-20220901101216-f2edd75033f2 // indirect
github.com/golangci/golangci-lint v1.52.2 // indirect
github.com/golangci/lint-1 v0.0.0-20191013205115-297bf364a8e0 // indirect
github.com/golangci/maligned v0.0.0-20180506175553-b1d89398deca // indirect
github.com/golangci/misspell v0.4.0 // indirect
Expand Down

0 comments on commit 8a6a6bb

Please sign in to comment.