assert: collect.FailNow() should not panic#1481
Conversation
|
@marshall-lee Your review of #1395 would be welcome. |
|
#1395 is now merged. @marshall-lee Please rebase. @czeslavo Please review. |
ab5d73e to
8bedbde
Compare
|
I'm wondering whether we could document this behavior somewhere. It would be cool to have an example of using |
|
@cszczepaniak Could you also help with a review of this MR? |
dolmen
left a comment
There was a problem hiding this comment.
I think that the CollectT.failed field is not necessary to fix the issue.
If we ensure c.errors is a non nil slice when c.FailNow is called, the patch can be much smaller.
34c6cc5 to
0ebb845
Compare
|
@czeslavo @cszczepaniak @dolmen Hi! Any plans on merging this? |
0292bb5 to
2749dce
Compare
|
It's a good thing this issue has been addressed. Thank you guys |
|
when will this be released? |
|
@marshall-lee This seems to have broken the tests for 1.18. |
…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 [@​fahimbagar](https://github.com/fahimbagar) in stretchr/testify#1337 - assert: deprecate CompareType by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1566 - assert: make YAML dependency pluggable via build tags by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1579 - assert: new assertion NotElementsMatch by [@​hendrywiranto](https://github.com/hendrywiranto) in stretchr/testify#1600 - mock: in order mock calls by [@​ReyOrtiz](https://github.com/ReyOrtiz) in stretchr/testify#1637 - Add assertion for NotErrorAs by [@​palsivertsen](https://github.com/palsivertsen) in stretchr/testify#1129 - Record Return Arguments of a Call by [@​jayd3e](https://github.com/jayd3e) in stretchr/testify#1636 - assert.EqualExportedValues: accepts everything by [@​redachl](https://github.com/redachl) in stretchr/testify#1586 ##### Fixes - assert: make tHelper a type alias by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1562 - Do not get argument again unnecessarily in Arguments.Error() by [@​TomWright](https://github.com/TomWright) in stretchr/testify#820 - Fix time.Time compare by [@​myxo](https://github.com/myxo) in stretchr/testify#1582 - assert.Regexp: handle \[]byte array properly by [@​kevinburkesegment](https://github.com/kevinburkesegment) in stretchr/testify#1587 - assert: collect.FailNow() should not panic by [@​marshall-lee](https://github.com/marshall-lee) in stretchr/testify#1481 - mock: simplify implementation of FunctionalOptions by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1571 - mock: caller information for unexpected method call by [@​spirin](https://github.com/spirin) in stretchr/testify#1644 - suite: fix test failures by [@​stevenh](https://github.com/stevenh) in stretchr/testify#1421 - Fix issue [#​1662](stretchr/testify#1662) (comparing infs should fail) by [@​ybrustin](https://github.com/ybrustin) in stretchr/testify#1663 - NotSame should fail if args are not pointers [#​1661](stretchr/testify#1661) by [@​sikehish](https://github.com/sikehish) in stretchr/testify#1664 - Increase timeouts in Test_Mock_Called_blocks to reduce flakiness in CI by [@​sikehish](https://github.com/sikehish) in stretchr/testify#1667 - fix: compare functional option names for indirect calls by [@​arjun-1](https://github.com/arjun-1) in stretchr/testify#1626 ##### Documantation, Build & CI - .gitignore: ignore "go test -c" binaries by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1565 - mock: improve doc by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1570 - mock: fix FunctionalOptions docs by [@​snirye](https://github.com/snirye) in stretchr/testify#1433 - README: link out to the excellent testifylint by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1568 - assert: fix typo in comment by [@​JohnEndson](https://github.com/JohnEndson) in stretchr/testify#1580 - Correct the EventuallyWithT and EventuallyWithTf example by [@​JonCrowther](https://github.com/JonCrowther) in stretchr/testify#1588 - CI: bump softprops/action-gh-release from 1 to 2 by [@​dependabot](https://github.com/dependabot) in stretchr/testify#1575 - mock: document more alternatives to deprecated AnythingOfTypeArgument by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1569 - assert: Correctly document EqualValues behavior by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1593 - fix: grammar in godoc by [@​miparnisari](https://github.com/miparnisari) in stretchr/testify#1607 - .github/workflows: Run tests for Go 1.22 by [@​HaraldNordgren](https://github.com/HaraldNordgren) in stretchr/testify#1629 - Document suite's lack of support for t.Parallel by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1645 - assert: fix typos in comments by [@​alexandear](https://github.com/alexandear) in stretchr/testify#1650 - mock: fix doc comment for NotBefore by [@​alexandear](https://github.com/alexandear) in stretchr/testify#1651 - Generate better comments for require package by [@​Neokil](https://github.com/Neokil) in stretchr/testify#1610 - README: replace Testify V2 notice with [@​dolmen](https://github.com/dolmen)'s V2 manifesto by [@​hendrywiranto](https://github.com/hendrywiranto) in stretchr/testify#1518 #### New Contributors - [@​fahimbagar](https://github.com/fahimbagar) made their first contribution in stretchr/testify#1337 - [@​TomWright](https://github.com/TomWright) made their first contribution in stretchr/testify#820 - [@​snirye](https://github.com/snirye) made their first contribution in stretchr/testify#1433 - [@​myxo](https://github.com/myxo) made their first contribution in stretchr/testify#1582 - [@​JohnEndson](https://github.com/JohnEndson) made their first contribution in stretchr/testify#1580 - [@​JonCrowther](https://github.com/JonCrowther) made their first contribution in stretchr/testify#1588 - [@​miparnisari](https://github.com/miparnisari) made their first contribution in stretchr/testify#1607 - [@​marshall-lee](https://github.com/marshall-lee) made their first contribution in stretchr/testify#1481 - [@​spirin](https://github.com/spirin) made their first contribution in stretchr/testify#1644 - [@​ReyOrtiz](https://github.com/ReyOrtiz) made their first contribution in stretchr/testify#1637 - [@​stevenh](https://github.com/stevenh) made their first contribution in stretchr/testify#1421 - [@​jayd3e](https://github.com/jayd3e) made their first contribution in stretchr/testify#1636 - [@​Neokil](https://github.com/Neokil) made their first contribution in stretchr/testify#1610 - [@​redachl](https://github.com/redachl) made their first contribution in stretchr/testify#1586 - [@​ybrustin](https://github.com/ybrustin) made their first contribution in stretchr/testify#1663 - [@​sikehish](https://github.com/sikehish) made their first contribution in stretchr/testify#1664 - [@​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==-->
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 [@​fahimbagar](https://github.com/fahimbagar) in stretchr/testify#1337 - assert: deprecate CompareType by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1566 - assert: make YAML dependency pluggable via build tags by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1579 - assert: new assertion NotElementsMatch by [@​hendrywiranto](https://github.com/hendrywiranto) in stretchr/testify#1600 - mock: in order mock calls by [@​ReyOrtiz](https://github.com/ReyOrtiz) in stretchr/testify#1637 - Add assertion for NotErrorAs by [@​palsivertsen](https://github.com/palsivertsen) in stretchr/testify#1129 - Record Return Arguments of a Call by [@​jayd3e](https://github.com/jayd3e) in stretchr/testify#1636 - assert.EqualExportedValues: accepts everything by [@​redachl](https://github.com/redachl) in stretchr/testify#1586 ##### Fixes - assert: make tHelper a type alias by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1562 - Do not get argument again unnecessarily in Arguments.Error() by [@​TomWright](https://github.com/TomWright) in stretchr/testify#820 - Fix time.Time compare by [@​myxo](https://github.com/myxo) in stretchr/testify#1582 - assert.Regexp: handle \[]byte array properly by [@​kevinburkesegment](https://github.com/kevinburkesegment) in stretchr/testify#1587 - assert: collect.FailNow() should not panic by [@​marshall-lee](https://github.com/marshall-lee) in stretchr/testify#1481 - mock: simplify implementation of FunctionalOptions by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1571 - mock: caller information for unexpected method call by [@​spirin](https://github.com/spirin) in stretchr/testify#1644 - suite: fix test failures by [@​stevenh](https://github.com/stevenh) in stretchr/testify#1421 - Fix issue [#​1662](stretchr/testify#1662) (comparing infs should fail) by [@​ybrustin](https://github.com/ybrustin) in stretchr/testify#1663 - NotSame should fail if args are not pointers [#​1661](stretchr/testify#1661) by [@​sikehish](https://github.com/sikehish) in stretchr/testify#1664 - Increase timeouts in Test_Mock_Called_blocks to reduce flakiness in CI by [@​sikehish](https://github.com/sikehish) in stretchr/testify#1667 - fix: compare functional option names for indirect calls by [@​arjun-1](https://github.com/arjun-1) in stretchr/testify#1626 ##### Documantation, Build & CI - .gitignore: ignore "go test -c" binaries by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1565 - mock: improve doc by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1570 - mock: fix FunctionalOptions docs by [@​snirye](https://github.com/snirye) in stretchr/testify#1433 - README: link out to the excellent testifylint by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1568 - assert: fix typo in comment by [@​JohnEndson](https://github.com/JohnEndson) in stretchr/testify#1580 - Correct the EventuallyWithT and EventuallyWithTf example by [@​JonCrowther](https://github.com/JonCrowther) in stretchr/testify#1588 - CI: bump softprops/action-gh-release from 1 to 2 by [@​dependabot](https://github.com/dependabot) in stretchr/testify#1575 - mock: document more alternatives to deprecated AnythingOfTypeArgument by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1569 - assert: Correctly document EqualValues behavior by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1593 - fix: grammar in godoc by [@​miparnisari](https://github.com/miparnisari) in stretchr/testify#1607 - .github/workflows: Run tests for Go 1.22 by [@​HaraldNordgren](https://github.com/HaraldNordgren) in stretchr/testify#1629 - Document suite's lack of support for t.Parallel by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1645 - assert: fix typos in comments by [@​alexandear](https://github.com/alexandear) in stretchr/testify#1650 - mock: fix doc comment for NotBefore by [@​alexandear](https://github.com/alexandear) in stretchr/testify#1651 - Generate better comments for require package by [@​Neokil](https://github.com/Neokil) in stretchr/testify#1610 - README: replace Testify V2 notice with [@​dolmen](https://github.com/dolmen)'s V2 manifesto by [@​hendrywiranto](https://github.com/hendrywiranto) in stretchr/testify#1518 #### New Contributors - [@​fahimbagar](https://github.com/fahimbagar) made their first contribution in stretchr/testify#1337 - [@​TomWright](https://github.com/TomWright) made their first contribution in stretchr/testify#820 - [@​snirye](https://github.com/snirye) made their first contribution in stretchr/testify#1433 - [@​myxo](https://github.com/myxo) made their first contribution in stretchr/testify#1582 - [@​JohnEndson](https://github.com/JohnEndson) made their first contribution in stretchr/testify#1580 - [@​JonCrowther](https://github.com/JonCrowther) made their first contribution in stretchr/testify#1588 - [@​miparnisari](https://github.com/miparnisari) made their first contribution in stretchr/testify#1607 - [@​marshall-lee](https://github.com/marshall-lee) made their first contribution in stretchr/testify#1481 - [@​spirin](https://github.com/spirin) made their first contribution in stretchr/testify#1644 - [@​ReyOrtiz](https://github.com/ReyOrtiz) made their first contribution in stretchr/testify#1637 - [@​stevenh](https://github.com/stevenh) made their first contribution in stretchr/testify#1421 - [@​jayd3e](https://github.com/jayd3e) made their first contribution in stretchr/testify#1636 - [@​Neokil](https://github.com/Neokil) made their first contribution in stretchr/testify#1610 - [@​redachl](https://github.com/redachl) made their first contribution in stretchr/testify#1586 - [@​ybrustin](https://github.com/ybrustin) made their first contribution in stretchr/testify#1663 - [@​sikehish](https://github.com/sikehish) made their first contribution in stretchr/testify#1664 - [@​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> --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC44MC4wIiwidXBkYXRlZEluVmVyIjoiMzguODAuMCIsInRhcmdldEJyYW5jaCI6Im1hc3RlciIsImxhYmVscyI6W119--> See merge request alpine/infra/build-server-status!18
## Summary `collect.FailNow()` should exit goroutine without a panic to be usable with `require` package. ## Changes `collect.FailNow()` just does `runtime.Goexit()` instead of `panic()`. For example `FailNow()` from `testing` package [behaves similarly](https://cs.opensource.google/go/go/+/refs/tags/go1.21.2:src/testing/testing.go;l=973). ## Motivation I just want `require` package to be usable with `EventuallyWithT` e.g. I want this example to pass but it panics: ```go package main import ( "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestRequireEventuallyWithT(t *testing.T) { state := 0 require.EventuallyWithT(t, func(c *assert.CollectT) { defer func() { state += 1 }() require.True(c, state == 2) }, 100*time.Millisecond, 10*time.Millisecond) } ``` See https://go.dev/play/p/Oqd95IT7qxQ ## Related issues Fixes stretchr#1396 Fixes stretchr#1457
…6913) ### What does this PR do? Replaces `if !assert.XXX(...) { return }` guard patterns with `require.XXX(...)` across test files. The testify PR stretchr/testify#1481 (merged June 2024, included in testify v1.11.1) made `require` work safely with `*assert.CollectT` inside `EventuallyWithT` callbacks. `CollectT.FailNow()` calls `runtime.Goexit()` which exits the callback goroutine — this is caught by the `EventuallyWithT` retry loop, so the test continues retrying as expected. **Changes across 31 test files (~115 replacements):** - `test/new-e2e/tests/containers/` — `base_test.go`, `ecs_test.go`, `k8s_test.go` - `test/new-e2e/tests/` — `discovery/`, `agent-runtimes/`, `cspm/`, `cws/`, `npm/`, `netpath/`, `remote-config/`, `windows/`, `agent-health/`, `agent-log-pipelines/` - `pkg/network/tracer/`, `pkg/dyninst/`, `pkg/util/`, `comp/core/telemetry/`, etc. - Also removed outdated `// Can be replaced by require... once PR #1481 is merged` comments ### Important: `require` must not be used inside raw goroutines While `require` is safe inside `EventuallyWithT` callbacks (thanks to `CollectT.FailNow()` integration), it must **not** be used inside raw `go func()` blocks or HTTP handler callbacks. [`t.FailNow()`](https://pkg.go.dev/testing#T.FailNow) calls `runtime.Goexit()` to stop the current goroutine. The [official Go `testing` documentation](https://pkg.go.dev/testing#T.FailNow) states: > FailNow must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test. `EventuallyWithT` is a special case: testify [runs the condition via `go checkCond()`](https://github.com/stretchr/testify/blob/v1.11.1/assert/assertions.go#L2108), but `CollectT` implements its own `FailNow()` that safely exits the callback goroutine and lets the retry loop continue. **Not converted** (intentionally left as `assert`): - Assertions inside raw `go func()` blocks (e.g., `pkg/util/winutil/eventlog/` tests) - Callbacks/predicates returning `bool` or `error` (not test flow control) - Bodies that only log debug info without returning ### Motivation Using `assert` as a guard is error-prone: if someone forgets the `if !... { return }` wrapper, the test continues and panics when accessing nil/empty results (e.g., `metrics[len(metrics)-1]` on an empty slice). `require` makes the "stop on failure" intent explicit and eliminates the boilerplate. ### Describe how you validated your changes - Verified `require` works correctly inside `EventuallyWithT` callbacks (via `CollectT.FailNow()` → `runtime.Goexit()` caught by retry loop) - Confirmed raw `go func()` call sites were left unchanged - These are mechanical replacements that preserve the exact same test semantics — `require` stops execution on failure, which is exactly what the `if !assert... { return }` pattern was doing manually ### Additional Notes The remaining `if !assert` occurrences across other files were reviewed and intentionally kept because they serve different purposes (returning values from helper functions, setting flags, logging diagnostics without stopping, running inside raw goroutines, etc.). Co-authored-by: nicolas.schweitzer <nicolas.schweitzer@datadoghq.com>
Summary
collect.FailNow()should exit goroutine without a panic to be usable withrequirepackage.Changes
collect.FailNow()just doesruntime.Goexit()instead ofpanic(). For exampleFailNow()fromtestingpackage behaves similarly.Motivation
I just want
requirepackage to be usable withEventuallyWithTe.g. I want this example to pass but it panics:See https://go.dev/play/p/Oqd95IT7qxQ
Related issues
Fixes #1396
Fixes #1457