Skip to content

Commit

Permalink
Do not ignore subfolder with name equal to root. (#546)
Browse files Browse the repository at this point in the history
Before this commit, a subfolder with the same name as the root/source
folder would be ignored from the coverage reports (see
#483). This was occurring because
the ancestor of the subfolder was being pruned (because it matched the
end of the source folder) in the method used to guess the absolute path.
This resulted in an incorrect guess of the absolute path.

With this commit, we first check whether the join of the subfolder
relative path with the source folder exists, and if so, return it as the
guess of the absolute path. If the join doesn't exist, we use the
previous guess method.
  • Loading branch information
vitorenesduarte authored Dec 16, 2020
1 parent 89ecd44 commit 33ecb24
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions src/path_rewriting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,20 @@ fn apply_mapping(mapping: &Option<Value>, path: &str) -> PathBuf {
PathBuf::from(path)
}

// Remove common part between the prefix's end and the path's start
// If the join of the source and the relative path is a file, return it.
// Otherwise, remove common part between the source's end and the relative
// path's start.
fn guess_abs_path(prefix_dir: &PathBuf, path: &PathBuf) -> PathBuf {
let full_path = prefix_dir.join(path);
if full_path.is_file() {
return full_path;
}
for ancestor in path.ancestors() {
if prefix_dir.ends_with(ancestor) && !ancestor.as_os_str().is_empty() {
return prefix_dir.join(path.strip_prefix(ancestor).unwrap().to_path_buf());
}
}
prefix_dir.join(path)
full_path
}

// Remove prefix from the source file's path.
Expand Down Expand Up @@ -1000,6 +1006,60 @@ mod tests {
assert_eq!(count, 2);
}

#[cfg(unix)]
#[test]
fn test_rewrite_paths_subfolder_same_as_root() {
let mut result_map: CovResultMap = FxHashMap::default();
result_map.insert("test/main.rs".to_string(), empty_result!());
let results = rewrite_paths(
result_map,
None,
Some(canonicalize_path("test").unwrap()),
None,
false,
&mut Vec::new(),
&Vec::new(),
None,
Default::default(),
);
let mut count = 0;
for (abs_path, rel_path, result) in results {
count += 1;
assert!(abs_path.is_absolute());
assert!(abs_path.ends_with("test/test/main.rs"));
assert_eq!(rel_path, PathBuf::from("test/main.rs"));
assert_eq!(result, empty_result!());
}
assert_eq!(count, 1);
}

#[cfg(windows)]
#[test]
fn test_rewrite_paths_subfolder_same_as_root() {
let mut result_map: CovResultMap = FxHashMap::default();
result_map.insert("test\\main.rs".to_string(), empty_result!());
let results = rewrite_paths(
result_map,
None,
Some(canonicalize_path("test").unwrap()),
None,
false,
&mut Vec::new(),
&Vec::new(),
None,
Default::default(),
);
let mut count = 0;
for (abs_path, rel_path, result) in results {
count += 1;
assert!(abs_path.is_absolute());
assert!(abs_path.ends_with("test\\test\\main.rs"));
assert_eq!(rel_path, PathBuf::from("test\\main.rs"));
assert_eq!(result, empty_result!());
}
assert_eq!(count, 1);
}

#[cfg(unix)]
#[test]
fn test_rewrite_paths_rewrite_path_for_java_and_rust() {
Expand Down
Empty file added test/test/main.rs
Empty file.

0 comments on commit 33ecb24

Please sign in to comment.