about summary refs log tree commit diff
path: root/src/tools/compiletest
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2023-08-16 12:42:33 +1000
committerZalathar <Zalathar@users.noreply.github.com>2023-08-17 10:03:32 +1000
commitbfb16545a3bbb602756710000acac3ac8acd5186 (patch)
treefe9bf9c525d1d09cd0359e3790ee0f528bba17c2 /src/tools/compiletest
parent07438b0928c6691d6ee734a5a77823ec143be94d (diff)
coverage: Anonymize line numbers in `run-coverage` test snapshots
This makes the test snapshots less sensitive to lines being added/removed.
Diffstat (limited to 'src/tools/compiletest')
-rw-r--r--src/tools/compiletest/src/runtest.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs
index 3d237eb25cc..4ef79af3124 100644
--- a/src/tools/compiletest/src/runtest.rs
+++ b/src/tools/compiletest/src/runtest.rs
@@ -18,6 +18,7 @@ use crate::ColorConfig;
 use regex::{Captures, Regex};
 use rustfix::{apply_suggestions, get_suggestions_from_json, Filter};
 
+use std::borrow::Cow;
 use std::collections::hash_map::DefaultHasher;
 use std::collections::{HashMap, HashSet};
 use std::env;
@@ -664,6 +665,7 @@ impl<'test> TestCx<'test> {
 
     fn normalize_coverage_output(&self, coverage: &str) -> Result<String, String> {
         let normalized = self.normalize_output(coverage, &[]);
+        let normalized = Self::anonymize_coverage_line_numbers(&normalized);
 
         let mut lines = normalized.lines().collect::<Vec<_>>();
 
@@ -674,6 +676,21 @@ impl<'test> TestCx<'test> {
         Ok(joined_lines)
     }
 
+    /// Replace line numbers in coverage reports with the placeholder `LL`,
+    /// so that the tests are less sensitive to lines being added/removed.
+    fn anonymize_coverage_line_numbers(coverage: &str) -> Cow<'_, str> {
+        // The coverage reporter prints line numbers at the start of a line.
+        // They are truncated or left-padded to occupy exactly 5 columns.
+        // (`LineNumberColumnWidth` in `SourceCoverageViewText.cpp`.)
+        // A pipe character `|` appears immediately after the final digit.
+        //
+        // Line numbers that appear inside expansion/instantiation subviews
+        // have an additional prefix of `  |` for each nesting level.
+        static LINE_NUMBER_RE: Lazy<Regex> =
+            Lazy::new(|| Regex::new(r"(?m:^)(?<prefix>(?:  \|)*) *[0-9]+\|").unwrap());
+        LINE_NUMBER_RE.replace_all(coverage, "$prefix   LL|")
+    }
+
     /// Coverage reports can describe multiple source files, separated by
     /// blank lines. The order of these files is unpredictable (since it
     /// depends on implementation details), so we need to sort the file