diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fca3ed65e5..2ffceb36a4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,6 +13,14 @@ jobs: name: Test Forge / Unit and Integration Tests runs-on: ubuntu-latest steps: + - name: Set NODE_URL for foundry-rs/starknet-foundry + if: ${{ github.event.pull_request.head.repo.full_name == 'foundry-rs/starknet-foundry' }} + run: echo "NODE_URL=$(echo ${{ secrets.NODE_URL }}:7070)" >> $GITHUB_ENV + + - name: Set NODE_URL for forks + if: ${{ github.event.pull_request.head.repo.full_name != 'foundry-rs/starknet-foundry' }} + run: echo "NODE_URL=$(echo ${{ secrets.NODE_URL }})" >> $GITHUB_ENV + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 @@ -72,6 +80,14 @@ jobs: name: Test Cheatnet runs-on: ubuntu-latest steps: + - name: Set NODE_URL for foundry-rs/starknet-foundry + if: ${{ github.event.pull_request.head.repo.full_name == 'foundry-rs/starknet-foundry' }} + run: echo "NODE_URL=$(echo ${{ secrets.NODE_URL }}:7070)" >> $GITHUB_ENV + + - name: Set NODE_URL for forks + if: ${{ github.event.pull_request.head.repo.full_name != 'foundry-rs/starknet-foundry' }} + run: echo "NODE_URL=$(echo ${{ secrets.NODE_URL }})" >> $GITHUB_ENV + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3235cfe0b3..4e5dff8a7b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -195,7 +195,7 @@ jobs: - name: Smoke test shell: bash env: - RPC_URL: ${{ secrets.CHEATNET_RPC_URL }} + RPC_URL: ${{ secrets.RPC_URL }} run: | BINARY_PATH="${{ env.BINARY_PATH }}" BINARY_PATH="${BINARY_PATH%.tar.gz}" diff --git a/Cargo.lock b/Cargo.lock index 8ead855739..5602de492c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1186,6 +1186,7 @@ dependencies = [ "scarb-metadata", "serde", "serde_json", + "shared", "starknet", "starknet_api", "tempfile", @@ -1993,6 +1994,7 @@ dependencies = [ "trace-data", "universal-sierra-compiler-api", "url", + "walkdir", ] [[package]] diff --git a/crates/cheatnet/Cargo.toml b/crates/cheatnet/Cargo.toml index 1eb1ffe3dc..3c34d3d1c7 100644 --- a/crates/cheatnet/Cargo.toml +++ b/crates/cheatnet/Cargo.toml @@ -47,5 +47,6 @@ ctor.workspace = true indoc.workspace = true rayon.workspace = true glob.workspace = true +shared.workspace = true test-case.workspace = true tempfile.workspace = true diff --git a/crates/cheatnet/tests/common/state.rs b/crates/cheatnet/tests/common/state.rs index 52f399afee..fe2611b5e0 100644 --- a/crates/cheatnet/tests/common/state.rs +++ b/crates/cheatnet/tests/common/state.rs @@ -4,6 +4,7 @@ use blockifier::state::cached_state::{ use cheatnet::constants::build_testing_state; use cheatnet::forking::state::ForkStateReader; use cheatnet::state::ExtendedStateReader; +use shared::test_utils::node_url_with_version; use starknet_api::block::BlockNumber; pub fn create_cached_state() -> CachedState { @@ -24,7 +25,7 @@ pub fn create_fork_cached_state_at( block_number: u64, cache_dir: &str, ) -> CachedState { - let node_url = "http://188.34.188.184:7070/rpc/v0_7".parse().unwrap(); + let node_url = node_url_with_version().unwrap(); CachedState::new( ExtendedStateReader { dict_state_reader: build_testing_state(), diff --git a/crates/cheatnet/tests/starknet/forking.rs b/crates/cheatnet/tests/starknet/forking.rs index 1b67113a3e..8bdd7c6c01 100644 --- a/crates/cheatnet/tests/starknet/forking.rs +++ b/crates/cheatnet/tests/starknet/forking.rs @@ -644,7 +644,7 @@ fn test_cached_block_info_merging() { #[test] fn test_calling_nonexistent_url() { let temp_dir = TempDir::new().unwrap(); - let nonexistent_url = "http://188.34.188.184:9546".parse().unwrap(); + let nonexistent_url = "http://nonexistent-node-address.com".parse().unwrap(); let mut cached_fork_state = CachedState::new( ExtendedStateReader { dict_state_reader: build_testing_state(), diff --git a/crates/forge/Cargo.toml b/crates/forge/Cargo.toml index 1ed0c9fd9a..7285f93873 100644 --- a/crates/forge/Cargo.toml +++ b/crates/forge/Cargo.toml @@ -74,3 +74,4 @@ axum.workspace = true lazy_static.workspace = true indoc.workspace = true tempfile.workspace = true +walkdir.workspace = true diff --git a/crates/forge/tests/data/diagnostics_and_plugins/tests/test_fork.cairo b/crates/forge/tests/data/diagnostics_and_plugins/tests/test_fork.cairo index dc48d7274d..630d56df8d 100644 --- a/crates/forge/tests/data/diagnostics_and_plugins/tests/test_fork.cairo +++ b/crates/forge/tests/data/diagnostics_and_plugins/tests/test_fork.cairo @@ -5,94 +5,91 @@ fn incorrect_fork_attributes() { } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Number(Latest))] +#[fork(url: "{{ RPC_URL }}", block_id: BlockId::Number(Latest))] fn incorrect_fork_attributes2() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Number(19446744073709551615))] +#[fork(url: "{{ RPC_URL }}", block_id: BlockId::Number(19446744073709551615))] fn incorrect_fork_attributes3() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Hash(Random))] +#[fork(url: "{{ RPC_URL }}", block_id: BlockId::Hash(Random))] fn incorrect_fork_attributes4() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Hash(Latest))] +#[fork(url: "{{ RPC_URL }}", block_id: BlockId::Hash(Latest))] fn incorrect_fork_attributes5() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Tag(12345))] +#[fork(url: "{{ RPC_URL }}", block_id: BlockId::Tag(12345))] fn incorrect_fork_attributes6() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Tag(0x12345))] +#[fork(url: "{{ RPC_URL }}", block_id: BlockId::Tag(0x12345))] fn incorrect_fork_attributes7() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Tag(Random))] +#[fork(url: "{{ RPC_URL }}", block_id: BlockId::Tag(Random))] fn incorrect_fork_attributes8() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Number(Random))] +#[fork(url: "{{ RPC_URL }}", block_id: BlockId::Number(Random))] fn incorrect_fork_attributes9() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: Number(12345))] +#[fork(url: "{{ RPC_URL }}", block_id: Number(12345))] fn incorrect_fork_attributes10() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: Hash(0x12345))] +#[fork(url: "{{ RPC_URL }}", block_id: Hash(0x12345))] fn incorrect_fork_attributes11() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: Tag(Latest))] +#[fork(url: "{{ RPC_URL }}", block_id: Tag(Latest))] fn incorrect_fork_attributes12() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockWhat::Number(12345))] +#[fork(url: "{{ RPC_URL }}", block_id: BlockWhat::Number(12345))] fn incorrect_fork_attributes13() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: Something::BlockId::Number(12345))] +#[fork(url: "{{ RPC_URL }}", block_id: Something::BlockId::Number(12345))] fn incorrect_fork_attributes14() { assert(1 == 1, 'ok') } #[test] -#[fork( - url: "http://188.34.188.184:7070/rpc/v0_7", - block_id: BlockId::Tag(xddd::d00pa::hehe::BlockTag::Latest) -)] +#[fork(url: "{{ RPC_URL }}", block_id: BlockId::Tag(xddd::d00pa::hehe::BlockTag::Latest))] fn incorrect_fork_attributes15() { assert(1 == 1, 'ok') } #[test] -#[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Tag(sumting::Latest))] +#[fork(url: "{{ RPC_URL }}", block_id: BlockId::Tag(sumting::Latest))] fn incorrect_fork_attributes16() { assert(1 == 1, 'ok') } diff --git a/crates/forge/tests/data/forking/src/lib.cairo b/crates/forge/tests/data/forking/src/lib.cairo index c82386142c..98e07c656d 100644 --- a/crates/forge/tests/data/forking/src/lib.cairo +++ b/crates/forge/tests/data/forking/src/lib.cairo @@ -11,7 +11,7 @@ mod tests { } #[test] - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Number(54060))] + #[fork(url: "{{ RPC_URL }}", block_id: BlockId::Number(54060))] fn test_fork_simple() { let dispatcher = IHelloStarknetDispatcher { contract_address: contract_address_const::< @@ -29,7 +29,7 @@ mod tests { } #[test] - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Number(0xd32c))] + #[fork(url: "{{ RPC_URL }}", block_id: BlockId::Number(0xd32c))] fn test_fork_simple_number_hex() { let dispatcher = IHelloStarknetDispatcher { contract_address: contract_address_const::< @@ -48,7 +48,7 @@ mod tests { #[test] #[fork( - url: "http://188.34.188.184:7070/rpc/v0_7", + url: "{{ RPC_URL }}", block_id: BlockId::Hash(0x06ae121e46f5375f93b00475fb130348ae38148e121f84b0865e17542e9485de) )] fn test_fork_simple_hash_hex() { @@ -69,7 +69,7 @@ mod tests { #[test] #[fork( - url: "http://188.34.188.184:7070/rpc/v0_7", + url: "{{ RPC_URL }}", block_id: BlockId::Hash( 3021433528476416000728121069095289682281028310523383289416465162415092565470 ) @@ -91,7 +91,7 @@ mod tests { } #[test] - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Tag(Latest))] + #[fork(url: "{{ RPC_URL }}", block_id: BlockId::Tag(Latest))] fn print_block_number_when_latest() { assert(1 == 1, ''); } diff --git a/crates/forge/tests/e2e/common/runner.rs b/crates/forge/tests/e2e/common/runner.rs index cf7449fc2a..45421be9f5 100644 --- a/crates/forge/tests/e2e/common/runner.rs +++ b/crates/forge/tests/e2e/common/runner.rs @@ -3,12 +3,15 @@ use assert_fs::TempDir; use camino::Utf8PathBuf; use indoc::formatdoc; use shared::command::CommandExt; +use shared::test_utils::node_url_with_version; use snapbox::cmd::{cargo_bin, Command as SnapboxCommand}; +use std::path::Path; use std::process::Command; use std::str::FromStr; use std::{env, fs}; use test_utils::tempdir_with_tool_versions; use toml_edit::{value, DocumentMut}; +use walkdir::WalkDir; pub(crate) fn runner(temp_dir: &TempDir) -> SnapboxCommand { SnapboxCommand::new(cargo_bin!("snforge")) @@ -56,6 +59,33 @@ pub(crate) fn setup_package(package_name: &str) -> TempDir { setup_package_with_file_patterns(package_name, BASE_FILE_PATTERNS) } +pub(crate) fn setup_package_with_rpc_url_placeholders( + package_name: &str, + file_patterns: &[&str], +) -> TempDir { + let temp_dir = setup_package_with_file_patterns(package_name, file_patterns); + replace_rpc_url_placeholders(temp_dir.path()); + temp_dir +} + +fn replace_rpc_url_placeholders(dir_path: &Path) { + let url = node_url_with_version().unwrap(); + let temp_dir_files = WalkDir::new(dir_path); + for entry in temp_dir_files { + let entry = entry.unwrap(); + + let path = entry.path(); + + if path.is_file() { + let content = fs::read_to_string(path).unwrap(); + + let modified_content = content.replace("{{ RPC_URL }}", url.as_str()); + + fs::write(path, modified_content).unwrap(); + } + } +} + pub(crate) fn setup_hello_workspace() -> TempDir { let temp = tempdir_with_tool_versions().unwrap(); temp.copy_from("tests/data/hello_workspaces", &["**/*.cairo", "**/*.toml"]) diff --git a/crates/forge/tests/e2e/diagnostics_and_plugins.rs b/crates/forge/tests/e2e/diagnostics_and_plugins.rs index 81bad29657..19242cd5bc 100644 --- a/crates/forge/tests/e2e/diagnostics_and_plugins.rs +++ b/crates/forge/tests/e2e/diagnostics_and_plugins.rs @@ -1,10 +1,15 @@ -use super::common::runner::{setup_package, test_runner}; +use super::common::runner::{ + setup_package_with_rpc_url_placeholders, test_runner, BASE_FILE_PATTERNS, +}; use indoc::formatdoc; +use shared::test_utils::node_url_with_version; use shared::test_utils::output_assert::assert_stderr_contains; #[test] fn print_error_if_attributes_incorrect() { - let mock_tests_dir = setup_package("diagnostics_and_plugins"); + let node_url = node_url_with_version().unwrap(); + let mock_tests_dir = + setup_package_with_rpc_url_placeholders("diagnostics_and_plugins", BASE_FILE_PATTERNS); let mock_tests_dir_path = mock_tests_dir.path().canonicalize().unwrap(); let mock_tests_dir_path_str = mock_tests_dir_path.to_str().unwrap(); @@ -29,42 +34,42 @@ fn print_error_if_attributes_incorrect() { error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:8:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Number(Latest))] + #[fork(url: "{node_url}", block_id: BlockId::Number(Latest))] ^*****************************************************************************^ error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:14:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Number(19446744073709551615))] + #[fork(url: "{node_url}", block_id: BlockId::Number(19446744073709551615))] ^*******************************************************************************************^ error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:20:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Hash(Random))] + #[fork(url: "{node_url}", block_id: BlockId::Hash(Random))] ^***************************************************************************^ error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:26:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Hash(Latest))] + #[fork(url: "{node_url}", block_id: BlockId::Hash(Latest))] ^***************************************************************************^ error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:32:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Tag(12345))] + #[fork(url: "{node_url}", block_id: BlockId::Tag(12345))] ^*************************************************************************^ error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:38:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Tag(0x12345))] + #[fork(url: "{node_url}", block_id: BlockId::Tag(0x12345))] ^***************************************************************************^ error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:44:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Tag(Random))] + #[fork(url: "{node_url}", block_id: BlockId::Tag(Random))] ^**************************************************************************^ error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:50:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Number(Random))] + #[fork(url: "{node_url}", block_id: BlockId::Number(Random))] ^*****************************************************************************^ error: Plugin diagnostic: Expected fuzzer config must be of the form `runs: , seed: ` @@ -85,27 +90,27 @@ fn print_error_if_attributes_incorrect() { error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:56:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: Number(12345))] + #[fork(url: "{node_url}", block_id: Number(12345))] ^*******************************************************************^ error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:62:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: Hash(0x12345))] + #[fork(url: "{node_url}", block_id: Hash(0x12345))] ^*******************************************************************^ error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:68:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: Tag(Latest))] + #[fork(url: "{node_url}", block_id: Tag(Latest))] ^*****************************************************************^ error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:74:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockWhat::Number(12345))] + #[fork(url: "{node_url}", block_id: BlockWhat::Number(12345))] ^******************************************************************************^ error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:80:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: Something::BlockId::Number(12345))] + #[fork(url: "{node_url}", block_id: Something::BlockId::Number(12345))] ^***************************************************************************************^ error: Plugin diagnostic: Expected panic must be of the form `expected: ` or `expected: "some string"` or `expected: `. @@ -121,7 +126,7 @@ fn print_error_if_attributes_incorrect() { error: Plugin diagnostic: Expected fork config must be of the form `url: , block_id: `. --> {mock_tests_dir_path_str}/tests/test_fork.cairo:95:7 - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Tag(sumting::Latest))] + #[fork(url: "{node_url}", block_id: BlockId::Tag(sumting::Latest))] ^***********************************************************************************^ "#}, ); diff --git a/crates/forge/tests/e2e/fork_warning.rs b/crates/forge/tests/e2e/fork_warning.rs index f9586f3874..c01ddd9d8c 100644 --- a/crates/forge/tests/e2e/fork_warning.rs +++ b/crates/forge/tests/e2e/fork_warning.rs @@ -1,8 +1,9 @@ use super::common::runner::{setup_package, test_runner}; use assert_fs::fixture::{FileWriteStr, PathChild}; use axum::{extract::Query, response::Redirect, routing::any, Router}; -use indoc::{formatdoc, indoc}; +use indoc::formatdoc; use lazy_static::lazy_static; +use shared::test_utils::node_url; use shared::{consts::EXPECTED_RPC_VERSION, test_utils::output_assert::assert_stdout_contains}; use std::{thread::sleep, time::Duration}; use tokio::{ @@ -39,17 +40,22 @@ fn setup_redirect_server() { #[test] fn should_print_warning() { let temp = setup_package("empty"); + let mut node_url = node_url().unwrap(); + node_url.set_path("rpc/v0_5"); temp.child("tests/test.cairo") - .write_str(indoc!( - r#" - #[fork(url: "http://188.34.188.184:7070/rpc/v0_5", block_id: BlockId::Tag(BlockTag::Latest))] + .write_str( + formatdoc!( + r#" + #[fork(url: "{node_url}", block_id: BlockId::Tag(BlockTag::Latest))] #[test] - fn t1() { + fn t1() {{ assert!(false); - } + }} "# - )) + ) + .as_str(), + ) .unwrap(); let output = test_runner(&temp).assert(); @@ -60,7 +66,7 @@ fn should_print_warning() { r" [..]Compiling[..] [..]Finished[..] - [WARNING] RPC node with the url http://188.34.188.184:7070/rpc/v0_5 uses incompatible version 0.5.1. Expected version: {EXPECTED_RPC_VERSION} + [WARNING] RPC node with the url {node_url} uses incompatible version 0.5.1. Expected version: {EXPECTED_RPC_VERSION} Collected 1 test(s) from empty package @@ -71,7 +77,7 @@ fn should_print_warning() { Failure[..] Tests: 0 passed, 1 failed, 0 skipped, 0 ignored, 0 filtered out - Latest block number = [..] for url = http://188.34.188.184:7070/rpc/v0_5 + Latest block number = [..] for url = {node_url} Failures: tests::test::t1 @@ -83,22 +89,27 @@ fn should_print_warning() { #[test] fn should_dedup_urls() { let temp = setup_package("empty"); + let mut node_url = node_url().unwrap(); + node_url.set_path("rpc/v0_5"); temp.child("tests/test.cairo") - .write_str(indoc!( - r#" - #[fork(url: "http://188.34.188.184:7070/rpc/v0_5", block_id: BlockId::Tag(BlockTag::Latest))] + .write_str( + formatdoc!( + r#" + #[fork(url: "{node_url}", block_id: BlockId::Tag(BlockTag::Latest))] #[test] - fn t1() { + fn t1() {{ assert!(false); - } - #[fork(url: "http://188.34.188.184:7070/rpc/v0_5", block_id: BlockId::Tag(BlockTag::Latest))] + }} + #[fork(url: "{node_url}", block_id: BlockId::Tag(BlockTag::Latest))] #[test] - fn t2() { + fn t2() {{ assert!(false); - } + }} "# - )) + ) + .as_str(), + ) .unwrap(); let output = test_runner(&temp).assert(); @@ -109,7 +120,7 @@ fn should_dedup_urls() { r" [..]Compiling[..] [..]Finished[..] - [WARNING] RPC node with the url http://188.34.188.184:7070/rpc/v0_5 uses incompatible version 0.5.1. Expected version: {EXPECTED_RPC_VERSION} + [WARNING] RPC node with the url {node_url} uses incompatible version 0.5.1. Expected version: {EXPECTED_RPC_VERSION} Collected 2 test(s) from empty package @@ -123,7 +134,7 @@ fn should_dedup_urls() { Failure[..] Tests: 0 passed, 2 failed, 0 skipped, 0 ignored, 0 filtered out - Latest block number = [..] for url = http://188.34.188.184:7070/rpc/v0_5 + Latest block number = [..] for url = {node_url} Failures: tests::test::t1 @@ -138,22 +149,24 @@ fn should_print_foreach() { setup_redirect_server(); let temp = setup_package("empty"); + let mut node_url = node_url().unwrap(); + node_url.set_path("rpc/v0_5"); temp.child("tests/test.cairo") - .write_str(indoc!( + .write_str(formatdoc!( r#" - #[fork(url: "http://127.0.0.1:3030?url=http://188.34.188.184:7070/rpc/v0_5", block_id: BlockId::Tag(BlockTag::Latest))] + #[fork(url: "http://127.0.0.1:3030?url={node_url}", block_id: BlockId::Tag(BlockTag::Latest))] #[test] - fn t1() { + fn t1() {{ assert!(false); - } - #[fork(url: "http://188.34.188.184:7070/rpc/v0_5", block_id: BlockId::Tag(BlockTag::Latest))] + }} + #[fork(url: "{node_url}", block_id: BlockId::Tag(BlockTag::Latest))] #[test] - fn t2() { + fn t2() {{ assert!(false); - } + }} "# - )) + ).as_str()) .unwrap(); let output = test_runner(&temp).assert(); @@ -164,8 +177,8 @@ fn should_print_foreach() { r" [..]Compiling[..] [..]Finished[..] - [WARNING] RPC node with the url http://127.0.0.1:3030?url=http://188.34.188.184:7070/rpc/v0_5 uses incompatible version 0.5.1. Expected version: {EXPECTED_RPC_VERSION} - [WARNING] RPC node with the url http://188.34.188.184:7070/rpc/v0_5 uses incompatible version 0.5.1. Expected version: {EXPECTED_RPC_VERSION} + [WARNING] RPC node with the url http://127.0.0.1:3030?url={node_url} uses incompatible version 0.5.1. Expected version: {EXPECTED_RPC_VERSION} + [WARNING] RPC node with the url {node_url} uses incompatible version 0.5.1. Expected version: {EXPECTED_RPC_VERSION} Collected 2 test(s) from empty package @@ -179,8 +192,8 @@ fn should_print_foreach() { Failure[..] Tests: 0 passed, 2 failed, 0 skipped, 0 ignored, 0 filtered out - Latest block number = [..] for url = http://127.0.0.1:3030?url=http://188.34.188.184:7070/rpc/v0_5 - Latest block number = [..] for url = http://188.34.188.184:7070/rpc/v0_5 + Latest block number = [..] for url = http://127.0.0.1:3030?url={node_url} + Latest block number = [..] for url = {node_url} Failures: tests::test::t1 diff --git a/crates/forge/tests/e2e/forking.rs b/crates/forge/tests/e2e/forking.rs index c7a87789bf..5da473ac9d 100644 --- a/crates/forge/tests/e2e/forking.rs +++ b/crates/forge/tests/e2e/forking.rs @@ -1,13 +1,14 @@ use super::common::runner::{ - runner, setup_package, setup_package_with_file_patterns, test_runner, BASE_FILE_PATTERNS, + runner, setup_package_with_rpc_url_placeholders, test_runner, BASE_FILE_PATTERNS, }; use forge::shared_cache::CACHE_DIR; -use indoc::indoc; +use indoc::{formatdoc, indoc}; +use shared::test_utils::node_url_with_version; use shared::test_utils::output_assert::assert_stdout_contains; #[test] fn without_cache() { - let temp = setup_package("forking"); + let temp = setup_package_with_rpc_url_placeholders("forking", BASE_FILE_PATTERNS); let output = test_runner(&temp) .arg("forking::tests::test_fork_simple") @@ -38,7 +39,7 @@ fn without_cache() { /// /// The test that passed when using data from network, should fail for fabricated data. fn with_cache() { - let temp = setup_package_with_file_patterns( + let temp = setup_package_with_rpc_url_placeholders( "forking", &[BASE_FILE_PATTERNS, &[&format!("{CACHE_DIR}/*.json")]].concat(), ); @@ -73,7 +74,7 @@ fn with_cache() { #[test] fn with_clean_cache() { - let temp = setup_package_with_file_patterns( + let temp = setup_package_with_rpc_url_placeholders( "forking", &[BASE_FILE_PATTERNS, &[&format!("{CACHE_DIR}/*.json")]].concat(), ); @@ -102,10 +103,11 @@ fn with_clean_cache() { #[test] fn printing_latest_block_number() { - let temp = setup_package_with_file_patterns( + let temp = setup_package_with_rpc_url_placeholders( "forking", &[BASE_FILE_PATTERNS, &[&format!("{CACHE_DIR}/*.json")]].concat(), ); + let node_url = node_url_with_version().unwrap(); let output = test_runner(&temp) .args(["--exact", "forking::tests::print_block_number_when_latest"]) @@ -114,7 +116,7 @@ fn printing_latest_block_number() { assert_stdout_contains( output, - indoc! {r" + formatdoc! {r" [..]Compiling[..] [..]Finished[..] @@ -124,7 +126,7 @@ fn printing_latest_block_number() { [PASS] forking::tests::print_block_number_when_latest [..] Tests: 1 passed, 0 failed, 0 skipped, 0 ignored, 4 filtered out - Latest block number = [..] for url = http://188.34.188.184:7070/rpc/v0_7 + Latest block number = [..] for url = {node_url} "}, ); } diff --git a/crates/forge/tests/integration/cheat_fork.rs b/crates/forge/tests/integration/cheat_fork.rs index f58a7be0f5..422c4c1a25 100644 --- a/crates/forge/tests/integration/cheat_fork.rs +++ b/crates/forge/tests/integration/cheat_fork.rs @@ -1,10 +1,9 @@ use indoc::formatdoc; +use shared::test_utils::node_url_with_version; use test_utils::runner::assert_passed; use test_utils::running_tests::run_test_case; use test_utils::test_case; -static CHEATNET_RPC_URL: &str = "http://188.34.188.184:7070/rpc/v0_7"; - #[test] fn prank_cairo0_contract() { let test = test_case!(formatdoc!( @@ -44,7 +43,7 @@ fn prank_cairo0_contract() { assert(unpranked_caller == caller, 'stop_prank does not work'); }} "#, - CHEATNET_RPC_URL, + node_url_with_version().unwrap(), ) .as_str()); @@ -92,7 +91,7 @@ fn roll_cairo0_contract() { assert(unrolled_block_number == block_number, 'stop_roll does not work'); }} "#, - CHEATNET_RPC_URL, + node_url_with_version().unwrap(), ) .as_str()); @@ -142,7 +141,7 @@ fn warp_cairo0_contract() { assert(unwarped_block_timestamp == block_timestamp, 'stop_warp does not work'); }} "#, - CHEATNET_RPC_URL, + node_url_with_version().unwrap(), ) .as_str()); @@ -183,7 +182,7 @@ fn mock_call_cairo0_contract() { assert(eth_dispatcher.name() == 'Ether', 'invalid name after mock'); }} "#, - CHEATNET_RPC_URL, + node_url_with_version().unwrap(), ) .as_str()); @@ -228,7 +227,7 @@ fn store_load_cairo0_contract() { assert(name == array!['NotEther'], 'invalid load2 name'); }} "#, - CHEATNET_RPC_URL, + node_url_with_version().unwrap(), ) .as_str()); diff --git a/crates/forge/tests/integration/setup_fork.rs b/crates/forge/tests/integration/setup_fork.rs index 2a9252e590..a9e0e119c2 100644 --- a/crates/forge/tests/integration/setup_fork.rs +++ b/crates/forge/tests/integration/setup_fork.rs @@ -1,4 +1,4 @@ -use indoc::formatdoc; +use indoc::{formatdoc, indoc}; use std::path::Path; use std::path::PathBuf; use std::process::Command; @@ -17,12 +17,11 @@ use cheatnet::runtime_extensions::forge_runtime_extension::contracts_data::Contr use forge::compiled_raw::RawForkParams; use forge_runner::{RunnerConfig, RunnerParams}; use shared::command::CommandExt; +use shared::test_utils::node_url_with_version; use test_utils::runner::{assert_case_output_contains, assert_failed, assert_passed, Contract}; use test_utils::running_tests::run_test_case; use test_utils::test_case; -const TESTNET_RPC_URL: &str = "http://188.34.188.184:7070/rpc/v0_7"; - #[test] fn fork_simple_decorator() { let test = test_case!(formatdoc!( @@ -58,7 +57,7 @@ fn fork_simple_decorator() { assert(balance == 100, 'Balance should be 100'); }} "#, - TESTNET_RPC_URL + node_url_with_version().unwrap() ).as_str()); let result = run_test_case(&test); @@ -68,7 +67,7 @@ fn fork_simple_decorator() { #[test] fn fork_aliased_decorator() { - let test = test_case!(formatdoc!( + let test = test_case!(indoc!( r#" use result::ResultTrait; use array::ArrayTrait; @@ -79,17 +78,17 @@ fn fork_aliased_decorator() { use starknet::contract_address_const; #[starknet::interface] - trait IHelloStarknet {{ + trait IHelloStarknet { fn increase_balance(ref self: TContractState, amount: felt252); fn get_balance(self: @TContractState) -> felt252; - }} + } #[test] #[fork("FORK_NAME_FROM_SCARB_TOML")] - fn fork_aliased_decorator() {{ - let dispatcher = IHelloStarknetDispatcher {{ + fn fork_aliased_decorator() { + let dispatcher = IHelloStarknetDispatcher { contract_address: contract_address_const::<0x202de98471a4fae6bcbabb96cab00437d381abc58b02509043778074d6781e9>() - }}; + }; let balance = dispatcher.get_balance(); assert(balance == 0, 'Balance should be 0'); @@ -98,9 +97,9 @@ fn fork_aliased_decorator() { let balance = dispatcher.get_balance(); assert(balance == 100, 'Balance should be 100'); - }} + } "# - ).as_str()); + )); let rt = Runtime::new().expect("Could not instantiate Runtime"); @@ -134,7 +133,7 @@ fn fork_aliased_decorator() { &[ForkTarget::new( "FORK_NAME_FROM_SCARB_TOML".to_string(), RawForkParams { - url: TESTNET_RPC_URL.to_string(), + url: node_url_with_version().unwrap().to_string(), block_id_type: "Tag".to_string(), block_id_value: "Latest".to_string(), }, @@ -168,7 +167,7 @@ fn fork_cairo0_contract() { assert(total_supply == 88730316280408105750094, 'Wrong total supply'); }} "#, - TESTNET_RPC_URL + node_url_with_version().unwrap() ).as_str()); let result = run_test_case(&test); @@ -193,7 +192,7 @@ fn get_block_info_in_forked_block() { }} #[test] - #[fork(url: "{TESTNET_RPC_URL}", block_id: BlockId::Number(54060))] + #[fork(url: "{node_url}", block_id: BlockId::Number(54060))] fn test_fork_get_block_info_contract_on_testnet() {{ let dispatcher = IBlockInfoCheckerDispatcher {{ contract_address: contract_address_const::<0x3d80c579ad7d83ff46634abe8f91f9d2080c5c076d4f0f59dd810f9b3f01164>() @@ -210,7 +209,7 @@ fn get_block_info_in_forked_block() { }} #[test] - #[fork(url: "{TESTNET_RPC_URL}", block_id: BlockId::Number(54060))] + #[fork(url: "{node_url}", block_id: BlockId::Number(54060))] fn test_fork_get_block_info_test_state() {{ let block_info = starknet::get_block_info().unbox(); assert(block_info.block_timestamp == 1711645884, block_info.block_timestamp.into()); @@ -220,7 +219,7 @@ fn get_block_info_in_forked_block() { }} #[test] - #[fork(url: "{TESTNET_RPC_URL}", block_id: BlockId::Number(54060))] + #[fork(url: "{node_url}", block_id: BlockId::Number(54060))] fn test_fork_get_block_info_contract_deployed() {{ let contract = declare("BlockInfoChecker").unwrap(); let (contract_address, _) = contract.deploy(@ArrayTrait::new()).unwrap(); @@ -237,13 +236,14 @@ fn get_block_info_in_forked_block() { }} #[test] - #[fork(url: "{TESTNET_RPC_URL}", block_id: BlockId::Tag(BlockTag::Latest))] + #[fork(url: "{node_url}", block_id: BlockId::Tag(BlockTag::Latest))] fn test_fork_get_block_info_latest_block() {{ let block_info = starknet::get_block_info().unbox(); assert(block_info.block_timestamp > 1711645884, block_info.block_timestamp.into()); assert(block_info.block_number > 54060, block_info.block_number.into()); }} - "# + "#, + node_url = node_url_with_version().unwrap() ).as_str(), Contract::from_code_path( "BlockInfoChecker".to_string(), @@ -260,11 +260,12 @@ fn fork_get_block_info_fails() { let test = test_case!(formatdoc!( r#" #[test] - #[fork(url: "{TESTNET_RPC_URL}", block_id: BlockId::Number(999999999999))] + #[fork(url: "{}", block_id: BlockId::Number(999999999999))] fn fork_get_block_info_fails() {{ starknet::get_block_info(); }} - "# + "#, + node_url_with_version().unwrap() ) .as_str()); @@ -295,7 +296,7 @@ fn incompatible_abi() { }} #[test] - #[fork(url: "{TESTNET_RPC_URL}", block_id: BlockId::Tag(BlockTag::Latest))] + #[fork(url: "{}", block_id: BlockId::Tag(BlockTag::Latest))] fn test_forking_functionality() {{ let gov_contract_addr: starknet::ContractAddress = 0x66e4b798c66160bd5fd04056938e5c9f65d67f183dfab9d7d0d2ed9413276fe.try_into().unwrap(); let dispatcher = IResponseWith2FeltsDispatcher {{ contract_address: gov_contract_addr }}; @@ -303,6 +304,7 @@ fn incompatible_abi() { assert(propdetails.payload == 8, 'payload not match'); }} "#, + node_url_with_version().unwrap() ) .as_str()); diff --git a/crates/forge/tests/integration/spy_events.rs b/crates/forge/tests/integration/spy_events.rs index 357c92afe0..6a3e472cab 100644 --- a/crates/forge/tests/integration/spy_events.rs +++ b/crates/forge/tests/integration/spy_events.rs @@ -1,4 +1,5 @@ -use indoc::indoc; +use indoc::{formatdoc, indoc}; +use shared::test_utils::node_url_with_version; use std::path::Path; use test_utils::runner::{assert_case_output_contains, assert_failed, assert_passed, Contract}; use test_utils::running_tests::run_test_case; @@ -621,52 +622,52 @@ fn assert_not_emitted_fails() { #[test] fn capture_cairo0_event() { let test = test_case!( - indoc!( + formatdoc!( r#" use array::ArrayTrait; use result::ResultTrait; - use starknet::{ContractAddress, contract_address_const}; - use snforge_std::{ declare, ContractClassTrait, spy_events, EventSpy, EventFetcher, - event_name_hash, EventAssertions, SpyOn }; + use starknet::{{ContractAddress, contract_address_const}}; + use snforge_std::{{ declare, ContractClassTrait, spy_events, EventSpy, EventFetcher, + event_name_hash, EventAssertions, SpyOn }}; #[starknet::interface] - trait ISpyEventsChecker { + trait ISpyEventsChecker {{ fn emit_one_event(ref self: TContractState, some_data: felt252); fn test_cairo0_event_collection(ref self: TContractState, cairo0_address: felt252); - } + }} #[starknet::contract] - mod SpyEventsChecker { + mod SpyEventsChecker {{ use starknet::ContractAddress; #[storage] - struct Storage {} + struct Storage {{}} #[event] #[derive(Drop, starknet::Event)] - enum Event { + enum Event {{ FirstEvent: FirstEvent, my_event: Cairo0Event, - } + }} #[derive(Drop, starknet::Event)] - struct FirstEvent { + struct FirstEvent {{ some_data: felt252 - } + }} #[derive(Drop, starknet::Event)] - struct Cairo0Event { + struct Cairo0Event {{ some_data: felt252 - } - } + }} + }} #[test] - #[fork(url: "http://188.34.188.184:7070/rpc/v0_7", block_id: BlockId::Tag(BlockTag::Latest))] - fn capture_cairo0_event() { + #[fork(url: "{}", block_id: BlockId::Tag(BlockTag::Latest))] + fn capture_cairo0_event() {{ let cairo0_contract_address = contract_address_const::<0x2c77ca97586968c6651a533bd5f58042c368b14cf5f526d2f42f670012e10ac>(); let contract = declare("SpyEventsChecker").unwrap(); let (contract_address, _) = contract.deploy(@ArrayTrait::new()).unwrap(); - let dispatcher = ISpyEventsCheckerDispatcher { contract_address }; + let dispatcher = ISpyEventsCheckerDispatcher {{ contract_address }}; let mut spy = spy_events(SpyOn::All); @@ -678,31 +679,32 @@ fn capture_cairo0_event() { ( cairo0_contract_address, SpyEventsChecker::Event::my_event( - SpyEventsChecker::Cairo0Event { + SpyEventsChecker::Cairo0Event {{ some_data: 123456789 - } + }} ) ), ( contract_address, SpyEventsChecker::Event::FirstEvent( - SpyEventsChecker::FirstEvent { + SpyEventsChecker::FirstEvent {{ some_data: 420 - } + }} ) ), ( cairo0_contract_address, SpyEventsChecker::Event::my_event( - SpyEventsChecker::Cairo0Event { + SpyEventsChecker::Cairo0Event {{ some_data: 123456789 - } + }} ) ) ]); - } - "# - ), + }} + "#, + node_url_with_version().unwrap() + ).as_str(), Contract::from_code_path( "SpyEventsChecker".to_string(), Path::new("tests/data/contracts/spy_events_checker.cairo"), diff --git a/crates/forge/tests/integration/store_load.rs b/crates/forge/tests/integration/store_load.rs index 86408b3e9b..7a47191307 100644 --- a/crates/forge/tests/integration/store_load.rs +++ b/crates/forge/tests/integration/store_load.rs @@ -1,4 +1,5 @@ use indoc::{formatdoc, indoc}; +use shared::test_utils::node_url_with_version; use std::path::Path; use test_utils::runner::{assert_case_output_contains, assert_failed, assert_passed, Contract}; use test_utils::running_tests::run_test_case; @@ -554,7 +555,6 @@ fn store_load_felt_to_felt() { assert_passed(&result); } -static INTEGRATION_RPC_URL: &str = "http://188.34.188.184:7070/rpc/v0_7"; #[test] fn fork_store_load() { @@ -587,7 +587,7 @@ fn fork_store_load() { assert(balance == 100, 'Balance should be 100'); }} "#, - INTEGRATION_RPC_URL + node_url_with_version().unwrap() ).as_str()); let result = run_test_case(&test);