From 1b75b4b39ac270e4412218774fc304fd5b0b9965 Mon Sep 17 00:00:00 2001 From: Karol Sewilo Date: Wed, 4 Dec 2024 12:45:12 +0100 Subject: [PATCH] Add tests commit-id:f6098cdc --- .../cairo-coverage/src/input/filter/ignore.rs | 86 +++++++++++++++++++ .../tests/data/coverage_ignore/Scarb.toml | 22 +++++ .../tests/data/coverage_ignore/src/lib.cairo | 2 + .../data/coverage_ignore/src/multiply.cairo | 13 +++ .../data/coverage_ignore/src/simple.cairo | 2 + .../src/simple/simple_add.cairo | 15 ++++ .../src/simple/simple_subtract.cairo | 14 +++ .../tests/e2e/cairo_coverage_ignore.rs | 33 +++++++ crates/cairo-coverage/tests/e2e/mod.rs | 1 + .../expected_output/coverage_ignore_dir.lcov | 10 +++ .../expected_output/coverage_ignore_file.lcov | 10 +++ .../tests/helpers/test_project.rs | 5 ++ 12 files changed, 213 insertions(+) create mode 100644 crates/cairo-coverage/tests/data/coverage_ignore/Scarb.toml create mode 100644 crates/cairo-coverage/tests/data/coverage_ignore/src/lib.cairo create mode 100644 crates/cairo-coverage/tests/data/coverage_ignore/src/multiply.cairo create mode 100644 crates/cairo-coverage/tests/data/coverage_ignore/src/simple.cairo create mode 100644 crates/cairo-coverage/tests/data/coverage_ignore/src/simple/simple_add.cairo create mode 100644 crates/cairo-coverage/tests/data/coverage_ignore/src/simple/simple_subtract.cairo create mode 100644 crates/cairo-coverage/tests/e2e/cairo_coverage_ignore.rs create mode 100644 crates/cairo-coverage/tests/expected_output/coverage_ignore_dir.lcov create mode 100644 crates/cairo-coverage/tests/expected_output/coverage_ignore_file.lcov diff --git a/crates/cairo-coverage/src/input/filter/ignore.rs b/crates/cairo-coverage/src/input/filter/ignore.rs index e677d7f..846a826 100644 --- a/crates/cairo-coverage/src/input/filter/ignore.rs +++ b/crates/cairo-coverage/src/input/filter/ignore.rs @@ -47,3 +47,89 @@ fn find_ignore_file(start_dir: &Utf8Path) -> Option { None } + +#[cfg(test)] +mod tests { + use super::*; + use assert_fs::prelude::*; + use assert_fs::TempDir; + use std::path::Path; + + trait TestUtils { + fn create_ignore_file(&self) -> Utf8PathBuf; + fn to_utf8_path_buf(&self) -> Utf8PathBuf; + } + + impl> TestUtils for T { + fn create_ignore_file(&self) -> Utf8PathBuf { + let ignore_file = self.child(CAIRO_COVERAGE_IGNORE); + ignore_file.touch().unwrap(); + ignore_file.to_utf8_path_buf() + } + + fn to_utf8_path_buf(&self) -> Utf8PathBuf { + Utf8PathBuf::from_path_buf(self.as_ref().to_path_buf()).unwrap() + } + } + + #[test] + fn test_finds_ignore_file_in_same_directory() { + let temp_dir = TempDir::new().unwrap(); + let ignore_file_path = temp_dir.create_ignore_file(); + + let result = find_ignore_file(&ignore_file_path); + + assert_eq!(result, Some(ignore_file_path)); + } + + #[test] + fn test_finds_ignore_file_in_parent_directory() { + let temp_dir = TempDir::new().unwrap(); + let parent_dir = temp_dir.child("parent"); + let child_dir = parent_dir.child("child"); + + let ignore_file_path = parent_dir.create_ignore_file(); + + let result = find_ignore_file(&child_dir.to_utf8_path_buf()); + + assert_eq!(result, Some(ignore_file_path)); + } + + #[test] + fn test_finds_ignore_file_multiple_levels_up() { + let temp_dir = TempDir::new().unwrap(); + let root_dir = temp_dir.child("root"); + let middle_dir = root_dir.child("middle"); + let child_dir = middle_dir.child("child"); + + let ignore_file = root_dir.create_ignore_file(); + + let result = find_ignore_file(&child_dir.to_utf8_path_buf()); + + assert_eq!(result, Some(ignore_file)); + } + + #[test] + fn test_no_ignore_file_found() { + let temp_dir = TempDir::new().unwrap(); + + let result = find_ignore_file(&temp_dir.to_utf8_path_buf()); + + assert_eq!(result, None); + } + + #[test] + fn test_stops_at_first_ignore_file() { + let temp_dir = TempDir::new().unwrap(); + let root_dir = temp_dir.child("root"); + let middle_dir = root_dir.child("middle"); + let child_dir = middle_dir.child("child"); + + root_dir.create_ignore_file(); + let middle_ignore_file = middle_dir.create_ignore_file(); + + let result = find_ignore_file(&child_dir.to_utf8_path_buf()); + + assert_eq!(result, Some(middle_ignore_file)); + } +} diff --git a/crates/cairo-coverage/tests/data/coverage_ignore/Scarb.toml b/crates/cairo-coverage/tests/data/coverage_ignore/Scarb.toml new file mode 100644 index 0000000..3fe61ce --- /dev/null +++ b/crates/cairo-coverage/tests/data/coverage_ignore/Scarb.toml @@ -0,0 +1,22 @@ +[package] +name = "coverage_ignore" +version = "0.1.0" +edition = "2024_07" + +# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html +[dependencies] +starknet = ">=2.8.0" + +[dev-dependencies] +snforge_std = "0.30.0" + +[[target.starknet-contract]] +sierra = true + +[scripts] +test = "snforge test" + +[profile.dev.cairo] +unstable-add-statements-functions-debug-info = true +unstable-add-statements-code-locations-debug-info = true +inlining-strategy= "avoid" diff --git a/crates/cairo-coverage/tests/data/coverage_ignore/src/lib.cairo b/crates/cairo-coverage/tests/data/coverage_ignore/src/lib.cairo new file mode 100644 index 0000000..aa53f54 --- /dev/null +++ b/crates/cairo-coverage/tests/data/coverage_ignore/src/lib.cairo @@ -0,0 +1,2 @@ +mod simple; +mod multiply; diff --git a/crates/cairo-coverage/tests/data/coverage_ignore/src/multiply.cairo b/crates/cairo-coverage/tests/data/coverage_ignore/src/multiply.cairo new file mode 100644 index 0000000..55450a1 --- /dev/null +++ b/crates/cairo-coverage/tests/data/coverage_ignore/src/multiply.cairo @@ -0,0 +1,13 @@ +pub fn multiply(a: i32, b: i32) -> i32 { + a * b +} + +#[cfg(test)] +mod tests { + use super::multiply; + + #[test] + fn it_works() { + assert(multiply(2, 1) == 2, ''); + } +} diff --git a/crates/cairo-coverage/tests/data/coverage_ignore/src/simple.cairo b/crates/cairo-coverage/tests/data/coverage_ignore/src/simple.cairo new file mode 100644 index 0000000..f9bbb53 --- /dev/null +++ b/crates/cairo-coverage/tests/data/coverage_ignore/src/simple.cairo @@ -0,0 +1,2 @@ +mod simple_add; +mod simple_subtract; diff --git a/crates/cairo-coverage/tests/data/coverage_ignore/src/simple/simple_add.cairo b/crates/cairo-coverage/tests/data/coverage_ignore/src/simple/simple_add.cairo new file mode 100644 index 0000000..8d1c2ea --- /dev/null +++ b/crates/cairo-coverage/tests/data/coverage_ignore/src/simple/simple_add.cairo @@ -0,0 +1,15 @@ +pub fn add(a: i32, b: i32) -> i32 { + a + b +} + + +#[cfg(test)] +mod tests { + use super::add; + + #[test] + fn it_works() { + assert(add(1, 2) == 3, ''); + } +} + diff --git a/crates/cairo-coverage/tests/data/coverage_ignore/src/simple/simple_subtract.cairo b/crates/cairo-coverage/tests/data/coverage_ignore/src/simple/simple_subtract.cairo new file mode 100644 index 0000000..dab4967 --- /dev/null +++ b/crates/cairo-coverage/tests/data/coverage_ignore/src/simple/simple_subtract.cairo @@ -0,0 +1,14 @@ +pub fn subtract(a: i32, b: i32) -> i32 { + a - b +} + + +#[cfg(test)] +mod tests { + use super::subtract; + + #[test] + fn it_works() { + assert(subtract(2, 1) == 1, ''); + } +} diff --git a/crates/cairo-coverage/tests/e2e/cairo_coverage_ignore.rs b/crates/cairo-coverage/tests/e2e/cairo_coverage_ignore.rs new file mode 100644 index 0000000..3d7cc4d --- /dev/null +++ b/crates/cairo-coverage/tests/e2e/cairo_coverage_ignore.rs @@ -0,0 +1,33 @@ +use crate::helpers::TestProject; + +#[test] +fn coverage_ignore_dir() { + TestProject::new("coverage_ignore") + .create_cairo_coverage_ignore("*/simple/*") + .run() + .output_same_as_in_file("coverage_ignore_dir.lcov"); +} + +#[test] +fn coverage_ignore_files2() { + TestProject::new("coverage_ignore") + .create_cairo_coverage_ignore("simple_*.cairo") + .run() + .output_same_as_in_file("coverage_ignore_dir.lcov"); +} + +#[test] +fn coverage_ignore_files() { + TestProject::new("coverage_ignore") + .create_cairo_coverage_ignore("multiply.cairo\nsimple_add.cairo") + .run() + .output_same_as_in_file("coverage_ignore_file.lcov"); +} + +#[test] +fn coverage_ignore_file_does_not_exists() { + TestProject::new("coverage_ignore") + .create_cairo_coverage_ignore("multiply.cairo\nsimple_add.cairo\nnot_existing.cairo") + .run() + .output_same_as_in_file("coverage_ignore_file.lcov"); +} diff --git a/crates/cairo-coverage/tests/e2e/mod.rs b/crates/cairo-coverage/tests/e2e/mod.rs index ec66909..cd6119d 100644 --- a/crates/cairo-coverage/tests/e2e/mod.rs +++ b/crates/cairo-coverage/tests/e2e/mod.rs @@ -1 +1,2 @@ +mod cairo_coverage_ignore; mod general; diff --git a/crates/cairo-coverage/tests/expected_output/coverage_ignore_dir.lcov b/crates/cairo-coverage/tests/expected_output/coverage_ignore_dir.lcov new file mode 100644 index 0000000..34bce5b --- /dev/null +++ b/crates/cairo-coverage/tests/expected_output/coverage_ignore_dir.lcov @@ -0,0 +1,10 @@ +TN: +SF:{dir}/src/multiply.cairo +FN:2,coverage_ignore::multiply::multiply +FNDA:1,coverage_ignore::multiply::multiply +FNF:1 +FNH:1 +DA:2,1 +LF:1 +LH:1 +end_of_record diff --git a/crates/cairo-coverage/tests/expected_output/coverage_ignore_file.lcov b/crates/cairo-coverage/tests/expected_output/coverage_ignore_file.lcov new file mode 100644 index 0000000..c7690d1 --- /dev/null +++ b/crates/cairo-coverage/tests/expected_output/coverage_ignore_file.lcov @@ -0,0 +1,10 @@ +TN: +SF:{dir}/src/simple/simple_subtract.cairo +FN:2,coverage_ignore::simple::simple_subtract::subtract +FNDA:1,coverage_ignore::simple::simple_subtract::subtract +FNF:1 +FNH:1 +DA:2,1 +LF:1 +LH:1 +end_of_record diff --git a/crates/cairo-coverage/tests/helpers/test_project.rs b/crates/cairo-coverage/tests/helpers/test_project.rs index e733161..a277bc4 100644 --- a/crates/cairo-coverage/tests/helpers/test_project.rs +++ b/crates/cairo-coverage/tests/helpers/test_project.rs @@ -45,6 +45,11 @@ impl TestProject { self } + pub fn create_cairo_coverage_ignore(self, content: &str) -> Self { + fs::write(self.dir.path().join(".cairo-coverage-ignore"), content).unwrap(); + self + } + fn generate_trace_files(self) -> Self { SnapboxCommand::new("snforge") .arg("test")