gomic

module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2019 License: MIT

README

gomic

GoDoc Build Status codecov Go Report Card GitHub last commit GitHub tag License

CLI tool to generate golang's mock for the test.

Features

  • Manage mocks with a configuration file, so it is easy to update mocks when interfaces are updated
  • Simple and flexible API. Complicated API and DSL aren't provided. So the learning cost is low and the test code is easy to read

Install

gomic is written with Golang and binary is distributed at the release page, so installation is easy and no dependency is needed.

Getting Started

  1. Write an interface.
type (
	OS interface {
		Getwd() (string, error)
		Mkdir(name string, perm os.FileMode) error
	}
)
  1. Generate the configuration file.
$ gomic init
  1. Edit the configuration file.
---
default:
  interface_suffix: Mock
items:
- src:
    file: os.go
    interface: OS
  dest:
    package: examples
    file: os_mock.go
- src:
    package: io
    interface: ReadCloser
  dest:
    package: examples
    file: readcloser_mock.go
  1. Generate mocks.
$ gomic gen

Note that the gen command overwrites the existing file without confirmation.

Examples

See examples.

Configuration

---
# the file path must be absolute or relative to the configuration file path.
# `default` is the default settings of each item. item's settings are preferred than default settings.
default:
#   vendor_dir: ""
#   interface_prefix: Mock
#   interface_suffix: Mock
items:
- src:
    # `package` or `file` or `dir` are required
    # `package` is the source package path.
    # `file` is the source file path.
    # `dir` is the source directory path.
    package: github.com/suzuki-shunsuke/gomic/examples
    # file: examples/example.go
    # source interface name. This is required.
    interface: Hello
    # generated mock name
    name: HelloMock
    # If `name` is not given, name is "{{interface_prefix}}{{interface}}{{interface_suffix}}".
    # If `name` is given, `interface_prefix` and `interface_suffix` are ignored.
    # interface_prefix: Mock
    # interface_suffix: Mock
    # `vendor_dir` is the path of the parent directory of `vendor`.
    # `vendor_dir` should be absolute path or relative to configuration file's parent directory.
    # By default `vendor_dir` is the configuration file's parent directory.
    #  vendor_dir: ""
  dest:
    # generated file's package name
    # If `package` is not set, gomic tries to get the package name with the file's parent directory path.
    package: example
    # output file path
    # `file` is required.
    # Currently it is not supported to output multiple mocks in the same file.
    # The parent directory must exist.
    file: example/example_mock.go

Note

  • gen command overwrites the existing file without confirmation
  • Before running the gen command, packages which the interface depends on should be installed at GOPATH or vendor directory. gomic supports vendor directory.
  • Currently, it is not supported to output multiple mocks in the same file
  • The generated code is not formatted. So we recommend to format them by gofmt.

Other Mocking Libraries

Troble shooting

go: cannot find GOROOT directory

If you encounter the following error when you run gomic gen, try to set the environment variable GOROOT.

go/build: importGo %s: exit status 2
go: cannot find GOROOT directory: %s
$ go version
go version go1.12.6 darwin/amd64
$ gomic -v
gomic version 0.5.6
$ echo $GOROOT  # GOROOT isn't defined

$ cat .gomic.yml
---
items:
- src:
    package: io
    interface: Writer
  dest:
    package: mock
    file: mock/io_writer.go
$ gomic gen
go/build: importGo io: exit status 2
go: cannot find GOROOT directory: /usr/local/go

We don't know the root cause, but we can resolve this issue in this way.

$ go env GOROOT
/usr/local/Cellar/go/1.12.6/libexec
GOROOT=`go env GOROOT` gomic gen

Change Log

Please see the Releases.

Contributing

Please see the CONTRIBUTING.md.

License

MIT

Directories

Path Synopsis
cmd
Package examples is an example of gomic.
Package examples is an example of gomic.
internal

Jump to

Keyboard shortcuts

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