Skip to content

Commit 80f7b90

Browse files
committed
add a test for touching deps
1 parent 3eaa70e commit 80f7b90

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

tests/testsuite/freshness.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::fs::{self, File, OpenOptions};
22
use std::io::prelude::*;
33
use std::net::TcpListener;
4+
use std::path::PathBuf;
45
use std::thread;
6+
use std::time::SystemTime;
57

68
use crate::support::paths::CargoPathExt;
79
use crate::support::registry::Package;
@@ -1178,6 +1180,83 @@ fn changing_rustflags_is_cached() {
11781180
.run();
11791181
}
11801182

1183+
fn simple_deps_cleaner(mut dir: PathBuf, timestamp: filetime::FileTime) {
1184+
// Cargo is experimenting with letting outside projects develop some
1185+
// limited forms of GC for target_dir. This is one of the forms.
1186+
// Specifically, Cargo is updating the mtime of files in
1187+
// target/profile/deps each time it uses the file.
1188+
// So a cleaner can remove files older then a time stamp without
1189+
// effecting any builds that happened since that time stamp.
1190+
let mut cleand = false;
1191+
dir.push("deps");
1192+
for dep in fs::read_dir(&dir).unwrap() {
1193+
let dep = dep.unwrap();
1194+
if filetime::FileTime::from_last_modification_time(&dep.metadata().unwrap()) <= timestamp {
1195+
fs::remove_file(dep.path()).unwrap();
1196+
cleand = true;
1197+
}
1198+
}
1199+
assert!(
1200+
cleand,
1201+
"called simple_deps_cleaner, but there was nothing to remove"
1202+
);
1203+
}
1204+
1205+
#[test]
1206+
fn simple_deps_cleaner_dose_not_rebuild() {
1207+
let p = project()
1208+
.file(
1209+
"Cargo.toml",
1210+
r#"
1211+
[package]
1212+
name = "foo"
1213+
version = "0.0.1"
1214+
1215+
[dependencies]
1216+
bar = { path = "bar" }
1217+
"#,
1218+
)
1219+
.file("src/lib.rs", "")
1220+
.file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
1221+
.file("bar/src/lib.rs", "")
1222+
.build();
1223+
1224+
p.cargo("build").run();
1225+
p.cargo("build")
1226+
.env("RUSTFLAGS", "-C target-cpu=native")
1227+
.with_stderr(
1228+
"\
1229+
[COMPILING] bar v0.0.1 ([..])
1230+
[COMPILING] foo v0.0.1 ([..])
1231+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
1232+
)
1233+
.run();
1234+
if is_coarse_mtime() {
1235+
sleep_ms(1000);
1236+
}
1237+
let timestamp = filetime::FileTime::from_system_time(SystemTime::now());
1238+
// This dose not make new files, but it dose update the mtime.
1239+
p.cargo("build")
1240+
.env("RUSTFLAGS", "-C target-cpu=native")
1241+
.with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
1242+
.run();
1243+
simple_deps_cleaner(p.target_debug_dir(), timestamp);
1244+
// This should not recompile!
1245+
p.cargo("build")
1246+
.env("RUSTFLAGS", "-C target-cpu=native")
1247+
.with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
1248+
.run();
1249+
// But this should be cleaned and so need a rebuild
1250+
p.cargo("build")
1251+
.with_stderr(
1252+
"\
1253+
[COMPILING] bar v0.0.1 ([..])
1254+
[COMPILING] foo v0.0.1 ([..])
1255+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
1256+
)
1257+
.run();
1258+
}
1259+
11811260
#[test]
11821261
fn reuse_panic_build_dep_test() {
11831262
let p = project()

0 commit comments

Comments
 (0)