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

assert: collect.FailNow() should not panic #1481

Merged
merged 1 commit into from
Jun 13, 2024

Conversation

marshall-lee
Copy link
Contributor

@marshall-lee marshall-lee commented Oct 8, 2023

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.

Motivation

I just want require package to be usable with EventuallyWithT e.g. I want this example to pass but it panics:

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 #1396
Fixes #1457

@dolmen dolmen added bug assert.Eventually About assert.Eventually/EventuallyWithT pkg-assert Change related to package testify/assert labels Oct 10, 2023
@dolmen
Copy link
Collaborator

dolmen commented Oct 10, 2023

@marshall-lee Your review of #1395 would be welcome.

@dolmen
Copy link
Collaborator

dolmen commented Oct 13, 2023

#1395 is now merged.

@marshall-lee Please rebase.

@czeslavo Please review.

@dolmen dolmen added the hacktoberfest-accepted Hacktoberfest label Oct 13, 2023
dolmen
dolmen previously approved these changes Oct 13, 2023
@czeslavo
Copy link
Contributor

I'm wondering whether we could document this behavior somewhere. It would be cool to have an example of using EventuallyWithT with a series of require and assert checks with an explanation of what happens underneath (i.e. if one of the require checks is not satisfied, this single condition run is aborted, but EventuallyWithT will retry if the timeout hasn't passed yet, etc.)

@dolmen
Copy link
Collaborator

dolmen commented Oct 16, 2023

@cszczepaniak Could you also help with a review of this MR?

Copy link
Collaborator

@dolmen dolmen left a comment

Choose a reason for hiding this comment

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

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.

assert/assertions.go Outdated Show resolved Hide resolved
assert/assertions.go Outdated Show resolved Hide resolved
@marshall-lee marshall-lee force-pushed the collect-t-failnow-goexit branch 4 times, most recently from 34c6cc5 to 0ebb845 Compare October 16, 2023 13:59
assert/assertions.go Outdated Show resolved Hide resolved
require/requirements_test.go Show resolved Hide resolved
require/requirements_test.go Outdated Show resolved Hide resolved
@marshall-lee marshall-lee force-pushed the collect-t-failnow-goexit branch 7 times, most recently from 4b3659f to cbb508a Compare November 25, 2023 11:03
@marshall-lee
Copy link
Contributor Author

@czeslavo @cszczepaniak @dolmen Hey I tried to address all the previous review issues. Please review.

assert/assertions_test.go Outdated Show resolved Hide resolved
@marshall-lee
Copy link
Contributor Author

@czeslavo @cszczepaniak @dolmen Hi! Any plans on merging this?

Copy link
Contributor

@czeslavo czeslavo left a comment

Choose a reason for hiding this comment

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

LGTM, but I'm just an ordinary contributor here, @dolmen decides 🙂

Copy link
Collaborator

@dolmen dolmen left a comment

Choose a reason for hiding this comment

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

LGTM.

Rebase needed.

@dolmen dolmen merged commit b074924 into stretchr:master Jun 13, 2024
7 checks passed
@ccoVeille
Copy link
Contributor

ccoVeille commented Jun 13, 2024

It's a good thing this issue has been addressed. Thank you guys

@adilansari
Copy link

when will this be released?

@HaraldNordgren
Copy link
Contributor

@marshall-lee This seems to have broken the tests for 1.18.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert.Eventually About assert.Eventually/EventuallyWithT bug hacktoberfest-accepted Hacktoberfest pkg-assert Change related to package testify/assert
Projects
None yet
7 participants