|
1 | 1 | //! Tests for the `cargo doc` command.
|
2 | 2 |
|
| 3 | +use cargo::core::compiler::RustDocFingerprint; |
3 | 4 | use cargo_test_support::paths::CargoPathExt;
|
4 | 5 | use cargo_test_support::registry::Package;
|
5 | 6 | use cargo_test_support::{basic_lib_manifest, basic_manifest, git, project};
|
6 | 7 | use cargo_test_support::{is_nightly, rustc_host};
|
7 |
| -use serde::{Deserialize, Serialize}; |
8 | 8 | use std::fs;
|
9 | 9 | use std::str;
|
10 | 10 |
|
@@ -1641,15 +1641,7 @@ fn crate_versions_flag_is_overridden() {
|
1641 | 1641 | }
|
1642 | 1642 |
|
1643 | 1643 | #[cargo_test]
|
1644 |
| -fn doc_fingerprint_versioning_consistent() { |
1645 |
| - #[derive(Debug, Serialize, Deserialize)] |
1646 |
| - pub struct RustDocFingerprint { |
1647 |
| - rustc_vv: String, |
1648 |
| - } |
1649 |
| - |
1650 |
| - // Test that using different Rustc versions forces a |
1651 |
| - // doc re-compilation producing new html, css & js files. |
1652 |
| - |
| 1644 | +fn doc_fingerprint_is_versioning_consistent() { |
1653 | 1645 | // Random rustc verbose version
|
1654 | 1646 | let old_rustc_verbose_version = format!(
|
1655 | 1647 | "\
|
@@ -1730,3 +1722,95 @@ LLVM version: 9.0
|
1730 | 1722 | (String::from_utf8_lossy(&output.stdout).as_ref())
|
1731 | 1723 | );
|
1732 | 1724 | }
|
| 1725 | + |
| 1726 | +#[cargo_test] |
| 1727 | +fn doc_fingerprint_respects_target_paths() { |
| 1728 | + // Random rustc verbose version |
| 1729 | + let old_rustc_verbose_version = format!( |
| 1730 | + "\ |
| 1731 | +rustc 1.41.1 (f3e1a954d 2020-02-24) |
| 1732 | +binary: rustc |
| 1733 | +commit-hash: f3e1a954d2ead4e2fc197c7da7d71e6c61bad196 |
| 1734 | +commit-date: 2020-02-24 |
| 1735 | +host: {} |
| 1736 | +release: 1.41.1 |
| 1737 | +LLVM version: 9.0 |
| 1738 | +", |
| 1739 | + rustc_host() |
| 1740 | + ); |
| 1741 | + |
| 1742 | + // Create the dummy project. |
| 1743 | + let dummy_project = project() |
| 1744 | + .file( |
| 1745 | + "Cargo.toml", |
| 1746 | + r#" |
| 1747 | + [package] |
| 1748 | + name = "foo" |
| 1749 | + version = "1.2.4" |
| 1750 | + authors = [] |
| 1751 | + "#, |
| 1752 | + ) |
| 1753 | + .file("src/lib.rs", "//! These are the docs!") |
| 1754 | + .build(); |
| 1755 | + |
| 1756 | + dummy_project |
| 1757 | + .cargo("doc --target x86_64-unknown-linux-gnu") |
| 1758 | + .run(); |
| 1759 | + |
| 1760 | + let fingerprint: RustDocFingerprint = |
| 1761 | + serde_json::from_str(&dummy_project.read_file("target/.rustdoc_fingerprint.json")) |
| 1762 | + .expect("JSON Serde fail"); |
| 1763 | + |
| 1764 | + // Check that the fingerprint contains the actual rustc version |
| 1765 | + // which has been used to compile the docs. |
| 1766 | + let output = std::process::Command::new("rustc") |
| 1767 | + .arg("-vV") |
| 1768 | + .output() |
| 1769 | + .expect("Failed to get actual rustc verbose version"); |
| 1770 | + assert_eq!( |
| 1771 | + fingerprint.rustc_vv, |
| 1772 | + (String::from_utf8_lossy(&output.stdout).as_ref()) |
| 1773 | + ); |
| 1774 | + |
| 1775 | + // As the test shows above. Now we have generated the `doc/` folder and inside |
| 1776 | + // the rustdoc fingerprint file is located with the correct rustc version. |
| 1777 | + // So we will remove it and create a new fingerprint with an old rustc version |
| 1778 | + // inside it. We will also place a bogus file inside of the `doc/` folder to ensure |
| 1779 | + // it gets removed as we expect on the next doc compilation. |
| 1780 | + dummy_project.change_file( |
| 1781 | + "target/.rustdoc_fingerprint.json", |
| 1782 | + &old_rustc_verbose_version, |
| 1783 | + ); |
| 1784 | + |
| 1785 | + fs::write( |
| 1786 | + dummy_project |
| 1787 | + .build_dir() |
| 1788 | + .join("x86_64-unknown-linux-gnu/doc/bogus_file"), |
| 1789 | + String::from("This is a bogus file and should be removed!"), |
| 1790 | + ) |
| 1791 | + .expect("Error writing test bogus file"); |
| 1792 | + |
| 1793 | + // Now if we trigger another compilation, since the fingerprint contains an old version |
| 1794 | + // of rustc, cargo should remove the entire `/doc` folder (including the fingerprint) |
| 1795 | + // and generating another one with the actual version. |
| 1796 | + // It should also remove the bogus file we created above. |
| 1797 | + dummy_project |
| 1798 | + .cargo("doc --target x86_64-unknown-linux-gnu") |
| 1799 | + .run(); |
| 1800 | + |
| 1801 | + assert!(!dummy_project |
| 1802 | + .build_dir() |
| 1803 | + .join("x86_64-unknown-linux-gnu/doc/bogus_file") |
| 1804 | + .exists()); |
| 1805 | + |
| 1806 | + let fingerprint: RustDocFingerprint = |
| 1807 | + serde_json::from_str(&dummy_project.read_file("target/.rustdoc_fingerprint.json")) |
| 1808 | + .expect("JSON Serde fail"); |
| 1809 | + |
| 1810 | + // Check that the fingerprint contains the actual rustc version |
| 1811 | + // which has been used to compile the docs. |
| 1812 | + assert_eq!( |
| 1813 | + fingerprint.rustc_vv, |
| 1814 | + (String::from_utf8_lossy(&output.stdout).as_ref()) |
| 1815 | + ); |
| 1816 | +} |
0 commit comments