-
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
assert: Fix Subset/NotSubset when map is missing keys from the subset #1261
Merged
boyan-soubachov
merged 1 commit into
stretchr:master
from
danielwhite:fix-subset-notsubset-map-missing-keys
Feb 25, 2023
Merged
assert: Fix Subset/NotSubset when map is missing keys from the subset #1261
boyan-soubachov
merged 1 commit into
stretchr:master
from
danielwhite:fix-subset-notsubset-map-missing-keys
Feb 25, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`MapIndex` returns a zero `reflect.Value` if the map value does not exist. This now aligns more closely with `InDeltaMapValues` by checking `reflect/Value.IsValid`. The use of `recover()` was hiding this error by setting the result of the test to be "false" despite the test suite passing. This led to flapping tests where they would succeed if the panic occurred on a missing key before comparing key values that didn't match! I've ensured the test suite now asserts on the expected error message and added another example where the subset has keys not found on the map under test.
@boyan-soubachov, any chance of taking a look at this? |
boyan-soubachov
approved these changes
Feb 25, 2023
Brilliant, thanks! |
This was referenced Feb 16, 2024
algitbot
pushed a commit
to alpinelinux/build-server-status
that referenced
this pull request
May 6, 2024
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This ensures that
assert.Subset
andassert.NotSubset
consistently fails for tests against maps a key in the subset map is not contained in the map under test.Changes
This ensures that the value obtained by
reflect/Value.MapIndex
from the map under test is checked for validity withreflect/Value.IsValid
.This now aligns more closely with
InDeltaMapValues
.The use of
recover()
was hiding this error by setting the result of the test to be "false" despite the test suite passing. This led to flapping tests where they would succeed if the panic occurred on a missing key before comparing key values that didn't match!I've ensured the test suite now asserts on the expected error message and added another example where the subset has keys not found on the map under test.
Motivation
The following demonstrates a test that would pass more often than fail:
Without the change, we see a mixture of results due to the random order of map keys:
With this change applied, the test passes consistently:
Related issues