summaryrefslogtreecommitdiffstats
path: root/src/tools/cargo/tests/testsuite/global_cache_tracker.rs
blob: 68a606902d13321568ce285624798675f7b63842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
//! Tests for last-use tracking and auto-gc.
//!
//! Cargo supports an environment variable called `__CARGO_TEST_LAST_USE_NOW`
//! to have cargo pretend that the current time is the given time (in seconds
//! since the unix epoch). This is used throughout these tests to simulate
//! what happens when time passes. The [`days_ago_unix`] and
//! [`months_ago_unix`] functions help with setting this value.

use super::config::ConfigBuilder;
use cargo::core::global_cache_tracker::{self, DeferredGlobalLastUse, GlobalCacheTracker};
use cargo::util::cache_lock::CacheLockMode;
use cargo::util::interning::InternedString;
use cargo::Config;
use cargo_test_support::paths::{self, CargoPathExt};
use cargo_test_support::registry::{Package, RegistryBuilder};
use cargo_test_support::{
    basic_manifest, cargo_process, execs, git, project, retry, sleep_ms, thread_wait_timeout,
    Project,
};
use itertools::Itertools;
use std::fmt::Write;
use std::path::PathBuf;
use std::process::Stdio;
use std::time::{Duration, SystemTime};

/// Helper to create a simple `foo` project which depends on a registry
/// dependency called `bar`.
fn basic_foo_bar_project() -> Project {
    Package::new("bar", "1.0.0").publish();
    project()
        .file(
            "Cargo.toml",
            r#"
                [package]
                name = "foo"
                version = "0.1.0"

                [dependencies]
                bar = "1.0"
            "#,
        )
        .file("src/lib.rs", "")
        .build()
}

/// Helper to get the names of files in a directory as strings.
fn get_names(glob: &str) -> Vec<String> {
    let mut names: Vec<_> = glob::glob(paths::home().join(glob).to_str().unwrap())
        .unwrap()
        .map(|p| p.unwrap().file_name().unwrap().to_str().unwrap().to_owned())
        .collect();
    names.sort();
    names
}

fn get_registry_names(which: &str) -> Vec<String> {
    get_names(&format!(".cargo/registry/{which}/*/*"))
}

fn get_index_names() -> Vec<String> {
    get_names(&format!(".cargo/registry/index/*"))
}

fn get_git_db_names() -> Vec<String> {
    get_names(&format!(".cargo/git/db/*"))
}

fn get_git_checkout_names(db_name: &str) -> Vec<String> {
    get_names(&format!(".cargo/git/checkouts/{db_name}/*"))
}

fn days_ago(n: u64) -> SystemTime {
    SystemTime::now() - Duration::from_secs(60 * 60 * 24 * n)
}

/// Helper for simulating running cargo in the past. Use with the
/// __CARGO_TEST_LAST_USE_NOW environment variable.
fn days_ago_unix(n: u64) -> String {
    days_ago(n)
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap()
        .as_secs()
        .to_string()
}

/// Helper for simulating running cargo in the past. Use with the
/// __CARGO_TEST_LAST_USE_NOW environment variable.
fn months_ago_unix(n: u64) -> String {
    days_ago_unix(n * 30)
}

/// Populates last-use database and the cache files.
///
/// This makes it easier to more accurately specify exact sizes. Creating
/// specific sizes with `Package` is too difficult.
fn populate_cache(config: &Config, test_crates: &[(&str, u64, u64, u64)]) -> (PathBuf, PathBuf) {
    let cache_dir = paths::home().join(".cargo/registry/cache/example.com-a6c4a5adcb232b9a");
    let src_dir = paths::home().join(".cargo/registry/src/example.com-a6c4a5adcb232b9a");

    GlobalCacheTracker::db_path(&config)
        .into_path_unlocked()
        .rm_rf();

    let _lock = config
        .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
        .unwrap();
    let mut tracker = GlobalCacheTracker::new(&config).unwrap();
    let mut deferred = DeferredGlobalLastUse::new();

    cache_dir.rm_rf();
    cache_dir.mkdir_p();
    src_dir.rm_rf();
    src_dir.mkdir_p();
    paths::home()
        .join(".cargo/registry/index/example.com-a6c4a5adcb232b9a")
        .mkdir_p();
    let mut create = |name: &str, age, crate_size: u64, src_size: u64| {
        let crate_filename = InternedString::new(&format!("{name}.crate"));
        deferred.mark_registry_crate_used_stamp(
            global_cache_tracker::RegistryCrate {
                encoded_registry_name: "example.com-a6c4a5adcb232b9a".into(),
                crate_filename,
                size: crate_size,
            },
            Some(&days_ago(age)),
        );
        deferred.mark_registry_src_used_stamp(
            global_cache_tracker::RegistrySrc {
                encoded_registry_name: "example.com-a6c4a5adcb232b9a".into(),
                package_dir: name.into(),
                size: Some(src_size),
            },
            Some(&days_ago(age)),
        );
        std::fs::write(
            cache_dir.join(crate_filename),
            "x".repeat(crate_size as usize),
        )
        .unwrap();
        let path = src_dir.join(name);
        path.mkdir_p();
        std::fs::write(path.join("data"), "x".repeat(src_size as usize)).unwrap()
    };

    for (name, age, crate_size, src_size) in test_crates {
        create(name, *age, *crate_size, *src_size);
    }
    deferred.save(&mut tracker).unwrap();

    (cache_dir, src_dir)
}

#[cargo_test]
fn auto_gc_gated() {
    // Requires -Zgc to both track last-use data and to run auto-gc.
    let p = basic_foo_bar_project();
    p.cargo("check")
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();
    // Check that it did not create a database or delete anything.
    let config = ConfigBuilder::new().build();
    assert!(!GlobalCacheTracker::db_path(&config)
        .into_path_unlocked()
        .exists());
    assert_eq!(get_index_names().len(), 1);

    // Again in the future, shouldn't auto-gc.
    p.cargo("check").run();
    assert!(!GlobalCacheTracker::db_path(&config)
        .into_path_unlocked()
        .exists());
    assert_eq!(get_index_names().len(), 1);
}

#[cargo_test]
fn clean_gc_gated() {
    cargo_process("clean gc")
        .with_status(101)
        .with_stderr(
            "\
error: the `cargo clean gc` command is unstable, and only available on the \
nightly channel of Cargo, but this is the `stable` channel
See [..]
See [..]
",
        )
        .run();
}

#[cargo_test]
fn implies_source() {
    // Checks that when a src, crate, or checkout is marked as used, the
    // corresponding index or git db also gets marked as used.
    let config = ConfigBuilder::new().unstable_flag("gc").build();
    let _lock = config
        .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
        .unwrap();
    let mut deferred = DeferredGlobalLastUse::new();
    let mut tracker = GlobalCacheTracker::new(&config).unwrap();

    deferred.mark_registry_crate_used(global_cache_tracker::RegistryCrate {
        encoded_registry_name: "example.com-a6c4a5adcb232b9a".into(),
        crate_filename: "regex-1.8.4.crate".into(),
        size: 123,
    });
    deferred.mark_registry_src_used(global_cache_tracker::RegistrySrc {
        encoded_registry_name: "index.crates.io-6f17d22bba15001f".into(),
        package_dir: "rand-0.8.5".into(),
        size: None,
    });
    deferred.mark_git_checkout_used(global_cache_tracker::GitCheckout {
        encoded_git_name: "cargo-e7ff1db891893a9e".into(),
        short_name: "f0a4ee0".into(),
        size: None,
    });
    deferred.save(&mut tracker).unwrap();

    let mut indexes = tracker.registry_index_all().unwrap();
    assert_eq!(indexes.len(), 2);
    indexes.sort_by(|a, b| a.0.encoded_registry_name.cmp(&b.0.encoded_registry_name));
    assert_eq!(
        indexes[0].0.encoded_registry_name,
        "example.com-a6c4a5adcb232b9a"
    );
    assert_eq!(
        indexes[1].0.encoded_registry_name,
        "index.crates.io-6f17d22bba15001f"
    );

    let dbs = tracker.git_db_all().unwrap();
    assert_eq!(dbs.len(), 1);
    assert_eq!(dbs[0].0.encoded_git_name, "cargo-e7ff1db891893a9e");
}

#[cargo_test]
fn auto_gc_defaults() {
    // Checks that the auto-gc deletes old entries, and leaves new ones intact.
    Package::new("old", "1.0.0").publish();
    Package::new("new", "1.0.0").publish();
    let p = project()
        .file(
            "Cargo.toml",
            r#"
                [package]
                name = "foo"
                version = "0.1.0"

                [dependencies]
                old = "1.0"
                new = "1.0"
            "#,
        )
        .file("src/lib.rs", "")
        .build();
    // Populate the last-use data.
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();
    assert_eq!(get_registry_names("src"), ["new-1.0.0", "old-1.0.0"]);
    assert_eq!(
        get_registry_names("cache"),
        ["new-1.0.0.crate", "old-1.0.0.crate"]
    );

    // Run again with just one package. Make sure the old src gets deleted,
    // but .crate does not.
    p.change_file(
        "Cargo.toml",
        r#"
            [package]
            name = "foo"
            version = "0.1.0"

            [dependencies]
            new = "1.0"
        "#,
    );
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(2))
        .run();
    assert_eq!(get_registry_names("src"), ["new-1.0.0"]);
    assert_eq!(
        get_registry_names("cache"),
        ["new-1.0.0.crate", "old-1.0.0.crate"]
    );

    // Run again after the .crate should have aged out.
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();
    assert_eq!(get_registry_names("src"), ["new-1.0.0"]);
    assert_eq!(get_registry_names("cache"), ["new-1.0.0.crate"]);
}

#[cargo_test]
fn auto_gc_config() {
    // Can configure auto gc settings.
    Package::new("old", "1.0.0").publish();
    Package::new("new", "1.0.0").publish();
    let p = project()
        .file(
            ".cargo/config.toml",
            r#"
                [gc.auto]
                frequency = "always"
                max-src-age = "1 day"
                max-crate-age = "3 days"
                max-index-age = "3 days"
                max-git-co-age = "1 day"
                max-git-db-age = "3 days"
            "#,
        )
        .file(
            "Cargo.toml",
            r#"
                [package]
                name = "foo"
                version = "0.1.0"

                [dependencies]
                old = "1.0"
                new = "1.0"
            "#,
        )
        .file("src/lib.rs", "")
        .build();
    // Populate the last-use data.
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", days_ago_unix(4))
        .run();
    assert_eq!(get_registry_names("src"), ["new-1.0.0", "old-1.0.0"]);
    assert_eq!(
        get_registry_names("cache"),
        ["new-1.0.0.crate", "old-1.0.0.crate"]
    );

    // Run again with just one package. Make sure the old src gets deleted,
    // but .crate does not.
    p.change_file(
        "Cargo.toml",
        r#"
            [package]
            name = "foo"
            version = "0.1.0"

            [dependencies]
            new = "1.0"
        "#,
    );
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", days_ago_unix(2))
        .run();
    assert_eq!(get_registry_names("src"), ["new-1.0.0"]);
    assert_eq!(
        get_registry_names("cache"),
        ["new-1.0.0.crate", "old-1.0.0.crate"]
    );

    // Run again after the .crate should have aged out.
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();
    assert_eq!(get_registry_names("src"), ["new-1.0.0"]);
    assert_eq!(get_registry_names("cache"), ["new-1.0.0.crate"]);
}

#[cargo_test]
fn frequency() {
    // gc.auto.frequency settings
    let p = basic_foo_bar_project();
    p.change_file(
        ".cargo/config.toml",
        r#"
            [gc.auto]
            frequency = "never"
        "#,
    );
    // Populate data in the past.
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();
    assert_eq!(get_index_names().len(), 1);
    assert_eq!(get_registry_names("src"), ["bar-1.0.0"]);
    assert_eq!(get_registry_names("cache"), ["bar-1.0.0.crate"]);

    p.change_file("Cargo.toml", &basic_manifest("foo", "0.2.0"));

    // Try after the default expiration time, with "never" it shouldn't gc.
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();
    assert_eq!(get_index_names().len(), 1);
    assert_eq!(get_registry_names("src"), ["bar-1.0.0"]);
    assert_eq!(get_registry_names("cache"), ["bar-1.0.0.crate"]);

    // Try again with a setting that allows it to run.
    p.cargo("check -Zgc")
        .env("CARGO_GC_AUTO_FREQUENCY", "1 day")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();
    assert_eq!(get_index_names().len(), 0);
    assert_eq!(get_registry_names("src").len(), 0);
    assert_eq!(get_registry_names("cache").len(), 0);
}

#[cargo_test]
fn auto_gc_index() {
    // Deletes the index if it hasn't been used in a while.
    let p = basic_foo_bar_project();
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();
    assert_eq!(get_index_names().len(), 1);

    // Make sure it stays within the time frame.
    p.change_file(
        "Cargo.toml",
        r#"
            [package]
            name = "foo"
            version = "0.1.0"
        "#,
    );
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(2))
        .run();
    assert_eq!(get_index_names().len(), 1);

    // After it expires, it should be deleted.
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();
    assert_eq!(get_index_names().len(), 0);
}

#[cargo_test]
fn auto_gc_git() {
    // auto-gc should delete git checkouts and dbs.

    // Returns the short git name of a a checkout.
    let short_id = |repo: &git2::Repository| -> String {
        let head = repo.revparse_single("HEAD").unwrap();
        let short_id = head.short_id().unwrap();
        short_id.as_str().unwrap().to_owned()
    };

    // Set up a git dependency and fetch it and populate the database,
    // 6 months in the past.
    let (git_project, git_repo) = git::new_repo("bar", |p| {
        p.file("Cargo.toml", &basic_manifest("bar", "1.0.0"))
            .file("src/lib.rs", "")
    });
    let p = project()
        .file(
            "Cargo.toml",
            &format!(
                r#"
                [package]
                name = "foo"
                version = "0.1.0"

                [dependencies]
                bar = {{ git = '{}' }}
            "#,
                git_project.url()
            ),
        )
        .file("src/lib.rs", "")
        .build();
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(6))
        .run();
    let db_names = get_git_db_names();
    assert_eq!(db_names.len(), 1);
    let first_short_oid = short_id(&git_repo);
    assert_eq!(
        get_git_checkout_names(&db_names[0]),
        [first_short_oid.clone()]
    );

    // Use a new git checkout, should keep both.
    git_project.change_file("src/lib.rs", "// modified");
    git::add(&git_repo);
    git::commit(&git_repo);
    p.cargo("update -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(6))
        .run();
    assert_eq!(get_git_db_names().len(), 1);
    let second_short_oid = short_id(&git_repo);
    let mut both = vec![first_short_oid, second_short_oid.clone()];
    both.sort();
    assert_eq!(get_git_checkout_names(&db_names[0]), both);

    // In the future, using the second checkout should delete the first.
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();
    assert_eq!(get_git_db_names().len(), 1);
    assert_eq!(
        get_git_checkout_names(&db_names[0]),
        [second_short_oid.clone()]
    );

    // After three months, the db should get deleted.
    p.change_file("Cargo.toml", &basic_manifest("foo", "0.2.0"));
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();
    assert_eq!(get_git_db_names().len(), 0);
    assert_eq!(get_git_checkout_names(&db_names[0]).len(), 0);
}

#[cargo_test]
fn auto_gc_various_commands() {
    // Checks that auto gc works with a variety of commands.
    //
    // Auto-gc is only run on a subset of commands. Generally it is run on
    // commands that are already doing a lot of work, or heavily involve the
    // use of the registry.
    Package::new("bar", "1.0.0").publish();
    let cmds = ["check", "fetch"];
    for cmd in cmds {
        eprintln!("checking command {cmd}");
        let p = project()
            .file(
                "Cargo.toml",
                r#"
                    [package]
                    name = "foo"
                    version = "0.1.0"

                    [dependencies]
                    bar = "1.0"
                "#,
            )
            .file("src/lib.rs", "")
            .build();
        // Populate the last-use data.
        p.cargo(cmd)
            .arg("-Zgc")
            .masquerade_as_nightly_cargo(&["gc"])
            .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
            .run();
        let config = ConfigBuilder::new().unstable_flag("gc").build();
        let lock = config
            .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
            .unwrap();
        let tracker = GlobalCacheTracker::new(&config).unwrap();
        let indexes = tracker.registry_index_all().unwrap();
        assert_eq!(indexes.len(), 1);
        let crates = tracker.registry_crate_all().unwrap();
        assert_eq!(crates.len(), 1);
        let srcs = tracker.registry_src_all().unwrap();
        assert_eq!(srcs.len(), 1);
        drop(lock);

        // After everything is aged out, it should all be deleted.
        p.change_file("Cargo.toml", &basic_manifest("foo", "0.2.0"));
        p.cargo(cmd)
            .arg("-Zgc")
            .masquerade_as_nightly_cargo(&["gc"])
            .run();
        let lock = config
            .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
            .unwrap();
        let indexes = tracker.registry_index_all().unwrap();
        assert_eq!(indexes.len(), 0);
        let crates = tracker.registry_crate_all().unwrap();
        assert_eq!(crates.len(), 0);
        let srcs = tracker.registry_src_all().unwrap();
        assert_eq!(srcs.len(), 0);
        drop(tracker);
        drop(lock);
        paths::home().join(".cargo/registry").rm_rf();
        GlobalCacheTracker::db_path(&config)
            .into_path_unlocked()
            .rm_rf();
    }
}

#[cargo_test]
fn updates_last_use_various_commands() {
    // Checks that last-use tracking is updated by various commands.
    //
    // Not *all* commands update the index tracking, even though they
    // technically involve reading the index. There isn't a convenient place
    // to ensure it gets saved while avoiding saving too often in other
    // commands. For the most part, this should be fine, since these commands
    // usually aren't run without running one of the commands that does save
    // the tracking. Some of the commands are:
    //
    // - login, owner, yank, search
    // - report future-incompatibilities
    // - package --no-verify
    // - fetch --locked
    Package::new("bar", "1.0.0").publish();
    let cmds = [
        // name, expected_crates (0=doesn't download)
        ("check", 1),
        ("fetch", 1),
        ("tree", 1),
        ("generate-lockfile", 0),
        ("update", 0),
        ("metadata", 1),
        ("vendor --respect-source-config", 1),
    ];
    for (cmd, expected_crates) in cmds {
        eprintln!("checking command {cmd}");
        let p = project()
            .file(
                "Cargo.toml",
                r#"
                    [package]
                    name = "foo"
                    version = "0.1.0"

                    [dependencies]
                    bar = "1.0"
                "#,
            )
            .file("src/lib.rs", "")
            .build();
        // Populate the last-use data.
        p.cargo(cmd)
            .arg("-Zgc")
            .masquerade_as_nightly_cargo(&["gc"])
            .run();
        let config = ConfigBuilder::new().unstable_flag("gc").build();
        let lock = config
            .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
            .unwrap();
        let tracker = GlobalCacheTracker::new(&config).unwrap();
        let indexes = tracker.registry_index_all().unwrap();
        assert_eq!(indexes.len(), 1);
        let crates = tracker.registry_crate_all().unwrap();
        assert_eq!(crates.len(), expected_crates);
        let srcs = tracker.registry_src_all().unwrap();
        assert_eq!(srcs.len(), expected_crates);
        drop(tracker);
        drop(lock);
        paths::home().join(".cargo/registry").rm_rf();
        GlobalCacheTracker::db_path(&config)
            .into_path_unlocked()
            .rm_rf();
    }
}

#[cargo_test]
fn both_git_and_http_index_cleans() {
    // Checks that either the git or http index cache gets cleaned.
    let _crates_io = RegistryBuilder::new().build();
    let _alternative = RegistryBuilder::new().alternative().http_index().build();
    Package::new("from_git", "1.0.0").publish();
    Package::new("from_http", "1.0.0")
        .alternative(true)
        .publish();
    let p = project()
        .file(
            "Cargo.toml",
            r#"
                [package]
                name = "foo"
                version = "0.1.0"

                [dependencies]
                from_git = "1.0"
                from_http = { version = "1.0", registry = "alternative" }
            "#,
        )
        .file("src/lib.rs", "")
        .build();

    p.cargo("update -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();
    let config = ConfigBuilder::new().unstable_flag("gc").build();
    let lock = config
        .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
        .unwrap();
    let tracker = GlobalCacheTracker::new(&config).unwrap();
    let indexes = tracker.registry_index_all().unwrap();
    assert_eq!(indexes.len(), 2);
    assert_eq!(get_index_names().len(), 2);
    drop(lock);

    // Running in the future without these indexes should delete them.
    p.change_file("Cargo.toml", &basic_manifest("foo", "0.2.0"));
    p.cargo("clean gc -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();
    let lock = config
        .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
        .unwrap();
    let indexes = tracker.registry_index_all().unwrap();
    assert_eq!(indexes.len(), 0);
    assert_eq!(get_index_names().len(), 0);
    drop(lock);
}

#[cargo_test]
fn clean_gc_dry_run() {
    // Basic `clean --gc --dry-run` test.
    let p = basic_foo_bar_project();
    // Populate the last-use data.
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();

    let registry_root = paths::home().join(".cargo/registry");
    let glob_registry = |name| -> PathBuf {
        let mut paths: Vec<_> = glob::glob(registry_root.join(name).join("*").to_str().unwrap())
            .unwrap()
            .map(|p| p.unwrap())
            .collect();
        assert_eq!(paths.len(), 1);
        paths.pop().unwrap()
    };
    let index = glob_registry("index").ls_r();
    let src = glob_registry("src").ls_r();
    let cache = glob_registry("cache").ls_r();
    let expected_files = index
        .iter()
        .chain(src.iter())
        .chain(cache.iter())
        .map(|p| p.to_str().unwrap())
        .join("\n");

    p.cargo("clean gc --dry-run -v -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stdout_unordered(&expected_files)
        .with_stderr(
            "[SUMMARY] [..] files, [..] total\n\
            [WARNING] no files deleted due to --dry-run",
        )
        .run();

    // Again, make sure the information is still tracked.
    p.cargo("clean gc --dry-run -v -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stdout_unordered(&expected_files)
        .with_stderr(
            "[SUMMARY] [..] files, [..] total\n\
            [WARNING] no files deleted due to --dry-run",
        )
        .run();
}

#[cargo_test]
fn clean_default_gc() {
    // `clean gc` without options should also gc
    let p = basic_foo_bar_project();
    // Populate the last-use data.
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();
    p.cargo("clean gc -v -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr_unordered(
            "\
[REMOVING] [ROOT]/home/.cargo/registry/index/[..]
[REMOVING] [ROOT]/home/.cargo/registry/src/[..]
[REMOVING] [ROOT]/home/.cargo/registry/cache/[..]
[REMOVED] [..] files, [..] total
",
        )
        .run();
}

#[cargo_test]
fn tracks_sizes() {
    // Checks that sizes are properly tracked in the db.
    Package::new("dep1", "1.0.0")
        .file("src/lib.rs", "")
        .publish();
    Package::new("dep2", "1.0.0")
        .file("src/lib.rs", "")
        .file("data", &"abcdefghijklmnopqrstuvwxyz".repeat(1000))
        .publish();
    let p = project()
        .file(
            "Cargo.toml",
            r#"
                [package]
                name = "foo"
                version = "0.1.0"

                [dependencies]
                dep1 = "1.0"
                dep2 = "1.0"
            "#,
        )
        .file("src/lib.rs", "")
        .build();
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();

    // Check that the crate sizes are the same as on disk.
    let config = ConfigBuilder::new().unstable_flag("gc").build();
    let _lock = config
        .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
        .unwrap();
    let tracker = GlobalCacheTracker::new(&config).unwrap();
    let mut crates = tracker.registry_crate_all().unwrap();
    crates.sort_by(|a, b| a.0.crate_filename.cmp(&b.0.crate_filename));
    let db_sizes: Vec<_> = crates.iter().map(|c| c.0.size).collect();

    let mut actual: Vec<_> = p
        .glob(paths::home().join(".cargo/registry/cache/*/*"))
        .map(|p| p.unwrap())
        .collect();
    actual.sort();
    let actual_sizes: Vec<_> = actual
        .iter()
        .map(|path| std::fs::metadata(path).unwrap().len())
        .collect();
    assert_eq!(db_sizes, actual_sizes);

    // Also check the src sizes are computed.
    let mut srcs = tracker.registry_src_all().unwrap();
    srcs.sort_by(|a, b| a.0.package_dir.cmp(&b.0.package_dir));
    let db_sizes: Vec<_> = srcs.iter().map(|c| c.0.size.unwrap()).collect();
    let mut actual: Vec<_> = p
        .glob(paths::home().join(".cargo/registry/src/*/*"))
        .map(|p| p.unwrap())
        .collect();
    actual.sort();
    // .cargo-ok is not tracked in the size.
    actual.iter().for_each(|p| p.join(".cargo-ok").rm_rf());
    let actual_sizes: Vec<_> = actual
        .iter()
        .map(|path| cargo_util::du(path, &[]).unwrap())
        .collect();
    assert_eq!(db_sizes, actual_sizes);
    assert!(db_sizes[1] > 26000);
}

#[cargo_test]
fn max_size() {
    // Checks --max-crate-size and --max-src-size with various cleaning thresholds.
    let config = ConfigBuilder::new().unstable_flag("gc").build();

    let test_crates = [
        // name, age, crate_size, src_size
        ("a-1.0.0", 5, 1, 1),
        ("b-1.0.0", 6, 2, 2),
        ("c-1.0.0", 3, 3, 3),
        ("d-1.0.0", 2, 4, 4),
        ("e-1.0.0", 2, 5, 5),
        ("f-1.0.0", 9, 6, 6),
        ("g-1.0.0", 1, 1, 1),
    ];

    // Determine the order things get deleted so they can be verified.
    let mut names_by_timestamp: Vec<_> = test_crates
        .iter()
        .map(|(name, age, _, _)| (days_ago_unix(*age), name))
        .collect();
    names_by_timestamp.sort();
    let names_by_timestamp: Vec<_> = names_by_timestamp
        .into_iter()
        .map(|(_, name)| name)
        .collect();

    // This exercises the different boundary conditions.
    for (clean_size, files, bytes) in [
        (22, 0, 0),
        (21, 1, 6),
        (16, 1, 6),
        (15, 2, 8),
        (14, 2, 8),
        (13, 3, 9),
        (12, 4, 12),
        (10, 4, 12),
        (9, 5, 16),
        (6, 5, 16),
        (5, 6, 21),
        (1, 6, 21),
        (0, 7, 22),
    ] {
        let (removed, kept) = names_by_timestamp.split_at(files);
        // --max-crate-size
        let (cache_dir, src_dir) = populate_cache(&config, &test_crates);
        let mut stderr = String::new();
        for name in removed {
            writeln!(stderr, "[REMOVING] [..]{name}.crate").unwrap();
        }
        let total_display = if removed.is_empty() {
            String::new()
        } else {
            format!(", {bytes}B total")
        };
        let files_display = if files == 1 {
            format!("1 file")
        } else {
            format!("{files} files")
        };
        write!(stderr, "[REMOVED] {files_display}{total_display}").unwrap();
        cargo_process(&format!("clean gc -Zgc -v --max-crate-size={clean_size}"))
            .masquerade_as_nightly_cargo(&["gc"])
            .with_stderr_unordered(&stderr)
            .run();
        for name in kept {
            assert!(cache_dir.join(format!("{name}.crate")).exists());
        }
        for name in removed {
            assert!(!cache_dir.join(format!("{name}.crate")).exists());
        }

        // --max-src-size
        populate_cache(&config, &test_crates);
        let mut stderr = String::new();
        for name in removed {
            writeln!(stderr, "[REMOVING] [..]{name}").unwrap();
        }
        let total_display = if files == 0 {
            String::new()
        } else {
            format!(", {bytes}B total")
        };
        write!(stderr, "[REMOVED] {files_display}{total_display}").unwrap();
        cargo_process(&format!("clean gc -Zgc -v --max-src-size={clean_size}"))
            .masquerade_as_nightly_cargo(&["gc"])
            .with_stderr_unordered(&stderr)
            .run();
        for name in kept {
            assert!(src_dir.join(name).exists());
        }
        for name in removed {
            assert!(!src_dir.join(name).exists());
        }
    }
}

#[cargo_test]
fn max_size_untracked_crate() {
    // When a .crate file exists from an older version of cargo that did not
    // track sizes, `clean --max-crate-size` should populate the db with the
    // sizes.
    let config = ConfigBuilder::new().unstable_flag("gc").build();
    let cache = paths::home().join(".cargo/registry/cache/example.com-a6c4a5adcb232b9a");
    cache.mkdir_p();
    paths::home()
        .join(".cargo/registry/index/example.com-a6c4a5adcb232b9a")
        .mkdir_p();
    // Create the `.crate files.
    let test_crates = [
        // name, size
        ("a-1.0.0.crate", 1234),
        ("b-1.0.0.crate", 42),
        ("c-1.0.0.crate", 0),
    ];
    for (name, size) in test_crates {
        std::fs::write(cache.join(name), "x".repeat(size as usize)).unwrap()
    }
    // This should scan the directory and populate the db with the size information.
    cargo_process("clean gc -Zgc -v --max-crate-size=100000")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr("[REMOVED] 0 files")
        .run();
    // Check that it stored the size data.
    let _lock = config
        .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
        .unwrap();
    let tracker = GlobalCacheTracker::new(&config).unwrap();
    let crates = tracker.registry_crate_all().unwrap();
    let mut actual: Vec<_> = crates
        .iter()
        .map(|(rc, _time)| (rc.crate_filename.as_str(), rc.size))
        .collect();
    actual.sort();
    assert_eq!(test_crates, actual.as_slice());
}

/// Helper to prepare the max-size test.
fn max_size_untracked_prepare() -> (Config, Project) {
    // First, publish and download a dependency.
    let p = basic_foo_bar_project();
    p.cargo("fetch").run();
    // Pretend it was an older version that did not track last-use.
    let config = ConfigBuilder::new().unstable_flag("gc").build();
    GlobalCacheTracker::db_path(&config)
        .into_path_unlocked()
        .rm_rf();
    (config, p)
}

/// Helper to verify the max-size test.
fn max_size_untracked_verify(config: &Config) {
    let actual: Vec<_> = glob::glob(
        paths::home()
            .join(".cargo/registry/src/*/*")
            .to_str()
            .unwrap(),
    )
    .unwrap()
    .map(|p| p.unwrap())
    .collect();
    assert_eq!(actual.len(), 1);
    let actual_size = cargo_util::du(&actual[0], &[]).unwrap();
    let lock = config
        .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
        .unwrap();
    let tracker = GlobalCacheTracker::new(&config).unwrap();
    let srcs = tracker.registry_src_all().unwrap();
    assert_eq!(srcs.len(), 1);
    assert_eq!(srcs[0].0.size, Some(actual_size));
    drop(lock);
}

#[cargo_test]
fn max_size_untracked_src_from_use() {
    // When a src directory exists from an older version of cargo that did not
    // track sizes, doing a build should populate the db with an entry with an
    // unknown size. `clean --max-src-size` should then fix the size.
    let (config, p) = max_size_untracked_prepare();

    // Run a command that will update the db with an unknown src size.
    p.cargo("tree -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();
    // Check that it is None.
    let lock = config
        .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
        .unwrap();
    let tracker = GlobalCacheTracker::new(&config).unwrap();
    let srcs = tracker.registry_src_all().unwrap();
    assert_eq!(srcs.len(), 1);
    assert_eq!(srcs[0].0.size, None);
    drop(lock);

    // Fix the size.
    p.cargo("clean gc -v --max-src-size=10000 -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr("[REMOVED] 0 files")
        .run();
    max_size_untracked_verify(&config);
}

#[cargo_test]
fn max_size_untracked_src_from_clean() {
    // When a src directory exists from an older version of cargo that did not
    // track sizes, `clean --max-src-size` should populate the db with the
    // sizes.
    let (config, p) = max_size_untracked_prepare();

    // Clean should scan the src and update the db.
    p.cargo("clean gc -v --max-src-size=10000 -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr("[REMOVED] 0 files")
        .run();
    max_size_untracked_verify(&config);
}

#[cargo_test]
fn max_download_size() {
    // --max-download-size
    //
    // This creates some sample crates of specific sizes, and then tries
    // deleting at various specific size thresholds that exercise different
    // edge conditions.
    let config = ConfigBuilder::new().unstable_flag("gc").build();

    let test_crates = [
        // name, age, crate_size, src_size
        ("d-1.0.0", 4, 4, 5),
        ("c-1.0.0", 3, 3, 3),
        ("a-1.0.0", 1, 2, 5),
        ("b-1.0.0", 1, 1, 7),
    ];

    for (max_size, num_deleted, files_deleted, bytes) in [
        (30, 0, 0, 0),
        (29, 1, 1, 5),
        (24, 2, 2, 9),
        (20, 3, 3, 12),
        (1, 7, 7, 29),
        (0, 8, 8, 30),
    ] {
        populate_cache(&config, &test_crates);
        // Determine the order things will be deleted.
        let delete_order: Vec<String> = test_crates
            .iter()
            .flat_map(|(name, _, _, _)| [name.to_string(), format!("{name}.crate")])
            .collect();
        let (removed, _kept) = delete_order.split_at(num_deleted);
        let mut stderr = String::new();
        for name in removed {
            writeln!(stderr, "[REMOVING] [..]{name}").unwrap();
        }
        let files_display = if files_deleted == 1 {
            format!("1 file")
        } else {
            format!("{files_deleted} files")
        };
        let total_display = if removed.is_empty() {
            String::new()
        } else {
            format!(", {bytes}B total")
        };
        write!(stderr, "[REMOVED] {files_display}{total_display}",).unwrap();
        cargo_process(&format!("clean gc -Zgc -v --max-download-size={max_size}"))
            .masquerade_as_nightly_cargo(&["gc"])
            .with_stderr_unordered(&stderr)
            .run();
    }
}

#[cargo_test]
fn package_cache_lock_during_build() {
    // Verifies that a shared lock is held during a build. Resolution and
    // downloads should be OK while that is held, but mutation should block.
    //
    // This works by launching a build with a build script that will pause.
    // Then it performs other cargo commands and verifies their behavior.
    Package::new("bar", "1.0.0").publish();
    let p_foo = project()
        .file(
            "Cargo.toml",
            r#"
                [package]
                name = "foo"
                version = "0.1.0"

                [dependencies]
                bar = "1.0"
            "#,
        )
        .file("src/lib.rs", "")
        .file(
            "build.rs",
            r#"
                fn main() {
                    std::fs::write("blocking", "").unwrap();
                    let path = std::path::Path::new("ready");
                    loop {
                        if path.exists() {
                            break;
                        } else {
                            std::thread::sleep(std::time::Duration::from_millis(100))
                        }
                    }
                }
            "#,
        )
        .build();
    let p_foo2 = project()
        .at("foo2")
        .file(
            "Cargo.toml",
            r#"
                [package]
                name = "foo2"
                version = "0.1.0"

                [dependencies]
                bar = "1.0"
            "#,
        )
        .file("src/lib.rs", "")
        .build();

    // Start a build that will pause once the build starts.
    let mut foo_child = p_foo
        .cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .build_command()
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();

    // Wait for it to enter build script.
    retry(100, || p_foo.root().join("blocking").exists().then_some(()));

    // Start a build with a different target directory. It should not block,
    // even though it gets a download lock, and then a shared lock.
    //
    // Also verify that auto-gc gets disabled.
    p_foo2
        .cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("CARGO_GC_AUTO_FREQUENCY", "always")
        .env("CARGO_LOG", "gc=debug")
        .with_stderr_contains("[UPDATING] `dummy-registry` index")
        .with_stderr_contains("[CHECKING] bar v1.0.0")
        .with_stderr_contains("[CHECKING] foo2 v0.1.0 [..]")
        .with_stderr_contains("[FINISHED] [..]")
        .with_stderr_contains("[..]unable to acquire mutate lock, auto gc disabled")
        .run();

    // Ensure that the first build really blocked.
    assert!(matches!(foo_child.try_wait(), Ok(None)));

    // Cleaning while a command is running should block.
    let mut clean_cmd = p_foo2
        .cargo("clean gc --max-download-size=0 -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .build_command();
    clean_cmd.stderr(Stdio::piped());
    let mut clean_child = clean_cmd.spawn().unwrap();

    // Give the clean command a chance to finish (it shouldn't).
    sleep_ms(500);
    // They should both still be running.
    assert!(matches!(foo_child.try_wait(), Ok(None)));
    assert!(matches!(clean_child.try_wait(), Ok(None)));

    // Let the original build finish.
    p_foo.change_file("ready", "");

    // Wait for clean to finish.
    let thread = std::thread::spawn(|| clean_child.wait_with_output().unwrap());
    let output = thread_wait_timeout(100, thread);
    assert!(output.status.success());
    // Validate the output of the clean.
    execs()
        .with_stderr(
            "\
[BLOCKING] waiting for file lock on package cache mutation
[REMOVED] [..]
",
        )
        .run_output(&output);
}

#[cargo_test]
fn read_only_locking_auto_gc() {
    // Tests the behavior for auto-gc on a read-only directory.
    let p = basic_foo_bar_project();
    // Populate cache.
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();
    let cargo_home = paths::home().join(".cargo");
    let mut perms = std::fs::metadata(&cargo_home).unwrap().permissions();
    // Test when it can't update auto-gc db.
    perms.set_readonly(true);
    std::fs::set_permissions(&cargo_home, perms.clone()).unwrap();
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[CHECKING] bar v1.0.0
[CHECKING] foo v0.1.0 [..]
[FINISHED] [..]
",
        )
        .run();
    // Try again without the last-use existing (such as if the cache was
    // populated by an older version of cargo).
    perms.set_readonly(false);
    std::fs::set_permissions(&cargo_home, perms.clone()).unwrap();
    let config = ConfigBuilder::new().build();
    GlobalCacheTracker::db_path(&config)
        .into_path_unlocked()
        .rm_rf();
    perms.set_readonly(true);
    std::fs::set_permissions(&cargo_home, perms.clone()).unwrap();
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr("[FINISHED] [..]")
        .run();
    perms.set_readonly(false);
    std::fs::set_permissions(&cargo_home, perms).unwrap();
}

#[cargo_test]
fn delete_index_also_deletes_crates() {
    // Checks that when an index is delete that src and cache directories also get deleted.
    let p = basic_foo_bar_project();
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();

    assert_eq!(get_registry_names("src"), ["bar-1.0.0"]);
    assert_eq!(get_registry_names("cache"), ["bar-1.0.0.crate"]);

    p.cargo("clean gc")
        .arg("--max-index-age=0 days")
        .arg("-Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr("[REMOVED] [..]")
        .run();

    assert_eq!(get_registry_names("src").len(), 0);
    assert_eq!(get_registry_names("cache").len(), 0);
}

#[cargo_test]
fn clean_syncs_missing_files() {
    // When files go missing in the cache, clean operations that need to track
    // the size should also remove them from the database.
    Package::new("bar", "1.0.0").publish();
    Package::new("baz", "1.0.0").publish();
    let p = project()
        .file(
            "Cargo.toml",
            r#"
                [package]
                name = "foo"
                version = "0.1.0"

                [dependencies]
                bar = "1.0"
                baz = "1.0"
            "#,
        )
        .file("src/lib.rs", "")
        .build();
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();

    // Verify things are tracked.
    let config = ConfigBuilder::new().unstable_flag("gc").build();
    let lock = config
        .acquire_package_cache_lock(CacheLockMode::MutateExclusive)
        .unwrap();
    let tracker = GlobalCacheTracker::new(&config).unwrap();
    let crates = tracker.registry_crate_all().unwrap();
    assert_eq!(crates.len(), 2);
    let srcs = tracker.registry_src_all().unwrap();
    assert_eq!(srcs.len(), 2);
    drop(lock);

    // Remove the files.
    for pattern in [
        ".cargo/registry/cache/*/bar-1.0.0.crate",
        ".cargo/registry/src/*/bar-1.0.0",
    ] {
        p.glob(paths::home().join(pattern))
            .map(|p| p.unwrap())
            .next()
            .unwrap()
            .rm_rf();
    }

    // Clean should update the db.
    p.cargo("clean gc -v --max-download-size=1GB -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr("[REMOVED] 0 files")
        .run();

    // Verify
    let crates = tracker.registry_crate_all().unwrap();
    assert_eq!(crates.len(), 1);
    let srcs = tracker.registry_src_all().unwrap();
    assert_eq!(srcs.len(), 1);
}

#[cargo_test]
fn offline_doesnt_auto_gc() {
    // When running offline, auto-gc shouldn't run.
    let p = basic_foo_bar_project();
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();
    // Remove the dependency.
    p.change_file("Cargo.toml", &basic_manifest("foo", "0.1.0"));
    // Run offline, make sure it doesn't delete anything
    p.cargo("check --offline -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr("[CHECKING] foo v0.1.0[..]\n[FINISHED][..]")
        .run();
    assert_eq!(get_registry_names("src"), ["bar-1.0.0"]);
    assert_eq!(get_registry_names("cache"), ["bar-1.0.0.crate"]);
    // Run online, make sure auto-gc runs.
    p.cargo("check -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr("[FINISHED][..]")
        .run();
    assert_eq!(get_registry_names("src"), &[] as &[String]);
    assert_eq!(get_registry_names("cache"), &[] as &[String]);
}

#[cargo_test]
fn can_handle_future_schema() -> anyhow::Result<()> {
    // It should work when a future version of cargo has made schema changes
    // to the database.
    let p = basic_foo_bar_project();
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();
    // Modify the schema to pretend this is done by a future version of cargo.
    let config = ConfigBuilder::new().build();
    let db_path = GlobalCacheTracker::db_path(&config).into_path_unlocked();
    let conn = rusqlite::Connection::open(&db_path)?;
    let user_version: u32 =
        conn.query_row("SELECT user_version FROM pragma_user_version", [], |row| {
            row.get(0)
        })?;
    conn.execute("ALTER TABLE global_data ADD COLUMN foo DEFAULT 123", [])?;
    conn.pragma_update(None, "user_version", &(user_version + 1))?;
    drop(conn);
    // Verify it doesn't blow up.
    p.cargo("clean gc --max-download-size=0 -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr("[REMOVED] 4 files, [..] total")
        .run();
    Ok(())
}

#[cargo_test]
fn clean_max_git_age() {
    // --max-git-*-age flags
    let (git_a, git_a_repo) = git::new_repo("git_a", |p| {
        p.file("Cargo.toml", &basic_manifest("git_a", "1.0.0"))
            .file("src/lib.rs", "")
    });
    let p = project()
        .file(
            "Cargo.toml",
            &format!(
                r#"
                [package]
                name = "foo"
                version = "0.1.0"

                [dependencies]
                git_a = {{ git = '{}' }}
            "#,
                git_a.url()
            ),
        )
        .file("src/lib.rs", "")
        .build();
    // Populate last-use tracking.
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", days_ago_unix(4))
        .run();
    // Update git_a to create a separate checkout.
    git_a.change_file("src/lib.rs", "// test");
    git::add(&git_a_repo);
    git::commit(&git_a_repo);
    // Update last-use tracking, where the first git checkout will stay "old".
    p.cargo("update -p git_a -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", days_ago_unix(2))
        .with_stderr(
            "\
[UPDATING] git repository [..]
[UPDATING] git_a v1.0.0 [..]
",
        )
        .run();

    let db_names = get_git_db_names();
    assert_eq!(db_names.len(), 1);
    let db_name = &db_names[0];
    let co_names = get_git_checkout_names(&db_name);
    assert_eq!(co_names.len(), 2);

    // Delete the first checkout
    p.cargo("clean gc -v -Zgc")
        .arg("--max-git-co-age=3 days")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [ROOT]/home/.cargo/git/checkouts/git_a-[..]/[..]
[REMOVED] [..]
",
        )
        .run();

    let db_names = get_git_db_names();
    assert_eq!(db_names.len(), 1);
    let co_names = get_git_checkout_names(&db_name);
    assert_eq!(co_names.len(), 1);

    // delete the second checkout
    p.cargo("clean gc -v -Zgc")
        .arg("--max-git-co-age=0 days")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [ROOT]/home/.cargo/git/checkouts/git_a-[..]/[..]
[REMOVED] [..]
",
        )
        .run();

    let db_names = get_git_db_names();
    assert_eq!(db_names.len(), 1);
    let co_names = get_git_checkout_names(&db_name);
    assert_eq!(co_names.len(), 0);

    // delete the db
    p.cargo("clean gc -v -Zgc")
        .arg("--max-git-db-age=1 days")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [ROOT]/home/.cargo/git/db/git_a-[..]
[REMOVING] [ROOT]/home/.cargo/git/checkouts/git_a-[..]
[REMOVED] [..]
",
        )
        .run();

    let db_names = get_git_db_names();
    assert_eq!(db_names.len(), 0);
    let co_names = get_git_checkout_names(&db_name);
    assert_eq!(co_names.len(), 0);
}

#[cargo_test]
fn clean_max_src_crate_age() {
    // --max-src-age and --max-crate-age flags
    let p = basic_foo_bar_project();
    // Populate last-use tracking.
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", days_ago_unix(4))
        .run();
    // Update bar to create a separate copy with a different timestamp.
    Package::new("bar", "1.0.1").publish();
    p.cargo("update -p bar -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", days_ago_unix(2))
        .with_stderr(
            "\
[UPDATING] `dummy-registry` index
[UPDATING] bar v1.0.0 -> v1.0.1
",
        )
        .run();
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", days_ago_unix(2))
        .with_stderr(
            "\
[DOWNLOADING] crates ...
[DOWNLOADED] bar v1.0.1 [..]
",
        )
        .run();

    assert_eq!(get_registry_names("src"), ["bar-1.0.0", "bar-1.0.1"]);
    assert_eq!(
        get_registry_names("cache"),
        ["bar-1.0.0.crate", "bar-1.0.1.crate"]
    );

    // Delete the old src.
    p.cargo("clean gc -v -Zgc")
        .arg("--max-src-age=3 days")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [..]/bar-1.0.0
[REMOVED] [..]
",
        )
        .run();

    // delete the second src
    p.cargo("clean gc -v -Zgc")
        .arg("--max-src-age=0 days")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [..]/bar-1.0.1
[REMOVED] [..]
",
        )
        .run();

    // delete the old crate
    p.cargo("clean gc -v -Zgc")
        .arg("--max-crate-age=3 days")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [..]/bar-1.0.0.crate
[REMOVED] [..]
",
        )
        .run();

    // delete the seecond crate
    p.cargo("clean gc -v -Zgc")
        .arg("--max-crate-age=0 days")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [..]/bar-1.0.1.crate
[REMOVED] [..]
",
        )
        .run();
}

#[cargo_test]
fn clean_max_git_size() {
    // clean --max-git-size
    //
    // Creates two checkouts. The sets a size threshold to delete one. And
    // then with 0 max size to delete everything.
    let (git_project, git_repo) = git::new_repo("bar", |p| {
        p.file("Cargo.toml", &basic_manifest("bar", "1.0.0"))
            .file("src/lib.rs", "")
    });
    let p = project()
        .file(
            "Cargo.toml",
            &format!(
                r#"
                [package]
                name = "foo"
                version = "0.1.0"

                [dependencies]
                bar = {{ git = '{}' }}
            "#,
                git_project.url()
            ),
        )
        .file("src/lib.rs", "")
        .build();
    // Fetch and populate db.
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", days_ago_unix(3))
        .run();

    // Figure out the name of the first checkout.
    let git_root = paths::home().join(".cargo/git");
    let db_names = get_git_db_names();
    assert_eq!(db_names.len(), 1);
    let db_name = &db_names[0];
    let co_names = get_git_checkout_names(&db_name);
    assert_eq!(co_names.len(), 1);
    let first_co_name = &co_names[0];

    // Make an update and create a new checkout.
    git_project.change_file("src/lib.rs", "// modified");
    git::add(&git_repo);
    git::commit(&git_repo);
    p.cargo("update -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        // Use a different time so that the first checkout timestamp is less
        // than the second.
        .env("__CARGO_TEST_LAST_USE_NOW", days_ago_unix(2))
        .run();

    // Figure out the threshold to use.
    let mut co_names = get_git_checkout_names(&db_name);
    assert_eq!(co_names.len(), 2);
    co_names.retain(|name| name != first_co_name);
    assert_eq!(co_names.len(), 1);
    let second_co_name = &co_names[0];
    let second_co_path = git_root
        .join("checkouts")
        .join(db_name)
        .join(second_co_name);
    let second_co_size = cargo_util::du(&second_co_path, &["!.git"]).unwrap();

    let db_size = cargo_util::du(&git_root.join("db").join(db_name), &[]).unwrap();

    let threshold = db_size + second_co_size;

    p.cargo(&format!("clean gc --max-git-size={threshold} -Zgc -v"))
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(&format!(
            "\
[REMOVING] [ROOT]/home/.cargo/git/checkouts/{db_name}/{first_co_name}
[REMOVED] [..]
"
        ))
        .run();

    // And then try cleaning everything.
    p.cargo("clean gc --max-git-size=0 -Zgc -v")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr_unordered(&format!(
            "\
[REMOVING] [ROOT]/home/.cargo/git/checkouts/{db_name}/{second_co_name}
[REMOVING] [ROOT]/home/.cargo/git/db/{db_name}
[REMOVED] [..]
"
        ))
        .run();
}

// Helper for setting up fake git sizes for git size cleaning.
fn setup_fake_git_sizes(db_name: &str, db_size: usize, co_sizes: &[usize]) {
    let base_git = paths::home().join(".cargo/git");
    let db_path = base_git.join("db").join(db_name);
    db_path.mkdir_p();
    std::fs::write(db_path.join("test"), "x".repeat(db_size)).unwrap();
    let base_co = base_git.join("checkouts").join(db_name);
    for (i, size) in co_sizes.iter().enumerate() {
        let co_name = format!("co{i}");
        let co_path = base_co.join(co_name);
        co_path.mkdir_p();
        std::fs::write(co_path.join("test"), "x".repeat(*size)).unwrap();
    }
}

#[cargo_test]
fn clean_max_git_size_untracked() {
    // If there are git directories that aren't tracked in the database,
    // `--max-git-size` should pick it up.
    //
    // The db_name of "example" depends on the sorting order of the names ("e"
    // should be after "c"), so that the db comes after the checkouts.
    setup_fake_git_sizes("example", 5000, &[1000, 2000]);
    cargo_process(&format!("clean gc -Zgc -v --max-git-size=7000"))
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [ROOT]/home/.cargo/git/checkouts/example/co0
[REMOVED] [..]
",
        )
        .run();
    cargo_process(&format!("clean gc -Zgc -v --max-git-size=5000"))
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [ROOT]/home/.cargo/git/checkouts/example/co1
[REMOVED] [..]
",
        )
        .run();
    cargo_process(&format!("clean gc -Zgc -v --max-git-size=0"))
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [ROOT]/home/.cargo/git/db/example
[REMOVED] [..]
",
        )
        .run();
}

#[cargo_test]
fn clean_max_git_size_deletes_co_from_db() {
    // In the scenario where it thinks it needs to delete the db, it should
    // also delete all the checkouts.
    //
    // The db_name of "abc" depends on the sorting order of the names ("a"
    // should be before "c"), so that the db comes before the checkouts.
    setup_fake_git_sizes("abc", 5000, &[1000, 2000]);
    // This deletes everything because it tries to delete the db, which then
    // deletes all checkouts.
    cargo_process(&format!("clean gc -Zgc -v --max-git-size=3000"))
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [ROOT]/home/.cargo/git/db/abc
[REMOVING] [ROOT]/home/.cargo/git/checkouts/abc/co1
[REMOVING] [ROOT]/home/.cargo/git/checkouts/abc/co0
[REMOVED] [..]
",
        )
        .run();
}

#[cargo_test]
fn handles_missing_index() {
    // Checks behavior when index is missing.
    let p = basic_foo_bar_project();
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();
    paths::home().join(".cargo/registry/index").rm_rf();
    cargo_process("clean gc -v --max-download-size=0 -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr_unordered(
            "\
[REMOVING] [ROOT]/home/.cargo/registry/cache/[..]
[REMOVING] [ROOT]/home/.cargo/registry/src/[..]
[REMOVED] [..]
",
        )
        .run();
}

#[cargo_test]
fn handles_missing_git_db() {
    // Checks behavior when git db is missing.
    let git_project = git::new("bar", |p| {
        p.file("Cargo.toml", &basic_manifest("bar", "1.0.0"))
            .file("src/lib.rs", "")
    });
    let p = project()
        .file(
            "Cargo.toml",
            &format!(
                r#"
                [package]
                name = "foo"
                version = "0.1.0"

                [dependencies]
                bar = {{ git = '{}' }}
            "#,
                git_project.url()
            ),
        )
        .file("src/lib.rs", "")
        .build();
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .run();
    paths::home().join(".cargo/git/db").rm_rf();
    cargo_process("clean gc -v --max-git-size=0 -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stderr(
            "\
[REMOVING] [ROOT]/home/.cargo/git/checkouts/[..]
[REMOVED] [..]
",
        )
        .run();
}

#[cargo_test]
fn clean_gc_quiet_is_quiet() {
    // Checks that --quiet works with `cargo clean gc`, since there was a
    // subtle issue with how the flag is defined as a global flag.
    let p = basic_foo_bar_project();
    p.cargo("fetch -Zgc")
        .masquerade_as_nightly_cargo(&["gc"])
        .env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
        .run();
    p.cargo("clean gc --quiet -Zgc --dry-run")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stdout("")
        .with_stderr("")
        .run();
    // Verify exact same command without -q would actually display something.
    p.cargo("clean gc -Zgc --dry-run")
        .masquerade_as_nightly_cargo(&["gc"])
        .with_stdout("")
        .with_stderr(
            "\
[SUMMARY] [..] files, [..] total
[WARNING] no files deleted due to --dry-run
",
        )
        .run();
}