Skip to content

Commit d0ba6a2

Browse files
klkvrmattsse
andauthored
refactor(tests): add snapbox (#8406)
* refactor(tests): add snapbox * update some cast tests * fix * use str * rm fixtures --------- Co-authored-by: Matthias Seitz <[email protected]>
1 parent 72e44fb commit d0ba6a2

15 files changed

+141
-117
lines changed

Cargo.lock

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cast/tests/cli/main.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use alloy_primitives::{address, b256, Address, B256};
44
use foundry_test_utils::{
55
casttest,
66
rpc::{next_http_rpc_endpoint, next_ws_rpc_endpoint},
7+
str,
78
util::OutputExt,
89
};
910
use std::{fs, io::Write, path::Path, str::FromStr};
@@ -103,9 +104,10 @@ casttest!(wallet_sign_message_hex_data, |_prj, cmd| {
103104
"--private-key",
104105
"0x0000000000000000000000000000000000000000000000000000000000000001",
105106
"0x0000000000000000000000000000000000000000000000000000000000000000",
106-
]);
107-
let output = cmd.stdout_lossy();
108-
assert_eq!(output.trim(), "0x23a42ca5616ee730ff3735890c32fc7b9491a9f633faca9434797f2c845f5abf4d9ba23bd7edb8577acebaa3644dc5a4995296db420522bb40060f1693c33c9b1c");
107+
]).assert_success().stdout_eq(str![[r#"
108+
0x23a42ca5616ee730ff3735890c32fc7b9491a9f633faca9434797f2c845f5abf4d9ba23bd7edb8577acebaa3644dc5a4995296db420522bb40060f1693c33c9b1c
109+
110+
"#]]);
109111
});
110112

111113
// tests that `cast wallet sign typed-data` outputs the expected signature, given a JSON string
@@ -117,9 +119,10 @@ casttest!(wallet_sign_typed_data_string, |_prj, cmd| {
117119
"0x0000000000000000000000000000000000000000000000000000000000000001",
118120
"--data",
119121
"{\"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!\"}}",
120-
]);
121-
let output = cmd.stdout_lossy();
122-
assert_eq!(output.trim(), "0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b");
122+
]).assert_success().stdout_eq(str![[r#"
123+
0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b
124+
125+
"#]]);
123126
});
124127

125128
// tests that `cast wallet sign typed-data` outputs the expected signature, given a JSON file
@@ -137,9 +140,10 @@ casttest!(wallet_sign_typed_data_file, |_prj, cmd| {
137140
.into_string()
138141
.unwrap()
139142
.as_str(),
140-
]);
141-
let output = cmd.stdout_lossy();
142-
assert_eq!(output.trim(), "0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b");
143+
]).assert_success().stdout_eq(str![[r#"
144+
0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b
145+
146+
"#]]);
143147
});
144148

145149
// tests that `cast wallet list` outputs the local accounts
@@ -177,9 +181,12 @@ casttest!(wallet_private_key_from_mnemonic_arg, |_prj, cmd| {
177181
"private-key",
178182
"test test test test test test test test test test test junk",
179183
"1",
180-
]);
181-
let output = cmd.stdout_lossy();
182-
assert_eq!(output.trim(), "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d");
184+
])
185+
.assert_success()
186+
.stdout_eq(str![[r#"
187+
0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
188+
189+
"#]]);
183190
});
184191

185192
// tests that `cast wallet private-key` with options outputs the private key

crates/forge/tests/cli/build.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use foundry_common::fs::read_json_file;
21
use foundry_config::Config;
3-
use foundry_test_utils::forgetest;
2+
use foundry_test_utils::{file, forgetest, str};
43
use globset::Glob;
5-
use std::{collections::BTreeMap, path::PathBuf};
64

75
// tests that json is printed when --json is passed
86
forgetest!(compile_json, |prj, cmd| {
@@ -20,26 +18,14 @@ contract Dummy {
2018
.unwrap();
2119

2220
// set up command
23-
cmd.args(["compile", "--format-json"]);
24-
25-
// Exclude build_infos from output as IDs depend on root dir and are not deterministic.
26-
let mut output: BTreeMap<String, serde_json::Value> =
27-
serde_json::from_str(&cmd.stdout_lossy()).unwrap();
28-
output.remove("build_infos");
29-
30-
let expected: BTreeMap<String, serde_json::Value> = read_json_file(
31-
&PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/compile_json.stdout"),
32-
)
33-
.unwrap();
34-
35-
similar_asserts::assert_eq!(output, expected);
21+
cmd.args(["compile", "--format-json"])
22+
.assert()
23+
.stdout_eq(file!["../fixtures/compile_json.stdout": Json]);
3624
});
3725

3826
// tests build output is as expected
3927
forgetest_init!(exact_build_output, |prj, cmd| {
40-
cmd.args(["build", "--force"]);
41-
let stdout = cmd.stdout_lossy();
42-
assert!(stdout.contains("Compiling"), "\n{stdout}");
28+
cmd.args(["build", "--force"]).assert_success().stdout_eq(str!["Compiling[..]\n..."]);
4329
});
4430

4531
// tests build output is as expected

crates/forge/tests/cli/create.rs

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use anvil::{spawn, NodeConfig};
99
use foundry_compilers::artifacts::{remappings::Remapping, BytecodeHash};
1010
use foundry_config::Config;
1111
use foundry_test_utils::{
12-
forgetest, forgetest_async,
13-
util::{OutputExt, TestCommand, TestProject},
12+
forgetest, forgetest_async, str,
13+
util::{TestCommand, TestProject},
1414
};
15-
use std::{path::PathBuf, str::FromStr};
15+
use std::str::FromStr;
1616

1717
/// This will insert _dummy_ contract that uses a library
1818
///
@@ -150,15 +150,22 @@ forgetest_async!(can_create_template_contract, |prj, cmd| {
150150
pk.as_str(),
151151
]);
152152

153-
cmd.unchecked_output().stdout_matches_path(
154-
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
155-
.join("tests/fixtures/can_create_template_contract.stdout"),
156-
);
153+
cmd.assert().stdout_eq(str![[r#"
154+
...
155+
Compiler run successful!
156+
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
157+
Deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
158+
Transaction hash: [..]
157159
158-
cmd.unchecked_output().stdout_matches_path(
159-
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
160-
.join("tests/fixtures/can_create_template_contract-2nd.stdout"),
161-
);
160+
"#]]);
161+
162+
cmd.assert().stdout_eq(str![[r#"
163+
No files changed, compilation skipped
164+
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
165+
Deployed to: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
166+
Transaction hash: [..]
167+
168+
"#]]);
162169
});
163170

164171
// tests that we can deploy the template contract
@@ -183,15 +190,21 @@ forgetest_async!(can_create_using_unlocked, |prj, cmd| {
183190
"--unlocked",
184191
]);
185192

186-
cmd.unchecked_output().stdout_matches_path(
187-
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
188-
.join("tests/fixtures/can_create_using_unlocked.stdout"),
189-
);
190-
191-
cmd.unchecked_output().stdout_matches_path(
192-
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
193-
.join("tests/fixtures/can_create_using_unlocked-2nd.stdout"),
194-
);
193+
cmd.assert().stdout_eq(str![[r#"
194+
...
195+
Compiler run successful!
196+
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
197+
Deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
198+
Transaction hash: [..]
199+
200+
"#]]);
201+
cmd.assert().stdout_eq(str![[r#"
202+
No files changed, compilation skipped
203+
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
204+
Deployed to: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
205+
Transaction hash: [..]
206+
207+
"#]]);
195208
});
196209

197210
// tests that we can deploy with constructor args
@@ -221,21 +234,26 @@ contract ConstructorContract {
221234
)
222235
.unwrap();
223236

224-
cmd.forge_fuse().args([
225-
"create",
226-
"./src/ConstructorContract.sol:ConstructorContract",
227-
"--rpc-url",
228-
rpc.as_str(),
229-
"--private-key",
230-
pk.as_str(),
231-
"--constructor-args",
232-
"My Constructor",
233-
]);
234-
235-
cmd.unchecked_output().stdout_matches_path(
236-
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
237-
.join("tests/fixtures/can_create_with_constructor_args.stdout"),
238-
);
237+
cmd.forge_fuse()
238+
.args([
239+
"create",
240+
"./src/ConstructorContract.sol:ConstructorContract",
241+
"--rpc-url",
242+
rpc.as_str(),
243+
"--private-key",
244+
pk.as_str(),
245+
"--constructor-args",
246+
"My Constructor",
247+
])
248+
.assert_success()
249+
.stdout_eq(str![[r#"
250+
...
251+
Compiler run successful!
252+
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
253+
Deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
254+
Transaction hash: [..]
255+
256+
"#]]);
239257

240258
prj.add_source(
241259
"TupleArrayConstructorContract",
@@ -252,21 +270,26 @@ contract TupleArrayConstructorContract {
252270
)
253271
.unwrap();
254272

255-
cmd.forge_fuse().args([
256-
"create",
257-
"./src/TupleArrayConstructorContract.sol:TupleArrayConstructorContract",
258-
"--rpc-url",
259-
rpc.as_str(),
260-
"--private-key",
261-
pk.as_str(),
262-
"--constructor-args",
263-
"[(1,2), (2,3), (3,4)]",
264-
]);
265-
266-
cmd.unchecked_output().stdout_matches_path(
267-
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
268-
.join("tests/fixtures/can_create_with_tuple_constructor_args.stdout"),
269-
);
273+
cmd.forge_fuse()
274+
.args([
275+
"create",
276+
"./src/TupleArrayConstructorContract.sol:TupleArrayConstructorContract",
277+
"--rpc-url",
278+
rpc.as_str(),
279+
"--private-key",
280+
pk.as_str(),
281+
"--constructor-args",
282+
"[(1,2), (2,3), (3,4)]",
283+
])
284+
.assert()
285+
.stdout_eq(str![[r#"
286+
...
287+
Compiler run successful!
288+
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
289+
Deployed to: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
290+
Transaction hash: [..]
291+
292+
"#]]);
270293
});
271294

272295
// <https://github.com/foundry-rs/foundry/issues/6332>

crates/forge/tests/cli/test_cmd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ contract Dummy {
548548
.unwrap();
549549

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

554554
forgetest_init!(should_not_shrink_fuzz_failure, |prj, cmd| {

crates/forge/tests/fixtures/can_create_template_contract-2nd.stdout

Lines changed: 0 additions & 4 deletions
This file was deleted.

crates/forge/tests/fixtures/can_create_template_contract.stdout

Lines changed: 0 additions & 6 deletions
This file was deleted.

crates/forge/tests/fixtures/can_create_using_unlocked-2nd.stdout

Lines changed: 0 additions & 4 deletions
This file was deleted.

crates/forge/tests/fixtures/can_create_using_unlocked.stdout

Lines changed: 0 additions & 6 deletions
This file was deleted.

crates/forge/tests/fixtures/can_create_with_constructor_args.stdout

Lines changed: 0 additions & 6 deletions
This file was deleted.

crates/forge/tests/fixtures/can_create_with_tuple_constructor_args.stdout

Lines changed: 0 additions & 6 deletions
This file was deleted.

crates/forge/tests/fixtures/compile_json.stdout

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"formattedMessage": "DeclarationError: Undeclared identifier. Did you mean \"newNumber\"?\n --> src/jsonError.sol:7:18:\n |\n7 | number = newnumber; // error here\n | ^^^^^^^^^\n\n"
1616
}
1717
],
18-
"sources": {}
18+
"sources": {},
19+
"build_infos": ["{...}"]
1920
}

crates/test-utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tracing.workspace = true
3333
tracing-subscriber = { workspace = true, features = ["env-filter"] }
3434
walkdir.workspace = true
3535
rand.workspace = true
36+
snapbox = { version = "0.6.9", features = ["json"] }
3637

3738
[features]
3839
# feature for integration tests that test external projects

crates/test-utils/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub use script::{ScriptOutcome, ScriptTester};
2828
// re-exports for convenience
2929
pub use foundry_compilers;
3030

31+
pub use snapbox::{file, str};
32+
3133
/// Initializes tracing for tests.
3234
pub fn init_tracing() {
3335
let _ = tracing_subscriber::FmtSubscriber::builder()

0 commit comments

Comments
 (0)