about summary refs log tree commit diff
path: root/src/tools/compiletest
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2017-07-06 13:54:58 +0800
committerkennytm <kennytm@gmail.com>2017-07-11 16:57:52 +0800
commit4582ecda64f6f9173281390ef5698fac29dd6264 (patch)
tree3dde868a179fef84c7b28db8284f80051b92c95e /src/tools/compiletest
parent18712e6edf685855ce0e0470730192165e56e0b1 (diff)
downloadrust-4582ecda64f6f9173281390ef5698fac29dd6264.tar.gz
rust-4582ecda64f6f9173281390ef5698fac29dd6264.zip
compiletest: Support custom normalization rules.
Diffstat (limited to 'src/tools/compiletest')
-rw-r--r--src/tools/compiletest/src/header.rs55
-rw-r--r--src/tools/compiletest/src/runtest.rs16
2 files changed, 65 insertions, 6 deletions
diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs
index 59d08ceae21..bb9bf57d55e 100644
--- a/src/tools/compiletest/src/header.rs
+++ b/src/tools/compiletest/src/header.rs
@@ -211,6 +211,9 @@ pub struct TestProps {
     // The test must be compiled and run successfully. Only used in UI tests for
     // now.
     pub run_pass: bool,
+    // customized normalization rules
+    pub normalize_stdout: Vec<(String, String)>,
+    pub normalize_stderr: Vec<(String, String)>,
 }
 
 impl TestProps {
@@ -237,6 +240,8 @@ impl TestProps {
             must_compile_successfully: false,
             check_test_line_numbers_match: false,
             run_pass: false,
+            normalize_stdout: vec![],
+            normalize_stderr: vec![],
         }
     }
 
@@ -351,6 +356,13 @@ impl TestProps {
             if !self.run_pass {
                 self.run_pass = config.parse_run_pass(ln);
             }
+
+            if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stdout") {
+                self.normalize_stdout.push(rule);
+            }
+            if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stderr") {
+                self.normalize_stderr.push(rule);
+            }
         });
 
         for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
@@ -399,7 +411,6 @@ fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut FnMut(&str)) {
 }
 
 impl Config {
-
     fn parse_error_pattern(&self, line: &str) -> Option<String> {
         self.parse_name_value_directive(line, "error-pattern")
     }
@@ -497,6 +508,22 @@ impl Config {
         }
     }
 
+    fn parse_custom_normalization(&self, mut line: &str, prefix: &str) -> Option<(String, String)> {
+        if self.parse_cfg_name_directive(line, prefix) {
+            let from = match parse_normalization_string(&mut line) {
+                Some(s) => s,
+                None => return None,
+            };
+            let to = match parse_normalization_string(&mut line) {
+                Some(s) => s,
+                None => return None,
+            };
+            Some((from, to))
+        } else {
+            None
+        }
+    }
+
     /// Parses a name-value directive which contains config-specific information, e.g. `ignore-x86`
     /// or `normalize-stderr-32bit`. Returns `true` if the line matches it.
     fn parse_cfg_name_directive(&self, line: &str, prefix: &str) -> bool {
@@ -568,3 +595,29 @@ fn expand_variables(mut value: String, config: &Config) -> String {
 
     value
 }
+
+/// Finds the next quoted string `"..."` in `line`, and extract the content from it. Move the `line`
+/// variable after the end of the quoted string.
+///
+/// # Examples
+///
+/// ```
+/// let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\".";
+/// let first = parse_normalization_string(&mut s);
+/// assert_eq!(first, Some("something (32 bits)".to_owned()));
+/// assert_eq!(s, " -> \"something ($WORD bits)\".");
+/// ```
+fn parse_normalization_string(line: &mut &str) -> Option<String> {
+    // FIXME support escapes in strings.
+    let begin = match line.find('"') {
+        Some(i) => i + 1,
+        None => return None,
+    };
+    let end = match line[begin..].find('"') {
+        Some(i) => i + begin,
+        None => return None,
+    };
+    let result = line[begin..end].to_owned();
+    *line = &line[end+1..];
+    Some(result)
+}
diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs
index 2254d6d23a8..45a733d411a 100644
--- a/src/tools/compiletest/src/runtest.rs
+++ b/src/tools/compiletest/src/runtest.rs
@@ -2228,8 +2228,10 @@ actual:\n\
         let expected_stdout_path = self.expected_output_path("stdout");
         let expected_stdout = self.load_expected_output(&expected_stdout_path);
 
-        let normalized_stdout = self.normalize_output(&proc_res.stdout);
-        let normalized_stderr = self.normalize_output(&proc_res.stderr);
+        let normalized_stdout =
+            self.normalize_output(&proc_res.stdout, &self.props.normalize_stdout);
+        let normalized_stderr =
+            self.normalize_output(&proc_res.stderr, &self.props.normalize_stderr);
 
         let mut errors = 0;
         errors += self.compare_output("stdout", &normalized_stdout, &expected_stdout);
@@ -2375,13 +2377,17 @@ actual:\n\
         mir_dump_dir
     }
 
-    fn normalize_output(&self, output: &str) -> String {
+    fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String {
         let parent_dir = self.testpaths.file.parent().unwrap();
         let parent_dir_str = parent_dir.display().to_string();
-        output.replace(&parent_dir_str, "$DIR")
+        let mut normalized = output.replace(&parent_dir_str, "$DIR")
               .replace("\\", "/") // normalize for paths on windows
               .replace("\r\n", "\n") // normalize for linebreaks on windows
-              .replace("\t", "\\t") // makes tabs visible
+              .replace("\t", "\\t"); // makes tabs visible
+        for rule in custom_rules {
+            normalized = normalized.replace(&rule.0, &rule.1);
+        }
+        normalized
     }
 
     fn expected_output_path(&self, kind: &str) -> PathBuf {