-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for a configuration file
Added support for a kelpie.yaml file to specify the mocks to generate. The advantage of this is that we can avoid re-parsing the same package multiple times if we're generating more than one mock from it. The existing `go:generate` method of generation is still supported - this will be up to personal preference. I've also added some extra output to the generate command to make it easier to tell if the mocks have been generated correctly or not.
- Loading branch information
1 parent
020011e
commit c1bacd3
Showing
21 changed files
with
388 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${workspaceFolder}/cmd/kelpie", | ||
"cwd": "${workspaceFolder}", | ||
"args": ["generate"] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,9 @@ Kelpie is the most magical mock generator for Go. Kelpie aims to be easy to use, | |
|
||
At the moment Kelpie is very much in development, and there are missing features and some pretty rough edges. You're of course welcome to use Kelpie, but just be prepared to hit problems and raise issues or PRs! | ||
|
||
The following is a list of known-outstanding features: | ||
The following is a list of known-outstanding features and known issues: | ||
|
||
- [ ] Add the ability to customize the name, package and output folder of the generated mocks. | ||
- [ ] Switch from `go:generate` to using a config file for more efficient mock generation in large code-bases. | ||
- [ ] For some reason Kelpie is generating multiple mocks when specifying built-in interfaces. See the [Mock Generation](#mock-generation) section for an example. | ||
|
||
## Quickstart | ||
|
||
|
@@ -54,6 +53,58 @@ service.Send("[email protected]", "[email protected]", "Hello") | |
|
||
## Using Kelpie | ||
|
||
### Mock Generation | ||
|
||
There are two main ways to generate your mocks: | ||
|
||
1. Using `go:generate` comments. | ||
2. Using a kelpie.yaml file. | ||
|
||
Using `go:generate` comments is simple - take a look at the [Quickstart](#quickstart) for an example. | ||
|
||
The other option is to add a kelpie.yaml file to your repo. The advantage of this is that all of your mocks are defined in one place, and mock generation can be significantly quicker than the `go:generate` approach because it avoids unnecessary duplicate parsing. | ||
|
||
To do this, add a kelpie.yaml file to the root of your repo like this: | ||
|
||
```yaml | ||
version: 1 | ||
packages: | ||
# You can mock packages that aren't part of your repo. To do this just specify the package | ||
# name as normal: | ||
- package: io | ||
# When mocking packages outside your source tree, remember to specify the directory the | ||
# mocks should be generated in. | ||
directory: examples/mocks | ||
mocks: | ||
- interface: Reader | ||
- package: github.com/adamconnelly/kelpie/examples | ||
# Mocks defines the interfaces within your package that you want to generate mocks for. | ||
mocks: | ||
- interface: Maths | ||
- interface: RegistrationService | ||
generation: | ||
# Package sets the package name generated for the mock. By default the package name | ||
# is the lower-cased interface name. | ||
package: regservice | ||
``` | ||
To generate the mocks, just run `kelpie generate`: | ||
|
||
```shell | ||
$ kelpie generate | ||
Kelpie mock generation starting - preparing to add some magic to your code-base! | ||
Parsing package 'io' for interfaces to mock. | ||
- Generating a mock for 'Reader'. | ||
- Generating a mock for 'Reader'. | ||
Parsing package 'github.com/adamconnelly/kelpie/examples' for interfaces to mock. | ||
- Generating a mock for 'Maths'. | ||
- Generating a mock for 'RegistrationService'. | ||
Mock generation complete! | ||
``` | ||
|
||
### Default Behaviour | ||
|
||
No setup, no big deal. Kelpie returns the default values for method calls instead of panicking: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
// ConfigVersion defines the version of Kelpie's config file. | ||
type ConfigVersion string | ||
|
||
const ( | ||
// ConfigVersion1 is v1 of the config file. | ||
ConfigVersion1 ConfigVersion = "1" | ||
) | ||
|
||
// Config represents Kelpie's generation config. | ||
type Config struct { | ||
// Version is the version of the config file. | ||
Version ConfigVersion | ||
|
||
// Packages contains the configuration of the packages to generate mocks from. | ||
Packages []PackageConfig | ||
} | ||
|
||
// PackageConfig defines the configuration for a single package to mock. | ||
type PackageConfig struct { | ||
// PackageName is the full path of the package, for example github.com/adamconnelly/kelpie/examples. | ||
PackageName string `yaml:"package"` | ||
|
||
// Mocks contains the list of interfaces to mock. | ||
Mocks []MockConfig | ||
|
||
// Output directory is the directory to output generated mocks for this package. Defaults | ||
// to a folder called "mocks" in the package directory if not specified. | ||
OutputDirectory string `yaml:"directory"` | ||
} | ||
|
||
// MockConfig is configuration of an individual mock. | ||
type MockConfig struct { | ||
// InterfaceName is the name of the interface to mock, for example "Maths" or "SomeService.SomeRepository". | ||
InterfaceName string `yaml:"interface"` | ||
|
||
// GenerationOptions allows generation of the mock to be customized. | ||
GenerationOptions MockGenerationOptions `yaml:"generation"` | ||
} | ||
|
||
// MockGenerationOptions allows generation of the mock to be customized. | ||
type MockGenerationOptions struct { | ||
// PackageName is the name of the generated package for the mock. Defaults to the lowercased | ||
// interface name. For example an interface called EmailSender would generate a package | ||
// called emailsender. | ||
PackageName string `yaml:"package"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import ( | |
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/adamconnelly/kelpie/examples/users" | ||
"github.com/adamconnelly/kelpie/examples/users/mocks/userrepository" | ||
"github.com/adamconnelly/kelpie/examples/users/mocks/userrepo" | ||
) | ||
|
||
type LocalTypesTests struct { | ||
|
@@ -15,8 +15,8 @@ type LocalTypesTests struct { | |
|
||
func (t *LocalTypesTests) Test_CanReturnAValue() { | ||
// Arrange | ||
mock := userrepository.NewMock() | ||
mock.Setup(userrepository.FindUserByUsername("[email protected]").Return(&users.User{ID: 123}, nil)) | ||
mock := userrepo.NewMock() | ||
mock.Setup(userrepo.FindUserByUsername("[email protected]").Return(&users.User{ID: 123}, nil)) | ||
|
||
// Act | ||
user, err := mock.Instance().FindUserByUsername("[email protected]") | ||
|
@@ -29,8 +29,8 @@ func (t *LocalTypesTests) Test_CanReturnAValue() { | |
|
||
func (t *LocalTypesTests) Test_CanMatchOnALocalType() { | ||
// Arrange | ||
mock := userrepository.NewMock() | ||
mock.Setup(userrepository.GetAllUsersOfType(users.UserTypeAdmin).Return([]users.User{{ID: 1}, {ID: 2}}, nil)) | ||
mock := userrepo.NewMock() | ||
mock.Setup(userrepo.GetAllUsersOfType(users.UserTypeAdmin).Return([]users.User{{ID: 1}, {ID: 2}}, nil)) | ||
|
||
// Act | ||
admins, err := mock.Instance().GetAllUsersOfType(users.UserTypeAdmin) | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.