From 48e9c74cb7943cf3c1a6c8e9dc6d84df511b8cf3 Mon Sep 17 00:00:00 2001 From: Shahzad Lone <shahzadlone@gmail.com> Date: Tue, 4 Jun 2024 00:33:53 -0400 Subject: [PATCH 1/4] PR: Change from `make mock` to `make mocks` --- Makefile | 12 ++++++------ tests/README.md | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 1f50ab816c..c4c02da3d7 100644 --- a/Makefile +++ b/Makefile @@ -158,8 +158,8 @@ deps\:chglog: deps\:modules: go mod download -.PHONY: deps\:mock -deps\:mock: +.PHONY: deps\:mocks +deps\:mocks: go install github.com/vektra/mockery/v2@v2.32.0 .PHONY: deps\:playground @@ -173,11 +173,11 @@ deps: $(MAKE) deps:chglog && \ $(MAKE) deps:lint && \ $(MAKE) deps:test && \ - $(MAKE) deps:mock + $(MAKE) deps:mocks -.PHONY: mock -mock: - @$(MAKE) deps:mock +.PHONY: mocks +mocks: + @$(MAKE) deps:mocks mockery --config="tools/configs/mockery.yaml" .PHONY: dev\:start diff --git a/tests/README.md b/tests/README.md index a17fbcaf08..2dfe2d17c7 100644 --- a/tests/README.md +++ b/tests/README.md @@ -23,7 +23,7 @@ These tests focus on small, isolated parts of the code to ensure each part is wo For unit tests, we sometimes use mocks. Mocks are automatically generated from Go interfaces using the mockery tool. This helps to isolate the code being tested and provide more focused and reliable tests. -To regenerate the mocks, run `make mock`. +To regenerate the mocks, run `make mocks`. The mocks are typically generated into a separate mocks directory. From 96781ff466084396259133a1bb38f4a6de8bdb27 Mon Sep 17 00:00:00 2001 From: Shahzad Lone <shahzadlone@gmail.com> Date: Tue, 4 Jun 2024 00:35:07 -0400 Subject: [PATCH 2/4] PR: Add workflow to ensure mocks are up to date --- .github/workflows/check-mocks.yml | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 .github/workflows/check-mocks.yml diff --git a/.github/workflows/check-mocks.yml b/.github/workflows/check-mocks.yml new file mode 100644 index 0000000000..80d9dbee20 --- /dev/null +++ b/.github/workflows/check-mocks.yml @@ -0,0 +1,50 @@ +# Copyright 2024 Democratized Data Foundation +# +# Use of this software is governed by the Business Source License +# included in the file licenses/BSL.txt. +# +# As of the Change Date specified in that file, in accordance with +# the Business Source License, use of this software will be governed +# by the Apache License, Version 2.0, included in the file +# licenses/APL.txt. + +# This workflow checks that all mocks are up to date. +# If the mocks are not up to date then this action will fail. +name: Check Mocks Workflow + +on: + pull_request: + branches: + - master + - develop + + push: + tags: + - 'v[0-9]+.[0-9]+.[0-9]+' + branches: + - master + - develop + +jobs: + check-mocks: + name: Check mocks job + + runs-on: ubuntu-latest + + steps: + - name: Checkout code into the directory + uses: actions/checkout@v3 + + - name: Setup Go environment explicitly + uses: actions/setup-go@v3 + with: + go-version: "1.21" + check-latest: true + + - name: Try generating mocks + run: make mocks + + - name: Check no new changes exist + uses: tj-actions/verify-changed-files@v20 + with: + fail-if-changed: true From bc9289acb32b99c9c6dd7d2674baebd520da65a3 Mon Sep 17 00:00:00 2001 From: Shahzad Lone <shahzadlone@gmail.com> Date: Tue, 4 Jun 2024 00:52:32 -0400 Subject: [PATCH 3/4] PR: Test out of date mocks --- datastore/mocks/results.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/datastore/mocks/results.go b/datastore/mocks/results.go index e1fee8f859..6a6051a2cb 100644 --- a/datastore/mocks/results.go +++ b/datastore/mocks/results.go @@ -4,18 +4,18 @@ package mocks import ( goprocess "github.com/jbenet/goprocess" - mock "github.com/stretchr/testify/mock" + mockCHANGED "github.com/stretchr/testify/mock" query "github.com/ipfs/go-datastore/query" ) // Results is an autogenerated mock type for the Results type type Results struct { - mock.Mock + mockCHANGED.Mock } type Results_Expecter struct { - mock *mock.Mock + mock *mockCHANGED.Mock } func (_m *Results) EXPECT() *Results_Expecter { @@ -38,7 +38,7 @@ func (_m *Results) Close() error { // Results_Close_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Close' type Results_Close_Call struct { - *mock.Call + *mockCHANGED.Call } // Close is a helper method to define mock.On call @@ -47,7 +47,7 @@ func (_e *Results_Expecter) Close() *Results_Close_Call { } func (_c *Results_Close_Call) Run(run func()) *Results_Close_Call { - _c.Call.Run(func(args mock.Arguments) { + _c.Call.Run(func(args mockCHANGED.Arguments) { run() }) return _c @@ -81,7 +81,7 @@ func (_m *Results) Next() <-chan query.Result { // Results_Next_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Next' type Results_Next_Call struct { - *mock.Call + *mockCHANGED.Call } // Next is a helper method to define mock.On call @@ -90,7 +90,7 @@ func (_e *Results_Expecter) Next() *Results_Next_Call { } func (_c *Results_Next_Call) Run(run func()) *Results_Next_Call { - _c.Call.Run(func(args mock.Arguments) { + _c.Call.Run(func(args mockCHANGED.Arguments) { run() }) return _c @@ -132,7 +132,7 @@ func (_m *Results) NextSync() (query.Result, bool) { // Results_NextSync_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'NextSync' type Results_NextSync_Call struct { - *mock.Call + *mockCHANGED.Call } // NextSync is a helper method to define mock.On call @@ -141,7 +141,7 @@ func (_e *Results_Expecter) NextSync() *Results_NextSync_Call { } func (_c *Results_NextSync_Call) Run(run func()) *Results_NextSync_Call { - _c.Call.Run(func(args mock.Arguments) { + _c.Call.Run(func(args mockCHANGED.Arguments) { run() }) return _c @@ -175,7 +175,7 @@ func (_m *Results) Process() goprocess.Process { // Results_Process_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Process' type Results_Process_Call struct { - *mock.Call + *mockCHANGED.Call } // Process is a helper method to define mock.On call @@ -184,7 +184,7 @@ func (_e *Results_Expecter) Process() *Results_Process_Call { } func (_c *Results_Process_Call) Run(run func()) *Results_Process_Call { - _c.Call.Run(func(args mock.Arguments) { + _c.Call.Run(func(args mockCHANGED.Arguments) { run() }) return _c @@ -216,7 +216,7 @@ func (_m *Results) Query() query.Query { // Results_Query_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Query' type Results_Query_Call struct { - *mock.Call + *mockCHANGED.Call } // Query is a helper method to define mock.On call @@ -225,7 +225,7 @@ func (_e *Results_Expecter) Query() *Results_Query_Call { } func (_c *Results_Query_Call) Run(run func()) *Results_Query_Call { - _c.Call.Run(func(args mock.Arguments) { + _c.Call.Run(func(args mockCHANGED.Arguments) { run() }) return _c @@ -269,7 +269,7 @@ func (_m *Results) Rest() ([]query.Entry, error) { // Results_Rest_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Rest' type Results_Rest_Call struct { - *mock.Call + *mockCHANGED.Call } // Rest is a helper method to define mock.On call @@ -278,7 +278,7 @@ func (_e *Results_Expecter) Rest() *Results_Rest_Call { } func (_c *Results_Rest_Call) Run(run func()) *Results_Rest_Call { - _c.Call.Run(func(args mock.Arguments) { + _c.Call.Run(func(args mockCHANGED.Arguments) { run() }) return _c @@ -297,7 +297,7 @@ func (_c *Results_Rest_Call) RunAndReturn(run func() ([]query.Entry, error)) *Re // NewResults creates a new instance of Results. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewResults(t interface { - mock.TestingT + mockCHANGED.TestingT Cleanup(func()) }) *Results { mock := &Results{} From 231b5da74c0cc7efa39990125f750f2331062d7d Mon Sep 17 00:00:00 2001 From: Shahzad Lone <shahzadlone@gmail.com> Date: Tue, 4 Jun 2024 01:09:17 -0400 Subject: [PATCH 4/4] Revert "PR: Test out of date mocks" This reverts commit 4b20f8f992866b58839c2f23c4c2dc1ac3d7f5d7. --- datastore/mocks/results.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/datastore/mocks/results.go b/datastore/mocks/results.go index 6a6051a2cb..e1fee8f859 100644 --- a/datastore/mocks/results.go +++ b/datastore/mocks/results.go @@ -4,18 +4,18 @@ package mocks import ( goprocess "github.com/jbenet/goprocess" - mockCHANGED "github.com/stretchr/testify/mock" + mock "github.com/stretchr/testify/mock" query "github.com/ipfs/go-datastore/query" ) // Results is an autogenerated mock type for the Results type type Results struct { - mockCHANGED.Mock + mock.Mock } type Results_Expecter struct { - mock *mockCHANGED.Mock + mock *mock.Mock } func (_m *Results) EXPECT() *Results_Expecter { @@ -38,7 +38,7 @@ func (_m *Results) Close() error { // Results_Close_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Close' type Results_Close_Call struct { - *mockCHANGED.Call + *mock.Call } // Close is a helper method to define mock.On call @@ -47,7 +47,7 @@ func (_e *Results_Expecter) Close() *Results_Close_Call { } func (_c *Results_Close_Call) Run(run func()) *Results_Close_Call { - _c.Call.Run(func(args mockCHANGED.Arguments) { + _c.Call.Run(func(args mock.Arguments) { run() }) return _c @@ -81,7 +81,7 @@ func (_m *Results) Next() <-chan query.Result { // Results_Next_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Next' type Results_Next_Call struct { - *mockCHANGED.Call + *mock.Call } // Next is a helper method to define mock.On call @@ -90,7 +90,7 @@ func (_e *Results_Expecter) Next() *Results_Next_Call { } func (_c *Results_Next_Call) Run(run func()) *Results_Next_Call { - _c.Call.Run(func(args mockCHANGED.Arguments) { + _c.Call.Run(func(args mock.Arguments) { run() }) return _c @@ -132,7 +132,7 @@ func (_m *Results) NextSync() (query.Result, bool) { // Results_NextSync_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'NextSync' type Results_NextSync_Call struct { - *mockCHANGED.Call + *mock.Call } // NextSync is a helper method to define mock.On call @@ -141,7 +141,7 @@ func (_e *Results_Expecter) NextSync() *Results_NextSync_Call { } func (_c *Results_NextSync_Call) Run(run func()) *Results_NextSync_Call { - _c.Call.Run(func(args mockCHANGED.Arguments) { + _c.Call.Run(func(args mock.Arguments) { run() }) return _c @@ -175,7 +175,7 @@ func (_m *Results) Process() goprocess.Process { // Results_Process_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Process' type Results_Process_Call struct { - *mockCHANGED.Call + *mock.Call } // Process is a helper method to define mock.On call @@ -184,7 +184,7 @@ func (_e *Results_Expecter) Process() *Results_Process_Call { } func (_c *Results_Process_Call) Run(run func()) *Results_Process_Call { - _c.Call.Run(func(args mockCHANGED.Arguments) { + _c.Call.Run(func(args mock.Arguments) { run() }) return _c @@ -216,7 +216,7 @@ func (_m *Results) Query() query.Query { // Results_Query_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Query' type Results_Query_Call struct { - *mockCHANGED.Call + *mock.Call } // Query is a helper method to define mock.On call @@ -225,7 +225,7 @@ func (_e *Results_Expecter) Query() *Results_Query_Call { } func (_c *Results_Query_Call) Run(run func()) *Results_Query_Call { - _c.Call.Run(func(args mockCHANGED.Arguments) { + _c.Call.Run(func(args mock.Arguments) { run() }) return _c @@ -269,7 +269,7 @@ func (_m *Results) Rest() ([]query.Entry, error) { // Results_Rest_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Rest' type Results_Rest_Call struct { - *mockCHANGED.Call + *mock.Call } // Rest is a helper method to define mock.On call @@ -278,7 +278,7 @@ func (_e *Results_Expecter) Rest() *Results_Rest_Call { } func (_c *Results_Rest_Call) Run(run func()) *Results_Rest_Call { - _c.Call.Run(func(args mockCHANGED.Arguments) { + _c.Call.Run(func(args mock.Arguments) { run() }) return _c @@ -297,7 +297,7 @@ func (_c *Results_Rest_Call) RunAndReturn(run func() ([]query.Entry, error)) *Re // NewResults creates a new instance of Results. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewResults(t interface { - mockCHANGED.TestingT + mock.TestingT Cleanup(func()) }) *Results { mock := &Results{}