Skip to content

Commit

Permalink
propagate config file name into template
Browse files Browse the repository at this point in the history
  • Loading branch information
tobby-s committed Aug 17, 2024
1 parent 910874a commit 8fae460
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
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 8fae460

Please sign in to comment.