]> git.lizzy.rs Git - rust.git/commitdiff
Extract a portion of diff writing code to separate function
authorNicholas-Baron <nicholas.baron.ten@gmail.com>
Sun, 3 Oct 2021 23:53:45 +0000 (16:53 -0700)
committerNicholas-Baron <nicholas.baron.ten@gmail.com>
Sun, 3 Oct 2021 23:53:45 +0000 (16:53 -0700)
src/tools/compiletest/src/compute_diff.rs
src/tools/compiletest/src/runtest.rs

index 9ca0c69dbd3e38433021002fa13c89559087c18a..fb837a6077a7f8cb2cd6e0bf5846ff7658b9f909 100644 (file)
@@ -1,4 +1,5 @@
 use std::collections::VecDeque;
+use std::path::Path;
 
 #[derive(Debug, PartialEq)]
 pub enum DiffLine {
@@ -104,3 +105,49 @@ pub(crate) fn write_diff(expected: &str, actual: &str, context_size: usize) -> S
     }
     output
 }
+
+/// Returns whether any data was actually written.
+pub(crate) fn write_rustdoc_diff(
+    diff_filename: &str,
+    out_dir: &Path,
+    compare_dir: &Path,
+    verbose: bool,
+) -> bool {
+    use std::fs::File;
+    use std::io::{Read, Write};
+    let mut diff_output = File::create(diff_filename).unwrap();
+    let mut wrote_data = false;
+    for entry in walkdir::WalkDir::new(out_dir) {
+        let entry = entry.expect("failed to read file");
+        let extension = entry.path().extension().and_then(|p| p.to_str());
+        if entry.file_type().is_file()
+            && (extension == Some("html".into()) || extension == Some("js".into()))
+        {
+            let expected_path = compare_dir.join(entry.path().strip_prefix(&out_dir).unwrap());
+            let expected = if let Ok(s) = std::fs::read(&expected_path) { s } else { continue };
+            let actual_path = entry.path();
+            let actual = std::fs::read(&actual_path).unwrap();
+            let diff = unified_diff::diff(
+                &expected,
+                &expected_path.to_string_lossy(),
+                &actual,
+                &actual_path.to_string_lossy(),
+                3,
+            );
+            wrote_data |= !diff.is_empty();
+            diff_output.write_all(&diff).unwrap();
+        }
+    }
+
+    if !wrote_data {
+        println!("note: diff is identical to nightly rustdoc");
+        assert!(diff_output.metadata().unwrap().len() == 0);
+        return false;
+    } else if verbose {
+        eprintln!("printing diff:");
+        let mut buf = Vec::new();
+        diff_output.read_to_end(&mut buf).unwrap();
+        std::io::stderr().lock().write_all(&mut buf).unwrap();
+    }
+    true
+}
index ec6c745b7d202ae713723cea0cf077a37e7caa99..ffe8f1bb56dfe9f688197be8638c67dde92621ea 100644 (file)
@@ -8,7 +8,7 @@
 use crate::common::{Config, TestPaths};
 use crate::common::{Pretty, RunPassValgrind};
 use crate::common::{UI_RUN_STDERR, UI_RUN_STDOUT};
-use crate::compute_diff::write_diff;
+use crate::compute_diff::{write_diff, write_rustdoc_diff};
 use crate::errors::{self, Error, ErrorKind};
 use crate::header::TestProps;
 use crate::json;
@@ -2403,43 +2403,8 @@ fn compare_to_default_rustdoc(&mut self, out_dir: &Path) {
 
         let diff_filename = format!("build/tmp/rustdoc-compare-{}.diff", std::process::id());
 
-        {
-            let mut diff_output = File::create(&diff_filename).unwrap();
-            let mut wrote_data = false;
-            for entry in walkdir::WalkDir::new(out_dir) {
-                let entry = entry.expect("failed to read file");
-                let extension = entry.path().extension().and_then(|p| p.to_str());
-                if entry.file_type().is_file()
-                    && (extension == Some("html".into()) || extension == Some("js".into()))
-                {
-                    let expected_path =
-                        compare_dir.join(entry.path().strip_prefix(&out_dir).unwrap());
-                    let expected =
-                        if let Ok(s) = std::fs::read(&expected_path) { s } else { continue };
-                    let actual_path = entry.path();
-                    let actual = std::fs::read(&actual_path).unwrap();
-                    let diff = unified_diff::diff(
-                        &expected,
-                        &expected_path.to_string_lossy(),
-                        &actual,
-                        &actual_path.to_string_lossy(),
-                        3,
-                    );
-                    wrote_data |= !diff.is_empty();
-                    diff_output.write_all(&diff).unwrap();
-                }
-            }
-
-            if !wrote_data {
-                println!("note: diff is identical to nightly rustdoc");
-                assert!(diff_output.metadata().unwrap().len() == 0);
-                return;
-            } else if self.config.verbose {
-                eprintln!("printing diff:");
-                let mut buf = Vec::new();
-                diff_output.read_to_end(&mut buf).unwrap();
-                std::io::stderr().lock().write_all(&mut buf).unwrap();
-            }
+        if !write_rustdoc_diff(&diff_filename, out_dir, &compare_dir, self.config.verbose) {
+            return;
         }
 
         match self.config.color {