Skip to content
Open
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
23 changes: 17 additions & 6 deletions crates/snforge-scarb-plugin/src/attributes/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{AttributeInfo, ErrorExt, internal_config_statement::InternalConfigSt
use crate::asserts::assert_is_used_once;
use crate::common::{has_fuzzer_attribute, has_test_case_attribute};
use crate::external_inputs::ExternalInput;
use crate::utils::create_single_token;
use crate::utils::{create_single_token, process_statements};
use crate::{
args::Arguments,
common::{into_proc_macro_result, with_parsed_values},
Expand Down Expand Up @@ -81,9 +81,6 @@ fn test_internal(
let signature = SyntaxNodeWithDb::new(&signature, db);
let signature = quote! { #signature };

let body = func.body(db).as_syntax_node();
let body = SyntaxNodeWithDb::new(&body, db);

let attributes = func.attributes(db).as_syntax_node();
let attributes = SyntaxNodeWithDb::new(&attributes, db);

Expand All @@ -98,13 +95,27 @@ fn test_internal(

let test_func_with_attrs = test_func_with_attrs(&test_func, &called_func, &call_args);

let statements = func
.body(db)
.statements(db)
.elements(db)
.collect::<Vec<_>>();
let (statements, if_content) = process_statements(db, &statements);

Ok(quote!(
#test_func_with_attrs

#attributes
#[#internal_config]
fn #func_ident #signature
#body
{
if snforge_std::_internals::is_config_run() {
#if_content

return;
Comment on lines +111 to +114
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be able to do the same in fuzzer wrapper in wrapper.rs file.

Replace appending #[#internal_config_attr] there with simply creating an another if branch like you did here.

Then we could get rid of the internal config statement attribute entirely.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @ddoktorski @franciszekjob please check if I'm not making things up

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I think that should be possible to do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cptartur This sounds like a good refactoring idea, but tbh I don't think this matters at all apart from code cleanliness - you would normally expect a lot more test attrs than any other.

Especially as I would need to have some equivalent of

pub fn get_statements(db: &dyn SyntaxGroup, func: &FunctionWithBody) -> (String, String) {
that accepts TokenStream instead of FunctionWithBody to keep the logic exactly the same.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do that refactor at a later date then.

}

#statements
}
))
} else {
Ok(quote!(
Expand Down
8 changes: 7 additions & 1 deletion crates/snforge-scarb-plugin/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ pub fn get_statements(
.statements(db)
.elements(db)
.collect::<Vec<_>>();
process_statements(db, &statements)
}

pub fn process_statements(
db: &SimpleParserDatabase,
statements: &[Statement],
) -> (TokenStream, TokenStream) {
let if_content = statements.first().and_then(|stmt| {
// first statement is `if`
let Statement::Expr(expr) = stmt else {
Expand Down Expand Up @@ -133,7 +139,7 @@ pub fn get_statements(
let statements = if if_content.is_some() {
&statements[1..]
} else {
&statements[..]
statements
}
.iter()
.map(|stmt| stmt.to_token_stream(db))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ fn works_with_few_attributes() {
core::array::ArrayTrait::span(@arr)
}

#[__internal_config_statement]
fn empty_fn() {

if snforge_std::_internals::is_config_run() {
return;
}
}
",
);
Expand All @@ -107,7 +108,6 @@ fn works_with_few_attributes() {
assert_output(
&result,
"
#[__internal_config_statement]
fn empty_fn() {
if snforge_std::_internals::is_config_run() {
let mut data = array![];
Expand Down Expand Up @@ -137,7 +137,6 @@ fn works_with_few_attributes() {
assert_output(
&result,
r#"
#[__internal_config_statement]
fn empty_fn() {
if snforge_std::_internals::is_config_run() {
let mut data = array![];
Expand Down Expand Up @@ -192,9 +191,10 @@ fn works_with_fuzzer() {
core::array::ArrayTrait::span(@arr)
}

#[__internal_config_statement]
fn empty_fn() {

if snforge_std::_internals::is_config_run() {
return;
}
}
",
);
Expand All @@ -211,8 +211,11 @@ fn works_with_fuzzer() {
r"
#[__fuzzer_config(runs: 123, seed: 321)]
#[__fuzzer_wrapper]
#[__internal_config_statement]
fn empty_fn() {}
fn empty_fn() {
if snforge_std::_internals::is_config_run() {
return;
}
}
",
);
}
Expand Down Expand Up @@ -262,8 +265,11 @@ fn works_with_fuzzer_before_test() {

#[__fuzzer_config(runs: 123, seed: 321)]
#[__fuzzer_wrapper]
#[__internal_config_statement]
fn empty_fn(f: felt252) {}
fn empty_fn(f: felt252) {
if snforge_std::_internals::is_config_run() {
return;
}
}
",
);

Expand Down Expand Up @@ -363,7 +369,6 @@ fn works_with_fuzzer_config_wrapper() {
}

#[fuzzer]
#[__internal_config_statement]
fn empty_fn(f: felt252) {
if snforge_std::_internals::is_config_run() {
let mut data = array![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ fn appends_internal_config_and_executable() {
core::array::ArrayTrait::span(@arr)
}

#[__internal_config_statement]
fn empty_fn() {}
fn empty_fn() {
if snforge_std::_internals::is_config_run() {
return;
}
}
",
);
}
Expand Down
Loading