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

Improve forge configuration acquired from tomls #2497

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
83 changes: 9 additions & 74 deletions crates/forge/src/run_tests/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use forge_runner::package_tests::{
TestTargetWithResolvedConfig,
},
};
use num_bigint::BigInt;
use starknet_api::block::BlockNumber;

pub async fn resolve_config(
Expand Down Expand Up @@ -78,27 +77,6 @@ async fn resolve_fork_config(
Ok(Some(ResolvedForkConfig { url, block_number }))
}

fn parse_block_id(fork_target: &ForkTarget) -> Result<BlockId> {
let block_id = match fork_target.block_id_type.as_str() {
"number" => BlockId::BlockNumber(fork_target.block_id_value.parse()?),
"hash" => {
let block_hash = fork_target.block_id_value.parse::<BigInt>()?;

BlockId::BlockHash(block_hash.into())
}
kkawula marked this conversation as resolved.
Show resolved Hide resolved
"tag" => {
if fork_target.block_id_value == "latest" {
BlockId::BlockTag
} else {
Err(anyhow!(r#"only "latest" block tag is supported"#))?
}
}
_ => Err(anyhow!("block_id must be one of (number | hash | tag)"))?,
};

Ok(block_id)
}

fn get_fork_target_from_runner_config<'a>(
fork_targets: &'a [ForkTarget],
name: &ByteArray,
Expand All @@ -122,18 +100,18 @@ fn replace_id_with_params(
let fork_target_from_runner_config =
get_fork_target_from_runner_config(fork_targets, &name)?;

let block_id = parse_block_id(fork_target_from_runner_config)?;
let block_id = fork_target_from_runner_config.block_id.clone();

Ok(InlineForkConfig {
url: fork_target_from_runner_config.url.parse()?,
url: fork_target_from_runner_config.url.clone(),
block: block_id,
})
}
RawForkConfig::Overridden(OverriddenForkConfig { name, block }) => {
let fork_target_from_runner_config =
get_fork_target_from_runner_config(fork_targets, &name)?;

let url = fork_target_from_runner_config.url.parse()?;
let url = fork_target_from_runner_config.url.clone();

Ok(InlineForkConfig { url, block })
}
Expand Down Expand Up @@ -163,50 +141,6 @@ mod tests {
}
}

#[tokio::test]
async fn to_runnable_unparsable_url() {
let mocked_tests = TestTargetWithConfig {
sierra_program: program_for_testing(),
casm_program: Arc::new(compile_sierra_to_casm(&program_for_testing().program).unwrap()),
test_cases: vec![TestCaseWithConfig {
name: "crate1::do_thing".to_string(),
config: TestCaseConfig {
available_gas: None,
ignored: false,
expected_result: ExpectedTestResult::Success,
fork_config: Some(RawForkConfig::Named("SOME_NAME".into())),
fuzzer_config: None,
},
test_details: TestDetails {
sierra_entry_point_statement_idx: 100,
parameter_types: vec![
(GenericTypeId("RangeCheck".into()), 1),
(GenericTypeId("GasBuiltin".into()), 1),
],
return_types: vec![
(GenericTypeId("RangeCheck".into()), 1),
(GenericTypeId("GasBuiltin".into()), 1),
(GenericTypeId("Enum".into()), 3),
],
},
}],
tests_location: TestTargetLocation::Lib,
};

assert!(resolve_config(
mocked_tests,
&[ForkTarget {
name: "SOME_NAME".to_string(),
url: "unparsable_url".to_string(),
block_id_type: "Tag".to_string(),
block_id_value: "Latest".to_string(),
}],
&mut BlockNumberMap::default()
)
.await
.is_err());
}

#[tokio::test]
async fn to_runnable_non_existent_id() {
let mocked_tests = TestTargetWithConfig {
Expand Down Expand Up @@ -240,11 +174,12 @@ mod tests {
assert!(resolve_config(
mocked_tests,
&[ForkTarget::new(
"definitely_non_existing".to_string(),
"https://not_taken.com".to_string(),
"Number".to_string(),
"120".to_string(),
)],
"definitely_non_existing",
"https://not_taken.com",
"number",
"120",
)
.unwrap()],
&mut BlockNumberMap::default()
)
.await
Expand Down
76 changes: 52 additions & 24 deletions crates/forge/src/scarb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ mod tests {
use assert_fs::fixture::{FileWriteStr, PathChild, PathCopy};
use assert_fs::TempDir;
use camino::Utf8PathBuf;
use cheatnet::runtime_extensions::forge_config_extension::config::BlockId;
use configuration::load_package_config;
use indoc::{formatdoc, indoc};
use scarb_api::metadata::MetadataCommandExt;
Expand Down Expand Up @@ -188,11 +189,16 @@ mod tests {
[[tool.snforge.fork]]
name = "SECOND_FORK_NAME"
url = "http://some.rpc.url"
block_id.hash = "1"
block_id.hash = "0xa"

[[tool.snforge.fork]]
name = "THIRD_FORK_NAME"
url = "http://some.rpc.url"
block_id.hash = "10"

[[tool.snforge.fork]]
name = "FOURTH_FORK_NAME"
url = "http://some.rpc.url"
block_id.tag = "latest"
"#,
package_name,
Expand Down Expand Up @@ -223,24 +229,14 @@ mod tests {
ForgeConfigFromScarb {
exit_first: false,
fork: vec![
ForkTarget::new(
"FIRST_FORK_NAME".to_string(),
"http://some.rpc.url".to_string(),
"number".to_string(),
"1".to_string(),
),
ForkTarget::new(
"SECOND_FORK_NAME".to_string(),
"http://some.rpc.url".to_string(),
"hash".to_string(),
"1".to_string(),
),
ForkTarget::new(
"THIRD_FORK_NAME".to_string(),
"http://some.rpc.url".to_string(),
"tag".to_string(),
"latest".to_string(),
)
ForkTarget::new("FIRST_FORK_NAME", "http://some.rpc.url", "number", "1",)
.unwrap(),
ForkTarget::new("SECOND_FORK_NAME", "http://some.rpc.url", "hash", "10",)
.unwrap(),
ForkTarget::new("THIRD_FORK_NAME", "http://some.rpc.url", "hash", "0xa",)
.unwrap(),
ForkTarget::new("FOURTH_FORK_NAME", "http://some.rpc.url", "tag", "latest",)
.unwrap()
],
fuzzer_runs: None,
fuzzer_seed: None,
Expand Down Expand Up @@ -431,6 +427,37 @@ mod tests {
assert!(format!("{err:?}").contains("block_id.tag can only be equal to latest"));
}

#[test]
fn get_forge_config_for_package_with_block_tag() {
let temp = setup_package("simple_package");
let content = indoc!(
r#"
[package]
name = "simple_package"
version = "0.1.0"

[[tool.snforge.fork]]
name = "SAME_NAME"
url = "http://some.rpc.url"
block_id.tag = "latest"
"#
);
temp.child("Scarb.toml").write_str(content).unwrap();

let scarb_metadata = ScarbCommand::metadata()
.inherit_stderr()
.current_dir(temp.path())
.run()
.unwrap();

let forge_config = load_package_config::<ForgeConfigFromScarb>(
&scarb_metadata,
&scarb_metadata.workspace.members[0],
)
.unwrap();
assert_eq!(forge_config.fork[0].block_id, BlockId::BlockTag);
}

#[test]
fn get_forge_config_resolves_env_variables() {
let temp = setup_package("simple_package");
Expand Down Expand Up @@ -466,11 +493,12 @@ mod tests {
ForgeConfigFromScarb {
exit_first: false,
fork: vec![ForkTarget::new(
"ENV_URL_FORK".to_string(),
"http://some.rpc.url_from_env".to_string(),
"number".to_string(),
"1".to_string(),
)],
"ENV_URL_FORK",
"http://some.rpc.url_from_env",
"number",
"1",
)
.unwrap()],
fuzzer_runs: None,
fuzzer_seed: None,
max_n_steps: None,
Expand Down
Loading
Loading