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: snapbox migration #8728

Merged
merged 50 commits into from
Aug 28, 2024
Merged

feat: snapbox migration #8728

merged 50 commits into from
Aug 28, 2024

Conversation

zerosnacks
Copy link
Member

@zerosnacks zerosnacks commented Aug 23, 2024

Motivation

The goal of the PR is to sufficiently address the request outlined in:

Closes: #8389

Migrates most tests to use Snapbox.

Cleans up a lot of old helpers. Ideally we can get rid of stdout_lossy() all together at some point.

Disallows not_empty pattern, favoring Snapbox. Helper internals have been refactored to use Snapbox.

Solution

Improves a number of tests by applying more thorough match assertions

In order to port the last test cases / make testing more ergonomic:

Known issues:

  • stdout_lossy is still used in contexts of capturing the value from two commands and determine equality or casting to a value is required to make a comparison:

casttest!(block_number_latest, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
let s = cmd.args(["block-number", "--rpc-url", eth_rpc_url.as_str(), "latest"]).stdout_lossy();
assert!(s.trim().parse::<u64>().unwrap() > 0, "{s}")
});

  • There is currently no good way to capture the output (to do the above) and perform the regular stdout_eq check
  • Sometimes ordering of output cannot be enforced, it is only possible to use .contains on the output here:

// tests that the `cast upload-signatures` command works correctly
casttest!(upload_signatures, |_prj, cmd| {
// test no prefix is accepted as function
cmd.args(["upload-signature", "transfer(address,uint256)"]);
let output = cmd.stdout_lossy();
assert!(output.contains("Function transfer(address,uint256): 0xa9059cbb"), "{}", output);
// test event prefix
cmd.args(["upload-signature", "event Transfer(address,uint256)"]);
let output = cmd.stdout_lossy();
assert!(output.contains("Event Transfer(address,uint256): 0x69ca02dd4edd7bf0a4abb9ed3b7af3f14778db5d61921c7dc7cd545266326de2"), "{}", output);
// test error prefix
cmd.args(["upload-signature", "error ERC20InsufficientBalance(address,uint256,uint256)"]);
let output = cmd.stdout_lossy();
assert!(
output.contains("Function ERC20InsufficientBalance(address,uint256,uint256): 0xe450d38c"),
"{}",
output
); // Custom error is interpreted as function
// test multiple sigs
cmd.args([
"upload-signature",
"event Transfer(address,uint256)",
"transfer(address,uint256)",
"approve(address,uint256)",
]);
let output = cmd.stdout_lossy();
assert!(output.contains("Event Transfer(address,uint256): 0x69ca02dd4edd7bf0a4abb9ed3b7af3f14778db5d61921c7dc7cd545266326de2"), "{}", output);
assert!(output.contains("Function transfer(address,uint256): 0xa9059cbb"), "{}", output);
assert!(output.contains("Function approve(address,uint256): 0x095ea7b3"), "{}", output);
// test abi
cmd.args([
"upload-signature",
"event Transfer(address,uint256)",
"transfer(address,uint256)",
"error ERC20InsufficientBalance(address,uint256,uint256)",
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/ERC20Artifact.json")
.into_os_string()
.into_string()
.unwrap()
.as_str(),
]);
let output = cmd.stdout_lossy();
assert!(output.contains("Event Transfer(address,uint256): 0x69ca02dd4edd7bf0a4abb9ed3b7af3f14778db5d61921c7dc7cd545266326de2"), "{}", output);
assert!(output.contains("Function transfer(address,uint256): 0xa9059cbb"), "{}", output);
assert!(output.contains("Function approve(address,uint256): 0x095ea7b3"), "{}", output);
assert!(output.contains("Function decimals(): 0x313ce567"), "{}", output);
assert!(output.contains("Function allowance(address,address): 0xdd62ed3e"), "{}", output);
assert!(
output.contains("Function ERC20InsufficientBalance(address,uint256,uint256): 0xe450d38c"),
"{}",
output
);
});

Suggestions on what we should add / request upstream:

  • file! should support dynamically constructed Path's or default to CARGO_MANIFEST_DIR, e.g. PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../testdata/etherscan")
  • There is currently no way to dynamically construct the data passed to stdout_eq, it has to be statically defined. It is therefore not possible to embed constants inside of the argument passed to stdout_eq. It is also not possible to have an array of static data passed to stdout_eq in a loop - it must be unrolled manually.

crates/forge/tests/cli/build.rs Show resolved Hide resolved
crates/test-utils/src/util.rs Outdated Show resolved Hide resolved
@zerosnacks zerosnacks marked this pull request as ready for review August 26, 2024 12:57
@zerosnacks zerosnacks marked this pull request as draft August 26, 2024 15:12
@zerosnacks zerosnacks marked this pull request as ready for review August 27, 2024 15:46
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

lgtm, pending @DaniPopes

Copy link
Member

@DaniPopes DaniPopes left a comment

Choose a reason for hiding this comment

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

great

crates/forge/tests/cli/build.rs Outdated Show resolved Hide resolved
@zerosnacks zerosnacks merged commit 0d83028 into master Aug 28, 2024
21 checks passed
@zerosnacks zerosnacks deleted the zerosnacks/snapbox-migration branch August 28, 2024 09:59
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.

meta(testing): migrate CLI / UI tests to snapbox
3 participants