Skip to content

Commit

Permalink
refactor(tests): add snapbox (#8406)
Browse files Browse the repository at this point in the history
* refactor(tests): add snapbox

* update some cast tests

* fix

* use str

* rm fixtures

---------

Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
klkvr and mattsse committed Jul 11, 2024
1 parent 72e44fb commit d0ba6a2
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 117 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

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

31 changes: 19 additions & 12 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use alloy_primitives::{address, b256, Address, B256};
use foundry_test_utils::{
casttest,
rpc::{next_http_rpc_endpoint, next_ws_rpc_endpoint},
str,
util::OutputExt,
};
use std::{fs, io::Write, path::Path, str::FromStr};
Expand Down Expand Up @@ -103,9 +104,10 @@ 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(str![[r#"
0x23a42ca5616ee730ff3735890c32fc7b9491a9f633faca9434797f2c845f5abf4d9ba23bd7edb8577acebaa3644dc5a4995296db420522bb40060f1693c33c9b1c
"#]]);
});

// tests that `cast wallet sign typed-data` outputs the expected signature, given a JSON string
Expand All @@ -117,9 +119,10 @@ 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(str![[r#"
0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b
"#]]);
});

// tests that `cast wallet sign typed-data` outputs the expected signature, given a JSON file
Expand All @@ -137,9 +140,10 @@ 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(str![[r#"
0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b
"#]]);
});

// tests that `cast wallet list` outputs the local accounts
Expand Down Expand Up @@ -177,9 +181,12 @@ 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(str![[r#"
0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
"#]]);
});

// tests that `cast wallet private-key` with options outputs the private key
Expand Down
24 changes: 5 additions & 19 deletions crates/forge/tests/cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use foundry_common::fs::read_json_file;
use foundry_config::Config;
use foundry_test_utils::forgetest;
use foundry_test_utils::{file, forgetest, str};
use globset::Glob;
use std::{collections::BTreeMap, path::PathBuf};

// tests that json is printed when --json is passed
forgetest!(compile_json, |prj, cmd| {
Expand All @@ -20,26 +18,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(str!["Compiling[..]\n..."]);
});

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

/// This will insert _dummy_ contract that uses a library
///
Expand Down Expand Up @@ -150,15 +150,22 @@ 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.assert().stdout_eq(str![[r#"
...
Compiler run successful!
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Transaction hash: [..]
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(str![[r#"
No files changed, compilation skipped
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
Transaction hash: [..]
"#]]);
});

// tests that we can deploy the template contract
Expand All @@ -183,15 +190,21 @@ 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(str![[r#"
...
Compiler run successful!
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Transaction hash: [..]
"#]]);
cmd.assert().stdout_eq(str![[r#"
No files changed, compilation skipped
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
Transaction hash: [..]
"#]]);
});

// tests that we can deploy with constructor args
Expand Down Expand Up @@ -221,21 +234,26 @@ 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(str![[r#"
...
Compiler run successful!
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Transaction hash: [..]
"#]]);

prj.add_source(
"TupleArrayConstructorContract",
Expand All @@ -252,21 +270,26 @@ 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(str![[r#"
...
Compiler run successful!
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
Transaction hash: [..]
"#]]);
});

// <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

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions crates/forge/tests/fixtures/can_create_using_unlocked.stdout

This file was deleted.

This file was deleted.

This file was deleted.

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 = { version = "0.6.9", features = ["json"] }

[features]
# feature for integration tests that test external projects
Expand Down
2 changes: 2 additions & 0 deletions crates/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub use script::{ScriptOutcome, ScriptTester};
// re-exports for convenience
pub use foundry_compilers;

pub use snapbox::{file, str};

/// Initializes tracing for tests.
pub fn init_tracing() {
let _ = tracing_subscriber::FmtSubscriber::builder()
Expand Down
Loading

0 comments on commit d0ba6a2

Please sign in to comment.