From 2a57a462498e3ce954e0b5b7ddd2502bd74875f4 Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Sun, 3 Oct 2021 16:53:45 -0700 Subject: [PATCH] Extract a portion of diff writing code to separate function --- src/tools/compiletest/src/compute_diff.rs | 47 +++++++++++++++++++++++ src/tools/compiletest/src/runtest.rs | 41 ++------------------ 2 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/tools/compiletest/src/compute_diff.rs b/src/tools/compiletest/src/compute_diff.rs index 9ca0c69dbd3..fb837a6077a 100644 --- a/src/tools/compiletest/src/compute_diff.rs +++ b/src/tools/compiletest/src/compute_diff.rs @@ -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 +} diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index ec6c745b7d2..ffe8f1bb56d 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -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 { -- 2.44.0