File is removed before mock generation, if it exists. This is useful
when the mock generation is likely to fail due to some reason, for
example, package load failure due to change in the interface being
mocked (the existing mock is no longer valid and cannot be compiled). In
such cases, the -rm flag can be used instead of manually removing the
file (which could have fixed the issue).
When the type and the package name is the same for an anonymous
parameter (ex: time.Time), and there are more than 1 such parameters,
the generated name for both was the same. And the generated code would
not be valid.
Fix the bug by ensuring the parameter name does not conflict with
package imports first before checking against other parameter names.
* Internal registry for disambiguated imports, vars
- Move functionality in the moq package partially into
internal/{registry,template}.
- Leverage registry to assign unique package and variable/method
parameter names. Use import aliases if present in interface source
package.
BREAKING CHANGE: When the interface definition does not mention the
parameter names, the field names in call info anonymous struct will be
different.
The new field names are generated using the type info (string -> s,
int -> n, chan int -> intCh, []MyType -> myTypes, map[string]int ->
stringToInt etc.).
For example, for a string parameter previously if the field name was
'In1', the new field could be 'S' or 'S1' (depends on number of
string method parameters).
* Refactor golden file tests to be table-driven
* Fix sync pkg alias handling for moq generation
* Improve, add tests (increase coverage)
* Use $.Foo in template, avoid declaring variables
$ is set to the data argument passed to Execute, that is, to the
starting value of dot.
Variables were declared to be able to refer to the parent context.
* Consistent template field formatting
* Use tabs in generated Godoc comments' example code
* Minor simplification
* go generate
* Fix conflict for generated param name of pointer type
Excellent work by @sudo-suhas.
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.