Skip to content

Commit de91ba4

Browse files
authored
feat(migrate): Add command to migrate v2 config to v3. (#953)
1 parent 5a42c2f commit de91ba4

File tree

15 files changed

+1021
-78
lines changed

15 files changed

+1021
-78
lines changed

.golangci.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,42 @@ linters-settings:
4040
- empty
4141
- expected-actual
4242
- len
43+
gosimple:
44+
checks:
45+
- S1000
46+
- S1001
47+
- S1003
48+
- S1004
49+
- S1005
50+
- S1006
51+
- S1007
52+
- S1008
53+
- S1009
54+
- S1010
55+
- S1011
56+
- S1012
57+
- S1016
58+
- S1017
59+
- S1018
60+
- S1019
61+
- S1020
62+
- S1021
63+
- S1023
64+
- S1024
65+
- S1025
66+
- S1028
67+
- S1029
68+
- S1030
69+
- S1031
70+
- S1032
71+
- S1033
72+
- S1034
73+
- S1035
74+
- S1036
75+
- S1037
76+
- S1038
77+
- S1039
78+
- S1040
4379
issues:
4480
exclude-rules:
4581
- linters:

config/config.go

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"bytes"
66
"context"
7-
"errors"
87
"fmt"
98
"go/ast"
109
"os"
@@ -24,6 +23,7 @@ import (
2423
"github.com/knadh/koanf/v2"
2524
"github.com/rs/zerolog"
2625
"github.com/spf13/pflag"
26+
internalConfig "github.com/vektra/mockery/v3/internal/config"
2727
"github.com/vektra/mockery/v3/internal/logging"
2828
"github.com/vektra/mockery/v3/internal/stackerr"
2929
"github.com/vektra/mockery/v3/shared"
@@ -48,8 +48,8 @@ func NewInterface(name string, filename string, file *ast.File, pkg *packages.Pa
4848
}
4949
}
5050

51-
// Data is the data sent to the template for the config file.
52-
type Data struct {
51+
// TemplateData is the data sent to the template for the config file.
52+
type TemplateData struct {
5353
// ConfigDir is the directory of where the mockery config file is located.
5454
ConfigDir string
5555
// InterfaceDir is the directory of the interface being mocked.
@@ -96,7 +96,7 @@ func NewDefaultKoanf(ctx context.Context) (*koanf.Koanf, error) {
9696
}
9797

9898
type RootConfig struct {
99-
Config `koanf:",squash" yaml:",inline"`
99+
*Config `koanf:",squash" yaml:",inline"`
100100
Packages map[string]*PackageConfig `koanf:"packages" yaml:"packages"`
101101
koanf *koanf.Koanf
102102
configFile *pathlib.Path
@@ -131,7 +131,7 @@ func NewRootConfig(
131131
return nil, nil, err
132132
}
133133
var rootConfig RootConfig = RootConfig{
134-
Config: *conf,
134+
Config: conf,
135135
koanf: k,
136136
}
137137

@@ -150,7 +150,7 @@ func NewRootConfig(
150150
}
151151
if configFile == nil {
152152
log.Debug().Msg("config file not specified, searching")
153-
configFile, err = findConfig()
153+
configFile, err = internalConfig.FindConfig()
154154
if err != nil {
155155
return nil, k, fmt.Errorf("discovering mockery config: %w", err)
156156
}
@@ -280,7 +280,7 @@ func (c *RootConfig) Initialize(ctx context.Context) error {
280280
pkgLog := log.With().Str("package-path", pkgName).Logger()
281281
pkgCtx := pkgLog.WithContext(ctx)
282282

283-
mergeConfigs(pkgCtx, c.Config, pkgConfig.Config)
283+
mergeConfigs(pkgCtx, *c.Config, pkgConfig.Config)
284284
if err := pkgConfig.Initialize(pkgCtx); err != nil {
285285
return fmt.Errorf("initializing root config: %w", err)
286286
}
@@ -506,28 +506,6 @@ type Config struct {
506506
Version *bool `koanf:"version" yaml:"version,omitempty"`
507507
}
508508

509-
func findConfig() (*pathlib.Path, error) {
510-
cwd, err := os.Getwd()
511-
if err != nil {
512-
return nil, fmt.Errorf("getting current working directory: %w", err)
513-
}
514-
currentPath := pathlib.NewPath(cwd)
515-
for len(currentPath.Parts()) != 1 {
516-
for _, confName := range []string{".mockery.yaml", ".mockery.yml"} {
517-
configPath := currentPath.Join(confName)
518-
isFile, err := configPath.Exists()
519-
if err != nil {
520-
return nil, fmt.Errorf("checking if %s is file: %w", configPath.String(), err)
521-
}
522-
if isFile {
523-
return configPath, nil
524-
}
525-
}
526-
currentPath = currentPath.Parent()
527-
}
528-
return nil, errors.New("mockery config file not found")
529-
}
530-
531509
func (c *Config) FilePath() *pathlib.Path {
532510
return pathlib.NewPath(*c.Dir).Join(*c.FileName).Clean()
533511
}
@@ -608,7 +586,7 @@ func (c *Config) ParseTemplates(ctx context.Context, iface *Interface, srcPkg *p
608586
}
609587
}
610588
// data is the struct sent to the template parser
611-
data := Data{
589+
data := TemplateData{
612590
ConfigDir: filepath.Dir(*c.ConfigFile),
613591
InterfaceDir: interfaceDir,
614592
InterfaceDirRelative: interfaceDirRelative,

docs/configuration.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ import (
109109
2. There are a number of template variables available to generalize config values.
110110
3. The style of mock to be generated is specified using the [`template`](template/index.md) parameter.
111111

112-
As you can see, configuration parameters are hierarchal such that:
113-
114-
- Interface-level config overrides package-level config.
115-
- Package-level config overrides top-level config.
116-
117112
Parameter Descriptions
118113
-----------------------
119114

@@ -139,6 +134,9 @@ Parameter Descriptions
139134
| `template` | :fontawesome-solid-x: | `#!yaml ""` | The template to use. The choices are `moq`, `mockery`, or a file path provided by `file://path/to/file.txt`. |
140135
| `template-data` | :fontawesome-solid-x: | `#!yaml {}` | A `map[string]any` that provides arbitrary options to the template. Each template will have a different set of accepted keys. Refer to each template's documentation for more details. |
141136

137+
!!! tip "`pkg.go.dev`"
138+
139+
The full schema definition can be viewed at our [pkg.go.dev](https://pkg.go.dev/github.com/vektra/mockery/v3/config#RootConfig) site.
142140

143141
Templates
144142
---------

docs/replace-type.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ title: replace-type
66

77
The `#!yaml replace-type:` parameter allows you to replace a type in the generated mocks with another type. Take for example the following interface:
88

9-
109
```go title="interface.go"
1110
package replace_type
1211

@@ -22,6 +21,8 @@ type RType interface {
2221

2322
You can selectively replace the `rt1.RType1` with a new type if so desired. For example:
2423

24+
### Schema
25+
2526
```yaml title=".mockery.yml"
2627
replace-type:
2728
github.com/vektra/mockery/v3/internal/fixtures/example_project/replace_type/rti/rt1:
@@ -30,6 +31,8 @@ replace-type:
3031
type-name: RType2
3132
```
3233
34+
### Result
35+
3336
The mock will now replace all instances of `rt1.RType1` with `rt2.RType2`. You can see the before and after of `mockery`-style mocks:
3437

3538
=== "before"
@@ -44,7 +47,7 @@ The mock will now replace all instances of `rt1.RType1` with `rt2.RType2`. You c
4447

4548
=== "after"
4649

47-
```go
50+
```go
4851
// Replace2 provides a mock function for the type RTypeReplaced1
4952
func (_mock *RTypeReplaced1) Replace1(f rt2.RType2) {
5053
_mock.Called(f)

0 commit comments

Comments
 (0)