From e3bc929cada0377f07d66329082d44b2ffd28255 Mon Sep 17 00:00:00 2001 From: David Smith Date: Sat, 8 Apr 2017 07:58:38 +0000 Subject: [PATCH 1/4] Add fixture target to Makefile --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ebd22a45..bfd7356a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ SHELL=bash -all: clean fmt test install integration +all: clean fmt test fixture install integration clean: rm -rf mocks @@ -11,6 +11,9 @@ fmt: test: go test ./... +fixture: + mockery -print -dir mockery/fixtures -name RequesterVariadic > mockery/fixtures/mocks/requester_variadic.go + install: go install ./... From 56a046ffa6f1e28780c2220804f080a40e2a49b8 Mon Sep 17 00:00:00 2001 From: David Smith Date: Sat, 8 Apr 2017 08:01:03 +0000 Subject: [PATCH 2/4] Compare text lines instead of full in GeneratorSuite --- mockery/generator_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mockery/generator_test.go b/mockery/generator_test.go index 12972642..1ea709ca 100644 --- a/mockery/generator_test.go +++ b/mockery/generator_test.go @@ -51,8 +51,13 @@ func (s *GeneratorSuite) checkGeneration( ) *Generator { generator := s.getGenerator(filepath, interfaceName, inPackage) s.NoError(generator.Generate(), "The generator ran without errors.") + + // Compare lines for easier debugging via testify's slice diff output + expectedLines := strings.Split(expected, "\n") + actualLines := strings.Split(generator.buf.String(), "\n") + s.Equal( - expected, generator.buf.String(), + expectedLines, actualLines, "The generator produced the expected output.", ) return generator From d0a5d341ed2999af3d2137661397d3f2485bea08 Mon Sep 17 00:00:00 2001 From: David Smith Date: Sat, 8 Apr 2017 10:14:15 +0000 Subject: [PATCH 3/4] Allow generator fixtures to use formatted source --- mockery/fixtures/mocks/requester_variadic.go | 3 +++ mockery/generator_test.go | 24 +++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/mockery/fixtures/mocks/requester_variadic.go b/mockery/fixtures/mocks/requester_variadic.go index 97bec2d4..8f6216ad 100644 --- a/mockery/fixtures/mocks/requester_variadic.go +++ b/mockery/fixtures/mocks/requester_variadic.go @@ -27,6 +27,7 @@ func (_m *RequesterVariadic) Get(values ...string) bool { return r0 } + // MultiWriteToFile provides a mock function with given fields: filename, w func (_m *RequesterVariadic) MultiWriteToFile(filename string, w ...io.Writer) string { _va := make([]interface{}, len(w)) @@ -47,6 +48,7 @@ func (_m *RequesterVariadic) MultiWriteToFile(filename string, w ...io.Writer) s return r0 } + // OneInterface provides a mock function with given fields: a func (_m *RequesterVariadic) OneInterface(a ...interface{}) bool { var _ca []interface{} @@ -62,6 +64,7 @@ func (_m *RequesterVariadic) OneInterface(a ...interface{}) bool { return r0 } + // Sprintf provides a mock function with given fields: format, a func (_m *RequesterVariadic) Sprintf(format string, a ...interface{}) string { var _ca []interface{} diff --git a/mockery/generator_test.go b/mockery/generator_test.go index 1ea709ca..67671067 100644 --- a/mockery/generator_test.go +++ b/mockery/generator_test.go @@ -1,6 +1,7 @@ package mockery import ( + "go/format" "io/ioutil" "path/filepath" "testing" @@ -52,9 +53,19 @@ func (s *GeneratorSuite) checkGeneration( generator := s.getGenerator(filepath, interfaceName, inPackage) s.NoError(generator.Generate(), "The generator ran without errors.") + // Mirror the formatting done by normally done by golang.org/x/tools/imports in Generator.Write + // + // While we could possibly reuse Generator.Write here in addition to Generator.Generate, + // it would require changing Write's signature to accept custom options, specifically to + // allow the fragmeents already used in test cases. It's assumed that this approximation, + // just formatting the source, is sufficient for the needs of the current test styles. + var actual []byte + actual, fmtErr := format.Source(generator.buf.Bytes()) + s.NoError(fmtErr, "The formatter ran without errors.") + // Compare lines for easier debugging via testify's slice diff output expectedLines := strings.Split(expected, "\n") - actualLines := strings.Split(generator.buf.String(), "\n") + actualLines := strings.Split(string(actual), "\n") s.Equal( expectedLines, actualLines, @@ -604,8 +615,7 @@ func (s *GeneratorSuite) TestGeneratorVariadicArgs() { // Read the expected output from a "golden" file that we can also import in CompatSuite // to asserts its actual behavior. // - // NOTE: After regenerating the mock, the blank lines between the method blocks currently - // need to be removed due to possible defect in the checker. + // To regenerate the golden file: make fixture expectedBytes, err := ioutil.ReadFile(filepath.Join(fixturePath, "mocks", "requester_variadic.go")) s.NoError(err) expected := string(expectedBytes) @@ -626,6 +636,7 @@ type Fooer struct { func (_m *Fooer) Bar(f func([]int)) { _m.Called(f) } + // Baz provides a mock function with given fields: path func (_m *Fooer) Baz(path string) func(string) string { ret := _m.Called(path) @@ -641,6 +652,7 @@ func (_m *Fooer) Baz(path string) func(string) string { return r0 } + // Foo provides a mock function with given fields: f func (_m *Fooer) Foo(f func(string) string) error { ret := _m.Called(f) @@ -681,6 +693,7 @@ func (_m *AsyncProducer) Input() chan<- bool { return r0 } + // Output provides a mock function with given fields: func (_m *AsyncProducer) Output() <-chan bool { ret := _m.Called() @@ -696,6 +709,7 @@ func (_m *AsyncProducer) Output() <-chan bool { return r0 } + // Whatever provides a mock function with given fields: func (_m *AsyncProducer) Whatever() chan bool { ret := _m.Called() @@ -779,6 +793,7 @@ func (_m *ConsulLock) Lock(_a0 <-chan struct{}) (<-chan struct{}, error) { return r0, r1 } + // Unlock provides a mock function with given fields: func (_m *ConsulLock) Unlock() error { ret := _m.Called() @@ -904,6 +919,7 @@ func (_m *Example) A() http.Flusher { return r0 } + // B provides a mock function with given fields: _a0 func (_m *Example) B(_a0 string) fixtureshttp.MyStruct { ret := _m.Called(_a0) @@ -943,6 +959,7 @@ func (_m *ImportsSameAsPackage) A() test.B { return r0 } + // B provides a mock function with given fields: func (_m *ImportsSameAsPackage) B() fixtures.KeyManager { ret := _m.Called() @@ -958,6 +975,7 @@ func (_m *ImportsSameAsPackage) B() fixtures.KeyManager { return r0 } + // C provides a mock function with given fields: _a0 func (_m *ImportsSameAsPackage) C(_a0 fixtures.C) { _m.Called(_a0) From 42ce937fba6ed0b1438a16575f38ed2ea7b9f4ff Mon Sep 17 00:00:00 2001 From: David Smith Date: Sat, 8 Apr 2017 15:56:41 -0700 Subject: [PATCH 4/4] Update generator_test.go --- mockery/generator_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mockery/generator_test.go b/mockery/generator_test.go index 67671067..72d451ae 100644 --- a/mockery/generator_test.go +++ b/mockery/generator_test.go @@ -53,11 +53,11 @@ func (s *GeneratorSuite) checkGeneration( generator := s.getGenerator(filepath, interfaceName, inPackage) s.NoError(generator.Generate(), "The generator ran without errors.") - // Mirror the formatting done by normally done by golang.org/x/tools/imports in Generator.Write + // Mirror the formatting done by normally done by golang.org/x/tools/imports in Generator.Write. // // While we could possibly reuse Generator.Write here in addition to Generator.Generate, // it would require changing Write's signature to accept custom options, specifically to - // allow the fragmeents already used in test cases. It's assumed that this approximation, + // allow the fragments in preexisting cases. It's assumed that this approximation, // just formatting the source, is sufficient for the needs of the current test styles. var actual []byte actual, fmtErr := format.Source(generator.buf.Bytes())