Extract a portion of diff writing code to separate function

This commit is contained in:
Nicholas-Baron 2021-10-03 16:53:45 -07:00
parent cb501a6fe6
commit 2a57a46249
2 changed files with 50 additions and 38 deletions

View 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
}

View 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 {