From 4dddc52361f0c4314f8ff7f649f8a6050fa20112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Fri, 30 May 2025 10:20:00 +0200 Subject: [PATCH 1/3] mock: isolate objx-dependent code in their own source files In order to ease maintenance of downstream forks of Testify that would remove dependency on github.com/stretchr/objx we move all uses of that module in isolated source files. A fork without that dependency could just remove those files. See https://github.com/stretchr/testify/issues/1752#issuecomment-2921545929 The use of objx is quite contained: it is only used in Mock.TestData(). Note that we can't just remove that method or change the return value because that would be a breaking change. --- mock/mock.go | 13 +------------ mock/mock_objx.go | 19 +++++++++++++++++++ mock/mock_objx_test.go | 19 +++++++++++++++++++ mock/mock_test.go | 12 ------------ 4 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 mock/mock_objx.go create mode 100644 mock/mock_objx_test.go diff --git a/mock/mock.go b/mock/mock.go index e88402b3e..fa1b363c4 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -13,7 +13,6 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/pmezard/go-difflib/difflib" - "github.com/stretchr/objx" "github.com/stretchr/testify/assert" ) @@ -311,7 +310,7 @@ type Mock struct { // TestData holds any data that might be useful for testing. Testify ignores // this data completely allowing you to do whatever you like with it. - testData objx.Map + testData testData mutex sync.Mutex } @@ -324,16 +323,6 @@ func (m *Mock) String() string { return fmt.Sprintf("%[1]T<%[1]p>", m) } -// TestData holds any data that might be useful for testing. Testify ignores -// this data completely allowing you to do whatever you like with it. -func (m *Mock) TestData() objx.Map { - if m.testData == nil { - m.testData = make(objx.Map) - } - - return m.testData -} - /* Setting expectations */ diff --git a/mock/mock_objx.go b/mock/mock_objx.go new file mode 100644 index 000000000..37fbdbf6e --- /dev/null +++ b/mock/mock_objx.go @@ -0,0 +1,19 @@ +// This source file isolates the uses of the objx module to ease +// maintenance of downstream forks that remove that dependency. +// See https://github.com/stretchr/testify/issues/1752 + +package mock + +import "github.com/stretchr/objx" + +type testData = objx.Map + +// TestData holds any data that might be useful for testing. Testify ignores +// this data completely allowing you to do whatever you like with it. +func (m *Mock) TestData() objx.Map { + if m.testData == nil { + m.testData = make(objx.Map) + } + + return m.testData +} diff --git a/mock/mock_objx_test.go b/mock/mock_objx_test.go new file mode 100644 index 000000000..37adf43ae --- /dev/null +++ b/mock/mock_objx_test.go @@ -0,0 +1,19 @@ +package mock + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_Mock_TestData(t *testing.T) { + t.Parallel() + + var mockedService = new(TestExampleImplementation) + + if assert.NotNil(t, mockedService.TestData()) { + + mockedService.TestData().Set("something", 123) + assert.Equal(t, 123, mockedService.TestData().Get("something").Data()) + } +} diff --git a/mock/mock_test.go b/mock/mock_test.go index c9fea8f6e..f3330f7ac 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -163,18 +163,6 @@ func (m *MockTestingT) FailNow() { Mock */ -func Test_Mock_TestData(t *testing.T) { - t.Parallel() - - var mockedService = new(TestExampleImplementation) - - if assert.NotNil(t, mockedService.TestData()) { - - mockedService.TestData().Set("something", 123) - assert.Equal(t, 123, mockedService.TestData().Get("something").Data()) - } -} - func Test_Mock_On(t *testing.T) { t.Parallel() From 580d80711200b03af0b827ba665e99c77a1f7b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Fri, 13 Jun 2025 23:26:42 +0200 Subject: [PATCH 2/3] mock: add build tag 'testify_no_objx' to opt-out of objx Add build tag testify_no_objx to allow to exclude method Mock.TestData() for avoiding dependency on github.com/stretchr/objx. --- mock/mock_no_objx.go | 5 +++++ mock/mock_objx.go | 2 ++ mock/mock_objx_test.go | 2 ++ 3 files changed, 9 insertions(+) create mode 100644 mock/mock_no_objx.go diff --git a/mock/mock_no_objx.go b/mock/mock_no_objx.go new file mode 100644 index 000000000..e8db50ec0 --- /dev/null +++ b/mock/mock_no_objx.go @@ -0,0 +1,5 @@ +//go:build testify_no_objx || testify_no_deps + +package mock + +type testData = struct{} diff --git a/mock/mock_objx.go b/mock/mock_objx.go index 37fbdbf6e..77f75dd65 100644 --- a/mock/mock_objx.go +++ b/mock/mock_objx.go @@ -2,6 +2,8 @@ // maintenance of downstream forks that remove that dependency. // See https://github.com/stretchr/testify/issues/1752 +//go:build !testify_no_objx && !testify_no_deps + package mock import "github.com/stretchr/objx" diff --git a/mock/mock_objx_test.go b/mock/mock_objx_test.go index 37adf43ae..556ac9c44 100644 --- a/mock/mock_objx_test.go +++ b/mock/mock_objx_test.go @@ -1,3 +1,5 @@ +//go:build !testify_no_objx && !testify_no_deps + package mock import ( From 3d9f3828904ea488ece5de4fdcfd3159eb70581b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Fri, 13 Jun 2025 23:34:19 +0200 Subject: [PATCH 3/3] CI: also run tests with build tag 'testify_no_objx' --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4c39ac045..40e291327 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -21,6 +21,7 @@ jobs: - run: ./.ci.readme.fmt.sh - run: ./.ci.govet.sh - run: go test -v -race ./... + - run: go test -tags testify_no_objx ./... test: runs-on: ubuntu-latest strategy: @@ -39,3 +40,4 @@ jobs: with: go-version: ${{ matrix.go_version }} - run: go test -v -race ./... + - run: go test -tags testify_no_objx ./...