hel

command module
v0.4.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2023 License: Unlicense Imports: 13 Imported by: 0

README

Package Docs Build Status

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:

  1. I felt like messing more with Go's ast package.
  2. 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

Documentation

Overview

Package main implements the hel command.

Directories

Path Synopsis
pkg
pers
Package pers (hel...pers ...
Package pers (hel...pers ...
Package sel contains some utilities for working with select statements and channels (or structs of channels).
Package sel contains some utilities for working with select statements and channels (or structs of channels).
Package types contains logic for parsing type definitions from ast packages and filtering those types.
Package types contains logic for parsing type definitions from ast packages and filtering those types.
Package vegr (hel...vegr - the road to helheim) is a place to store library calls that hel mocks need in order to work.
Package vegr (hel...vegr - the road to helheim) is a place to store library calls that hel mocks need in order to work.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL