From 2b30e7eeba7366629e0a676f91cbaec75073db86 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:19:11 +0200 Subject: [PATCH 01/26] Add mixed variant for 'fork' configuration --- .../forge_config_extension/config.rs | 7 ++++ crates/forge/src/run_tests/resolve_config.rs | 33 +++++++++++++----- .../src/attributes/fork.rs | 34 ++++++++++++++++++- crates/snforge-scarb-plugin/src/utils.rs | 34 ++++++++++++------- snforge_std/src/_config_types.cairo | 6 ++++ 5 files changed, 92 insertions(+), 22 deletions(-) diff --git a/crates/cheatnet/src/runtime_extensions/forge_config_extension/config.rs b/crates/cheatnet/src/runtime_extensions/forge_config_extension/config.rs index 0308592777..c12065817a 100644 --- a/crates/cheatnet/src/runtime_extensions/forge_config_extension/config.rs +++ b/crates/cheatnet/src/runtime_extensions/forge_config_extension/config.rs @@ -25,10 +25,17 @@ pub struct InlineForkConfig { pub block: BlockId, } +#[derive(Debug, Clone, CairoDeserialize, PartialEq)] +pub struct MixedForkConfig { + pub name: ByteArray, + pub block: BlockId, +} + #[derive(Debug, Clone, CairoDeserialize, PartialEq)] pub enum RawForkConfig { Inline(InlineForkConfig), Named(ByteArray), + Mixed(MixedForkConfig), } // fuzzer diff --git a/crates/forge/src/run_tests/resolve_config.rs b/crates/forge/src/run_tests/resolve_config.rs index cf900d276d..2755c694fb 100644 --- a/crates/forge/src/run_tests/resolve_config.rs +++ b/crates/forge/src/run_tests/resolve_config.rs @@ -3,6 +3,7 @@ use anyhow::{anyhow, Result}; use cheatnet::runtime_extensions::forge_config_extension::config::{ BlockId, InlineForkConfig, RawForkConfig, }; +use conversions::byte_array::ByteArray; use forge_runner::package_tests::{ with_config::TestTargetWithConfig, with_config_resolved::{ @@ -98,6 +99,19 @@ fn parse_block_id(fork_target: &ForkTarget) -> Result { Ok(block_id) } +fn get_fork_target_from_runner_config<'a>( + fork_targets: &'a [ForkTarget], + name: &ByteArray, +) -> Result<&'a ForkTarget> { + fork_targets + .iter() + .find(|fork| fork.name == String::from(name.clone())) + .ok_or_else(|| { + let name = String::from(name.clone()); + anyhow!("Fork configuration named = {name} not found in the Scarb.toml") + }) +} + fn replace_id_with_params( raw_fork_config: RawForkConfig, fork_targets: &[ForkTarget], @@ -105,14 +119,8 @@ fn replace_id_with_params( match raw_fork_config { RawForkConfig::Inline(raw_fork_params) => Ok(raw_fork_params), RawForkConfig::Named(name) => { - let fork_target_from_runner_config = fork_targets - .iter() - .find(|fork| fork.name == String::from(name.clone())) - .ok_or_else(|| { - let name = String::from(name); - - anyhow!("Fork configuration named = {name} not found in the Scarb.toml") - })?; + 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)?; @@ -121,6 +129,15 @@ fn replace_id_with_params( block: block_id, }) } + RawForkConfig::Mixed(mixed) => { + let fork_target_from_runner_config = + get_fork_target_from_runner_config(fork_targets, &mixed.name)?; + + Ok(InlineForkConfig { + url: fork_target_from_runner_config.url.parse()?, + block: mixed.block, + }) + } } } diff --git a/crates/snforge-scarb-plugin/src/attributes/fork.rs b/crates/snforge-scarb-plugin/src/attributes/fork.rs index e6a8d5aa61..24366c4294 100644 --- a/crates/snforge-scarb-plugin/src/attributes/fork.rs +++ b/crates/snforge-scarb-plugin/src/attributes/fork.rs @@ -30,7 +30,11 @@ impl AttributeCollector for ForkCollector { args: Arguments, _warns: &mut Vec, ) -> Result { - let expr = branch(inline_args(db, &args), || from_file_args(db, &args))?; + let expr = branch( + inline_args(db, &args), + || mixed_args(db, &args), + || from_file_args(db, &args), + )?; Ok(expr) } @@ -78,6 +82,34 @@ fn from_file_args(db: &dyn SyntaxGroup, args: &Arguments) -> Result Result { + let named_args = args.named_only::()?; + let &[arg] = args + .unnamed_only::()? + .of_length::<1, ForkCollector>()?; + + let block_id = named_args.one_of_once(&[ + BlockIdVariants::Hash, + BlockIdVariants::Number, + BlockIdVariants::Tag, + ])?; + + let block_id = BlockId::parse_from_expr::(db, &block_id, block_id.0.as_ref())?; + let name = String::parse_from_expr::(db, arg, "0")?; + + let block_id = block_id.as_cairo_expression(); + let name = name.as_cairo_expression(); + + Ok(formatdoc!( + " + snforge_std::_config_types::ForkConfig::Mixed( + block: {block_id}, + name: {name} + ) + " + )) +} + #[must_use] pub fn fork(args: TokenStream, item: TokenStream) -> ProcMacroResult { extend_with_config_cheatcodes::(args, item) diff --git a/crates/snforge-scarb-plugin/src/utils.rs b/crates/snforge-scarb-plugin/src/utils.rs index 65ca117279..1b167201b9 100644 --- a/crates/snforge-scarb-plugin/src/utils.rs +++ b/crates/snforge-scarb-plugin/src/utils.rs @@ -10,21 +10,29 @@ fn higher_severity(a: Severity, b: Severity) -> Severity { pub fn branch( left: Result, + middle: impl Fn() -> Result, right: impl Fn() -> Result, ) -> Result { - left.or_else(|error| { - right().map_err(|next_error| Diagnostic { - severity: higher_severity(error.severity, next_error.severity), - message: formatdoc!( - " - Both options failed - First variant: {} - Second variant: {} - Resolve at least one of them - ", - error.message, - next_error.message - ), + left.or_else(|left_error| { + middle().or_else(|middle_error| { + right().map_err(|right_error| Diagnostic { + severity: higher_severity( + higher_severity(left_error.severity, middle_error.severity), + right_error.severity, + ), + message: formatdoc!( + " + All options failed + First variant: {} + Second variant: {} + Third variant: {} + Resolve at least one of them + ", + left_error.message, + middle_error.message, + right_error.message + ), + }) }) }) } diff --git a/snforge_std/src/_config_types.cairo b/snforge_std/src/_config_types.cairo index 94f4b8b7db..843b579f8d 100644 --- a/snforge_std/src/_config_types.cairo +++ b/snforge_std/src/_config_types.cairo @@ -16,10 +16,16 @@ struct InlineForkConfig { block: BlockId } +struct MixedForkConfig { + name: ByteArray, + block: BlockId +} + #[derive(Drop, Serde)] enum ForkConfig { Inline: InlineForkConfig, Named: ByteArray + Mixed: MixedForkConfig } #[derive(Drop, Serde)] From 65d538a68c139cb596e07334175b54200684c261 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:52:23 +0200 Subject: [PATCH 02/26] Lint --- snforge_std/src/_config_types.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snforge_std/src/_config_types.cairo b/snforge_std/src/_config_types.cairo index 843b579f8d..0f4668afb1 100644 --- a/snforge_std/src/_config_types.cairo +++ b/snforge_std/src/_config_types.cairo @@ -24,7 +24,7 @@ struct MixedForkConfig { #[derive(Drop, Serde)] enum ForkConfig { Inline: InlineForkConfig, - Named: ByteArray + Named: ByteArray, Mixed: MixedForkConfig } From e72575ffd1a2b2eef92894cabb1b7266ea9aa107 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:02:10 +0200 Subject: [PATCH 03/26] Lint --- snforge_std/src/_config_types.cairo | 1 + 1 file changed, 1 insertion(+) diff --git a/snforge_std/src/_config_types.cairo b/snforge_std/src/_config_types.cairo index 0f4668afb1..24f1c2f1ed 100644 --- a/snforge_std/src/_config_types.cairo +++ b/snforge_std/src/_config_types.cairo @@ -16,6 +16,7 @@ struct InlineForkConfig { block: BlockId } +#[derive(Drop, Serde)] struct MixedForkConfig { name: ByteArray, block: BlockId From 211ed294c39d020678cee440dbbb05c45d4d51ac Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:25:30 +0200 Subject: [PATCH 04/26] Add mixed args assertion --- crates/snforge-scarb-plugin/src/args.rs | 9 +++++++++ crates/snforge-scarb-plugin/src/attributes/fork.rs | 9 ++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/snforge-scarb-plugin/src/args.rs b/crates/snforge-scarb-plugin/src/args.rs index 924bc83756..642970ba15 100644 --- a/crates/snforge-scarb-plugin/src/args.rs +++ b/crates/snforge-scarb-plugin/src/args.rs @@ -75,6 +75,15 @@ impl Arguments { } } + #[inline] + pub fn assert_mixed(&self) -> Result<(), Diagnostic> { + if self.shorthand.is_empty() && !self.named.is_empty() && !self.unnamed.is_empty() { + Ok(()) + } else { + Err(T::error("can be used with mixed attributes only")) + } + } + #[inline] pub fn unnamed_only(&self) -> Result { if self.shorthand.is_empty() && self.named.is_empty() { diff --git a/crates/snforge-scarb-plugin/src/attributes/fork.rs b/crates/snforge-scarb-plugin/src/attributes/fork.rs index 24366c4294..51840da68c 100644 --- a/crates/snforge-scarb-plugin/src/attributes/fork.rs +++ b/crates/snforge-scarb-plugin/src/attributes/fork.rs @@ -1,4 +1,5 @@ use self::block_id::{BlockId, BlockIdVariants}; +use crate::args::unnamed::UnnamedArgs; use crate::{ args::Arguments, attributes::{AttributeCollector, AttributeInfo, AttributeTypeData}, @@ -83,12 +84,10 @@ fn from_file_args(db: &dyn SyntaxGroup, args: &Arguments) -> Result Result { - let named_args = args.named_only::()?; - let &[arg] = args - .unnamed_only::()? - .of_length::<1, ForkCollector>()?; + args.assert_mixed()?; + let arg = UnnamedArgs::new(&args.unnamed).of_length::<1, ForkCollector>()?[0]; - let block_id = named_args.one_of_once(&[ + let block_id = args.named.one_of_once(&[ BlockIdVariants::Hash, BlockIdVariants::Number, BlockIdVariants::Tag, From 5c2b75a72bc5f2ebb0862d2584265327a73271aa Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:27:44 +0200 Subject: [PATCH 05/26] Lint --- crates/snforge-scarb-plugin/src/attributes/fork.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/snforge-scarb-plugin/src/attributes/fork.rs b/crates/snforge-scarb-plugin/src/attributes/fork.rs index 51840da68c..95728fedbd 100644 --- a/crates/snforge-scarb-plugin/src/attributes/fork.rs +++ b/crates/snforge-scarb-plugin/src/attributes/fork.rs @@ -84,7 +84,7 @@ fn from_file_args(db: &dyn SyntaxGroup, args: &Arguments) -> Result Result { - args.assert_mixed()?; + args.assert_mixed::()?; let arg = UnnamedArgs::new(&args.unnamed).of_length::<1, ForkCollector>()?[0]; let block_id = args.named.one_of_once(&[ From 31ecfd498590b0bd05e03f2e3060e493af7a17db Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Tue, 17 Sep 2024 17:07:01 +0200 Subject: [PATCH 06/26] Adjust test --- .../integration/single_attributes/fork.rs | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs index 9aacc2ab67..0172a85816 100644 --- a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs +++ b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs @@ -15,9 +15,10 @@ fn fails_without_block() { &[ Diagnostic::error(formatdoc!( " - Both options failed + All options failed First variant: exactly one of | | should be specified, got 0 - Second variant: #[fork] can be used with unnamed attributes only + Second variant: #[fork] can be used with mixed attributes only + Third variant: #[fork] can be used with unnamed attributes only Resolve at least one of them " )) @@ -36,9 +37,10 @@ fn fails_without_url() { &result, &[Diagnostic::error(formatdoc!( " - Both options failed + All options failed First variant: argument is missing - Second variant: #[fork] can be used with unnamed attributes only + Second variant: #[fork] can be used with mixed attributes only + Third variant: #[fork] can be used with unnamed attributes only Resolve at least one of them " ))], @@ -54,15 +56,15 @@ fn fails_without_args() { assert_diagnostics( &result, - &[ - Diagnostic::error(formatdoc!( + &[Diagnostic::error(formatdoc!( " - Both options failed + All options failed First variant: exactly one of | | should be specified, got 0 - Second variant: #[fork] expected 1 arguments, got: 0 + Second variant: #[fork] can be used with mixed attributes only + Third variant: #[fork] expected 1 arguments, got: 0 Resolve at least one of them - ")) - ], + " + ))], ); } @@ -77,9 +79,10 @@ fn fails_with_invalid_url() { &result, &[Diagnostic::error(formatdoc!( " - Both options failed + All options failed First variant: #[fork] is not a valid url - Second variant: #[fork] can be used with unnamed attributes only + Second variant: #[fork] can be used with mixed attributes only + Third variant: #[fork] can be used with unnamed attributes only Resolve at least one of them " ))], From ae95ac931537364801ed9747b4cb53d84dfb40b7 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Wed, 18 Sep 2024 12:26:13 +0200 Subject: [PATCH 07/26] Add test for new feature --- .../integration/single_attributes/fork.rs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs index 0172a85816..9b664cbc43 100644 --- a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs +++ b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs @@ -152,6 +152,38 @@ fn accepts_inline_config() { ); } +#[test] +fn overriding_config() { + let item = TokenStream::new(EMPTY_FN.into()); + let args = TokenStream::new(r#"("MAINNET", block_number: 23)"#.into()); + + let result = fork(args, item); + + assert_diagnostics(&result, &[]); + + assert_output( + &result, + r#" + fn empty_fn() { + if snforge_std::_cheatcode::_is_config_run() { + + let mut data = array![]; + + snforge_std::_config_types::ForkConfig::Mixed( + block: snforge_std::_config_types::BlockId::BlockNumber(0x17), + name: "MAINNET" + ) + .serialize(ref data); + + starknet::testing::cheatcode::<'set_config_fork'>(data.span()); + + return; + } + } + "#, + ); +} + #[test] fn is_used_once() { let item = TokenStream::new(formatdoc!( From b9eae034fce0575369c77d3c7b2053f5a9aebc4b Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Wed, 18 Sep 2024 15:27:09 +0200 Subject: [PATCH 08/26] Rename 'mixed' -> 'overridden' --- .../forge_config_extension/config.rs | 4 ++-- crates/forge/src/run_tests/resolve_config.rs | 6 +++--- crates/snforge-scarb-plugin/src/args.rs | 9 --------- crates/snforge-scarb-plugin/src/attributes/fork.rs | 7 +++---- .../tests/integration/single_attributes/fork.rs | 10 +++++----- snforge_std/src/_config_types.cairo | 4 ++-- 6 files changed, 15 insertions(+), 25 deletions(-) diff --git a/crates/cheatnet/src/runtime_extensions/forge_config_extension/config.rs b/crates/cheatnet/src/runtime_extensions/forge_config_extension/config.rs index c12065817a..f6a1603323 100644 --- a/crates/cheatnet/src/runtime_extensions/forge_config_extension/config.rs +++ b/crates/cheatnet/src/runtime_extensions/forge_config_extension/config.rs @@ -26,7 +26,7 @@ pub struct InlineForkConfig { } #[derive(Debug, Clone, CairoDeserialize, PartialEq)] -pub struct MixedForkConfig { +pub struct OverriddenForkConfig { pub name: ByteArray, pub block: BlockId, } @@ -35,7 +35,7 @@ pub struct MixedForkConfig { pub enum RawForkConfig { Inline(InlineForkConfig), Named(ByteArray), - Mixed(MixedForkConfig), + Overridden(OverriddenForkConfig), } // fuzzer diff --git a/crates/forge/src/run_tests/resolve_config.rs b/crates/forge/src/run_tests/resolve_config.rs index 2755c694fb..07974575d1 100644 --- a/crates/forge/src/run_tests/resolve_config.rs +++ b/crates/forge/src/run_tests/resolve_config.rs @@ -129,13 +129,13 @@ fn replace_id_with_params( block: block_id, }) } - RawForkConfig::Mixed(mixed) => { + RawForkConfig::Overridden(overridden) => { let fork_target_from_runner_config = - get_fork_target_from_runner_config(fork_targets, &mixed.name)?; + get_fork_target_from_runner_config(fork_targets, &overridden.name)?; Ok(InlineForkConfig { url: fork_target_from_runner_config.url.parse()?, - block: mixed.block, + block: overridden.block, }) } } diff --git a/crates/snforge-scarb-plugin/src/args.rs b/crates/snforge-scarb-plugin/src/args.rs index 642970ba15..924bc83756 100644 --- a/crates/snforge-scarb-plugin/src/args.rs +++ b/crates/snforge-scarb-plugin/src/args.rs @@ -75,15 +75,6 @@ impl Arguments { } } - #[inline] - pub fn assert_mixed(&self) -> Result<(), Diagnostic> { - if self.shorthand.is_empty() && !self.named.is_empty() && !self.unnamed.is_empty() { - Ok(()) - } else { - Err(T::error("can be used with mixed attributes only")) - } - } - #[inline] pub fn unnamed_only(&self) -> Result { if self.shorthand.is_empty() && self.named.is_empty() { diff --git a/crates/snforge-scarb-plugin/src/attributes/fork.rs b/crates/snforge-scarb-plugin/src/attributes/fork.rs index 95728fedbd..9b4f7f3788 100644 --- a/crates/snforge-scarb-plugin/src/attributes/fork.rs +++ b/crates/snforge-scarb-plugin/src/attributes/fork.rs @@ -33,7 +33,7 @@ impl AttributeCollector for ForkCollector { ) -> Result { let expr = branch( inline_args(db, &args), - || mixed_args(db, &args), + || overridden_args(db, &args), || from_file_args(db, &args), )?; @@ -83,8 +83,7 @@ fn from_file_args(db: &dyn SyntaxGroup, args: &Arguments) -> Result Result { - args.assert_mixed::()?; +fn overridden_args(db: &dyn SyntaxGroup, args: &Arguments) -> Result { let arg = UnnamedArgs::new(&args.unnamed).of_length::<1, ForkCollector>()?[0]; let block_id = args.named.one_of_once(&[ @@ -101,7 +100,7 @@ fn mixed_args(db: &dyn SyntaxGroup, args: &Arguments) -> Result | | should be specified, got 0 - Second variant: #[fork] can be used with mixed attributes only + Second variant: #[fork] expected 1 arguments, got: 0 Third variant: #[fork] can be used with unnamed attributes only Resolve at least one of them " @@ -39,7 +39,7 @@ fn fails_without_url() { " All options failed First variant: argument is missing - Second variant: #[fork] can be used with mixed attributes only + Second variant: #[fork] expected 1 arguments, got: 0 Third variant: #[fork] can be used with unnamed attributes only Resolve at least one of them " @@ -60,7 +60,7 @@ fn fails_without_args() { " All options failed First variant: exactly one of | | should be specified, got 0 - Second variant: #[fork] can be used with mixed attributes only + Second variant: #[fork] expected 1 arguments, got: 0 Third variant: #[fork] expected 1 arguments, got: 0 Resolve at least one of them " @@ -81,7 +81,7 @@ fn fails_with_invalid_url() { " All options failed First variant: #[fork] is not a valid url - Second variant: #[fork] can be used with mixed attributes only + Second variant: #[fork] expected 1 arguments, got: 0 Third variant: #[fork] can be used with unnamed attributes only Resolve at least one of them " @@ -169,7 +169,7 @@ fn overriding_config() { let mut data = array![]; - snforge_std::_config_types::ForkConfig::Mixed( + snforge_std::_config_types::ForkConfig::Overridden( block: snforge_std::_config_types::BlockId::BlockNumber(0x17), name: "MAINNET" ) diff --git a/snforge_std/src/_config_types.cairo b/snforge_std/src/_config_types.cairo index 24f1c2f1ed..31ba8a3d47 100644 --- a/snforge_std/src/_config_types.cairo +++ b/snforge_std/src/_config_types.cairo @@ -17,7 +17,7 @@ struct InlineForkConfig { } #[derive(Drop, Serde)] -struct MixedForkConfig { +struct OverriddenForkConfig { name: ByteArray, block: BlockId } @@ -26,7 +26,7 @@ struct MixedForkConfig { enum ForkConfig { Inline: InlineForkConfig, Named: ByteArray, - Mixed: MixedForkConfig + Overridden: OverriddenForkConfig } #[derive(Drop, Serde)] From d645eae83f411fd3ceb0efd917684aef8486b93c Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Wed, 18 Sep 2024 16:11:40 +0200 Subject: [PATCH 09/26] Add argument api --- crates/snforge-scarb-plugin/src/args.rs | 5 +++++ crates/snforge-scarb-plugin/src/attributes/fork.rs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/snforge-scarb-plugin/src/args.rs b/crates/snforge-scarb-plugin/src/args.rs index 924bc83756..2f4c2af7db 100644 --- a/crates/snforge-scarb-plugin/src/args.rs +++ b/crates/snforge-scarb-plugin/src/args.rs @@ -84,6 +84,11 @@ impl Arguments { } } + #[inline] + pub fn unnamed(&self) -> UnnamedArgs { + UnnamedArgs::new(&self.unnamed) + } + #[inline] pub fn assert_is_empty(&self) -> Result<(), Diagnostic> { if self.is_empty() { diff --git a/crates/snforge-scarb-plugin/src/attributes/fork.rs b/crates/snforge-scarb-plugin/src/attributes/fork.rs index 9b4f7f3788..054eaf3b28 100644 --- a/crates/snforge-scarb-plugin/src/attributes/fork.rs +++ b/crates/snforge-scarb-plugin/src/attributes/fork.rs @@ -84,7 +84,7 @@ fn from_file_args(db: &dyn SyntaxGroup, args: &Arguments) -> Result Result { - let arg = UnnamedArgs::new(&args.unnamed).of_length::<1, ForkCollector>()?[0]; + let &[arg] = args.unnamed().of_length::<1, ForkCollector>()?; let block_id = args.named.one_of_once(&[ BlockIdVariants::Hash, From ce464c139c11f4a641652dcca4d5cbf110743abb Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Wed, 18 Sep 2024 16:13:55 +0200 Subject: [PATCH 10/26] Lint --- crates/snforge-scarb-plugin/src/attributes/fork.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/snforge-scarb-plugin/src/attributes/fork.rs b/crates/snforge-scarb-plugin/src/attributes/fork.rs index 054eaf3b28..2b6dc46475 100644 --- a/crates/snforge-scarb-plugin/src/attributes/fork.rs +++ b/crates/snforge-scarb-plugin/src/attributes/fork.rs @@ -1,5 +1,4 @@ use self::block_id::{BlockId, BlockIdVariants}; -use crate::args::unnamed::UnnamedArgs; use crate::{ args::Arguments, attributes::{AttributeCollector, AttributeInfo, AttributeTypeData}, From 1f145bf6b80daaf3ce7f09cd926779e531e1a488 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Thu, 19 Sep 2024 10:12:34 +0200 Subject: [PATCH 11/26] Add pos for unnamed args --- .../snforge-scarb-plugin/src/args/unnamed.rs | 8 ++--- .../src/attributes/available_gas.rs | 2 +- .../src/attributes/fork.rs | 4 +-- .../integration/single_attributes/fork.rs | 34 ++++++++++++++++++- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/crates/snforge-scarb-plugin/src/args/unnamed.rs b/crates/snforge-scarb-plugin/src/args/unnamed.rs index 687e118035..26dbdc2f44 100644 --- a/crates/snforge-scarb-plugin/src/args/unnamed.rs +++ b/crates/snforge-scarb-plugin/src/args/unnamed.rs @@ -3,10 +3,10 @@ use cairo_lang_macro::Diagnostic; use cairo_lang_syntax::node::ast::Expr; use std::{collections::HashMap, ops::Deref}; -pub struct UnnamedArgs<'a>(Vec<&'a Expr>); +pub struct UnnamedArgs<'a>(Vec<(usize, &'a Expr)>); impl<'a> Deref for UnnamedArgs<'a> { - type Target = Vec<&'a Expr>; + type Target = Vec<(usize, &'a Expr)>; fn deref(&self) -> &Self::Target { &self.0 @@ -19,7 +19,7 @@ impl UnnamedArgs<'_> { args.sort_by(|(a, _), (b, _)| a.cmp(b)); - let args = args.into_iter().map(|(_, expr)| expr).collect(); + let args = args.into_iter().map(|(&pos, expr)| (pos, expr)).collect(); UnnamedArgs(args) } @@ -28,7 +28,7 @@ impl UnnamedArgs<'_> { impl<'a> UnnamedArgs<'a> { pub fn of_length( &self, - ) -> Result<&[&'a Expr; N], Diagnostic> { + ) -> Result<&[(usize, &'a Expr); N], Diagnostic> { self.as_slice() .try_into() .map_err(|_| T::error(format!("expected {} arguments, got: {}", N, self.len()))) diff --git a/crates/snforge-scarb-plugin/src/attributes/available_gas.rs b/crates/snforge-scarb-plugin/src/attributes/available_gas.rs index 38c7fc06e8..01413f01e2 100644 --- a/crates/snforge-scarb-plugin/src/attributes/available_gas.rs +++ b/crates/snforge-scarb-plugin/src/attributes/available_gas.rs @@ -26,7 +26,7 @@ impl AttributeCollector for AvailableGasCollector { ) -> Result { let &[arg] = args.unnamed_only::()?.of_length::<1, Self>()?; - let gas = Number::parse_from_expr::(db, arg, "0")?; + let gas = Number::parse_from_expr::(db, arg.1, arg.0.to_string().as_str())?; let gas = gas.as_cairo_expression(); diff --git a/crates/snforge-scarb-plugin/src/attributes/fork.rs b/crates/snforge-scarb-plugin/src/attributes/fork.rs index 2b6dc46475..b9961afd73 100644 --- a/crates/snforge-scarb-plugin/src/attributes/fork.rs +++ b/crates/snforge-scarb-plugin/src/attributes/fork.rs @@ -73,7 +73,7 @@ fn from_file_args(db: &dyn SyntaxGroup, args: &Arguments) -> Result()? .of_length::<1, ForkCollector>()?; - let name = String::parse_from_expr::(db, arg, "0")?; + let name = String::parse_from_expr::(db, arg.1, arg.0.to_string().as_str())?; let name = name.as_cairo_expression(); @@ -92,7 +92,7 @@ fn overridden_args(db: &dyn SyntaxGroup, args: &Arguments) -> Result(db, &block_id, block_id.0.as_ref())?; - let name = String::parse_from_expr::(db, arg, "0")?; + let name = String::parse_from_expr::(db, arg.1, arg.0.to_string().as_str())?; let block_id = block_id.as_cairo_expression(); let name = name.as_cairo_expression(); diff --git a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs index 38a1a3ba9b..08725f236c 100644 --- a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs +++ b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs @@ -153,7 +153,7 @@ fn accepts_inline_config() { } #[test] -fn overriding_config() { +fn overriding_config_1() { let item = TokenStream::new(EMPTY_FN.into()); let args = TokenStream::new(r#"("MAINNET", block_number: 23)"#.into()); @@ -184,6 +184,38 @@ fn overriding_config() { ); } +#[test] +fn overriding_config_2() { + let item = TokenStream::new(EMPTY_FN.into()); + let args = TokenStream::new(r#"(block_number: 23, "MAINNET")"#.into()); + + let result = fork(args, item); + + assert_diagnostics(&result, &[]); + + assert_output( + &result, + r#" + fn empty_fn() { + if snforge_std::_cheatcode::_is_config_run() { + + let mut data = array![]; + + snforge_std::_config_types::ForkConfig::Overridden( + block: snforge_std::_config_types::BlockId::BlockNumber(0x17), + name: "MAINNET" + ) + .serialize(ref data); + + starknet::testing::cheatcode::<'set_config_fork'>(data.span()); + + return; + } + } + "#, + ); +} + #[test] fn is_used_once() { let item = TokenStream::new(formatdoc!( From aa846866d982ff38eaa19d7e1dda4082079591e1 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Thu, 19 Sep 2024 10:54:09 +0200 Subject: [PATCH 12/26] Update branch func --- .../src/attributes/fork.rs | 8 ++--- crates/snforge-scarb-plugin/src/utils.rs | 36 +++++++++++-------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/crates/snforge-scarb-plugin/src/attributes/fork.rs b/crates/snforge-scarb-plugin/src/attributes/fork.rs index b9961afd73..ef15cef539 100644 --- a/crates/snforge-scarb-plugin/src/attributes/fork.rs +++ b/crates/snforge-scarb-plugin/src/attributes/fork.rs @@ -30,11 +30,9 @@ impl AttributeCollector for ForkCollector { args: Arguments, _warns: &mut Vec, ) -> Result { - let expr = branch( - inline_args(db, &args), - || overridden_args(db, &args), - || from_file_args(db, &args), - )?; + let expr = branch(inline_args(db, &args), || { + branch(overridden_args(db, &args), || from_file_args(db, &args)) + })?; Ok(expr) } diff --git a/crates/snforge-scarb-plugin/src/utils.rs b/crates/snforge-scarb-plugin/src/utils.rs index 1b167201b9..891d4b2631 100644 --- a/crates/snforge-scarb-plugin/src/utils.rs +++ b/crates/snforge-scarb-plugin/src/utils.rs @@ -10,29 +10,35 @@ fn higher_severity(a: Severity, b: Severity) -> Severity { pub fn branch( left: Result, - middle: impl Fn() -> Result, right: impl Fn() -> Result, ) -> Result { - left.or_else(|left_error| { - middle().or_else(|middle_error| { - right().map_err(|right_error| Diagnostic { - severity: higher_severity( - higher_severity(left_error.severity, middle_error.severity), - right_error.severity, - ), + left.or_else(|error| { + right().map_err(|next_error| { + let next_message = if next_error.message.contains("All options failed") { + let mut lines: Vec<&str> = next_error.message.lines().collect(); + lines = lines[1..lines.len() - 1].to_vec(); + let mut next_message = lines.join("\n"); + if let Some(pos) = next_message.find("- variant: ") { + next_message.replace_range(pos..pos + 11, ""); + } + next_message + } else { + next_error.message.clone() + }; + + Diagnostic { + severity: higher_severity(error.severity, next_error.severity), message: formatdoc!( " All options failed - First variant: {} - Second variant: {} - Third variant: {} + - variant: {} + - variant: {} Resolve at least one of them ", - left_error.message, - middle_error.message, - right_error.message + error.message, + next_message ), - }) + } }) }) } From 1ab55aeefe0b8f9482b8ebb2db0773691f05aafe Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Thu, 19 Sep 2024 10:58:48 +0200 Subject: [PATCH 13/26] Update test --- .../integration/single_attributes/fork.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs index 08725f236c..3e61593bd2 100644 --- a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs +++ b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs @@ -38,9 +38,9 @@ fn fails_without_url() { &[Diagnostic::error(formatdoc!( " All options failed - First variant: argument is missing - Second variant: #[fork] expected 1 arguments, got: 0 - Third variant: #[fork] can be used with unnamed attributes only + - variant: argument is missing + - variant: #[fork] expected 1 arguments, got: 0 + - variant: #[fork] can be used with unnamed attributes only Resolve at least one of them " ))], @@ -59,9 +59,9 @@ fn fails_without_args() { &[Diagnostic::error(formatdoc!( " All options failed - First variant: exactly one of | | should be specified, got 0 - Second variant: #[fork] expected 1 arguments, got: 0 - Third variant: #[fork] expected 1 arguments, got: 0 + - variant: argument is missing + - variant: #[fork] expected 1 arguments, got: 0 + - variant: #[fork] can be used with unnamed attributes only Resolve at least one of them " ))], @@ -80,9 +80,9 @@ fn fails_with_invalid_url() { &[Diagnostic::error(formatdoc!( " All options failed - First variant: #[fork] is not a valid url - Second variant: #[fork] expected 1 arguments, got: 0 - Third variant: #[fork] can be used with unnamed attributes only + - variant: exactly one of | | should be specified, got 0 + - variant: #[fork] expected 1 arguments, got: 0 + - variant: #[fork] expected 1 arguments, got: 0 Resolve at least one of them " ))], From cfe6cd5c4b5e54d68484638065fd1fb15cca9a1e Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:02:05 +0200 Subject: [PATCH 14/26] Update test --- .../tests/integration/single_attributes/fork.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs index 3e61593bd2..4f4fcc9b43 100644 --- a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs +++ b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs @@ -16,9 +16,9 @@ fn fails_without_block() { Diagnostic::error(formatdoc!( " All options failed - First variant: exactly one of | | should be specified, got 0 - Second variant: #[fork] expected 1 arguments, got: 0 - Third variant: #[fork] can be used with unnamed attributes only + - variant: exactly one of | | should be specified, got 0 + - variant: #[fork] expected 1 arguments, got: 0 + - variant: #[fork] can be used with unnamed attributes only Resolve at least one of them " )) @@ -59,9 +59,9 @@ fn fails_without_args() { &[Diagnostic::error(formatdoc!( " All options failed - - variant: argument is missing + - variant: exactly one of | | should be specified, got 0 + - variant: #[fork] expected 1 arguments, got: 0 - variant: #[fork] expected 1 arguments, got: 0 - - variant: #[fork] can be used with unnamed attributes only Resolve at least one of them " ))], @@ -80,9 +80,9 @@ fn fails_with_invalid_url() { &[Diagnostic::error(formatdoc!( " All options failed - - variant: exactly one of | | should be specified, got 0 - - variant: #[fork] expected 1 arguments, got: 0 + - variant: #[fork] is not a valid url - variant: #[fork] expected 1 arguments, got: 0 + - variant: #[fork] can be used with unnamed attributes only Resolve at least one of them " ))], From 0afafe8a2075deab9b1810c35bc74619c9a25e69 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Thu, 19 Sep 2024 12:44:06 +0200 Subject: [PATCH 15/26] Add 'branch' macro --- .../src/attributes/fork.rs | 12 ++-- crates/snforge-scarb-plugin/src/utils.rs | 70 ++++++++++--------- .../integration/single_attributes/fork.rs | 2 +- 3 files changed, 45 insertions(+), 39 deletions(-) diff --git a/crates/snforge-scarb-plugin/src/attributes/fork.rs b/crates/snforge-scarb-plugin/src/attributes/fork.rs index ef15cef539..064e7f3239 100644 --- a/crates/snforge-scarb-plugin/src/attributes/fork.rs +++ b/crates/snforge-scarb-plugin/src/attributes/fork.rs @@ -2,12 +2,12 @@ use self::block_id::{BlockId, BlockIdVariants}; use crate::{ args::Arguments, attributes::{AttributeCollector, AttributeInfo, AttributeTypeData}, + branch, cairo_expression::CairoExpression, config_statement::extend_with_config_cheatcodes, types::ParseFromExpr, - utils::branch, }; -use cairo_lang_macro::{Diagnostic, Diagnostics, ProcMacroResult, TokenStream}; +use cairo_lang_macro::{Diagnostic, Diagnostics, ProcMacroResult, Severity, TokenStream}; use cairo_lang_syntax::node::db::SyntaxGroup; use indoc::formatdoc; use url::Url; @@ -30,9 +30,11 @@ impl AttributeCollector for ForkCollector { args: Arguments, _warns: &mut Vec, ) -> Result { - let expr = branch(inline_args(db, &args), || { - branch(overridden_args(db, &args), || from_file_args(db, &args)) - })?; + let expr = branch!( + inline_args(db, &args), + overridden_args(db, &args), + from_file_args(db, &args) + )?; Ok(expr) } diff --git a/crates/snforge-scarb-plugin/src/utils.rs b/crates/snforge-scarb-plugin/src/utils.rs index 891d4b2631..00487e71c6 100644 --- a/crates/snforge-scarb-plugin/src/utils.rs +++ b/crates/snforge-scarb-plugin/src/utils.rs @@ -1,44 +1,48 @@ use cairo_lang_macro::{Diagnostic, Severity}; use indoc::formatdoc; -fn higher_severity(a: Severity, b: Severity) -> Severity { +pub fn higher_severity(a: Severity, b: Severity) -> Severity { match (a, b) { (Severity::Warning, Severity::Warning) => Severity::Warning, _ => Severity::Error, } } +pub fn format_error_message(variants: &[Diagnostic]) -> String { + let formatted_variants: Vec = variants + .iter() + .map(|variant| format!("- variant: {}", variant.message)) + .collect(); -pub fn branch( - left: Result, - right: impl Fn() -> Result, -) -> Result { - left.or_else(|error| { - right().map_err(|next_error| { - let next_message = if next_error.message.contains("All options failed") { - let mut lines: Vec<&str> = next_error.message.lines().collect(); - lines = lines[1..lines.len() - 1].to_vec(); - let mut next_message = lines.join("\n"); - if let Some(pos) = next_message.find("- variant: ") { - next_message.replace_range(pos..pos + 11, ""); - } - next_message - } else { - next_error.message.clone() - }; + formatdoc! {" + All options failed + {} + Resolve at least one of them + ", formatted_variants.join("\n")} +} + +#[macro_export] +macro_rules! branch { + ($($result:expr),+) => {{ + let mut messages = Vec::new(); + let mut result = None; - Diagnostic { - severity: higher_severity(error.severity, next_error.severity), - message: formatdoc!( - " - All options failed - - variant: {} - - variant: {} - Resolve at least one of them - ", - error.message, - next_message - ), - } - }) - }) + $( + if result.is_none() {match $result { + Ok(val) => { + result = Some(val); + }, + Err(err) => { + messages.push(err); + }, + }} + )+ + if let Some(result) = result { + Ok(result) + } else { + Err(Diagnostic { + message: crate::utils::format_error_message(&messages), + severity: messages.into_iter().fold(Severity::Warning, |acc, x| crate::utils::higher_severity(acc, x.severity)) + }) + } + }}; } diff --git a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs index 4f4fcc9b43..b6cb812b71 100644 --- a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs +++ b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs @@ -153,7 +153,7 @@ fn accepts_inline_config() { } #[test] -fn overriding_config_1() { +fn overriding_config_name_first() { let item = TokenStream::new(EMPTY_FN.into()); let args = TokenStream::new(r#"("MAINNET", block_number: 23)"#.into()); From db9d4a0adcc7486aab08c84cf5f264af729a7a19 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Thu, 19 Sep 2024 12:44:47 +0200 Subject: [PATCH 16/26] Lint --- crates/snforge-scarb-plugin/src/utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/snforge-scarb-plugin/src/utils.rs b/crates/snforge-scarb-plugin/src/utils.rs index 00487e71c6..c507c7552e 100644 --- a/crates/snforge-scarb-plugin/src/utils.rs +++ b/crates/snforge-scarb-plugin/src/utils.rs @@ -40,8 +40,8 @@ macro_rules! branch { Ok(result) } else { Err(Diagnostic { - message: crate::utils::format_error_message(&messages), - severity: messages.into_iter().fold(Severity::Warning, |acc, x| crate::utils::higher_severity(acc, x.severity)) + message: $crate::utils::format_error_message(&messages), + severity: messages.into_iter().fold(Severity::Warning, |acc, x| $crate::utils::higher_severity(acc, x.severity)) }) } }}; From 599c287d3f2f5b8af49b62d3a866a7310354e655 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Thu, 19 Sep 2024 12:50:34 +0200 Subject: [PATCH 17/26] Rename test --- .../tests/integration/single_attributes/fork.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs index b6cb812b71..4e55937103 100644 --- a/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs +++ b/crates/snforge-scarb-plugin/tests/integration/single_attributes/fork.rs @@ -185,7 +185,7 @@ fn overriding_config_name_first() { } #[test] -fn overriding_config_2() { +fn overriding_config_name_second() { let item = TokenStream::new(EMPTY_FN.into()); let args = TokenStream::new(r#"(block_number: 23, "MAINNET")"#.into()); From 33e7fe5b9f162113b65e25a29cb193de8ba85402 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Thu, 19 Sep 2024 13:13:30 +0200 Subject: [PATCH 18/26] Fromat --- crates/snforge-scarb-plugin/src/utils.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/snforge-scarb-plugin/src/utils.rs b/crates/snforge-scarb-plugin/src/utils.rs index c507c7552e..35d77ccdee 100644 --- a/crates/snforge-scarb-plugin/src/utils.rs +++ b/crates/snforge-scarb-plugin/src/utils.rs @@ -27,15 +27,18 @@ macro_rules! branch { let mut result = None; $( - if result.is_none() {match $result { - Ok(val) => { - result = Some(val); - }, - Err(err) => { - messages.push(err); - }, - }} + if result.is_none() { + match $result { + Ok(val) => { + result = Some(val); + }, + Err(err) => { + messages.push(err); + }, + } + } )+ + if let Some(result) = result { Ok(result) } else { From 22ad6e642b528c555471500d2e01934a7f900d6d Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Fri, 20 Sep 2024 09:56:28 +0200 Subject: [PATCH 19/26] Update match --- crates/forge/src/run_tests/resolve_config.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/crates/forge/src/run_tests/resolve_config.rs b/crates/forge/src/run_tests/resolve_config.rs index 07974575d1..54b4d14118 100644 --- a/crates/forge/src/run_tests/resolve_config.rs +++ b/crates/forge/src/run_tests/resolve_config.rs @@ -1,7 +1,7 @@ use crate::{block_number_map::BlockNumberMap, scarb::config::ForkTarget}; use anyhow::{anyhow, Result}; use cheatnet::runtime_extensions::forge_config_extension::config::{ - BlockId, InlineForkConfig, RawForkConfig, + BlockId, InlineForkConfig, OverriddenForkConfig, RawForkConfig, }; use conversions::byte_array::ByteArray; use forge_runner::package_tests::{ @@ -129,14 +129,13 @@ fn replace_id_with_params( block: block_id, }) } - RawForkConfig::Overridden(overridden) => { + RawForkConfig::Overridden(OverriddenForkConfig { name, block }) => { let fork_target_from_runner_config = - get_fork_target_from_runner_config(fork_targets, &overridden.name)?; + get_fork_target_from_runner_config(fork_targets, &name)?; - Ok(InlineForkConfig { - url: fork_target_from_runner_config.url.parse()?, - block: overridden.block, - }) + let url = fork_target_from_runner_config.url.parse()?; + + Ok(InlineForkConfig { url, block }) } } } From bc71b0b4d99af4b46b84575d32333e55022b021a Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Fri, 20 Sep 2024 11:29:13 +0200 Subject: [PATCH 20/26] Add docs for macro --- crates/snforge-scarb-plugin/src/utils.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/snforge-scarb-plugin/src/utils.rs b/crates/snforge-scarb-plugin/src/utils.rs index 35d77ccdee..99fad7f14a 100644 --- a/crates/snforge-scarb-plugin/src/utils.rs +++ b/crates/snforge-scarb-plugin/src/utils.rs @@ -20,6 +20,10 @@ pub fn format_error_message(variants: &[Diagnostic]) -> String { ", formatted_variants.join("\n")} } +/// The `branch` macro is used to evaluate multiple expressions and return the first successful result. +/// If all expressions fail, it collects the error messages and returns a combined error. +/// +/// This macro is used instead of a function because it can perform lazy evaluation and has better readability. #[macro_export] macro_rules! branch { ($($result:expr),+) => {{ From a81b886892e7d10b253647b458a832bff1147b6c Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Fri, 20 Sep 2024 12:58:04 +0200 Subject: [PATCH 21/26] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5bc5636fd..d705de04de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 #### Added - Project generated by `snforge` contains `assert_macros` dependency with version 0.1.0 for Scarb <= 2.8.0, otherwise equal to Scarb +- Support for overriding fork configuration in test attribute with a different block ID, tag, or hash. ## [0.30.0] - 2024-09-04 From bb35f24ef561ad35e8fbf30b65b8b4922a1da29e Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Fri, 20 Sep 2024 12:58:39 +0200 Subject: [PATCH 22/26] Rename fold args --- crates/snforge-scarb-plugin/src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/snforge-scarb-plugin/src/utils.rs b/crates/snforge-scarb-plugin/src/utils.rs index 99fad7f14a..1872e054e1 100644 --- a/crates/snforge-scarb-plugin/src/utils.rs +++ b/crates/snforge-scarb-plugin/src/utils.rs @@ -48,7 +48,7 @@ macro_rules! branch { } else { Err(Diagnostic { message: $crate::utils::format_error_message(&messages), - severity: messages.into_iter().fold(Severity::Warning, |acc, x| $crate::utils::higher_severity(acc, x.severity)) + severity: messages.into_iter().fold(Severity::Warning, |acc, diagnostic| $crate::utils::higher_severity(acc, diagnostic.severity)) }) } }}; From b1d0a8fd5d98ea1a7e4bda15a22a0d86fa52f856 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Fri, 20 Sep 2024 13:10:39 +0200 Subject: [PATCH 23/26] Add intefration test --- crates/forge/tests/integration/setup_fork.rs | 91 ++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/crates/forge/tests/integration/setup_fork.rs b/crates/forge/tests/integration/setup_fork.rs index e0eca83869..529361cb7c 100644 --- a/crates/forge/tests/integration/setup_fork.rs +++ b/crates/forge/tests/integration/setup_fork.rs @@ -178,6 +178,97 @@ fn fork_aliased_decorator() { assert_passed(&result); } +#[test] +fn fork_aliased_decorator_overrding() { + let test = test_case!(indoc!( + r#" + use starknet::syscalls::get_execution_info_syscall; + + #[test] + #[fork("FORK_NAME_FROM_SCARB_TOML", block_number: 2137)] + fn test_increase_balance() { + let execution_info = get_execution_info_syscall().unwrap().deref(); + let block_info = execution_info.block_info.deref(); + let block_number = block_info.block_number; + + assert(block_number == 2137, 'Invalid block'); + } + "# + )); + + let rt = Runtime::new().expect("Could not instantiate Runtime"); + + ScarbCommand::new_with_stdio() + .current_dir(test.path().unwrap()) + .arg("build") + .arg("--test") + .run() + .unwrap(); + + let metadata = ScarbCommand::metadata() + .current_dir(test.path().unwrap()) + .run() + .unwrap(); + + let package = metadata + .packages + .iter() + .find(|p| p.name == "test_package") + .unwrap(); + + let raw_test_targets = + load_test_artifacts(&test.path().unwrap().join("target/dev"), package).unwrap(); + + let result = rt + .block_on(run_for_package( + RunForPackageArgs { + test_targets: raw_test_targets, + package_name: "test_package".to_string(), + tests_filter: TestsFilter::from_flags( + None, + false, + false, + false, + false, + Default::default(), + ), + forge_config: Arc::new(ForgeConfig { + test_runner_config: Arc::new(TestRunnerConfig { + exit_first: false, + fuzzer_runs: NonZeroU32::new(256).unwrap(), + fuzzer_seed: 12345, + max_n_steps: None, + is_vm_trace_needed: false, + cache_dir: Utf8PathBuf::from_path_buf(tempdir().unwrap().into_path()) + .unwrap() + .join(CACHE_DIR), + contracts_data: ContractsData::try_from(test.contracts().unwrap()).unwrap(), + environment_variables: test.env().clone(), + }), + output_config: Arc::new(OutputConfig { + detailed_resources: false, + execution_data_to_save: ExecutionDataToSave::default(), + versioned_programs_dir: Utf8PathBuf::from_path_buf( + tempdir().unwrap().into_path(), + ) + .unwrap() + .join(VERSIONED_PROGRAMS_DIR), + }), + }), + fork_targets: vec![ForkTarget::new( + "FORK_NAME_FROM_SCARB_TOML".to_string(), + node_rpc_url().to_string(), + "tag".to_string(), + "latest".to_string(), + )], + }, + &mut BlockNumberMap::default(), + )) + .expect("Runner fail"); + + assert_passed(&result); +} + #[test] fn fork_cairo0_contract() { let test = test_case!(formatdoc!( From b24957c167d7aa42a9b70d67ba0386008c0b5edf Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Fri, 20 Sep 2024 13:47:38 +0200 Subject: [PATCH 24/26] Rename test --- crates/forge/tests/integration/setup_fork.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/forge/tests/integration/setup_fork.rs b/crates/forge/tests/integration/setup_fork.rs index 529361cb7c..62abc3f7b4 100644 --- a/crates/forge/tests/integration/setup_fork.rs +++ b/crates/forge/tests/integration/setup_fork.rs @@ -186,7 +186,7 @@ fn fork_aliased_decorator_overrding() { #[test] #[fork("FORK_NAME_FROM_SCARB_TOML", block_number: 2137)] - fn test_increase_balance() { + fn test_get_block_number() { let execution_info = get_execution_info_syscall().unwrap().deref(); let block_info = execution_info.block_info.deref(); let block_number = block_info.block_number; From 7abdc48505345e279d4aa86b2a9447498b5b3285 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Fri, 20 Sep 2024 14:33:24 +0200 Subject: [PATCH 25/26] Fix generating config setup --- .../snforge-scarb-plugin/src/attributes/fork.rs | 6 ++++-- .../tests/integration/single_attributes/fork.rs | 16 ++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/snforge-scarb-plugin/src/attributes/fork.rs b/crates/snforge-scarb-plugin/src/attributes/fork.rs index 064e7f3239..691e929bc6 100644 --- a/crates/snforge-scarb-plugin/src/attributes/fork.rs +++ b/crates/snforge-scarb-plugin/src/attributes/fork.rs @@ -100,8 +100,10 @@ fn overridden_args(db: &dyn SyntaxGroup, args: &Arguments) -> Result(data.span()); @@ -202,9 +204,11 @@ fn overriding_config_name_second() { let mut data = array![]; snforge_std::_config_types::ForkConfig::Overridden( - block: snforge_std::_config_types::BlockId::BlockNumber(0x17), - name: "MAINNET" - ) + snforge_std::_config_types::OverriddenForkConfig { + block: snforge_std::_config_types::BlockId::BlockNumber(0x17), + name: "MAINNET" + } + ) .serialize(ref data); starknet::testing::cheatcode::<'set_config_fork'>(data.span()); From 0a05d6684807783e8a1c94c574064a751c7981d1 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Fri, 20 Sep 2024 14:39:03 +0200 Subject: [PATCH 26/26] Make 'fork_aliased_decorator_overrding' more stable --- crates/forge/tests/integration/setup_fork.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/forge/tests/integration/setup_fork.rs b/crates/forge/tests/integration/setup_fork.rs index 62abc3f7b4..cf751b523b 100644 --- a/crates/forge/tests/integration/setup_fork.rs +++ b/crates/forge/tests/integration/setup_fork.rs @@ -258,8 +258,8 @@ fn fork_aliased_decorator_overrding() { fork_targets: vec![ForkTarget::new( "FORK_NAME_FROM_SCARB_TOML".to_string(), node_rpc_url().to_string(), - "tag".to_string(), - "latest".to_string(), + "number".to_string(), + "12341234".to_string(), )], }, &mut BlockNumberMap::default(),