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

Enable features for main package during integration tests compilation #1646

Merged
merged 9 commits into from
Oct 16, 2024
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
6 changes: 5 additions & 1 deletion scarb/src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,11 @@ fn cairo_compilation_unit_for_target(
components.push(CompilationUnitComponent::try_new(
member.clone(),
vec![target],
no_test_cfg_set,
get_cfg_with_features(
no_test_cfg_set.unwrap_or(CfgSet::new()),
&member.manifest.features,
enabled_features,
)?,
)?);

// Set test package as main package for this compilation unit.
Expand Down
87 changes: 87 additions & 0 deletions scarb/tests/build_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,93 @@ fn integration_tests_cannot_use_itself_by_target_name() {
"#});
}

#[test]
fn features_enabled_in_integration_tests() {
let t = TempDir::new().unwrap();
ProjectBuilder::start()
.name("hello")
.dep_cairo_test()
.manifest_extra(indoc! {r#"
[features]
x = []
"#})
.lib_cairo(indoc! {r#"
#[cfg(feature: 'x')]
fn f() -> felt252 { 42 }

fn main() -> felt252 {
0
}
"#})
.build(&t);

t.child("tests/test1.cairo")
.write_str(indoc! {r#"
#[cfg(test)]
mod tests {
use hello::f;

#[test]
fn test_feature_function() {
assert(f() == 42, 'it works!');
}
}
"#})
.unwrap();

Scarb::quick_snapbox()
.arg("build")
.arg("--test")
.current_dir(&t)
.assert()
.failure()
.stdout_matches(indoc! {r#"
[..] Compiling test(hello_unittest) hello v1.0.0 ([..]Scarb.toml)
[..] Compiling test(hello_integrationtest) hello_integrationtest v1.0.0 ([..])
error: Identifier not found.
--> [..]test1.cairo:3:16
use hello::f;
^

error: Type annotations needed. Failed to infer ?0.
--> [..]test1.cairo:7:16
assert(f() == 42, 'it works!');
^*******^

error: could not compile `hello_integrationtest` due to previous error
"#});

Scarb::quick_snapbox()
.arg("build")
.arg("--test")
.arg("--features")
.arg("x")
.current_dir(&t)
.assert()
.success();

assert_eq!(
t.child("target/dev").files(),
vec![
"hello_integrationtest.test.json",
"hello_integrationtest.test.sierra.json",
"hello_unittest.test.json",
"hello_unittest.test.sierra.json",
]
);

t.child("target/dev/hello_integrationtest.test.json")
.assert_is_json::<serde_json::Value>();
t.child("target/dev/hello_integrationtest.test.sierra.json")
.assert_is_json::<VersionedProgram>();
let content = t
.child("target/dev/hello_integrationtest.test.json")
.read_to_string();
let json: serde_json::Value = serde_json::from_str(&content).unwrap();
let tests = json.get("named_tests").unwrap().as_array().unwrap();
assert_eq!(tests.len(), 1);
}

#[test]
fn detect_single_file_test_targets() {
let t = TempDir::new().unwrap();
Expand Down
Loading