From 8fae46050e6f2cac24d35f4f461775b8b42a873b Mon Sep 17 00:00:00 2001 From: Tobby Satyarama Date: Sat, 17 Aug 2024 19:43:03 +0800 Subject: [PATCH 1/2] propagate config file name into template --- pkg/config/config_test.go | 70 ++++++++++++++++++++++++--------------- pkg/outputter.go | 2 ++ pkg/outputter_test.go | 19 +++++++++++ 3 files changed, 64 insertions(+), 27 deletions(-) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 94ff3fa7..e9f638cb 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -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 @@ -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{}, @@ -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", @@ -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{ @@ -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", @@ -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{ @@ -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", @@ -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, @@ -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 @@ -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{ @@ -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", @@ -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{ @@ -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", @@ -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{ @@ -383,6 +398,7 @@ func TestConfig_GetInterfaceConfig(t *testing.T) { }, want: []*Config{ { + Config: "path/to/config/.mockery.yaml", All: false, BuildTags: "foobar", Case: "upper", diff --git a/pkg/outputter.go b/pkg/outputter.go index e3f916fc..53567756 100644 --- a/pkg/outputter.go +++ b/pkg/outputter.go @@ -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 @@ -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, diff --git a/pkg/outputter_test.go b/pkg/outputter_test.go index f8bf4a6b..91344272 100644 --- a/pkg/outputter_test.go +++ b/pkg/outputter_test.go @@ -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" @@ -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{ From 7c9f776cc1d75348b4c9457090a5bd38f228f946 Mon Sep 17 00:00:00 2001 From: Tobby Satyarama Date: Sat, 17 Aug 2024 19:46:01 +0800 Subject: [PATCH 2/2] add documentation --- docs/configuration.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/configuration.md b/docs/configuration.md index af53ba97..5f050565 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -196,6 +196,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
`#!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. |