Skip to content

Commit 53192c2

Browse files
committed
Add docs and hint in log message on how to enable force-file-write.
1 parent f02dab9 commit 53192c2

File tree

7 files changed

+15
-19
lines changed

7 files changed

+15
-19
lines changed

Taskfile.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ tasks:
8585
test.ci:
8686
deps: [lint]
8787
cmds:
88-
- task: mocks.remove
89-
- task: mocks.generate
88+
- task: mocks
9089
- task: test
90+
- task: mocks.remove
9191
- task: test.e2e
9292

9393
default:

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Parameter Descriptions
5757
| `exclude-subpkg-regex` | :fontawesome-solid-x: | `#!yaml []` | A list of regular expressions that denote which subpackages should be excluded when `#!yaml recursive: true` |
5858
| `exclude-regex` | :fontawesome-solid-x: | `#!yaml ""` | When set along with `include-regex`, then interfaces which match `include-regex` but also match `exclude-regex` will not be generated. If `all` is set, or if `include-regex` is not set, then `exclude-regex` has no effect. |
5959
| `filename` | :fontawesome-solid-check: | `#!yaml "mock_{{.InterfaceName}}.go"` | The name of the file the mock will reside in. |
60+
| `force-file-write` | :fontawesome-solid-x: | `#!yaml false` | When set to `#!yaml force-file-write: true`, mockery will forcibly overwrite any existing files. |
6061
| `formatter` | :fontawesome-solid-x: | `#!yaml "goimports"` | The formatter to use on the rendered template. Choices are: `gofmt`, `goimports`, `noop`. |
6162
| `include-regex` | :fontawesome-solid-x: | `#!yaml ""` | When set, only interface names that match the expression will be generated. This setting is ignored if `all: True` is specified in the configuration. To further refine the interfaces generated, use `exclude-regex`. |
6263
| `log-level` | :fontawesome-solid-x: | `#!yaml "info"` | Set the level of the logger |

e2e/test_infinite_mocking.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
# New mocks may legimitately be created, so we run mockery once first
66
num_files_before=$(find . -type f | wc -l)
7+
export MOCKERY_FORCE_FILE_WRITE="true"
78
go run github.com/go-task/task/v3/cmd/task mocks.generate
89
num_files_after=$(find . -type f | wc -l)
910

e2e/test_mockery_generation.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
go run github.com/go-task/task/v3/cmd/task mocks.generate
3+
go run github.com/go-task/task/v3/cmd/task mocks
44
rt=$?
55
if [ $rt -ne 0 ]; then
66
echo "ERROR: non-zero return code from mockery"

e2e/test_recursive_package_with_only_autogenerated_files.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.

internal/cmd/mockery.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ func (r *RootApp) Run() error {
312312
fileLog.Err(err).Msg("can't determine if outfile exists")
313313
return fmt.Errorf("determining if outfile exists: %w", err)
314314
}
315-
if outFileExists {
316-
fileLog.Error().Msg("output file exists, can't write mocks")
315+
if outFileExists && !packageConfig.Config.ForceFileWrite {
316+
fileLog.Error().Bool("force-file-write", packageConfig.Config.ForceFileWrite).Msg("output file exists, can't write mocks")
317317
return fmt.Errorf("outfile exists")
318318
}
319319

template/config.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,14 @@ type Config struct {
474474
ExcludeSubpkgRegex []string `koanf:"exclude-subpkg-regex"`
475475
ExcludeRegex *string `koanf:"exclude-regex"`
476476
FileName *string `koanf:"filename"`
477-
Formatter *string `koanf:"formatter"`
478-
IncludeRegex *string `koanf:"include-regex"`
479-
LogLevel *string `koanf:"log-level"`
480-
MockName *string `koanf:"mockname"`
481-
PkgName *string `koanf:"pkgname"`
482-
Recursive *bool `koanf:"recursive"`
477+
// ForceFileWrite controls whether mockery will overwrite existing files when generating mocks. This is by default set to false.
478+
ForceFileWrite bool `koanf:"force-file-write"`
479+
Formatter *string `koanf:"formatter"`
480+
IncludeRegex *string `koanf:"include-regex"`
481+
LogLevel *string `koanf:"log-level"`
482+
MockName *string `koanf:"mockname"`
483+
PkgName *string `koanf:"pkgname"`
484+
Recursive *bool `koanf:"recursive"`
483485
// ReplaceType is a nested map of format map["package path"]["type name"]*ReplaceType
484486
ReplaceType map[string]map[string]*ReplaceType `koanf:"replace-type"`
485487
Template *string `koanf:"template"`

0 commit comments

Comments
 (0)