-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
EqualExportedValues: Handle nested pointer, slice and map fields #1379
EqualExportedValues: Handle nested pointer, slice and map fields #1379
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a question about handling of map
. Looks good otherwise!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this approach! I left a few nits but it looks good to me otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't look like ObjectsExportedFieldsAreEqual
is actually used anymore beyond tests, so we might want to remove it. Other than that though ✅ . (And thanks for adding map support!)
edit: I think a data structure that references itself would cause an infinite loop (example test case from a different PR). IMO this is acceptable for a test helper package like testify
, but I think it would be worth noting in the function comment on EqualExportedValues
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Thanks for adding support for maps - I think this covers all of the composite data types.
Hi again @boyan-soubachov, would you have some time to look at this? |
assert/assertions.go
Outdated
result := reflect.New(expectedType).Elem() | ||
for i := 0; i < expectedType.NumField(); i++ { | ||
field := expectedType.Field(i) | ||
isExported := field.PkgPath == "" // should use field.IsExported() but it's not available in Go 1.16.5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably shouldn't support Go 1.16 any more since it's not officially supported anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to change the build to only support 1.20, 1.19, and 1.18 -- you can then change this code to not consider Go 1.16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@boyan-soubachov Updated!
Be aware the the auto-formatting rules seem to have changed for Go versions >= 1.19. I'm re-formatting the files and committing the new ones as well.
And we are now skipping that part of the build job for 1.18 because it otherwise fails because of formatting issues.
da2f494
to
b39be5d
Compare
.github/workflows/main.yml
Outdated
- run: ./.ci.gogenerate.sh | ||
- run: ./.ci.gofmt.sh | ||
- name: Go generate | ||
if: ${{ matrix.go_version != 1.18 }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we excluding gogenerate.sh
and gofmt.sh
from running if it's Go 1.18?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because it failed. I wrote a comment about it here: #1379 (comment)
If we don't want this extra logic, we could also drop support for 1.18, maybe it's old enough at this point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair -> https://endoflife.date/go
We can drop 1.18 support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@boyan-soubachov Updated to drop 1.18!
b39be5d
to
0a7ed06
Compare
Co-authored-by: Michael Pu <michael.pu123@gmail.com>
0a7ed06
to
ae7aab5
Compare
ae7aab5
to
9c3edea
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you :)
@HaraldNordgren : Hi. We have a Go application that uses the $ go version
go version go1.13.8 linux/amd64 The failure symptom is the following (pasted from https://gitlab.com/YottaDB/Lang/YDBGo/-/jobs/4268493955) $ go test -short $(go list ./... | grep -v /vendor/)
# github.com/stretchr/testify/assert
/go/src/github.com/stretchr/testify/assert/assertions.go:94:23: field.IsExported undefined (type reflect.StructField has no field or method IsExported) The same application also fails on a system with 4c93d8f changed Is this seeming regression expected? Thanks. |
Hi @nars1! In general I would definitely advise against using the 'master' build of any library. You are better off pinning the version to testify@v1.8.2 and you would avoid this issue. This repo has been updated to only support Go versions >=1.18. Be aware the the Go versions you are using are not officially supported anymore: https://endoflife.date/go. It would also be a good idea to upgrade your Go versions 🤗 |
@HaraldNordgren : Will pin the version like you suggest and try to upgrade our Go versions. Thank you for the prompt response. |
@HaraldNordgren : How do I pin the version like you suggest. My project module lang.yottadb.com/go/yottadb
go 1.13
require github.com/stretchr/testify v1.4.0 How do I say don't go more than 1.8.2? I tried searching for pinning but could not find any references. Hoping you can point me in the right direction. Thanks. |
…hr/testify/assert package) Background ---------- * Below is pasted from stretchr/testify#1379 (comment) where I had asked a question on a recent failure we saw in the YDBGo pipelines (and in the YDBTest/go/unit_test subtest failures in in-house systems which had go 1.13 and 1.15). @HaraldNordgren : Hi. We have a Go application that uses the `stretchry/testify` package. It was working fine on various systems with different go versions. But for the past 4 days, our nightly pipelines have been failing when run with go 1.13.8. ```sh $ go version go version go1.13.8 linux/amd64 ``` The failure symptom is the following (pasted from https://gitlab.com/YottaDB/Lang/YDBGo/-/jobs/4268493955) ```sh $ go test -short $(go list ./... | grep -v /vendor/) # github.com/stretchr/testify/assert /go/src/github.com/stretchr/testify/assert/assertions.go:94:23: field.IsExported undefined (type reflect.StructField has no field or method IsExported) ``` The same application also fails on a system with `go version go1.15.15 linux/amd64` but passes on a system with `go version go1.18.1 linux/amd64`. 4c93d8f changed `assertions.go` to add the `field.isExported` call. But looks like that call is only supported by recent versions of go. Is this seeming regression expected? Thanks. * And below was the response (pasted from stretchr/testify#1379 (comment)) In general I would definitely advise against using the 'master' build of any library. You are better off pinning the version to testify@v1.8.2 and you would avoid this issue. This repo has been updated to only support Go versions >=1.18. Be aware the the Go versions you are using are not officially supported anymore: https://endoflife.date/go. It would also be a good idea to upgrade your Go versions. Fix --- * I tried pinning the version like was suggested. I tried updating go.mod as follows. ```diff $ git diff go.mod @@ -4,2 +4,3 @@ go 1.13 -require github.com/stretchr/testify v1.4.0 +require github.com/stretchr/testify v1.8.2 +replace github.com/stretchr/testify => github.com/stretchr/testify v1.8.2 ``` But the YDBGo pipeline still failed as before when it used go 1.13 or go 1.15. * I then tried the following change. ```diff $ git diff go.mod @@ -4,2 +4,3 @@ go 1.13 -require github.com/stretchr/testify v1.8.2 +require github.com/stretchr/testify v1.4.0 +replace github.com/stretchr/testify => github.com/stretchr/testify v1.8.2 ``` This one failed the same way as well. * Finally decided to bump the go version required by the pipeline to 1.18 as suggested above. Searched for all references of `1.13` in the YDBGo repository in the `develop` branch and changed all of them to be `1.18`. In some cases, used `1.18.10` as that was the latest golang version in the 1.18 series. And regenerated `go.mod` using the following steps. This auto-generated go.mod file is included in this commit. ```sh $ cd YDBGo $ echo "module lang.yottadb.com/go/yottadb" > go.mod $ go get -t go: added github.com/davecgh/go-spew v1.1.1 go: added github.com/pmezard/go-difflib v1.0.0 go: added github.com/stretchr/testify v1.8.2 go: added gopkg.in/yaml.v3 v3.0.1 ``` * And in the in-house systems that have go 1.13 (Ubuntu 20.04) and go 1.15 (Debian 11), decided to install the latest Go version using snap instead of using the old go version from the package manager. The install worked fine on x86_64, AARCH64 and ARM32 architectures. With those changes, the `go/unit_tests` subtest passes fine on all those architectures when it previously reliably failed.
This change has set the Go minimum version hint to 1.20 in |
Hi @SuperQ, it’s testing both for 1.19 and 1.20. |
Fix for go.mod: #1394 |
This MR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [github.com/stretchr/testify](https://github.com/stretchr/testify) | require | minor | `v1.8.0` -> `v1.9.0` | --- ### Release Notes <details> <summary>stretchr/testify (github.com/stretchr/testify)</summary> ### [`v1.9.0`](https://github.com/stretchr/testify/releases/tag/v1.9.0) [Compare Source](stretchr/testify@v1.8.4...v1.9.0) #### What's Changed - Fix Go modules version by [@​SuperQ](https://github.com/SuperQ) in stretchr/testify#1394 - Document that require is not safe to call in created goroutines by [@​programmer04](https://github.com/programmer04) in stretchr/testify#1392 - Remove myself from MAINTAINERS.md by [@​mvdkleijn](https://github.com/mvdkleijn) in stretchr/testify#1367 - Correct spelling/grammar by [@​echarrod](https://github.com/echarrod) in stretchr/testify#1389 - docs: Update URLs in README by [@​davidjb](https://github.com/davidjb) in stretchr/testify#1349 - Update mockery link to Github Pages in README by [@​LandonTClipp](https://github.com/LandonTClipp) in stretchr/testify#1346 - docs: Fix typos in tests and comments by [@​alexandear](https://github.com/alexandear) in stretchr/testify#1410 - CI: tests from go1.17 by [@​SuperQ](https://github.com/SuperQ) in stretchr/testify#1409 - Fix adding ? when no values passed by [@​lesichkovm](https://github.com/lesichkovm) in stretchr/testify#1320 - codegen: use standard header for generated files by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1406 - mock: AssertExpectations log reason only on failure by [@​hikyaru-suzuki](https://github.com/hikyaru-suzuki) in stretchr/testify#1360 - assert: fix flaky TestNeverTrue by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1417 - README: fix typos "set up" vs "setup" by [@​ossan-dev](https://github.com/ossan-dev) in stretchr/testify#1428 - mock: move regexp compilation outside of `Called` by [@​aud10slave](https://github.com/aud10slave) in stretchr/testify#631 - assert: refactor internal func getLen() by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1445 - mock: deprecate type AnythingOfTypeArgument ([#​1434](stretchr/testify#1434)) by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1441 - Remove no longer needed assert.canConvert by [@​alexandear](https://github.com/alexandear) in stretchr/testify#1470 - assert: ObjectsAreEqual: use time.Equal for time.Time types by [@​tscales](https://github.com/tscales) in stretchr/testify#1464 - Bump actions/checkout from 3 to 4 by [@​dependabot](https://github.com/dependabot) in stretchr/testify#1466 - Bump actions/setup-go from 3.2.0 to 4.1.0 by [@​dependabot](https://github.com/dependabot) in stretchr/testify#1451 - fix: make EventuallyWithT concurrency safe by [@​czeslavo](https://github.com/czeslavo) in stretchr/testify#1395 - assert: fix httpCode and HTTPBody occur panic when http.Handler read Body by [@​hidu](https://github.com/hidu) in stretchr/testify#1484 - assert.EqualExportedValues: fix handling of arrays by [@​zrbecker](https://github.com/zrbecker) in stretchr/testify#1473 - .github: use latest Go versions by [@​kevinburkesegment](https://github.com/kevinburkesegment) in stretchr/testify#1489 - assert: Deprecate EqualExportedValues by [@​HaraldNordgren](https://github.com/HaraldNordgren) in stretchr/testify#1488 - suite: refactor test assertions by [@​alexandear](https://github.com/alexandear) in stretchr/testify#1474 - suite: fix SetupSubTest and TearDownSubTest execution order by [@​linusbarth](https://github.com/linusbarth) in stretchr/testify#1471 - docs: Fix deprecation comments for http package by [@​alexandear](https://github.com/alexandear) in stretchr/testify#1335 - Add map support doc comments to Subset and NotSubset by [@​jedevc](https://github.com/jedevc) in stretchr/testify#1306 - TestErrorIs/TestNotErrorIs: check error message contents by [@​craig65535](https://github.com/craig65535) in stretchr/testify#1435 - suite: fix subtest names (fix [#​1501](stretchr/testify#1501)) by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1504 - assert: improve unsafe.Pointer tests by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1505 - assert: simplify isNil implementation by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1506 - assert.InEpsilonSlice: fix expected/actual order and other improvements by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1483 - Fix dependency cycle with objx [#​1292](stretchr/testify#1292) by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1453 - mock: refactor TestIsArgsEqual by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1444 - mock: optimize argument matching checks by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1416 - assert: fix TestEventuallyTimeout by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1412 - CI: add go 1.21 in GitHub Actions by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1450 - suite: fix recoverAndFailOnPanic to report test failure at the right location by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1502 - Update maintainers by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1533 - assert: Fix EqualValues to handle overflow/underflow by [@​arjunmahishi](https://github.com/arjunmahishi) in stretchr/testify#1531 - assert: better formatting for Len() error by [@​kevinburkesegment](https://github.com/kevinburkesegment) in stretchr/testify#1485 - Ensure AssertExpectations does not fail in skipped tests by [@​ianrose14](https://github.com/ianrose14) in stretchr/testify#1331 - suite: fix deadlock in suite.Require()/Assert() by [@​arjunmahishi](https://github.com/arjunmahishi) in stretchr/testify#1535 - Revert "assert: ObjectsAreEqual: use time.Equal for time.Time type" by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1537 - \[chore] Add issue templates by [@​arjunmahishi](https://github.com/arjunmahishi) in stretchr/testify#1538 - Update the build status badge by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1540 - Update Github workflows setup-go to V5 by [@​hendrywiranto](https://github.com/hendrywiranto) in stretchr/testify#1545 - Support Pointer to Struct in EqualExportedValues by [@​Lucaber](https://github.com/Lucaber) in stretchr/testify#1517 - README: drop link to gorc by [@​guettli](https://github.com/guettli) in stretchr/testify#1248 - http_assertions: honour the msgAndArgs provided with each assertion by [@​arjunmahishi](https://github.com/arjunmahishi) in stretchr/testify#1548 - fix typos in comments and tests by [@​ccoVeille](https://github.com/ccoVeille) in stretchr/testify#1247 - Include the auto-release notes in releases by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1550 - Add `NotImplements` and variants by [@​hslatman](https://github.com/hslatman) in stretchr/testify#1385 - Add support to compare uintptr by [@​bogdandrutu](https://github.com/bogdandrutu) in stretchr/testify#1339 - build(deps): bump github.com/stretchr/objx from 0.5.1 to 0.5.2 by [@​dependabot](https://github.com/dependabot) in stretchr/testify#1552 #### New Contributors - [@​SuperQ](https://github.com/SuperQ) made their first contribution in stretchr/testify#1394 - [@​programmer04](https://github.com/programmer04) made their first contribution in stretchr/testify#1392 - [@​echarrod](https://github.com/echarrod) made their first contribution in stretchr/testify#1389 - [@​davidjb](https://github.com/davidjb) made their first contribution in stretchr/testify#1349 - [@​LandonTClipp](https://github.com/LandonTClipp) made their first contribution in stretchr/testify#1346 - [@​alexandear](https://github.com/alexandear) made their first contribution in stretchr/testify#1410 - [@​lesichkovm](https://github.com/lesichkovm) made their first contribution in stretchr/testify#1320 - [@​dolmen](https://github.com/dolmen) made their first contribution in stretchr/testify#1406 - [@​hikyaru-suzuki](https://github.com/hikyaru-suzuki) made their first contribution in stretchr/testify#1360 - [@​ossan-dev](https://github.com/ossan-dev) made their first contribution in stretchr/testify#1428 - [@​aud10slave](https://github.com/aud10slave) made their first contribution in stretchr/testify#631 - [@​tscales](https://github.com/tscales) made their first contribution in stretchr/testify#1464 - [@​czeslavo](https://github.com/czeslavo) made their first contribution in stretchr/testify#1395 - [@​hidu](https://github.com/hidu) made their first contribution in stretchr/testify#1484 - [@​zrbecker](https://github.com/zrbecker) made their first contribution in stretchr/testify#1473 - [@​kevinburkesegment](https://github.com/kevinburkesegment) made their first contribution in stretchr/testify#1489 - [@​linusbarth](https://github.com/linusbarth) made their first contribution in stretchr/testify#1471 - [@​jedevc](https://github.com/jedevc) made their first contribution in stretchr/testify#1306 - [@​craig65535](https://github.com/craig65535) made their first contribution in stretchr/testify#1435 - [@​arjunmahishi](https://github.com/arjunmahishi) made their first contribution in stretchr/testify#1531 - [@​ianrose14](https://github.com/ianrose14) made their first contribution in stretchr/testify#1331 - [@​hendrywiranto](https://github.com/hendrywiranto) made their first contribution in stretchr/testify#1545 - [@​Lucaber](https://github.com/Lucaber) made their first contribution in stretchr/testify#1517 - [@​guettli](https://github.com/guettli) made their first contribution in stretchr/testify#1248 - [@​ccoVeille](https://github.com/ccoVeille) made their first contribution in stretchr/testify#1247 - [@​hslatman](https://github.com/hslatman) made their first contribution in stretchr/testify#1385 - [@​bogdandrutu](https://github.com/bogdandrutu) made their first contribution in stretchr/testify#1339 **Full Changelog**: stretchr/testify@v1.8.4...v1.9.0 ### [`v1.8.4`](https://github.com/stretchr/testify/releases/tag/v1.8.4) [Compare Source](stretchr/testify@v1.8.3...v1.8.4) #### What's Changed - Create GitHub release when new release tag is pushed by [@​aldas](https://github.com/aldas) in stretchr/testify#1354 #### New Contributors - [@​aldas](https://github.com/aldas) made their first contribution in stretchr/testify#1354 **Full Changelog**: stretchr/testify@v1.8.3...v1.8.4 ### [`v1.8.3`](https://github.com/stretchr/testify/releases/tag/v1.8.3) [Compare Source](stretchr/testify@v1.8.2...v1.8.3) #### What's Changed - Compare public elements of struct by [@​mchlp](https://github.com/mchlp) in stretchr/testify#1309 - assert: fix error message formatting for NotContains by [@​wwade](https://github.com/wwade) in stretchr/testify#1362 - allow testing for functional options by [@​nbaztec](https://github.com/nbaztec) in stretchr/testify#1023 - add EventuallyWithT assertion by [@​tobikris](https://github.com/tobikris) in stretchr/testify#1264 - EqualExportedValues: Handle nested pointer, slice and map fields by [@​HaraldNordgren](https://github.com/HaraldNordgren) in stretchr/testify#1379 #### New Contributors - [@​mchlp](https://github.com/mchlp) made their first contribution in stretchr/testify#1309 - [@​wwade](https://github.com/wwade) made their first contribution in stretchr/testify#1362 - [@​nbaztec](https://github.com/nbaztec) made their first contribution in stretchr/testify#1023 - [@​tobikris](https://github.com/tobikris) made their first contribution in stretchr/testify#1264 **Full Changelog**: stretchr/testify@v1.8.2...v1.8.3 ### [`v1.8.2`](https://github.com/stretchr/testify/releases/tag/v1.8.2) [Compare Source](stretchr/testify@v1.8.1...v1.8.2) #### What's Changed - Add opportunity to trigger setup/teardown for subtest by [@​qerdcv](https://github.com/qerdcv) in stretchr/testify#1246 - fix: fix bug for check unsafe.Pointer isNil by [@​sunpe](https://github.com/sunpe) in stretchr/testify#1319 - Fix Call.Unset() panic (issue [#​1236](stretchr/testify#1236)) by [@​lisitsky](https://github.com/lisitsky) in stretchr/testify#1250 - Fix `CallerInfo()` source file paths by [@​bozaro](https://github.com/bozaro) in stretchr/testify#1288 - assert: Fix Subset/NotSubset when map is missing keys from the subset by [@​danielwhite](https://github.com/danielwhite) in stretchr/testify#1261 #### New Contributors - [@​qerdcv](https://github.com/qerdcv) made their first contribution in stretchr/testify#1246 - [@​sunpe](https://github.com/sunpe) made their first contribution in stretchr/testify#1319 - [@​lisitsky](https://github.com/lisitsky) made their first contribution in stretchr/testify#1250 - [@​bozaro](https://github.com/bozaro) made their first contribution in stretchr/testify#1288 - [@​danielwhite](https://github.com/danielwhite) made their first contribution in stretchr/testify#1261 **Full Changelog**: stretchr/testify@v1.8.1...v1.8.2 ### [`v1.8.1`](https://github.com/stretchr/testify/releases/tag/v1.8.1) [Compare Source](stretchr/testify@v1.8.0...v1.8.1) #### What's Changed - Bump github.com/stretchr/objx from 0.4.0 to 0.5.0 by [@​dependabot](https://github.com/dependabot) in stretchr/testify#1283 **Full Changelog**: stretchr/testify@v1.8.0...v1.8.1 </details> --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yODYuMSIsInVwZGF0ZWRJblZlciI6IjM3LjI4Ni4xIiwidGFyZ2V0QnJhbmNoIjoibWFzdGVyIiwibGFiZWxzIjpbXX0=--> See merge request alpine/infra/build-server-status!12
The current implementation of
ObjectsExportedFieldsAreEqual
does not handle (nested) pointer or slices fields. This means it doesn't work for comparing e.g. any Protobuf struct if there is any of these on the Protobuf message.Here, I am hoisting the reflect.Kind() checking from inside the loop to earlier in the logic. This allows me to take better advantage of the recursiveness of the function for checking equality for different types of structs. All while still maintaining the original functionality, as seen by the fact that the original tests are not altered.
I added a lot of tests cove different cases. It may seems like a lot of tests for this feature, but I used them when developing and since the reflection logic is pretty brittle in general, they were quite necessary for me to be able to refactor with any kind of confidence.
Thanks a bunch to @mchlp for the original implementation, this function is very needed for using Testify together with Protobuf! 🙌🏻