Skip to content

Commit c3fbb4d

Browse files
committed
test: show unpack source have no deterministic mtime
see rust-lang/cargo 16237
1 parent e7246c6 commit c3fbb4d

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,3 +1735,21 @@ pub fn assert_deps_contains(project: &Project, fingerprint: &str, expected: &[(u
17351735
}
17361736
})
17371737
}
1738+
1739+
#[track_caller]
1740+
pub fn assert_deterministic_mtime(path: impl AsRef<Path>) {
1741+
// Hardcoded value be removed once alexcrichton/tar-rs#420 is merged and released.
1742+
// See also rust-lang/cargo#16237
1743+
const DETERMINISTIC_TIMESTAMP: u64 = 1153704088;
1744+
1745+
let path = path.as_ref();
1746+
let mtime = path.metadata().unwrap().modified().unwrap();
1747+
let timestamp = mtime
1748+
.duration_since(std::time::UNIX_EPOCH)
1749+
.unwrap()
1750+
.as_secs();
1751+
assert_eq!(
1752+
timestamp, DETERMINISTIC_TIMESTAMP,
1753+
"expected deterministic mtime for {path:?}, got {timestamp}"
1754+
);
1755+
}

tests/testsuite/registry.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::sync::Mutex;
99
use crate::prelude::*;
1010
use crate::utils::cargo_process;
1111
use cargo::core::SourceId;
12+
use cargo_test_support::assert_deterministic_mtime;
1213
use cargo_test_support::paths;
1314
use cargo_test_support::registry::{
1415
self, Dependency, Package, RegistryBuilder, Response, TestRegistry, registry_path,
@@ -4653,3 +4654,45 @@ required by package `foo v0.0.1 ([ROOT]/foo)`
46534654
"#]])
46544655
.run();
46554656
}
4657+
4658+
#[cargo_test]
4659+
#[should_panic]
4660+
fn deterministic_mtime() {
4661+
let registry = registry::init();
4662+
Package::new("foo", "0.1.0")
4663+
// content doesn't matter, we just want to check mtime
4664+
.file("Cargo.lock", "")
4665+
.file(".cargo_vcs_info.json", "")
4666+
.file("src/lib.rs", "")
4667+
.publish();
4668+
4669+
let p = project()
4670+
.file(
4671+
"Cargo.toml",
4672+
r#"
4673+
[package]
4674+
name = "a"
4675+
edition = "2015"
4676+
4677+
[dependencies]
4678+
foo = '0.1.0'
4679+
"#,
4680+
)
4681+
.file("src/lib.rs", "")
4682+
.build();
4683+
4684+
p.cargo("fetch").run();
4685+
4686+
let id = SourceId::for_registry(registry.index_url()).unwrap();
4687+
let hash = cargo::util::hex::short_hash(&id);
4688+
let pkg_root = paths::cargo_home()
4689+
.join("registry")
4690+
.join("src")
4691+
.join(format!("-{hash}"))
4692+
.join("foo-0.1.0");
4693+
4694+
// Generated files should have deterministic mtime after unpacking.
4695+
assert_deterministic_mtime(pkg_root.join("Cargo.lock"));
4696+
assert_deterministic_mtime(pkg_root.join("Cargo.toml"));
4697+
assert_deterministic_mtime(pkg_root.join(".cargo_vcs_info.json"));
4698+
}

tests/testsuite/vendor.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use std::fs;
88

99
use crate::prelude::*;
10+
use cargo_test_support::assert_deterministic_mtime;
1011
use cargo_test_support::compare::assert_e2e;
1112
use cargo_test_support::git;
1213
use cargo_test_support::registry::{self, Package, RegistryBuilder};
@@ -2119,3 +2120,36 @@ fn vendor_rename_fallback() {
21192120

21202121
assert!(p.root().join("vendor/log/Cargo.toml").exists());
21212122
}
2123+
2124+
#[cargo_test]
2125+
#[should_panic]
2126+
fn deterministic_mtime() {
2127+
Package::new("foo", "0.1.0")
2128+
// content doesn't matter, we just want to check mtime
2129+
.file("Cargo.lock", "")
2130+
.file(".cargo_vcs_info.json", "")
2131+
.file("src/lib.rs", "")
2132+
.publish();
2133+
2134+
let p = project()
2135+
.file(
2136+
"Cargo.toml",
2137+
r#"
2138+
[package]
2139+
name = "a"
2140+
edition = "2015"
2141+
2142+
[dependencies]
2143+
foo = '0.1.0'
2144+
"#,
2145+
)
2146+
.file("src/lib.rs", "")
2147+
.build();
2148+
2149+
p.cargo("vendor --respect-source-config").run();
2150+
2151+
// Generated files should have deterministic mtime after unpacking.
2152+
assert_deterministic_mtime(p.root().join("vendor/foo/Cargo.lock"));
2153+
assert_deterministic_mtime(p.root().join("vendor/foo/Cargo.toml"));
2154+
assert_deterministic_mtime(p.root().join("vendor/foo/.cargo_vcs_info.json"));
2155+
}

0 commit comments

Comments
 (0)