Add tests for rustdoc fingerprint impl

- Check wether the fingerprint is created with the
correct data after a `rustdoc` execution.
- Check wether the previous docs are removed if the version
used to compile them reported in the fingerprint is different
from the actual one being used by the compiler.
This commit is contained in:
CPerezz 2020-12-16 09:24:59 +01:00
parent 65f17e1ca6
commit 2fea14e449
No known key found for this signature in database
GPG key ID: 6EE573EDC452F806

View file

@ -1554,72 +1554,65 @@ fn crate_versions_flag_is_overridden() {
}
#[cargo_test]
fn doc_versioning_works() {
fn rustc_adds_doc_fingerprint() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
authors = []
"#,
)
.file("src/lib.rs", "//! Hello")
.build();
p.cargo("doc").run();
// Read fingerprint rustc version and check it matches the expected one
let mut fingerprint = fs::File::open(
p.root()
.join("target")
.join("doc")
.join(".rustdoc_fingerprint.json"),
)
.expect("Failed to read fingerprint");
let mut rustc_verbose_version = String::new();
fingerprint
.read_to_string(&mut rustc_verbose_version)
.expect("Error reading the fingerprint contents");
let output = std::process::Command::new("rustc")
.arg("-vV")
.stdout(std::process::Stdio::inherit())
.output()
.expect("Failed to get actual rustc verbose version");
assert!(rustc_verbose_version
.as_str()
.contains(String::from_utf8_lossy(&output.stdout).as_ref()));
}
#[cargo_test]
fn doc_fingerprint_versioning_consistent() {
// Test that using different Rustc versions forces a
// doc re-compilation producing new html, css & js files.
// Create separate rustc binaries to emulate running different toolchains.
let nightly1 = format!(
// Random rustc verbose version
let stable_rustc_verbose_version = format!(
"\
rustc 1.44.0-nightly (38114ff16 2020-03-21)
rustc 1.41.1 (f3e1a954d 2020-02-24)
binary: rustc
commit-hash: 38114ff16e7856f98b2b4be7ab4cd29b38bed59a
commit-date: 2020-03-21
commit-hash: f3e1a954d2ead4e2fc197c7da7d71e6c61bad196
commit-date: 2020-02-24
host: {}
release: 1.44.0-nightly
release: 1.41.1
LLVM version: 9.0
",
rustc_host()
);
let nightly2 = format!(
"\
rustc 1.44.0-nightly (a5b09d354 2020-03-31)
binary: rustc
commit-hash: a5b09d35473615e7142f5570f5c5fad0caf68bd2
commit-date: 2020-03-31
host: {}
release: 1.44.0-nightly
LLVM version: 9.0
",
rustc_host()
);
let compiler = project()
.at("compiler")
.file("Cargo.toml", &basic_manifest("compiler", "0.1.0"))
.file(
"src/main.rs",
r#"
fn main() {
if std::env::args_os().any(|a| a == "-vV") {
print!("{}", env!("FUNKY_VERSION_TEST"));
return;
}
let mut cmd = std::process::Command::new("rustc");
cmd.args(std::env::args_os().skip(1));
assert!(cmd.status().unwrap().success());
}
"#,
)
.build();
let makeit = |version, vv| {
// Force a rebuild.
compiler.target_debug_dir().join("deps").rm_rf();
compiler.cargo("build").env("FUNKY_VERSION_TEST", vv).run();
fs::rename(compiler.bin("compiler"), compiler.bin(version)).unwrap();
};
makeit("nightly1", nightly1);
let get_fingerprint_v = |target_path: &std::path::Path| -> String {
let mut contents = Vec::new();
let mut fingerp_file = std::fs::File::open(target_path).unwrap();
fingerp_file.read(&mut contents).unwrap();
String::from_utf8(contents).unwrap()
};
// Create the dummy project and compile it in "nightly1"
// Create the dummy project.
let dummy_project = project()
.file(
"Cargo.toml",
@ -1633,25 +1626,60 @@ LLVM version: 9.0
.file("src/lib.rs", "//! These are the docs!")
.build();
// Build should have created the `.rustdoc_fingerprint.json` under `target/`
assert!(fs::File::open(dummy_project.build_dir().join(".rustdoc_fingerprint.json")).is_ok());
// The file should contain the `nightly1` version of Rustc
assert_eq!(get_fingerprint_v(&dummy_project.build_dir()), "nightly1");
// Compiling the project with "nightly2" should remove the /doc folder and re-build
// the docs. Therefore, we place a bogus file to check that this happens.
makeit("nightly2", nightly2);
// Add a bogus_file to check that indeed the /doc folder will be removed due to
// the change of toolchain version.
fs::File::create(dummy_project.build_dir().join("doc/bogus_file.json")).unwrap();
// Re-document the crate
dummy_project
.cargo("doc --no-deps")
.exec_with_output()
.unwrap();
// Build should have removed /doc and therefore bogus_file.json
assert!(fs::File::open(dummy_project.target_debug_dir().join("doc/bogus_file.json")).is_err());
// Build should have created the `.rustdoc_fingerprint.json` under `target/`
assert!(fs::File::open(dummy_project.build_dir().join(".rustdoc_fingerprint.json")).is_ok());
// The file should contain the `nightly2` version of Rustc
assert_eq!(get_fingerprint_v(&dummy_project.build_dir()), "nightly2");
dummy_project.cargo("doc").run();
// As the test shows avobe. Now we have generated the `doc/` folder and inside
// the rustdoc fingerprint file is located.
// So we will remove it and create a new fingerprint with an old rustc version
// inside it.
fs::remove_file(
dummy_project
.root()
.join("target")
.join("doc")
.join(".rustdoc_fingerprint.json"),
)
.expect("Failed to read fingerprint on tests");
fs::write(
dummy_project
.root()
.join("target")
.join("doc")
.join(".rustdoc_fingerprint.json"),
stable_rustc_verbose_version,
)
.expect("Error writing old rustc version");
// Now if we trigger another compilation, since the fingerprint contains an old version
// of rustc, cargo should remove the entire `/doc` folder (including the fingerprint)
// and generating another one with the actual version.
dummy_project.cargo("doc").run();
// Edit the fingerprint rustc version.
// Read fingerprint rustc version and return it as String
let get_fingerprint_version = |root_path: std::path::PathBuf| -> String {
let mut fingerprint = fs::File::open(
root_path
.join("target")
.join("doc")
.join(".rustdoc_fingerprint.json"),
)
.expect("Failed to read fingerprint on tests");
let mut rustc_verbose_version = String::new();
fingerprint
.read_to_string(&mut rustc_verbose_version)
.expect("Error reading the fingerprint contents");
rustc_verbose_version
};
let output = std::process::Command::new("rustc")
.arg("-vV")
.stdout(std::process::Stdio::inherit())
.output()
.expect("Failed to get actual rustc verbose version");
assert!(get_fingerprint_version(dummy_project.root())
.as_str()
.contains(String::from_utf8_lossy(&output.stdout).as_ref()));
}