Skip to content

Commit

Permalink
Merge pull request #807 from tobby-s/FEAT-configdir
Browse files Browse the repository at this point in the history
Feature: ConfigDir Template
  • Loading branch information
LandonTClipp authored Sep 17, 2024
2 parents 2abbb49 + 7c9f776 commit 6423255
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 27 deletions.
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ Variables that are marked as being templated are capable of using mockery-provid

| name | description |
|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ConfigDir | The directory path of the config file used. This is used to allow generation of mocks in a directory relative to the `.mockery.yaml` file, e.g. external interfaces. |
| InterfaceDir | The directory path of the original interface being mocked. This can be used as <br>`#!yaml dir: "{{.InterfaceDir}}"` to place your mocks adjacent to the original interface. This should not be used for external interfaces. |
| InterfaceDirRelative | The directory path of the original interface being mocked, relative to the current working directory. If the path cannot be made relative to the current working directory, this variable will be set equal to `PackagePath` |
| InterfaceFile | The file path of the original interface being mocked. **NOTE:** This option will only write one mock implementation to the output file. If multiple mocks are defined in your original file, only one mock will be written to the output. |
Expand Down
70 changes: 43 additions & 27 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektra/mockery/v2/pkg/logging"
"gopkg.in/yaml.v3"

"github.com/vektra/mockery/v2/pkg/logging"
)

func TestConfig_GetPackageConfig(t *testing.T) {
type fields struct {
All bool
BuildTags string
Case string
Packages map[string]interface{}
ConfigFile string
All bool
BuildTags string
Case string
Packages map[string]interface{}
}
type args struct {
packageName string
Expand Down Expand Up @@ -72,9 +74,10 @@ func TestConfig_GetPackageConfig(t *testing.T) {
{
name: "config section provided but no values defined",
fields: fields{
All: true,
BuildTags: "default_tags",
Case: "upper",
ConfigFile: "path/to/config/.mockery.yaml",
All: true,
BuildTags: "default_tags",
Case: "upper",
Packages: map[string]any{
"github.com/vektra/mockery/v2/pkg": map[string]any{
"config": map[string]any{},
Expand All @@ -85,6 +88,7 @@ func TestConfig_GetPackageConfig(t *testing.T) {
packageName: "github.com/vektra/mockery/v2/pkg",
},
want: &Config{
Config: "path/to/config/.mockery.yaml",
All: true,
BuildTags: "default_tags",
Case: "upper",
Expand All @@ -96,9 +100,10 @@ func TestConfig_GetPackageConfig(t *testing.T) {
{
name: "two values overridden in pkg config",
fields: fields{
All: true,
BuildTags: "default_tags",
Case: "upper",
ConfigFile: "path/to/config/.mockery.yaml",
All: true,
BuildTags: "default_tags",
Case: "upper",
Packages: map[string]any{
"github.com/vektra/mockery/v2/pkg": map[string]any{
"config": map[string]any{
Expand All @@ -112,6 +117,7 @@ func TestConfig_GetPackageConfig(t *testing.T) {
packageName: "github.com/vektra/mockery/v2/pkg",
},
want: &Config{
Config: "path/to/config/.mockery.yaml",
All: false,
BuildTags: "foobar",
Case: "upper",
Expand All @@ -123,9 +129,10 @@ func TestConfig_GetPackageConfig(t *testing.T) {
{
name: "repeated calls gives same cached result",
fields: fields{
All: true,
BuildTags: "default_tags",
Case: "upper",
ConfigFile: "path/to/config/.mockery.yaml",
All: true,
BuildTags: "default_tags",
Case: "upper",
Packages: map[string]any{
"github.com/vektra/mockery/v2/pkg": map[string]any{
"config": map[string]any{
Expand All @@ -139,6 +146,7 @@ func TestConfig_GetPackageConfig(t *testing.T) {
packageName: "github.com/vektra/mockery/v2/pkg",
},
want: &Config{
Config: "path/to/config/.mockery.yaml",
All: false,
BuildTags: "foobar",
Case: "upper",
Expand Down Expand Up @@ -172,6 +180,7 @@ func TestConfig_GetPackageConfig(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Config{
Config: tt.fields.ConfigFile,
All: tt.fields.All,
BuildTags: tt.fields.BuildTags,
Case: tt.fields.Case,
Expand Down Expand Up @@ -205,10 +214,11 @@ func TestConfig_GetPackageConfig(t *testing.T) {

func TestConfig_GetInterfaceConfig(t *testing.T) {
type fields struct {
All bool
BuildTags string
Case string
Packages map[string]interface{}
ConfigFile string
All bool
BuildTags string
Case string
Packages map[string]interface{}
}
type args struct {
packageName string
Expand Down Expand Up @@ -246,9 +256,10 @@ func TestConfig_GetInterfaceConfig(t *testing.T) {
{
name: "config defined for package",
fields: fields{
All: true,
BuildTags: "default_tags",
Case: "upper",
ConfigFile: "path/to/config/.mockery.yaml",
All: true,
BuildTags: "default_tags",
Case: "upper",
Packages: map[string]any{
"github.com/vektra/mockery/v2/pkg": map[string]any{
"config": map[string]any{
Expand All @@ -263,6 +274,7 @@ func TestConfig_GetInterfaceConfig(t *testing.T) {
},
want: []*Config{
{
Config: "path/to/config/.mockery.yaml",
All: false,
BuildTags: "default_tags",
Case: "upper",
Expand Down Expand Up @@ -299,9 +311,10 @@ func TestConfig_GetInterfaceConfig(t *testing.T) {
{
name: "interface defined, but not config section",
fields: fields{
All: true,
BuildTags: "default_tags",
Case: "upper",
ConfigFile: "path/to/config/.mockery.yaml",
All: true,
BuildTags: "default_tags",
Case: "upper",
Packages: map[string]any{
"github.com/vektra/mockery/v2/pkg": map[string]any{
"config": map[string]any{
Expand All @@ -319,6 +332,7 @@ func TestConfig_GetInterfaceConfig(t *testing.T) {
},
want: []*Config{
{
Config: "path/to/config/.mockery.yaml",
All: false,
BuildTags: "default_tags",
Case: "upper",
Expand Down Expand Up @@ -359,9 +373,10 @@ func TestConfig_GetInterfaceConfig(t *testing.T) {
{
name: "interface defined with non-empty config",
fields: fields{
All: true,
BuildTags: "default_tags",
Case: "upper",
ConfigFile: "path/to/config/.mockery.yaml",
All: true,
BuildTags: "default_tags",
Case: "upper",
Packages: map[string]any{
"github.com/vektra/mockery/v2/pkg": map[string]any{
"config": map[string]any{
Expand All @@ -383,6 +398,7 @@ func TestConfig_GetInterfaceConfig(t *testing.T) {
},
want: []*Config{
{
Config: "path/to/config/.mockery.yaml",
All: false,
BuildTags: "foobar",
Case: "upper",
Expand Down
2 changes: 2 additions & 0 deletions pkg/outputter.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func parseConfigTemplates(ctx context.Context, c *config.Config, iface *Interfac

// data is the struct sent to the template parser
data := struct {
ConfigDir string
InterfaceDir string
InterfaceDirRelative string
InterfaceFile string
Expand All @@ -221,6 +222,7 @@ func parseConfigTemplates(ctx context.Context, c *config.Config, iface *Interfac
PackageName string
PackagePath string
}{
ConfigDir: filepath.Dir(c.Config),
InterfaceDir: filepath.Dir(iface.FileName),
InterfaceDirRelative: interfaceDirRelative,
InterfaceFile: iface.FileName,
Expand Down
19 changes: 19 additions & 0 deletions pkg/outputter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

pkgMocks "github.com/vektra/mockery/v2/mocks/github.com/vektra/mockery/v2/pkg"
"github.com/vektra/mockery/v2/pkg/config"
"github.com/vektra/mockery/v2/pkg/logging"
Expand Down Expand Up @@ -172,6 +173,24 @@ func Test_parseConfigTemplates(t *testing.T) {
Dir: "mocks/github.com/user/project/package",
},
},
{
name: "ConfigDir template",
args: args{
c: &config.Config{
Config: "path_to/config/.mockery.yaml",
Dir: "{{.ConfigDir}}/mocks",
},
iface: &Interface{
Name: "FooBar",
FileName: "/path/to/foobar.go",
},
},
pkg: mockPkg,
want: &config.Config{
Config: "path_to/config/.mockery.yaml",
Dir: "path_to/config/mocks",
},
},
{
name: "infinite loop in template variables",
args: args{
Expand Down

0 comments on commit 6423255

Please sign in to comment.