diff --git a/extensions/scarb-snforge-test-collector/src/metadata.rs b/extensions/scarb-snforge-test-collector/src/metadata.rs index 5fa27fac3..edd8c1590 100644 --- a/extensions/scarb-snforge-test-collector/src/metadata.rs +++ b/extensions/scarb-snforge-test-collector/src/metadata.rs @@ -1,9 +1,11 @@ -use anyhow::{anyhow, Context, Result}; +use anyhow::{anyhow, ensure, Context, Result}; use cairo_lang_filesystem::db::{CrateSettings, Edition}; use cairo_lang_project::AllCratesConfig; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; use camino::{Utf8Path, Utf8PathBuf}; +use itertools::Itertools; use scarb_metadata::{CompilationUnitMetadata, Metadata, PackageMetadata}; +use serde_json::json; use smol_str::SmolStr; use std::path::PathBuf; @@ -18,23 +20,54 @@ pub fn compilation_unit_for_package<'a>( metadata: &'a Metadata, package_metadata: &PackageMetadata, ) -> Result> { - let compilation_unit_metadata = metadata + let unit_test_cu = metadata .compilation_units .iter() - .filter(|unit| unit.package == package_metadata.id) - .min_by_key(|unit| match unit.target.kind.as_str() { - name @ "starknet-contract" => (0, name), - name @ "lib" => (1, name), - name => (2, name), + .find(|unit| { + unit.package == package_metadata.id + && unit.target.kind == "test" + && unit.target.params.get("test-type") == Some(&json!("unit")) }) .ok_or_else(|| { anyhow!( - "Failed to find compilation unit for package = {}", + "Failed to find unit test compilation unit for package = {}", package_metadata.name ) })?; + let all_test_cus = metadata + .compilation_units + .iter() + .filter(|unit| unit.package == package_metadata.id && unit.target.kind == "test") + .collect_vec(); + + let unit_test_deps = unit_test_cu.components.iter().collect_vec(); + + for cu in all_test_cus { + let test_type = cu + .target + .params + .get("test-type") + .expect("Test target missing test-type param") + .as_str() + .expect("test-type param is not a string"); + + let test_deps_without_tests = cu + .components + .iter() + .filter(|du| match test_type { + "unit" => true, + _ => !du.source_root().starts_with(cu.target.source_root()), + }) + .collect_vec(); + + ensure!( + unit_test_deps == test_deps_without_tests, + "Dependencies mismatch between test compilation units" + ); + } + Ok(CompilationUnit { - unit_metadata: compilation_unit_metadata, + unit_metadata: unit_test_cu, metadata, }) } diff --git a/extensions/scarb-snforge-test-collector/tests/test.rs b/extensions/scarb-snforge-test-collector/tests/test.rs index e6094828e..961c3e217 100644 --- a/extensions/scarb-snforge-test-collector/tests/test.rs +++ b/extensions/scarb-snforge-test-collector/tests/test.rs @@ -273,7 +273,6 @@ fn forge_test_with_should_panic_message_attribute() { .read_to_string(); let json: Value = serde_json::from_str(&snforge_sierra).unwrap(); - dbg!(); assert_eq!( &json[0]["test_cases"][0]["expected_result"]["Panics"].to_string(), "{\"Exact\":[{\"value\":{\"val\":[1935763301,544040307,1634625891,112]}},{\"value\":{\"val\":[1935763301,544040307,1668247140,1814066021,1853125985,6649445]}}]}" @@ -325,3 +324,46 @@ fn can_disallow_warnings() { .assert() .failure(); } + +#[test] +fn uses_dev_dependencies() { + let t = TempDir::new().unwrap(); + let q = t.child("q"); + ProjectBuilder::start() + .name("q") + .lib_cairo("fn dev_dep_function() -> felt252 { 42 }") + .build(&q); + + ProjectBuilder::start() + .name("x") + .dev_dep("q", &q) + .lib_cairo(indoc! {r#" + #[cfg(test)] + mod tests { + use q::dev_dep_function; + + #[test] + fn test() { + assert(dev_dep_function() == 42, ''); + } + } + "#}) + .build(&t); + + let test_path = t.child("tests/test.cairo"); + test_path + .write_str(indoc! {r#" + use q::dev_dep_function; + + fn test() { + assert(dev_dep_function() == 42, ''); + } + "#}) + .unwrap(); + + Scarb::quick_snapbox() + .arg("snforge-test-collector") + .current_dir(&t) + .assert() + .success(); +}