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

Add file prefix flag #32

Merged
merged 2 commits into from
Jun 6, 2022
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## [Unreleased]

No changes yet.

- Added `--file-prefix` flag. [#32](https://github.com/derision-test/go-mockgen/pull/32)

## [v1.3.0] - 2022-06-06

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The following flags are defined by the binary.
| disable-formatting | | Do not run goimports over the rendered files (enabled by default). |
| goimports | | Path to the goimports binary (uses goimports on your PATH by default). |
| for-test | | Append _test suffix to generated package names and file names. |
| file-prefix | | Content that is written at the top of each generated file. |

### Configuration file

Expand All @@ -52,7 +53,7 @@ mocks:
filename: foo/bar/mock_cache_test.go
```

The top level of the configuration file may also set the keys `exclude`, `prefix`, `constructor-prefix`, `goimports`, `force`, `disable-formatting`, and `for-tests`. Top-level excludes will also be applied to each mock generator entry. The values for prefixes and goimports will apply to each mock generator entry if a value is not set. The remaining boolean values will be true for each mock generator entry if set at the top level (regardless of the setting of each entry).
The top level of the configuration file may also set the keys `exclude`, `prefix`, `constructor-prefix`, `goimports`, `file-prefix`, `force`, `disable-formatting`, and `for-tests`. Top-level excludes will also be applied to each mock generator entry. The values for interface and constructor prefixes, goimports, and file content prefixes will apply to each mock generator entry if a value is not set. The remaining boolean values will be true for each mock generator entry if set at the top level (regardless of the setting of each entry).

## Testing with Mocks

Expand Down
7 changes: 7 additions & 0 deletions cmd/go-mockgen/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func parseFlags() (*generation.Options, error) {
app.Flag("disable-formatting", "Do not run goimports over the rendered files.").BoolVar(&opts.DisableFormatting)
app.Flag("goimports", "Path to the goimports binary.").Default("goimports").StringVar(&opts.GoImportsBinary)
app.Flag("for-test", "Append _test suffix to generated package names and file names.").Default("false").BoolVar(&opts.ForTest)
app.Flag("file-prefix", "Content that is written at the top of each generated file.").StringVar(&opts.FilePrefix)

if _, err := app.Parse(os.Args[1:]); err != nil {
return nil, err
Expand All @@ -99,6 +100,7 @@ func parseManifest() ([]*generation.Options, error) {
DisableFormatting bool `yaml:"disable-formatting"`
Goimports string `yaml:"goimports"`
ForTest bool `yaml:"for-test"`
FilePrefix string `yaml:"file-prefix"`

Mocks []struct {
Path string `yaml:"path"`
Expand All @@ -115,6 +117,7 @@ func parseManifest() ([]*generation.Options, error) {
DisableFormatting bool `yaml:"disable-formatting"`
Goimports string `yaml:"goimports"`
ForTest bool `yaml:"for-test"`
FilePrefix string `yaml:"file-prefix"`
} `yaml:"mocks"`
}
if err := yaml.Unmarshal(contents, &payload); err != nil {
Expand All @@ -136,6 +139,9 @@ func parseManifest() ([]*generation.Options, error) {
if opts.Goimports == "" {
opts.Goimports = payload.Goimports
}
if opts.FilePrefix == "" {
opts.FilePrefix = payload.FilePrefix
}

// Overwrite
if payload.Force {
Expand Down Expand Up @@ -171,6 +177,7 @@ func parseManifest() ([]*generation.Options, error) {
DisableFormatting: opts.DisableFormatting,
GoImportsBinary: opts.Goimports,
ForTest: opts.ForTest,
FilePrefix: opts.FilePrefix,
})
}

Expand Down
12 changes: 9 additions & 3 deletions internal/mockgen/generation/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Options struct {
DisableFormatting bool
GoImportsBinary string
ForTest bool
FilePrefix string
}

func Generate(ifaces []*types.Interface, opts *Options) error {
Expand Down Expand Up @@ -106,7 +107,7 @@ func generateAndRender(ifaces []*types.Interface, filename string, opts *Options
pkgName += "_test"
}

content, err := generateContent(ifaces, pkgName, opts.Prefix, opts.ConstructorPrefix, opts.OutputImportPath)
content, err := generateContent(ifaces, pkgName, opts.Prefix, opts.ConstructorPrefix, opts.FilePrefix, opts.OutputImportPath)
if err != nil {
return err
}
Expand All @@ -132,9 +133,14 @@ func generateAndRender(ifaces []*types.Interface, filename string, opts *Options
return nil
}

func generateContent(ifaces []*types.Interface, pkgName, prefix, constructorPrefix, outputImportPath string) (string, error) {
func generateContent(ifaces []*types.Interface, pkgName, prefix, constructorPrefix, fileContentPrefix, outputImportPath string) (string, error) {
if fileContentPrefix != "" {
separator := "\n// "
fileContentPrefix = "\n//" + separator + strings.Join(strings.Split(strings.TrimSpace(fileContentPrefix), "\n"), separator)
}

file := jen.NewFile(pkgName)
file.HeaderComment(fmt.Sprintf("Code generated by %s %s; DO NOT EDIT.", consts.Name, consts.Version))
file.HeaderComment(fmt.Sprintf("// Code generated by %s %s; DO NOT EDIT.%s", consts.Name, consts.Version, fileContentPrefix))

for _, iface := range ifaces {
log.Printf("generating code for interface '%s'\n", iface.Name)
Expand Down