|
| 1 | +use std::fs::{copy, read, read_dir}; |
| 2 | +use std::path::{Path, PathBuf}; |
| 3 | + |
| 4 | +use run_make_support::{copy_dir_all, rustdoc, tmp_dir}; |
| 5 | + |
| 6 | +fn generate_docs(out_dir: &Path, json_output: bool) { |
| 7 | + let mut rustdoc = rustdoc(); |
| 8 | + rustdoc.input("src/lib.rs").crate_name("foobar").crate_type("lib").out_dir(&out_dir); |
| 9 | + if json_output { |
| 10 | + rustdoc.arg("-Zunstable-options").output_format("json"); |
| 11 | + } |
| 12 | + rustdoc.run(); |
| 13 | +} |
| 14 | + |
| 15 | +fn read_file(path: PathBuf) -> Vec<u8> { |
| 16 | + match read(&path) { |
| 17 | + Ok(c) => c, |
| 18 | + Err(e) => panic!("Failed to read `{}`: {:?}", path.display(), e), |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +fn recursive_diff(dir1: &Path, dir2: &Path) { |
| 23 | + for entry in read_dir(dir1).unwrap() { |
| 24 | + let entry = entry.unwrap(); |
| 25 | + let entry_name = entry.file_name(); |
| 26 | + let path = entry.path(); |
| 27 | + |
| 28 | + if path.is_dir() { |
| 29 | + recursive_diff(&path, &dir2.join(entry_name)); |
| 30 | + } else { |
| 31 | + let file1 = read_file(path); |
| 32 | + let file2 = read_file(dir2.join(entry_name)); |
| 33 | + |
| 34 | + assert!(file1 == file2); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +fn main() { |
| 40 | + let out_dir = tmp_dir().join("rustdoc"); |
| 41 | + let tmp_out_dir = tmp_dir().join("tmp-rustdoc"); |
| 42 | + |
| 43 | + // Generate HTML docs. |
| 44 | + generate_docs(&out_dir, false); |
| 45 | + |
| 46 | + // Copy first output for to check if it's exactly same after second compilation. |
| 47 | + copy_dir_all(&out_dir, &tmp_out_dir).unwrap(); |
| 48 | + |
| 49 | + // Generate html docs once again on same output. |
| 50 | + generate_docs(&out_dir, false); |
| 51 | + |
| 52 | + // Generate json doc on the same output. |
| 53 | + generate_docs(&out_dir, true); |
| 54 | + |
| 55 | + // Check if expected json file is generated. |
| 56 | + assert!(out_dir.join("foobar.json").is_file()); |
| 57 | + |
| 58 | + // Copy first json output to check if it's exactly same after second compilation. |
| 59 | + copy(out_dir.join("foobar.json"), tmp_out_dir.join("foobar.json")).unwrap(); |
| 60 | + |
| 61 | + // Generate json doc on the same output. |
| 62 | + generate_docs(&out_dir, true); |
| 63 | + |
| 64 | + // Check if all docs(including both json and html formats) are still the same after multiple |
| 65 | + // compilations. |
| 66 | + recursive_diff(&out_dir, &tmp_out_dir); |
| 67 | +} |
0 commit comments