Commit Graph

181 Commits

Author SHA1 Message Date
Umputun
be64288727
add skip-ensure flag to avoid import cycle (#140)
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
2020-09-17 09:49:08 +01:00
matryer
a69ca93dec ignored dist folder 2020-09-02 10:36:09 +01:00
matryer
545b7d74ae fixed whitespace 2020-09-02 10:34:06 +01:00
matryer
9581d77413 testing releasing 2020-09-02 10:30:40 +01:00
Suhas Karanth
720d53e65d Run go generate
Minor changes to generated test file because of c5b1da6 (#128).
2020-08-16 16:55:11 +05:30
Suhas Karanth
005b899ec8
Add noop formatter as an option (#133)
Useful to print the generated source code for debugging.
2020-08-16 12:55:01 +05:30
Suhas Karanth
e3d1bd8a7e Update README for stub flag 2020-08-16 12:29:28 +05:30
Suhas Karanth
83ab8dd79f
Add -stub flag to stub func implementation (#132)
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>
2020-08-16 12:24:12 +05:30
Suhas Karanth
24883c20a6
Use fallback name for parameter when name is _ (#131) 2020-08-16 12:16:52 +05:30
Suhas Karanth
70fed06ee3
Check errors on loading package (#130) 2020-08-16 12:10:38 +05:30
Suhas Karanth
c5b1da6fe9
Move mutexes inside mock struct (#128)
Better to not be constrained by global locks when having multiple moq
instances.

Co-authored-by: Diego Bernardes <diego.bernardes@outlook.com>
2020-08-16 12:03:17 +05:30
Suhas Karanth
80177adba8 Replace Travis CI with GitHub Actions workflow 2020-08-16 11:56:22 +05:30
Suhas Karanth
1163963381 Migrate to Go modules 2020-08-16 11:56:22 +05:30
Suhas Karanth
af008c214b Revert "added go modules"
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.
2020-08-16 08:49:03 +05:30
Mat Ryer
1822a01c56 simpler build list 2020-08-15 18:02:13 +01:00
Mat Ryer
f76652f379 added go modules 2020-08-15 17:59:44 +01:00
Suhas Karanth
4638a53893
No variadic return types (#126)
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.
2020-06-07 18:15:40 +05:30
Suhas Karanth
7721994d1b
Add fmt flag to specify formatter (#117)
- 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.
2020-03-10 18:38:14 +05:30
Suhas Karanth
7615cbe602
Improve Windows compatibility (#118)
- 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>
2020-01-25 16:51:10 +05:30
Suhas Karanth
072c0cd09b
Handle target interface inside vendor with other pkg name (#122)
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.
2020-01-20 07:16:57 +05:30
Suhas Karanth
8f50fb6793
Avoid deprecated LoadModes packages.{LoadFiles, LoadSyntax} (#116)
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.
2020-01-17 10:25:50 +05:30
Suhas Karanth
75d0ddfc00
Run go generate (#119)
Minor changes to generated test file because of #110.
2020-01-06 18:41:00 +05:30
Suhas Karanth
4203548722 Run travis CI only on master branch and PRs (#114)
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>
2019-12-23 15:52:52 +00:00
Suhas Karanth
fc1865052c Refactor flags, add run() for better extensibility (#115) 2019-12-23 15:10:26 +00:00
Suhas Karanth
0e0395200a
Improve comment on type guard in template (#113)
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.
2019-11-06 08:58:47 +05:30
Suhas Karanth
1206bf1e2a
Allow aliasing interface name (#110)
Implementation based on #57.
2019-11-05 13:13:49 +05:30
Lucas Bremgartner
382bd174c0 Ensure target dir for mock exists (#112)
Fixes: #111
2019-11-03 15:12:56 +00:00
Vegard Løkken
ea57f91120 Fix typo in README (#107) 2019-11-03 07:54:11 +00:00
Pedram Hajesmaeeli
5e2b3bdd91 Fix moq for go 1.13 and avoid creating empty directory (#105)
* Fix moq for go 1.13 and avoid creating empty directory

This fixes #103 and also fixes #102

* Update .travis.yml
2019-11-03 07:46:59 +00:00
Mat Ryer
6cfb0558e1
Merge pull request #93 from IvanSafonov/master
fix cyclic dependency with go modules
2019-03-12 15:43:09 +00:00
Ivan Safonov
2fdf5e1752 fix cyclic dependency with go modules 2019-03-01 00:22:20 +07:00
Ivan Safonov
e17abc4d5d fix cyclic dependency with go modules 2019-02-28 23:49:51 +07:00
Mat Ryer
a838c8bc30
Merge pull request #89 from IvanSafonov/master
Go modules support
2019-02-26 12:02:13 +00:00
Mat Ryer
c26fdca584
Merge branch 'master' into master 2019-02-11 10:48:04 +00:00
Mat Ryer
70d7959eb2
Merge pull request #87 from breml/fix-imports
Add import, if source pkg is not destination pkg
2019-02-11 10:46:50 +00:00
Ivan Safonov
6c76dadc23 Support go modules by using go/packages instead of go/loader 2019-01-23 21:36:46 +07:00
Lucas Bremgartner
81c463c9b9 Add import, if source pkg is not destination pkg
Fixes #86
2019-01-22 21:40:29 +01:00
Ivan Safonov
1174b6968d Support go modules by using go/packages instead of go/loader 2019-01-23 00:52:12 +07:00
Mat Ryer
39f6db0ed3
Merge pull request #73 from HaraldNordgren/TestVendoredBuildConstraints
Create 'TestVendoredBuildConstraints'
2019-01-21 17:46:39 +00:00
Mat Ryer
effe366417
Merge branch 'master' into TestVendoredBuildConstraints 2019-01-21 17:39:53 +00:00
Mat Ryer
bb199b05e0
Merge pull request #59 from breml/static-interface-check
Add static interface implementation check
2019-01-21 17:39:36 +00:00
Mat Ryer
5b2282d8e5
Merge branch 'master' into static-interface-check 2019-01-21 17:22:41 +00:00
Mat Ryer
055cc3eebc
Merge pull request #80 from breml/abs-path-modules
Import package by files if outside of GOPATH
2019-01-21 17:22:14 +00:00
Mat Ryer
2647bdb8ea
Merge branch 'master' into TestVendoredBuildConstraints 2019-01-21 17:17:03 +00:00
Mat Ryer
7855d5d508
Merge branch 'master' into static-interface-check 2019-01-21 17:15:50 +00:00
Mat Ryer
84ab8b08ea
Merge branch 'master' into abs-path-modules 2019-01-21 17:12:59 +00:00
Mat Ryer
18bfd0159a
Merge pull request #82 from breml/update-go-generate
Update mocks with go generate
2019-01-21 17:12:46 +00:00
Lucas Bremgartner
b21592468b Add static interface implementation check
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.
2019-01-17 20:20:37 +01:00
Lucas Bremgartner
2eb3df732a Update mocks with go generate 2019-01-17 20:19:34 +01:00
Lucas Bremgartner
d95e87fb17 Import package by files if outside of GOPATH
This is for example the case, if a mock is generated for an external
interface while go modules are enabled.

Fixes: #79
2019-01-17 20:18:30 +01:00