From 33ecb244787c97532d3ee65ddcb5c435512efcd4 Mon Sep 17 00:00:00 2001 From: Vitor Enes Date: Wed, 16 Dec 2020 23:01:16 +0000 Subject: [PATCH] Do not ignore subfolder with name equal to root. (#546) Before this commit, a subfolder with the same name as the root/source folder would be ignored from the coverage reports (see https://github.com/mozilla/grcov/issues/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. --- src/path_rewriting.rs | 64 +++++++++++++++++++++++++++++++++++++++++-- test/test/main.rs | 0 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 test/test/main.rs diff --git a/src/path_rewriting.rs b/src/path_rewriting.rs index a67b0d0c4..15c551562 100644 --- a/src/path_rewriting.rs +++ b/src/path_rewriting.rs @@ -91,14 +91,20 @@ fn apply_mapping(mapping: &Option, 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. @@ -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() { diff --git a/test/test/main.rs b/test/test/main.rs new file mode 100644 index 000000000..e69de29bb