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

refactor(tests): add snapbox #8406

Merged
merged 6 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,5 @@ tower = "0.4"
tower-http = "0.5"
# soldeer
soldeer = "0.2.17"

snapbox = "0.6.9"
18 changes: 6 additions & 12 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ casttest!(wallet_sign_message_hex_data, |_prj, cmd| {
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000000000000000000000000000000",
]);
let output = cmd.stdout_lossy();
assert_eq!(output.trim(), "0x23a42ca5616ee730ff3735890c32fc7b9491a9f633faca9434797f2c845f5abf4d9ba23bd7edb8577acebaa3644dc5a4995296db420522bb40060f1693c33c9b1c");
]).assert_success().stdout_eq("0x23a42ca5616ee730ff3735890c32fc7b9491a9f633faca9434797f2c845f5abf4d9ba23bd7edb8577acebaa3644dc5a4995296db420522bb40060f1693c33c9b1c\n");
});

// tests that `cast wallet sign typed-data` outputs the expected signature, given a JSON string
Expand All @@ -117,9 +115,7 @@ casttest!(wallet_sign_typed_data_string, |_prj, cmd| {
"0x0000000000000000000000000000000000000000000000000000000000000001",
"--data",
"{\"types\": {\"EIP712Domain\": [{\"name\": \"name\",\"type\": \"string\"},{\"name\": \"version\",\"type\": \"string\"},{\"name\": \"chainId\",\"type\": \"uint256\"},{\"name\": \"verifyingContract\",\"type\": \"address\"}],\"Message\": [{\"name\": \"data\",\"type\": \"string\"}]},\"primaryType\": \"Message\",\"domain\": {\"name\": \"example.metamask.io\",\"version\": \"1\",\"chainId\": \"1\",\"verifyingContract\": \"0x0000000000000000000000000000000000000000\"},\"message\": {\"data\": \"Hello!\"}}",
]);
let output = cmd.stdout_lossy();
assert_eq!(output.trim(), "0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b");
]).assert_success().stdout_eq("0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b\n");
klkvr marked this conversation as resolved.
Show resolved Hide resolved
});

// tests that `cast wallet sign typed-data` outputs the expected signature, given a JSON file
Expand All @@ -137,9 +133,7 @@ casttest!(wallet_sign_typed_data_file, |_prj, cmd| {
.into_string()
.unwrap()
.as_str(),
]);
let output = cmd.stdout_lossy();
assert_eq!(output.trim(), "0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b");
]).assert_success().stdout_eq("0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b\n");
});

// tests that `cast wallet list` outputs the local accounts
Expand Down Expand Up @@ -177,9 +171,9 @@ casttest!(wallet_private_key_from_mnemonic_arg, |_prj, cmd| {
"private-key",
"test test test test test test test test test test test junk",
"1",
]);
let output = cmd.stdout_lossy();
assert_eq!(output.trim(), "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d");
])
.assert_success()
.stdout_eq("0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d\n");
});

// tests that `cast wallet private-key` with options outputs the private key
Expand Down
2 changes: 2 additions & 0 deletions crates/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ tracing-subscriber = { workspace = true, features = ["env-filter"] }

alloy-signer-local.workspace = true

snapbox = { workspace = true, features = ["json"] }

[features]
default = ["rustls", "jemalloc"]
rustls = [
Expand Down
23 changes: 5 additions & 18 deletions crates/forge/tests/cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use foundry_common::fs::read_json_file;
use foundry_config::Config;
use foundry_test_utils::forgetest;
use globset::Glob;
use std::{collections::BTreeMap, path::PathBuf};
use snapbox::file;

// tests that json is printed when --json is passed
forgetest!(compile_json, |prj, cmd| {
Expand All @@ -20,26 +19,14 @@ contract Dummy {
.unwrap();

// set up command
cmd.args(["compile", "--format-json"]);

// Exclude build_infos from output as IDs depend on root dir and are not deterministic.
let mut output: BTreeMap<String, serde_json::Value> =
serde_json::from_str(&cmd.stdout_lossy()).unwrap();
output.remove("build_infos");

let expected: BTreeMap<String, serde_json::Value> = read_json_file(
&PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/compile_json.stdout"),
)
.unwrap();

similar_asserts::assert_eq!(output, expected);
cmd.args(["compile", "--format-json"])
.assert()
.stdout_eq(file!["../fixtures/compile_json.stdout": Json]);
});

// tests build output is as expected
forgetest_init!(exact_build_output, |prj, cmd| {
cmd.args(["build", "--force"]);
let stdout = cmd.stdout_lossy();
assert!(stdout.contains("Compiling"), "\n{stdout}");
cmd.args(["build", "--force"]).assert_success().stdout_eq("Compiling[..]\n...");
});

// tests build output is as expected
Expand Down
83 changes: 33 additions & 50 deletions crates/forge/tests/cli/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use foundry_compilers::artifacts::{remappings::Remapping, BytecodeHash};
use foundry_config::Config;
use foundry_test_utils::{
forgetest, forgetest_async,
util::{OutputExt, TestCommand, TestProject},
util::{TestCommand, TestProject},
};
use std::{path::PathBuf, str::FromStr};
use snapbox::file;
use std::str::FromStr;

/// This will insert _dummy_ contract that uses a library
///
Expand Down Expand Up @@ -150,15 +151,8 @@ forgetest_async!(can_create_template_contract, |prj, cmd| {
pk.as_str(),
]);

cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_create_template_contract.stdout"),
);

cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_create_template_contract-2nd.stdout"),
);
cmd.assert().stdout_eq(file!["../fixtures/can_create_template_contract.stdout"]);
klkvr marked this conversation as resolved.
Show resolved Hide resolved
cmd.assert().stdout_eq(file!["../fixtures/can_create_template_contract-2nd.stdout"]);
});

// tests that we can deploy the template contract
Expand All @@ -183,15 +177,8 @@ forgetest_async!(can_create_using_unlocked, |prj, cmd| {
"--unlocked",
]);

cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_create_using_unlocked.stdout"),
);

cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_create_using_unlocked-2nd.stdout"),
);
cmd.assert().stdout_eq(file!["../fixtures/can_create_using_unlocked.stdout"]);
cmd.assert().stdout_eq(file!["../fixtures/can_create_using_unlocked-2nd.stdout"]);
});

// tests that we can deploy with constructor args
Expand Down Expand Up @@ -221,21 +208,19 @@ contract ConstructorContract {
)
.unwrap();

cmd.forge_fuse().args([
"create",
"./src/ConstructorContract.sol:ConstructorContract",
"--rpc-url",
rpc.as_str(),
"--private-key",
pk.as_str(),
"--constructor-args",
"My Constructor",
]);

cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_create_with_constructor_args.stdout"),
);
cmd.forge_fuse()
.args([
"create",
"./src/ConstructorContract.sol:ConstructorContract",
"--rpc-url",
rpc.as_str(),
"--private-key",
pk.as_str(),
"--constructor-args",
"My Constructor",
])
.assert_success()
.stdout_eq(file!["../fixtures/can_create_with_constructor_args.stdout"]);

prj.add_source(
"TupleArrayConstructorContract",
Expand All @@ -252,21 +237,19 @@ contract TupleArrayConstructorContract {
)
.unwrap();

cmd.forge_fuse().args([
"create",
"./src/TupleArrayConstructorContract.sol:TupleArrayConstructorContract",
"--rpc-url",
rpc.as_str(),
"--private-key",
pk.as_str(),
"--constructor-args",
"[(1,2), (2,3), (3,4)]",
]);

cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_create_with_tuple_constructor_args.stdout"),
);
cmd.forge_fuse()
.args([
"create",
"./src/TupleArrayConstructorContract.sol:TupleArrayConstructorContract",
"--rpc-url",
rpc.as_str(),
"--private-key",
pk.as_str(),
"--constructor-args",
"[(1,2), (2,3), (3,4)]",
])
.assert()
.stdout_eq(file!["../fixtures/can_create_with_tuple_constructor_args.stdout"]);
});

// <https://github.com/foundry-rs/foundry/issues/6332>
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/tests/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ contract Dummy {
.unwrap();

cmd.args(["test", "--match-path", "src/dummy.sol"]);
cmd.assert_success()
cmd.assert_success();
});

forgetest_init!(should_not_shrink_fuzz_failure, |prj, cmd| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
No files changed, compilation skipped
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
Transaction hash: 0x3d78b08c411f05d5e79adc92a4c814e0f818d1a09c111b0ab688270f35a07ae7
Transaction hash: [..]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Compiling 1 files with 0.8.23
Solc 0.8.23 finished in 2.27s
...
Compiler run successful!
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Transaction hash: 0x4c3d9f7c4cc26876b43a11ba7ff218374471786a8ae8bf5574deb1d97fc1e851
Transaction hash: [..]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
No files changed, compilation skipped
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
Transaction hash: 0x3d78b08c411f05d5e79adc92a4c814e0f818d1a09c111b0ab688270f35a07ae7
Transaction hash: [..]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Compiling 1 files with 0.8.23
Solc 0.8.23 finished in 1.95s
...
Compiler run successful!
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Transaction hash: 0x4c3d9f7c4cc26876b43a11ba7ff218374471786a8ae8bf5574deb1d97fc1e851
Transaction hash: [..]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Compiling 1 files with 0.8.23
Solc 0.8.23 finished in 2.82s
...
Compiler run successful!
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Transaction hash: 0x294df85109c991ec2760cd51e5ddc869bf5dc3b249b296305ffcd1a0563b2eea
Transaction hash: [..]
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
Compiling 1 files with 0.8.23
Solc 0.8.23 finished in 26.44ms
Compiler run successful!
...
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
Transaction hash: 0x69625b76d83634603a9dbc5b836ef89bafdd9fc7c180fc6d636c5088353cf501
Transaction hash: [..]
3 changes: 2 additions & 1 deletion crates/forge/tests/fixtures/compile_json.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"formattedMessage": "DeclarationError: Undeclared identifier. Did you mean \"newNumber\"?\n --> src/jsonError.sol:7:18:\n |\n7 | number = newnumber; // error here\n | ^^^^^^^^^\n\n"
}
],
"sources": {}
"sources": {},
"build_infos": ["{...}"]
}
1 change: 1 addition & 0 deletions crates/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tracing.workspace = true
tracing-subscriber = { workspace = true, features = ["env-filter"] }
walkdir.workspace = true
rand.workspace = true
snapbox.workspace = true

[features]
# feature for integration tests that test external projects
Expand Down
9 changes: 7 additions & 2 deletions crates/test-utils/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use foundry_config::Config;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use regex::Regex;
use snapbox::cmd::OutputAssert;
use std::{
env,
ffi::OsStr,
Expand Down Expand Up @@ -921,8 +922,8 @@ impl TestCommand {

/// Runs the command and asserts that it resulted in success
#[track_caller]
pub fn assert_success(&mut self) {
self.output();
pub fn assert_success(&mut self) -> OutputAssert {
self.assert().success()
}

/// Executes command, applies stdin function and returns output
Expand Down Expand Up @@ -1055,6 +1056,10 @@ stderr:
lossy_string(&out.stderr),
)
}

pub fn assert(&mut self) -> OutputAssert {
OutputAssert::new(self.execute())
}
}

/// Extension trait for [`Output`].
Expand Down
Loading