Skip to content

Commit e637a95

Browse files
authored
Unrolled build for rust-lang#125542
Rollup merge of rust-lang#125542 - GuillaumeGomez:migrate-rustdoc-verify-output-files, r=jieyouxu Migrate rustdoc verify output files Part of rust-lang#121876. r? ```@jieyouxu```
2 parents f00b02e + bdf3864 commit e637a95

File tree

6 files changed

+127
-34
lines changed

6 files changed

+127
-34
lines changed

src/tools/run-make-support/src/diff/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ impl Diff {
5151
/// Specify the actual output for the diff from a file.
5252
pub fn actual_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
5353
let path = path.as_ref();
54-
let content = std::fs::read_to_string(path).expect("failed to read file");
54+
let content = match std::fs::read_to_string(path) {
55+
Ok(c) => c,
56+
Err(e) => panic!("failed to read `{}`: {:?}", path.display(), e),
57+
};
5558
let name = path.to_string_lossy().to_string();
5659

5760
self.actual = Some(content);

src/tools/run-make-support/src/lib.rs

+67
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub mod rustc;
1212
pub mod rustdoc;
1313

1414
use std::env;
15+
use std::fs;
16+
use std::io;
1517
use std::path::{Path, PathBuf};
1618
use std::process::{Command, Output};
1719

@@ -201,6 +203,71 @@ pub fn set_host_rpath(cmd: &mut Command) {
201203
});
202204
}
203205

206+
/// Copy a directory into another.
207+
pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
208+
fn copy_dir_all_inner(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
209+
let dst = dst.as_ref();
210+
if !dst.is_dir() {
211+
fs::create_dir_all(&dst)?;
212+
}
213+
for entry in fs::read_dir(src)? {
214+
let entry = entry?;
215+
let ty = entry.file_type()?;
216+
if ty.is_dir() {
217+
copy_dir_all_inner(entry.path(), dst.join(entry.file_name()))?;
218+
} else {
219+
fs::copy(entry.path(), dst.join(entry.file_name()))?;
220+
}
221+
}
222+
Ok(())
223+
}
224+
225+
if let Err(e) = copy_dir_all_inner(&src, &dst) {
226+
// Trying to give more context about what exactly caused the failure
227+
panic!(
228+
"failed to copy `{}` to `{}`: {:?}",
229+
src.as_ref().display(),
230+
dst.as_ref().display(),
231+
e
232+
);
233+
}
234+
}
235+
236+
/// Check that all files in `dir1` exist and have the same content in `dir2`. Panic otherwise.
237+
pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
238+
fn read_file(path: &Path) -> Vec<u8> {
239+
match fs::read(path) {
240+
Ok(c) => c,
241+
Err(e) => panic!("Failed to read `{}`: {:?}", path.display(), e),
242+
}
243+
}
244+
245+
let dir2 = dir2.as_ref();
246+
for entry in fs::read_dir(dir1).unwrap() {
247+
let entry = entry.unwrap();
248+
let entry_name = entry.file_name();
249+
let path = entry.path();
250+
251+
if path.is_dir() {
252+
recursive_diff(&path, &dir2.join(entry_name));
253+
} else {
254+
let path2 = dir2.join(entry_name);
255+
let file1 = read_file(&path);
256+
let file2 = read_file(&path2);
257+
258+
// We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display.
259+
// Why not using String? Because there might be minified files or even potentially
260+
// binary ones, so that would display useless output.
261+
assert!(
262+
file1 == file2,
263+
"`{}` and `{}` have different content",
264+
path.display(),
265+
path2.display(),
266+
);
267+
}
268+
}
269+
}
270+
204271
/// Implement common helpers for command wrappers. This assumes that the command wrapper is a struct
205272
/// containing a `cmd: Command` field and a `output` function. The provided helpers are:
206273
///

src/tools/run-make-support/src/rustdoc.rs

+7
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ impl Rustdoc {
151151
self
152152
}
153153

154+
/// Specify the output format.
155+
pub fn output_format(&mut self, format: &str) -> &mut Self {
156+
self.cmd.arg("--output-format");
157+
self.cmd.arg(format);
158+
self
159+
}
160+
154161
#[track_caller]
155162
pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
156163
let caller_location = std::panic::Location::caller();

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ run-make/rlib-format-packed-bundled-libs/Makefile
226226
run-make/rmeta-preferred/Makefile
227227
run-make/rustc-macro-dep-files/Makefile
228228
run-make/rustdoc-io-error/Makefile
229-
run-make/rustdoc-verify-output-files/Makefile
230229
run-make/sanitizer-cdylib-link/Makefile
231230
run-make/sanitizer-dylib-link/Makefile
232231
run-make/sanitizer-staticlib-link/Makefile

tests/run-make/rustdoc-verify-output-files/Makefile

-32
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::fs::copy;
2+
use std::path::{Path, PathBuf};
3+
4+
use run_make_support::{copy_dir_all, recursive_diff, rustdoc, tmp_dir};
5+
6+
#[derive(PartialEq)]
7+
enum JsonOutput {
8+
Yes,
9+
No,
10+
}
11+
12+
fn generate_docs(out_dir: &Path, json_output: JsonOutput) {
13+
let mut rustdoc = rustdoc();
14+
rustdoc.input("src/lib.rs").crate_name("foobar").crate_type("lib").out_dir(&out_dir);
15+
if json_output == JsonOutput::Yes {
16+
rustdoc.arg("-Zunstable-options").output_format("json");
17+
}
18+
rustdoc.run();
19+
}
20+
21+
fn main() {
22+
let out_dir = tmp_dir().join("rustdoc");
23+
let tmp_out_dir = tmp_dir().join("tmp-rustdoc");
24+
25+
// Generate HTML docs.
26+
generate_docs(&out_dir, JsonOutput::No);
27+
28+
// Copy first output for to check if it's exactly same after second compilation.
29+
copy_dir_all(&out_dir, &tmp_out_dir);
30+
31+
// Generate html docs once again on same output.
32+
generate_docs(&out_dir, JsonOutput::No);
33+
34+
// Generate json doc on the same output.
35+
generate_docs(&out_dir, JsonOutput::Yes);
36+
37+
// Check if expected json file is generated.
38+
assert!(out_dir.join("foobar.json").is_file());
39+
40+
// Copy first json output to check if it's exactly same after second compilation.
41+
copy(out_dir.join("foobar.json"), tmp_out_dir.join("foobar.json")).unwrap();
42+
43+
// Generate json doc on the same output.
44+
generate_docs(&out_dir, JsonOutput::Yes);
45+
46+
// Check if all docs(including both json and html formats) are still the same after multiple
47+
// compilations.
48+
recursive_diff(&out_dir, &tmp_out_dir);
49+
}

0 commit comments

Comments
 (0)