Skip to content

Commit

Permalink
Use dev-deps during compilation in snforge-test-collector (#1033)
Browse files Browse the repository at this point in the history
Closes foundry-rs/starknet-foundry#1460

The solution is to fetch dependencies from unit test compilation unit.

After talking with @mkaput we decided that to ensure this heuristic
solution doesn't break we check if all other test compilation units have
the same dependencies (excluding dependencies being sources for
integration tests i. e., starting with `{package_path}/tests/`).
  • Loading branch information
piotmag769 authored Jan 8, 2024
1 parent c6a0ef5 commit 1fe9f8f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 10 deletions.
51 changes: 42 additions & 9 deletions extensions/scarb-snforge-test-collector/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,23 +20,54 @@ pub fn compilation_unit_for_package<'a>(
metadata: &'a Metadata,
package_metadata: &PackageMetadata,
) -> Result<CompilationUnit<'a>> {
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,
})
}
Expand Down
44 changes: 43 additions & 1 deletion extensions/scarb-snforge-test-collector/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]}}]}"
Expand Down Expand Up @@ -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();
}

0 comments on commit 1fe9f8f

Please sign in to comment.