|
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};
|
@@ -1638,3 +1639,179 @@ fn crate_versions_flag_is_overridden() {
|
1638 | 1639 | p.cargo("rustdoc -- --crate-version 2.0.3").run();
|
1639 | 1640 | asserts(output_documentation());
|
1640 | 1641 | }
|
| 1642 | + |
| 1643 | +#[cargo_test] |
| 1644 | +fn doc_fingerprint_is_versioning_consistent() { |
| 1645 | + // Random rustc verbose version |
| 1646 | + let old_rustc_verbose_version = format!( |
| 1647 | + "\ |
| 1648 | +rustc 1.41.1 (f3e1a954d 2020-02-24) |
| 1649 | +binary: rustc |
| 1650 | +commit-hash: f3e1a954d2ead4e2fc197c7da7d71e6c61bad196 |
| 1651 | +commit-date: 2020-02-24 |
| 1652 | +host: {} |
| 1653 | +release: 1.41.1 |
| 1654 | +LLVM version: 9.0 |
| 1655 | +", |
| 1656 | + rustc_host() |
| 1657 | + ); |
| 1658 | + |
| 1659 | + // Create the dummy project. |
| 1660 | + let dummy_project = project() |
| 1661 | + .file( |
| 1662 | + "Cargo.toml", |
| 1663 | + r#" |
| 1664 | + [package] |
| 1665 | + name = "foo" |
| 1666 | + version = "1.2.4" |
| 1667 | + authors = [] |
| 1668 | + "#, |
| 1669 | + ) |
| 1670 | + .file("src/lib.rs", "//! These are the docs!") |
| 1671 | + .build(); |
| 1672 | + |
| 1673 | + dummy_project.cargo("doc").run(); |
| 1674 | + |
| 1675 | + let fingerprint: RustDocFingerprint = |
| 1676 | + serde_json::from_str(&dummy_project.read_file("target/.rustdoc_fingerprint.json")) |
| 1677 | + .expect("JSON Serde fail"); |
| 1678 | + |
| 1679 | + // Check that the fingerprint contains the actual rustc version |
| 1680 | + // which has been used to compile the docs. |
| 1681 | + let output = std::process::Command::new("rustc") |
| 1682 | + .arg("-vV") |
| 1683 | + .output() |
| 1684 | + .expect("Failed to get actual rustc verbose version"); |
| 1685 | + assert_eq!( |
| 1686 | + fingerprint.rustc_vv, |
| 1687 | + (String::from_utf8_lossy(&output.stdout).as_ref()) |
| 1688 | + ); |
| 1689 | + |
| 1690 | + // As the test shows above. Now we have generated the `doc/` folder and inside |
| 1691 | + // the rustdoc fingerprint file is located with the correct rustc version. |
| 1692 | + // So we will remove it and create a new fingerprint with an old rustc version |
| 1693 | + // inside it. We will also place a bogus file inside of the `doc/` folder to ensure |
| 1694 | + // it gets removed as we expect on the next doc compilation. |
| 1695 | + dummy_project.change_file( |
| 1696 | + "target/.rustdoc_fingerprint.json", |
| 1697 | + &old_rustc_verbose_version, |
| 1698 | + ); |
| 1699 | + |
| 1700 | + fs::write( |
| 1701 | + dummy_project.build_dir().join("doc/bogus_file"), |
| 1702 | + String::from("This is a bogus file and should be removed!"), |
| 1703 | + ) |
| 1704 | + .expect("Error writing test bogus file"); |
| 1705 | + |
| 1706 | + // Now if we trigger another compilation, since the fingerprint contains an old version |
| 1707 | + // of rustc, cargo should remove the entire `/doc` folder (including the fingerprint) |
| 1708 | + // and generating another one with the actual version. |
| 1709 | + // It should also remove the bogus file we created above. |
| 1710 | + dummy_project.cargo("doc").run(); |
| 1711 | + |
| 1712 | + assert!(!dummy_project.build_dir().join("doc/bogus_file").exists()); |
| 1713 | + |
| 1714 | + let fingerprint: RustDocFingerprint = |
| 1715 | + serde_json::from_str(&dummy_project.read_file("target/.rustdoc_fingerprint.json")) |
| 1716 | + .expect("JSON Serde fail"); |
| 1717 | + |
| 1718 | + // Check that the fingerprint contains the actual rustc version |
| 1719 | + // which has been used to compile the docs. |
| 1720 | + assert_eq!( |
| 1721 | + fingerprint.rustc_vv, |
| 1722 | + (String::from_utf8_lossy(&output.stdout).as_ref()) |
| 1723 | + ); |
| 1724 | +} |
| 1725 | + |
| 1726 | +#[cfg(target_os = "linux")] |
| 1727 | +#[cargo_test] |
| 1728 | +fn doc_fingerprint_respects_target_paths() { |
| 1729 | + // Random rustc verbose version |
| 1730 | + let old_rustc_verbose_version = format!( |
| 1731 | + "\ |
| 1732 | +rustc 1.41.1 (f3e1a954d 2020-02-24) |
| 1733 | +binary: rustc |
| 1734 | +commit-hash: f3e1a954d2ead4e2fc197c7da7d71e6c61bad196 |
| 1735 | +commit-date: 2020-02-24 |
| 1736 | +host: {} |
| 1737 | +release: 1.41.1 |
| 1738 | +LLVM version: 9.0 |
| 1739 | +", |
| 1740 | + rustc_host() |
| 1741 | + ); |
| 1742 | + |
| 1743 | + // Create the dummy project. |
| 1744 | + let dummy_project = project() |
| 1745 | + .file( |
| 1746 | + "Cargo.toml", |
| 1747 | + r#" |
| 1748 | + [package] |
| 1749 | + name = "foo" |
| 1750 | + version = "1.2.4" |
| 1751 | + authors = [] |
| 1752 | + "#, |
| 1753 | + ) |
| 1754 | + .file("src/lib.rs", "//! These are the docs!") |
| 1755 | + .build(); |
| 1756 | + |
| 1757 | + dummy_project |
| 1758 | + .cargo("doc --target x86_64-unknown-linux-gnu") |
| 1759 | + .run(); |
| 1760 | + |
| 1761 | + let fingerprint: RustDocFingerprint = |
| 1762 | + serde_json::from_str(&dummy_project.read_file("target/.rustdoc_fingerprint.json")) |
| 1763 | + .expect("JSON Serde fail"); |
| 1764 | + |
| 1765 | + // Check that the fingerprint contains the actual rustc version |
| 1766 | + // which has been used to compile the docs. |
| 1767 | + let output = std::process::Command::new("rustc") |
| 1768 | + .arg("-vV") |
| 1769 | + .output() |
| 1770 | + .expect("Failed to get actual rustc verbose version"); |
| 1771 | + assert_eq!( |
| 1772 | + fingerprint.rustc_vv, |
| 1773 | + (String::from_utf8_lossy(&output.stdout).as_ref()) |
| 1774 | + ); |
| 1775 | + |
| 1776 | + // As the test shows above. Now we have generated the `doc/` folder and inside |
| 1777 | + // the rustdoc fingerprint file is located with the correct rustc version. |
| 1778 | + // So we will remove it and create a new fingerprint with an old rustc version |
| 1779 | + // inside it. We will also place a bogus file inside of the `doc/` folder to ensure |
| 1780 | + // it gets removed as we expect on the next doc compilation. |
| 1781 | + dummy_project.change_file( |
| 1782 | + "target/.rustdoc_fingerprint.json", |
| 1783 | + &old_rustc_verbose_version, |
| 1784 | + ); |
| 1785 | + |
| 1786 | + fs::write( |
| 1787 | + dummy_project |
| 1788 | + .build_dir() |
| 1789 | + .join("x86_64-unknown-linux-gnu/doc/bogus_file"), |
| 1790 | + String::from("This is a bogus file and should be removed!"), |
| 1791 | + ) |
| 1792 | + .expect("Error writing test bogus file"); |
| 1793 | + |
| 1794 | + // Now if we trigger another compilation, since the fingerprint contains an old version |
| 1795 | + // of rustc, cargo should remove the entire `/doc` folder (including the fingerprint) |
| 1796 | + // and generating another one with the actual version. |
| 1797 | + // It should also remove the bogus file we created above. |
| 1798 | + dummy_project |
| 1799 | + .cargo("doc --target x86_64-unknown-linux-gnu") |
| 1800 | + .run(); |
| 1801 | + |
| 1802 | + assert!(!dummy_project |
| 1803 | + .build_dir() |
| 1804 | + .join("x86_64-unknown-linux-gnu/doc/bogus_file") |
| 1805 | + .exists()); |
| 1806 | + |
| 1807 | + let fingerprint: RustDocFingerprint = |
| 1808 | + serde_json::from_str(&dummy_project.read_file("target/.rustdoc_fingerprint.json")) |
| 1809 | + .expect("JSON Serde fail"); |
| 1810 | + |
| 1811 | + // Check that the fingerprint contains the actual rustc version |
| 1812 | + // which has been used to compile the docs. |
| 1813 | + assert_eq!( |
| 1814 | + fingerprint.rustc_vv, |
| 1815 | + (String::from_utf8_lossy(&output.stdout).as_ref()) |
| 1816 | + ); |
| 1817 | +} |
0 commit comments