![Build Status](https://builds.sr.ht/~nelsam/hel.svg)
Hel
Hel is the norse goddess that rules over Helheim, where the souls
of those who did not die in battle go.
This little tool is similar; it generates (hopefully simple) mocks
of Go interface types and stores them in helheim_test.go (by default).
Motivation
There are plenty of mock generators out there, but:
- I felt like messing more with Go's
ast
package.
- The mock generators that I know about seem to generate complicated
mocks, and often generate their mocks as exported types in a
separate package (by default).
- A side note on exported mocks: Exporting mocks seems to, on some
projects, encourage the idea that an interface defines everything
that a type is capable of. In Go, I find that to be a bad
practice; local interfaces should be defined as functionality that
the local logic needs, and concrete types implementing said
functionality should be passed in. This keeps interfaces very
small, which keeps mocks (mostly) simple.
I wanted a mock generator that:
- would generate code that was easy to prove correctness in.
- is easy to use correctly (and correspondingly, hard to use incorrectly) in
highly concurrent tests.
- generates mocks in test files, rather than being exported in some sub-package
for tests to import.
- generates mocks for types that are actually in use by the business logic,
rather than some giant monolith of an interface type that contains every
method a type implements.
Installation
Pre-reqs
Hel shells out to goimports
to set up its import
clause(s), so you'll need that installed somewhere
in your PATH
.
Go Install
Hel is go-installable. We got to v4 before realizing that modules work a lot
better if you stay at v0, so we're back at v0.x now.
go install git.sr.ht/~nelsam/hel
Usage
At its simplest, you can just run hel
without any options in the
directory you want to generate mocks for. Mocks will be saved in a
file called helheim_test.go
by default.
See hel -h
or hel --help
for command line options. Most flags
allow multiple calls (e.g. -t ".*Foo" -t ".*Bar"
).
Which types will be mocked?
By default, hel looks at each package it needs to generate mocks for and
(recursively) generates mocks for:
- Any locally defined interface type.
- Any interface type used as a parameter by any function or method.
- Any interface type used as a field in a struct.
- For every interface it will generate a mock for:
- Any interface type used as a parameter or return type by methods on that
interface.
By default, only exported functions, types, and methods will be considered. If
you pass the --no-test-package
option, then unexported functions, types, and
methods will be considered for the package that mocks are being generated for.
Go Generate
Adding comments for go generate
to use Hel is relatively flexible.
Some examples:
In a file (e.g. generate.go
) in the root of your project:
//go:generate hel --package ./...
The above command would find all exported interface types in the
project and generate mocks in helheim_test.go
in each of the
packages it finds interfaces to mock.
In a file (e.g. generate.go
) in each package you want mocks to be generated for:
//go:generate hel
The above command would generate mocks for all exported types in
the current package in helheim_test.go
Above each interface type you want a mock for
//go:generate hel --type Foo --output mock_foo_test.go
type Foo interface {
Foo() string
}
The above command would generate a mock for the Foo type in
mock_foo_test.go