Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compare functional option names for indirect calls #1626

Conversation

arjun-1
Copy link
Contributor

@arjun-1 arjun-1 commented Jul 23, 2024

Summary

Closes Functional Options testing broken for indirect calls #1380. The fix is consists of not comparing the the function names, as it is already asserted when comparing expectedValues and actualValues

Changes

  • Adds test Test_Mock_AssertExpectationsFunctionalOptionsTypeIndirectly which would fail without the fix
  • Adds test Test_Mock_AssertExpectationsFunctionalOptionsType_Diff to examine behavior of slightly different functional options in expected vs actual.
  • Changes the funcName implementation to only compare the relevant last part, if applicable. This aids in reading the diff that is produced when the function names don't. match (for go1.20 or higher)

Motivation

The choice was made to omit the comparison of function names completely, instead of comparing only the relevant last part. The reason is that go1.19 and lower don't give enough any meaningful comparable part in the function name. Nevertheless the most relevant part of the function name is included in the diff result. Without it, the diff yielded in Test_Mock_AssertExpectationsFunctionalOptionsType_Diff would have resulted in (go1.20):

mock.Test_Mock_AssertExpectationsFunctionalOptionsType_Diff.OpNum &{num:0 str:1} != mock.Test_Mock_AssertExpectationsFunctionalOptionsType_Diff.OpNum &{num:1 str:}

The current implementation instead yields a diff:

OpStr &{num:0 str:1} != OpNum &{num:1 str:}

Though there could be added benefit in displaying the full location of the function, I can revert that part if desired.

Related issues

Closes #1380

@arjun-1
Copy link
Contributor Author

arjun-1 commented Jul 23, 2024

Looks like the approach to compare a relevant part only fails for older Go versions, I'll rethink that
Done

@arjun-1 arjun-1 marked this pull request as draft July 23, 2024 14:36
@arjun-1 arjun-1 marked this pull request as ready for review July 23, 2024 15:01
@arjun-1
Copy link
Contributor Author

arjun-1 commented Aug 5, 2024

Hey @dolmen I believe you were involved in reviewing related change #1381. Anything specific required for this review?

@pmokeev
Copy link

pmokeev commented Aug 30, 2024

@dolmen can you have a look at this PR? I've also encountered the same problem

@dolmen dolmen added pkg-mock Any issues related to Mock bug labels Oct 4, 2024
Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR would regress this highly contrived test:

package kata_test

import (
	"testing"

	"github.com/stretchr/testify/mock"
)

type MyStruct struct {
	A []byte
}

type Opt func(*MyStruct)

func WithString(s string) Opt {
	return func(m *MyStruct) {
		m.A = []byte(s)
	}
}

func WithBytes(b []byte) Opt {
	return func(m *MyStruct) {
		m.A = b
	}
}

type myMockFactory struct {
	mock.Mock
}

func (m *myMockFactory) New(opts ...Opt) *MyStruct {
	return m.Called(opts).Get(0).(*MyStruct)
}

func TestIfy(t *testing.T) {
	mockFactory := &myMockFactory{}
	mockFactory.Test(t)
	mockFactory.On("New", mock.FunctionalOptions(WithBytes([]byte("this")))).Return(&MyStruct{})
	mockFactory.New(WithString("this"))
}

The existing function name checking doesn't sit well with me either because it stripped the package name off. Can we use reflect.Value.Pointer to check that it's the same function? The docs suggest that multiple functions can share a code pointer but I don't know in what cases that happens. If it's not practical lets drop to testing only the behaviour as you propose.

On the diff output, we should show what we compare, which in the current state of this PR is just the behaviour, so I'd like the diff to look somewhat like:

Diff: 0: FAIL:  &MyStruct{A:1 B:0} != &MyStruct{A:0 B:1} 

@brackendawson
Copy link
Collaborator

Considering the diff some more, the function name could make a mistake more obvious to the user. But I'd want some clearer separation between the function name and the bahaviour because it's quite hard to grok, how about:

Diff: 0: FAIL:  WithB(*MyMock) -> &MyMock{A:0 B:1} != WithA(*MyMock) -> &MyMock{A:1 B:0}

@brackendawson
Copy link
Collaborator

brackendawson commented Oct 7, 2024

The docstring for FunctionalOptions should also be changed to state that only the behaviour of the functional option argument is compared, and that functional options with identical outcomes compare as equal. Currently it says:

FunctionalOptions returns an [FunctionalOptionsArgument] object containing
the expected functional-options to check for.

For example:

  Assert(t, FunctionalOptions(foo.Opt1("strValue"), foo.Opt2(613)))

@arjun-1 arjun-1 force-pushed the fix-functional-options-diff-indirect-calls branch from 2b8f363 to 55bac84 Compare October 24, 2024 09:30
@arjun-1
Copy link
Contributor Author

arjun-1 commented Oct 27, 2024

@brackendawson I agree with you that the regression you mention is not ideal. Originally I removed the function name comparison (only focussing on behavior) to make the tests (fixing the bug of this issue) pass for older Go versions older than 1.20. The function name retrieved via FuncForPC yields false unexpected calls for these versions. For example

github.com/stretchr/testify/mock.Test_Mock_AssertExpectationsFunctionalOptionsType_Indirectly.func1
github.com/stretchr/testify/mock.TheExampleMethodFunctionalOptionsIndirect.func1

I investigated the solution of using Pointer() directly on the reflected value. The pointer value is again falsely different, as I believe it points to the inner closed anonymous function (func1).

I explored a different comparison using FileLine. It's imperfect as two different function values on the same line would not be detected as equal. But it is the only solution that prevents the regression you mention (see Test_Mock_AssertExpectationsFunctionalOptionsType_Diff_Func) and fixes the original issue (Test_Mock_AssertExpectationsFunctionalOptionsType_Indirectly) for all Go versions.

The result is now a comparison based on function file line, and if that is the same a comparison based on function behavior. Let me know if the docstring for FunctionalOptions should still be updated.

Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pulling on all those threads. I'm happy to take a function matching strategy that falsely matches different functions with the same behaviour defined on the same line because the original match also wasn't complete, it considered the same function defined in different packages to be equal.

  • Your proposed change fixes the described bug, anonymous functions now match. They are actually quite a common code pattern to use.
  • Different functions with the same behaviour still fail (unless they are defined on the same line).
  • Different functions defined in different packages with the same name and the same behaviour now fail. This is faithful to our function's promise and is such a contrived example that I'm not concerned by the change to behaviour.

I'm confident that this is a safe enough way to resolve the issue. Thanks for all your work here. I'm also going to give other maintainers some time to review.

@brackendawson brackendawson merged commit 89cbdd9 into stretchr:master Nov 12, 2024
8 checks passed
apricote pushed a commit to hetznercloud/fleeting-plugin-hetzner that referenced this pull request Nov 25, 2024
…nercloud/fleeting-plugin-hetzner!175)

This MR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [github.com/stretchr/testify](https://github.com/stretchr/testify) | require | minor | `v1.9.0` -> `v1.10.0` |

---

### Release Notes

<details>
<summary>stretchr/testify (github.com/stretchr/testify)</summary>

### [`v1.10.0`](https://github.com/stretchr/testify/releases/tag/v1.10.0)

[Compare Source](stretchr/testify@v1.9.0...v1.10.0)

#### What's Changed

##### Functional Changes

-   Add PanicAssertionFunc by [@&#8203;fahimbagar](https://github.com/fahimbagar) in stretchr/testify#1337
-   assert: deprecate CompareType by [@&#8203;dolmen](https://github.com/dolmen) in stretchr/testify#1566
-   assert: make YAML dependency pluggable via build tags by [@&#8203;dolmen](https://github.com/dolmen) in stretchr/testify#1579
-   assert: new assertion NotElementsMatch by [@&#8203;hendrywiranto](https://github.com/hendrywiranto) in stretchr/testify#1600
-   mock: in order mock calls by [@&#8203;ReyOrtiz](https://github.com/ReyOrtiz) in stretchr/testify#1637
-   Add assertion for NotErrorAs by [@&#8203;palsivertsen](https://github.com/palsivertsen) in stretchr/testify#1129
-   Record Return Arguments of a Call by [@&#8203;jayd3e](https://github.com/jayd3e) in stretchr/testify#1636
-   assert.EqualExportedValues: accepts everything by [@&#8203;redachl](https://github.com/redachl) in stretchr/testify#1586

##### Fixes

-   assert: make tHelper a type alias by [@&#8203;dolmen](https://github.com/dolmen) in stretchr/testify#1562
-   Do not get argument again unnecessarily in Arguments.Error() by [@&#8203;TomWright](https://github.com/TomWright) in stretchr/testify#820
-   Fix time.Time compare by [@&#8203;myxo](https://github.com/myxo) in stretchr/testify#1582
-   assert.Regexp: handle \[]byte array properly by [@&#8203;kevinburkesegment](https://github.com/kevinburkesegment) in stretchr/testify#1587
-   assert: collect.FailNow() should not panic by [@&#8203;marshall-lee](https://github.com/marshall-lee) in stretchr/testify#1481
-   mock: simplify implementation of FunctionalOptions by [@&#8203;dolmen](https://github.com/dolmen) in stretchr/testify#1571
-   mock: caller information for unexpected method call by [@&#8203;spirin](https://github.com/spirin) in stretchr/testify#1644
-   suite: fix test failures by [@&#8203;stevenh](https://github.com/stevenh) in stretchr/testify#1421
-   Fix issue [#&#8203;1662](stretchr/testify#1662) (comparing infs should fail) by [@&#8203;ybrustin](https://github.com/ybrustin) in stretchr/testify#1663
-   NotSame should fail if args are not pointers [#&#8203;1661](stretchr/testify#1661) by [@&#8203;sikehish](https://github.com/sikehish) in stretchr/testify#1664
-   Increase timeouts in Test_Mock_Called_blocks to reduce flakiness in CI by [@&#8203;sikehish](https://github.com/sikehish) in stretchr/testify#1667
-   fix: compare functional option names for indirect calls by [@&#8203;arjun-1](https://github.com/arjun-1) in stretchr/testify#1626

##### Documantation, Build & CI

-   .gitignore: ignore "go test -c" binaries by [@&#8203;dolmen](https://github.com/dolmen) in stretchr/testify#1565
-   mock: improve doc by [@&#8203;dolmen](https://github.com/dolmen) in stretchr/testify#1570
-   mock: fix FunctionalOptions docs by [@&#8203;snirye](https://github.com/snirye) in stretchr/testify#1433
-   README: link out to the excellent testifylint by [@&#8203;brackendawson](https://github.com/brackendawson) in stretchr/testify#1568
-   assert: fix typo in comment by [@&#8203;JohnEndson](https://github.com/JohnEndson) in stretchr/testify#1580
-   Correct the EventuallyWithT and EventuallyWithTf example by [@&#8203;JonCrowther](https://github.com/JonCrowther) in stretchr/testify#1588
-   CI: bump softprops/action-gh-release from 1 to 2 by [@&#8203;dependabot](https://github.com/dependabot) in stretchr/testify#1575
-   mock: document more alternatives to deprecated AnythingOfTypeArgument by [@&#8203;dolmen](https://github.com/dolmen) in stretchr/testify#1569
-   assert: Correctly document EqualValues behavior by [@&#8203;brackendawson](https://github.com/brackendawson) in stretchr/testify#1593
-   fix: grammar in godoc by [@&#8203;miparnisari](https://github.com/miparnisari) in stretchr/testify#1607
-   .github/workflows: Run tests for Go 1.22 by [@&#8203;HaraldNordgren](https://github.com/HaraldNordgren) in stretchr/testify#1629
-   Document suite's lack of support for t.Parallel by [@&#8203;brackendawson](https://github.com/brackendawson) in stretchr/testify#1645
-   assert: fix typos in comments by [@&#8203;alexandear](https://github.com/alexandear) in stretchr/testify#1650
-   mock: fix doc comment for NotBefore by [@&#8203;alexandear](https://github.com/alexandear) in stretchr/testify#1651
-   Generate better comments for require package by [@&#8203;Neokil](https://github.com/Neokil) in stretchr/testify#1610
-   README: replace Testify V2 notice with [@&#8203;dolmen](https://github.com/dolmen)'s V2 manifesto by [@&#8203;hendrywiranto](https://github.com/hendrywiranto) in stretchr/testify#1518

#### New Contributors

-   [@&#8203;fahimbagar](https://github.com/fahimbagar) made their first contribution in stretchr/testify#1337
-   [@&#8203;TomWright](https://github.com/TomWright) made their first contribution in stretchr/testify#820
-   [@&#8203;snirye](https://github.com/snirye) made their first contribution in stretchr/testify#1433
-   [@&#8203;myxo](https://github.com/myxo) made their first contribution in stretchr/testify#1582
-   [@&#8203;JohnEndson](https://github.com/JohnEndson) made their first contribution in stretchr/testify#1580
-   [@&#8203;JonCrowther](https://github.com/JonCrowther) made their first contribution in stretchr/testify#1588
-   [@&#8203;miparnisari](https://github.com/miparnisari) made their first contribution in stretchr/testify#1607
-   [@&#8203;marshall-lee](https://github.com/marshall-lee) made their first contribution in stretchr/testify#1481
-   [@&#8203;spirin](https://github.com/spirin) made their first contribution in stretchr/testify#1644
-   [@&#8203;ReyOrtiz](https://github.com/ReyOrtiz) made their first contribution in stretchr/testify#1637
-   [@&#8203;stevenh](https://github.com/stevenh) made their first contribution in stretchr/testify#1421
-   [@&#8203;jayd3e](https://github.com/jayd3e) made their first contribution in stretchr/testify#1636
-   [@&#8203;Neokil](https://github.com/Neokil) made their first contribution in stretchr/testify#1610
-   [@&#8203;redachl](https://github.com/redachl) made their first contribution in stretchr/testify#1586
-   [@&#8203;ybrustin](https://github.com/ybrustin) made their first contribution in stretchr/testify#1663
-   [@&#8203;sikehish](https://github.com/sikehish) made their first contribution in stretchr/testify#1664
-   [@&#8203;arjun-1](https://github.com/arjun-1) made their first contribution in stretchr/testify#1626

**Full Changelog**: stretchr/testify@v1.9.0...v1.10.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4yOC4wIiwidXBkYXRlZEluVmVyIjoiMzkuMjguMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug pkg-mock Any issues related to Mock
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Functional Options testing broken for indirect calls
4 participants