Make write_rustdoc_diff a more generic function

This commit is contained in:
Nicholas-Baron 2021-10-03 17:07:43 -07:00
parent 2a57a46249
commit 3760c91252
2 changed files with 21 additions and 8 deletions

View File

@ -1,4 +1,5 @@
use std::collections::VecDeque;
use std::fs::{File, FileType};
use std::path::Path;
#[derive(Debug, PartialEq)]
@ -106,23 +107,26 @@ pub(crate) fn write_diff(expected: &str, actual: &str, context_size: usize) -> S
output
}
/// Filters based on filetype and extension whether to diff a file.
///
/// Returns whether any data was actually written.
pub(crate) fn write_rustdoc_diff(
pub(crate) fn write_filtered_diff<Filter>(
diff_filename: &str,
out_dir: &Path,
compare_dir: &Path,
verbose: bool,
) -> bool {
use std::fs::File;
filter: Filter,
) -> bool
where
Filter: Fn(FileType, Option<&str>) -> bool,
{
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()))
{
if filter(entry.file_type(), extension) {
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();

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, write_rustdoc_diff};
use crate::compute_diff::{write_diff, write_filtered_diff};
use crate::errors::{self, Error, ErrorKind};
use crate::header::TestProps;
use crate::json;
@ -2403,7 +2403,16 @@ fn compare_to_default_rustdoc(&mut self, out_dir: &Path) {
let diff_filename = format!("build/tmp/rustdoc-compare-{}.diff", std::process::id());
if !write_rustdoc_diff(&diff_filename, out_dir, &compare_dir, self.config.verbose) {
if !write_filtered_diff(
&diff_filename,
out_dir,
&compare_dir,
self.config.verbose,
|file_type, extension| {
file_type.is_file()
&& (extension == Some("html".into()) || extension == Some("js".into()))
},
) {
return;
}