Skip to content

Commit

Permalink
Make write_rustdoc_diff a more generic function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas-Baron committed Oct 4, 2021
1 parent 2a57a46 commit 3760c91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/tools/compiletest/src/compute_diff.rs
@@ -1,4 +1,5 @@
use std::collections::VecDeque;
use std::fs::{File, FileType};
use std::path::Path;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 11 additions & 2 deletions src/tools/compiletest/src/runtest.rs
Expand Up @@ -8,7 +8,7 @@ use crate::common::{CompareMode, FailMode, PassMode};
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;
Expand Down Expand Up @@ -2403,7 +2403,16 @@ impl<'test> TestCx<'test> {

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

Expand Down

0 comments on commit 3760c91

Please sign in to comment.