For mocks generated outside of the tested package with tests lives inside the same package as the tested code (i.e. pkg_test not used) --skip-ensure suppresses import of the source pkg
https://github.com/matryer/moq/issues/139
fix typo in readme
When a mock is generated with the flag enabled, it introduces the
following changes:
- Does not panic on calling the method without a mock implementation.
- Return zero values iff the implementation is not provided and the
method has return parameters.
Co-authored-by: Scott Leuthaeuser <scott_leuthaeuser@homedepot.com>
This reverts commit f76652f379.
Go test no longer picks up the tests when run from the project root.
$ go test -v ./...
? github.com/matryer/moq [no test files]
testing: warning: no tests to run
PASS
ok github.com/matryer/moq/example (cached) [no tests to run]
? github.com/matryer/moq/generate [no test files]
Migrating to Go modules needs special handling for fake modules such as
github.com/matryer/buildconstraints.
While extracting the method and parameters from the method signature,
only check and apply the variadic flag for the input parameters and not
for the return arguments.
Issue details:
For an interface with variadic arguments and slice return type, moq was
generating an invalid mock:
type I interface {
Func(params ...interface{}) []byte
}
// ...
type IMock struct {
// FuncFunc mocks the Func method.
FuncFunc func(params ...interface{}) ...byte
// calls tracks calls to the methods.
calls struct {
// Func holds details about calls to the Func method.
Func []struct {
// Params is the params argument value.
Params []interface{}
}
}
}
On attempting to generate the mock in such an instance, the command
would fail on the formatting step:
m.Mock: go/format: 35:30: expected ';', found '...' (and 3 more errors)
See https://github.com/matryer/moq/issues/124,
https://github.com/matryer/moq/pull/125.
- Support formatting output with gofmt(default) or goimports via flag.
- Introduce moq.Config struct to configure moq.Mocker instance. This
breaks backward compatibility but facilitates it in the future if and
when any features are added.
- Use golden file tests for validating formatters. Use
github.com/pmezard/go-difflib to print the diff between expected and
actual.
- Use travis build matrix to test on Windows and macOS
- Make utility func compatible with host OS. Use filepath package to
manipulate path strings for stripping $GOPATH from working directory.
- Delegate to go/build pkg for getting Go paths
Co-authored-by: Mike Lee <mike.lee@safeguardproperties.com>
Co-authored-by: Lucas Bremgartner <lucas@bremis.ch>
When the interface that we are trying to moq is present inside the vendor
directory and the output package name is different, the import needs to be
stripped of the vendor path.
From the docs for packages.{LoadFiles, LoadSyntax} -
https://godoc.org/golang.org/x/tools/go/packages#pkg-constants
// Deprecated: LoadFiles exists for historical compatibility
// and should not be used. Please directly specify the needed
// fields using the Need values.
// ...
// Deprecated: LoadSyntax exists for historical compatibility
// and should not be used. Please directly specify the needed
// fields using the Need values.
If a PR is created using a branch pushed to origin, this triggers travis
CI twice, once for the PR and another for the branch. So run travis CI for
only the master branch and when PRs are created using origin branches.
If CI needs to be run on any specific branch other than master, it would
need to be whitelisted.
Co-authored-by: Mat Ryer <matryer@users.noreply.github.com>
If the user specifies a different package and the mock name is the same
as the interface name, the type guard comment will be of the following
format:
// Ensure, that MyInterface does implement MyInterface.
So use the $sourcePackagePrefix in the comment.
Add an additional line of code per interface to the generated mock file,
which allows the go compiler to statically check if the mock implements
the mocked interface.