Skip to content

Commit

Permalink
test: enhance tests (#9334)
Browse files Browse the repository at this point in the history
* test: enhance tests

* update ws url

* Assert json unordered

* Update crates/test-utils/src/util.rs

Co-authored-by: DaniPopes <[email protected]>

* Changes after review

* Fix rpc url test

---------

Co-authored-by: grandizzy <[email protected]>
Co-authored-by: grandizzy <[email protected]>
Co-authored-by: DaniPopes <[email protected]>
  • Loading branch information
4 people authored Nov 16, 2024
1 parent 3eb47ea commit d14c09f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
9 changes: 3 additions & 6 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,7 @@ casttest!(rpc_with_args, |_prj, cmd| {

// Call `cast rpc eth_getBlockByNumber 0x123 false`
cmd.args(["rpc", "--rpc-url", eth_rpc_url.as_str(), "eth_getBlockByNumber", "0x123", "false"])
.assert_success()
.stdout_eq(str![[r#"
.assert_json_stdout(str![[r#"
{"number":"0x123","hash":"0xc5dab4e189004a1312e9db43a40abb2de91ad7dd25e75880bf36016d8e9df524","transactions":[],"totalDifficulty":"0x4dea420908b","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","extraData":"0x476574682f4c5649562f76312e302e302f6c696e75782f676f312e342e32","nonce":"0x29d6547c196e00e0","miner":"0xbb7b8287f3f0a933474a79eae42cbca977791171","difficulty":"0x494433b31","gasLimit":"0x1388","gasUsed":"0x0","uncles":[],"sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x220","transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","stateRoot":"0x3fe6bd17aa85376c7d566df97d9f2e536f37f7a87abb3a6f9e2891cf9442f2e4","mixHash":"0x943056aa305aa6d22a3c06110942980342d1f4d4b11c17711961436a0f963ea0","parentHash":"0x7abfd11e862ccde76d6ea8ee20978aac26f4bcb55de1188cc0335be13e817017","timestamp":"0x55ba4564"}
"#]]);
Expand All @@ -670,8 +669,7 @@ casttest!(rpc_raw_params, |_prj, cmd| {
"--raw",
r#"["0x123", false]"#,
])
.assert_success()
.stdout_eq(str![[r#"
.assert_json_stdout(str![[r#"
{"number":"0x123","hash":"0xc5dab4e189004a1312e9db43a40abb2de91ad7dd25e75880bf36016d8e9df524","transactions":[],"totalDifficulty":"0x4dea420908b","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","extraData":"0x476574682f4c5649562f76312e302e302f6c696e75782f676f312e342e32","nonce":"0x29d6547c196e00e0","miner":"0xbb7b8287f3f0a933474a79eae42cbca977791171","difficulty":"0x494433b31","gasLimit":"0x1388","gasUsed":"0x0","uncles":[],"sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x220","transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","stateRoot":"0x3fe6bd17aa85376c7d566df97d9f2e536f37f7a87abb3a6f9e2891cf9442f2e4","mixHash":"0x943056aa305aa6d22a3c06110942980342d1f4d4b11c17711961436a0f963ea0","parentHash":"0x7abfd11e862ccde76d6ea8ee20978aac26f4bcb55de1188cc0335be13e817017","timestamp":"0x55ba4564"}
"#]]);
Expand All @@ -687,8 +685,7 @@ casttest!(rpc_raw_params_stdin, |_prj, cmd| {
stdin.write_all(b"\n[\n\"0x123\",\nfalse\n]\n").unwrap();
},
)
.assert_success()
.stdout_eq(str![[r#"
.assert_json_stdout(str![[r#"
{"number":"0x123","hash":"0xc5dab4e189004a1312e9db43a40abb2de91ad7dd25e75880bf36016d8e9df524","transactions":[],"totalDifficulty":"0x4dea420908b","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","extraData":"0x476574682f4c5649562f76312e302e302f6c696e75782f676f312e342e32","nonce":"0x29d6547c196e00e0","miner":"0xbb7b8287f3f0a933474a79eae42cbca977791171","difficulty":"0x494433b31","gasLimit":"0x1388","gasUsed":"0x0","uncles":[],"sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x220","transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","stateRoot":"0x3fe6bd17aa85376c7d566df97d9f2e536f37f7a87abb3a6f9e2891cf9442f2e4","mixHash":"0x943056aa305aa6d22a3c06110942980342d1f4d4b11c17711961436a0f963ea0","parentHash":"0x7abfd11e862ccde76d6ea8ee20978aac26f4bcb55de1188cc0335be13e817017","timestamp":"0x55ba4564"}
"#]]);
Expand Down
5 changes: 4 additions & 1 deletion crates/test-utils/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use std::sync::{
// List of general purpose infura keys to rotate through
static INFURA_KEYS: LazyLock<Vec<&'static str>> = LazyLock::new(|| {
let mut keys = vec![
"6cb19d07ca2d44f59befd61563b1037b",
"6d46c0cca653407b861f3f93f7b0236a",
"69a36846dec146e3a2898429be60be85",
// "16a8be88795540b9b3903d8de0f7baa5",
// "f4a0bdad42674adab5fc0ac077ffab2b",
// "5c812e02193c4ba793f8c214317582bd",
Expand Down Expand Up @@ -184,7 +187,7 @@ fn next_url(is_ws: bool, chain: NamedChain) -> String {

match (is_ws, is_infura) {
(false, true) => format!("https://{full}.infura.io/v3/{key}"),
(true, true) => format!("wss://{full}.infura.io/v3/{key}"),
(true, true) => format!("wss://{full}.infura.io/ws/v3/{key}"),
(false, false) => format!("https://{full}.g.alchemy.com/v2/{key}"),
(true, false) => format!("wss://{full}.g.alchemy.com/v2/{key}"),
}
Expand Down
11 changes: 10 additions & 1 deletion crates/test-utils/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use foundry_compilers::{
use foundry_config::Config;
use parking_lot::Mutex;
use regex::Regex;
use snapbox::{cmd::OutputAssert, str};
use snapbox::{assert_data_eq, cmd::OutputAssert, str, IntoData};
use std::{
env,
ffi::OsStr,
Expand Down Expand Up @@ -893,6 +893,15 @@ impl TestCommand {
self.assert().success()
}

/// Runs the command and asserts that it resulted in success, with expected JSON data.
#[track_caller]
pub fn assert_json_stdout(&mut self, expected: impl IntoData) {
let expected = expected.is(snapbox::data::DataFormat::Json).unordered();
let stdout = self.assert_success().get_output().stdout.clone();
let actual = stdout.into_data().is(snapbox::data::DataFormat::Json).unordered();
assert_data_eq!(actual, expected);
}

/// Runs the command and asserts that it **failed** nothing was printed to stdout.
#[track_caller]
pub fn assert_empty_stdout(&mut self) {
Expand Down
2 changes: 1 addition & 1 deletion testdata/default/cheats/RpcUrls.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract RpcUrlTest is DSTest {
// returns the correct url
function testCanGetRpcUrl() public {
string memory url = vm.rpcUrl("mainnet");
assertEq(bytes(url).length, 69);
assertTrue(bytes(url).length == 61 || bytes(url).length == 69);
}

// returns an error if env alias does not exist
Expand Down

0 comments on commit d14c09f

Please sign in to comment.