hel

command module
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 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 reside.

This little tool generates (hopefully simple) mocks of Go interface types and stores them in helheim_test.go (by default).

Motivation

I wanted mocks that are:

  • Provably correct
    • Generated code must be trivial
    • Non-trivial code must come from tested libraries
  • Goroutine-safe in every valid use case
    • Method calls are handled via goroutine-safe types, no field access in typical use cases.
  • Capable of tracking sequences of calls.
    • Channels are great for this.
  • Generated based on what the business logic uses, not what it implements.
    • We generate mocks for any interface types that the local code depends on, and the mocks are not exported.
  • Does not encourage mocks in a non-test package.
    • Using imported mocks is a code smell.

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@latest

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.
ret

Jump to

Keyboard shortcuts

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