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

feat: add modifier to run afterTest in tests that inherit StdAfterTest #616

Closed
wants to merge 1 commit into from

Conversation

yash-atreya
Copy link
Member

@yash-atreya yash-atreya commented Oct 4, 2024

Ref: #617

Implementing this directly in foundry would add unnecessary complexity to ContractRunner. See foundry-rs/foundry#8985 (comment)

This approach is cleaner and easier.

Implemented the modifier and afterTest() fn that needs to be overridden in a separate abstract contract StdAfterTest; and not in existing StdCheatsSafe as this would break existing tests. Hence, making this feature opt-in.

---------

Co-authored-by: grandizzy <grandizzy.the.egg@gmail.com>
@yash-atreya yash-atreya marked this pull request as ready for review October 4, 2024 09:01
@grandizzy
Copy link
Contributor

makes sense to me, would be great to have @mds1 input too

pragma solidity >=0.6.2 <0.9.0;

// Inheritted by test contracts where afterTest() is needed
abstract contract StdAfterTest {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We will need to inherit from this here:

abstract contract Test is TestBase, StdAssertions, StdChains, StdCheats, StdInvariant, StdUtils {

Comment on lines +12 to +13
// This is needs to be overriden by the Test contract that inherits this
function afterTest() public virtual;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add some comments to document how this works exactly and what it's intended for? Does it run only after successful tests, or also failed ones? If both, I can see a use case like cleaning up tmp files. What's the use case for only running after successful test cases?

Copy link
Member Author

@yash-atreya yash-atreya Oct 8, 2024

Choose a reason for hiding this comment

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

afterTest() won't run after tests that revert. Runs in other cases such as successful tests, tests that fail due to assertions.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh I see this is just a pure solidity implementation with no foundry cheat/hook, so actually doesn't it only run after successful tests?

Given that, since we want to reduce the amount of code/functionality in forge-std, this seems very simple so I would suggest closing this PR and recommending users to implement this functionality themselves as needed

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense.
Closing this and adding an example impl to the linked issue.

@yash-atreya yash-atreya closed this Oct 9, 2024
@DaniPopes DaniPopes deleted the yash/after-test-modifier branch October 24, 2024 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: add tearDown() method that is called at the end for each unit test for Test contract
3 participants