Skip to content

Commit 2a57a46

Browse files
Extract a portion of diff writing code to separate function
1 parent cb501a6 commit 2a57a46

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

src/tools/compiletest/src/compute_diff.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::VecDeque;
2+
use std::path::Path;
23

34
#[derive(Debug, PartialEq)]
45
pub enum DiffLine {
@@ -104,3 +105,49 @@ pub(crate) fn write_diff(expected: &str, actual: &str, context_size: usize) -> S
104105
}
105106
output
106107
}
108+
109+
/// Returns whether any data was actually written.
110+
pub(crate) fn write_rustdoc_diff(
111+
diff_filename: &str,
112+
out_dir: &Path,
113+
compare_dir: &Path,
114+
verbose: bool,
115+
) -> bool {
116+
use std::fs::File;
117+
use std::io::{Read, Write};
118+
let mut diff_output = File::create(diff_filename).unwrap();
119+
let mut wrote_data = false;
120+
for entry in walkdir::WalkDir::new(out_dir) {
121+
let entry = entry.expect("failed to read file");
122+
let extension = entry.path().extension().and_then(|p| p.to_str());
123+
if entry.file_type().is_file()
124+
&& (extension == Some("html".into()) || extension == Some("js".into()))
125+
{
126+
let expected_path = compare_dir.join(entry.path().strip_prefix(&out_dir).unwrap());
127+
let expected = if let Ok(s) = std::fs::read(&expected_path) { s } else { continue };
128+
let actual_path = entry.path();
129+
let actual = std::fs::read(&actual_path).unwrap();
130+
let diff = unified_diff::diff(
131+
&expected,
132+
&expected_path.to_string_lossy(),
133+
&actual,
134+
&actual_path.to_string_lossy(),
135+
3,
136+
);
137+
wrote_data |= !diff.is_empty();
138+
diff_output.write_all(&diff).unwrap();
139+
}
140+
}
141+
142+
if !wrote_data {
143+
println!("note: diff is identical to nightly rustdoc");
144+
assert!(diff_output.metadata().unwrap().len() == 0);
145+
return false;
146+
} else if verbose {
147+
eprintln!("printing diff:");
148+
let mut buf = Vec::new();
149+
diff_output.read_to_end(&mut buf).unwrap();
150+
std::io::stderr().lock().write_all(&mut buf).unwrap();
151+
}
152+
true
153+
}

src/tools/compiletest/src/runtest.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::common::{CompareMode, FailMode, PassMode};
88
use crate::common::{Config, TestPaths};
99
use crate::common::{Pretty, RunPassValgrind};
1010
use crate::common::{UI_RUN_STDERR, UI_RUN_STDOUT};
11-
use crate::compute_diff::write_diff;
11+
use crate::compute_diff::{write_diff, write_rustdoc_diff};
1212
use crate::errors::{self, Error, ErrorKind};
1313
use crate::header::TestProps;
1414
use crate::json;
@@ -2403,43 +2403,8 @@ impl<'test> TestCx<'test> {
24032403

24042404
let diff_filename = format!("build/tmp/rustdoc-compare-{}.diff", std::process::id());
24052405

2406-
{
2407-
let mut diff_output = File::create(&diff_filename).unwrap();
2408-
let mut wrote_data = false;
2409-
for entry in walkdir::WalkDir::new(out_dir) {
2410-
let entry = entry.expect("failed to read file");
2411-
let extension = entry.path().extension().and_then(|p| p.to_str());
2412-
if entry.file_type().is_file()
2413-
&& (extension == Some("html".into()) || extension == Some("js".into()))
2414-
{
2415-
let expected_path =
2416-
compare_dir.join(entry.path().strip_prefix(&out_dir).unwrap());
2417-
let expected =
2418-
if let Ok(s) = std::fs::read(&expected_path) { s } else { continue };
2419-
let actual_path = entry.path();
2420-
let actual = std::fs::read(&actual_path).unwrap();
2421-
let diff = unified_diff::diff(
2422-
&expected,
2423-
&expected_path.to_string_lossy(),
2424-
&actual,
2425-
&actual_path.to_string_lossy(),
2426-
3,
2427-
);
2428-
wrote_data |= !diff.is_empty();
2429-
diff_output.write_all(&diff).unwrap();
2430-
}
2431-
}
2432-
2433-
if !wrote_data {
2434-
println!("note: diff is identical to nightly rustdoc");
2435-
assert!(diff_output.metadata().unwrap().len() == 0);
2436-
return;
2437-
} else if self.config.verbose {
2438-
eprintln!("printing diff:");
2439-
let mut buf = Vec::new();
2440-
diff_output.read_to_end(&mut buf).unwrap();
2441-
std::io::stderr().lock().write_all(&mut buf).unwrap();
2442-
}
2406+
if !write_rustdoc_diff(&diff_filename, out_dir, &compare_dir, self.config.verbose) {
2407+
return;
24432408
}
24442409

24452410
match self.config.color {

0 commit comments

Comments
 (0)