|
1 | 1 | use std::fs::{self, File, OpenOptions};
|
2 | 2 | use std::io::prelude::*;
|
3 | 3 | use std::net::TcpListener;
|
| 4 | +use std::path::PathBuf; |
4 | 5 | use std::thread;
|
| 6 | +use std::time::SystemTime; |
5 | 7 |
|
6 | 8 | use crate::support::paths::CargoPathExt;
|
7 | 9 | use crate::support::registry::Package;
|
@@ -1178,6 +1180,83 @@ fn changing_rustflags_is_cached() {
|
1178 | 1180 | .run();
|
1179 | 1181 | }
|
1180 | 1182 |
|
| 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 | + |
1181 | 1260 | #[test]
|
1182 | 1261 | fn reuse_panic_build_dep_test() {
|
1183 | 1262 | let p = project()
|
|
0 commit comments