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

Add .cairo-coverage-ignore tests #110

Merged
merged 1 commit into from
Dec 6, 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
86 changes: 86 additions & 0 deletions crates/cairo-coverage/src/input/filter/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,89 @@ fn find_ignore_file(start_dir: &Utf8Path) -> Option<Utf8PathBuf> {

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<T: PathChild + AsRef<Path>> 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));
}
}
22 changes: 22 additions & 0 deletions crates/cairo-coverage/tests/data/coverage_ignore/Scarb.toml
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod simple;
mod multiply;
Original file line number Diff line number Diff line change
@@ -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, '');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod simple_add;
mod simple_subtract;
Original file line number Diff line number Diff line change
@@ -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, '');
}
}

Original file line number Diff line number Diff line change
@@ -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, '');
}
}
33 changes: 33 additions & 0 deletions crates/cairo-coverage/tests/e2e/cairo_coverage_ignore.rs
Original file line number Diff line number Diff line change
@@ -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");
}
1 change: 1 addition & 0 deletions crates/cairo-coverage/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod cairo_coverage_ignore;
mod general;
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions crates/cairo-coverage/tests/helpers/test_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading