Skip to content

Support providing files and lines to include into report via json file #1310

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/file_filter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use regex::Regex;
use std::path::Path;
use std::path::{Path, PathBuf};

pub enum FilterType {
Line(u32),
Expand All @@ -15,6 +15,7 @@ pub struct FileFilter {
excl_br_line: Option<Regex>,
excl_br_start: Option<Regex>,
excl_br_stop: Option<Regex>,
pub(crate) incl_explicit: Option<PathBuf>,
}

impl FileFilter {
Expand All @@ -25,6 +26,7 @@ impl FileFilter {
excl_br_line: Option<Regex>,
excl_br_start: Option<Regex>,
excl_br_stop: Option<Regex>,
incl_explicit: Option<PathBuf>,
) -> Self {
Self {
excl_line,
Expand All @@ -33,6 +35,7 @@ impl FileFilter {
excl_br_line,
excl_br_start,
excl_br_stop,
incl_explicit,
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ struct Opt {
/// Set the log level.
#[arg(long, value_name = "LEVEL", default_value = "ERROR", value_enum)]
log_level: LevelFilterArg,
/// Provides a json file with explicit list of files and lines to include into the report.
/// The json format is `{ "path": [[1, 2], ...], ... }`, where the numbers are inclusive
/// and 1-based line ranges.
#[arg(long, value_name = "PATH")]
incl_explicit: Option<PathBuf>,
/// Lines in covered files containing this marker will be excluded.
#[arg(long, value_name = "regex")]
excl_line: Option<Regex>,
Expand Down Expand Up @@ -359,6 +364,7 @@ fn main() {
opt.excl_br_line,
opt.excl_br_start,
opt.excl_br_stop,
opt.incl_explicit,
);
let demangle = !opt.no_demangle;

Expand Down
51 changes: 42 additions & 9 deletions src/path_rewriting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ pub fn rewrite_paths(
}
}

let incl_explicit = file_filter.incl_explicit.as_ref().map(|incl_explicit| {
let json_str = fs::read_to_string(incl_explicit).unwrap();
let entries: FxHashMap<PathBuf, Vec<(u32, u32)>> = serde_json::from_str(&json_str).unwrap();
entries
.into_iter()
.map(|(mut path, ranges)| {
if let Some(source_dir) = source_dir {
if let Ok(abs_path) = canonicalize_path(source_dir.join(&path)) {
path = abs_path;
}
}
(path, ranges)
})
.collect::<FxHashMap<_, _>>()
});

let results = result_map
.into_par_iter()
.filter_map(move |(path, mut result)| {
Expand Down Expand Up @@ -324,6 +340,21 @@ pub fn rewrite_paths(
return None;
}

if let Some(incl_explicit) = &incl_explicit {
match incl_explicit.get(&abs_path) {
Some(ranges) => {
let included = |line| {
ranges
.iter()
.any(|&(start, end)| line >= start && line <= end)
};
result.lines.retain(|&line, _| included(line));
result.branches.retain(|&line, _| included(line));
}
None => return None,
}
}

// Always return results with '/'.
let rel_path = PathBuf::from(rel_path.to_str().unwrap().replace('\\', "/"));

Expand Down Expand Up @@ -425,7 +456,7 @@ mod tests {
macro_rules! skipping_result {
() => {{
let mut result = empty_result!();
for i in 1..20 {
for i in 1..27 {
result.lines.insert(i, 1);
result.branches.insert(i, vec![true]);
}
Expand Down Expand Up @@ -1658,22 +1689,23 @@ mod tests {
Some(regex::Regex::new("excluded branch").unwrap()),
Some(regex::Regex::new("skip branch start").unwrap()),
Some(regex::Regex::new("skip branch end").unwrap()),
Some(PathBuf::from("test/java/skip.json")),
),
);
let mut count = 0;
for (_, _, result) in results {
count += 1;
for inc in [1, 2, 3, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16].iter() {
for inc in [1, 2, 3, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 26].iter() {
assert!(result.lines.contains_key(inc));
}
for inc in [4, 6, 7, 17, 18, 19, 20].iter() {
for inc in [4, 6, 7, 17, 18, 19, 20, 21, 22, 23, 24, 25].iter() {
assert!(!result.lines.contains_key(inc));
}

for inc in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 17].iter() {
for inc in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 17, 26].iter() {
assert!(result.branches.contains_key(inc));
}
for inc in [11, 13, 14, 18, 19, 20].iter() {
for inc in [11, 13, 14, 18, 19, 20, 21, 22, 23, 24, 25].iter() {
assert!(!result.branches.contains_key(inc));
}
}
Expand Down Expand Up @@ -1701,22 +1733,23 @@ mod tests {
Some(regex::Regex::new("excluded branch").unwrap()),
Some(regex::Regex::new("skip branch start").unwrap()),
Some(regex::Regex::new("skip branch end").unwrap()),
Some(PathBuf::from("test\\java\\skip.json")),
),
);
let mut count = 0;
for (_, _, result) in results {
count += 1;
for inc in [1, 2, 3, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16].iter() {
for inc in [1, 2, 3, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 26].iter() {
assert!(result.lines.contains_key(inc));
}
for inc in [4, 6, 7, 17, 18, 19, 20].iter() {
for inc in [4, 6, 7, 17, 18, 19, 20, 21, 22, 23, 24, 25].iter() {
assert!(!result.lines.contains_key(inc));
}

for inc in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 17].iter() {
for inc in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 17, 26].iter() {
assert!(result.branches.contains_key(inc));
}
for inc in [11, 13, 14, 18, 19, 20].iter() {
for inc in [11, 13, 14, 18, 19, 20, 21, 22, 23, 24, 25].iter() {
assert!(!result.branches.contains_key(inc));
}
}
Expand Down
10 changes: 8 additions & 2 deletions test/java/skip.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ void Foo() {

// included branch
// excluded branch

// skip branch start
// skipped branch
// skip branch end

// skip line start
// skip branch start
}
}
}

// skip line end skip branch end
class ExcludedViaJson {
void Bar() {
}
}
1 change: 1 addition & 0 deletions test/java/skip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "java/skip.java": [[1, 20], [26, 26]] }
Loading