Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: ConfigDir Template #807

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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
Loading