Skip to content

Commit 6e9da76

Browse files
committed
Auto merge of #11278 - ehuss:deleted-entry-test, r=epage
Add test for deleted index entry This adds a test for when an entry is deleted from the index. This is done on crates.io occasionally for things like copyright takedown requests. This behavior was tripping a debug assert which this removes. I'm not entirely certain why the debug assert is there, but I think it is not correct, since this is obviously a scenario where there might be a cached file, but the index responds with NotFound.
2 parents 071eeaf + 5e6da66 commit 6e9da76

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

src/cargo/sources/registry/index.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,6 @@ impl Summaries {
581581
return Poll::Ready(Ok(cached_summaries));
582582
}
583583
LoadResponse::NotFound => {
584-
debug_assert!(cached_summaries.is_none());
585584
if let Err(e) = fs::remove_file(cache_path) {
586585
if e.kind() != ErrorKind::NotFound {
587586
log::debug!("failed to remove from cache: {}", e);

tests/testsuite/registry.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,3 +2827,117 @@ internal server error
28272827
)
28282828
.run();
28292829
}
2830+
2831+
#[cargo_test]
2832+
fn deleted_entry() {
2833+
// Checks the behavior when a package is removed from the index.
2834+
// This is done occasionally on crates.io to handle things like
2835+
// copyright takedowns.
2836+
let p = project()
2837+
.file(
2838+
"Cargo.toml",
2839+
r#"
2840+
[package]
2841+
name = "foo"
2842+
version = "0.1.0"
2843+
2844+
[dependencies]
2845+
bar = "0.1"
2846+
"#,
2847+
)
2848+
.file("src/lib.rs", "")
2849+
.build();
2850+
2851+
// First, test removing a single version, but leaving an older version.
2852+
Package::new("bar", "0.1.0").publish();
2853+
let bar_path = Path::new("3/b/bar");
2854+
let bar_reg_path = registry_path().join(&bar_path);
2855+
let old_index = fs::read_to_string(&bar_reg_path).unwrap();
2856+
Package::new("bar", "0.1.1").publish();
2857+
p.cargo("tree")
2858+
.with_stderr(
2859+
"\
2860+
[UPDATING] `dummy-registry` index
2861+
[DOWNLOADING] crates ...
2862+
[DOWNLOADED] bar v0.1.1 (registry `dummy-registry`)
2863+
",
2864+
)
2865+
.with_stdout(
2866+
"\
2867+
foo v0.1.0 ([ROOT]/foo)
2868+
└── bar v0.1.1
2869+
",
2870+
)
2871+
.run();
2872+
2873+
// Remove 0.1.1
2874+
fs::remove_file(paths::root().join("dl/bar/0.1.1/download")).unwrap();
2875+
let repo = git2::Repository::open(registry_path()).unwrap();
2876+
let mut index = repo.index().unwrap();
2877+
fs::write(&bar_reg_path, &old_index).unwrap();
2878+
index.add_path(&bar_path).unwrap();
2879+
index.write().unwrap();
2880+
git::commit(&repo);
2881+
2882+
// With `Cargo.lock` unchanged, it shouldn't have an impact.
2883+
p.cargo("tree")
2884+
.with_stderr("")
2885+
.with_stdout(
2886+
"\
2887+
foo v0.1.0 ([ROOT]/foo)
2888+
└── bar v0.1.1
2889+
",
2890+
)
2891+
.run();
2892+
2893+
// Regenerating Cargo.lock should switch to old version.
2894+
fs::remove_file(p.root().join("Cargo.lock")).unwrap();
2895+
p.cargo("tree")
2896+
.with_stderr(
2897+
"\
2898+
[UPDATING] `dummy-registry` index
2899+
[DOWNLOADING] crates ...
2900+
[DOWNLOADED] bar v0.1.0 (registry `dummy-registry`)
2901+
",
2902+
)
2903+
.with_stdout(
2904+
"\
2905+
foo v0.1.0 ([ROOT]/foo)
2906+
└── bar v0.1.0
2907+
",
2908+
)
2909+
.run();
2910+
2911+
// Remove the package entirely.
2912+
fs::remove_file(paths::root().join("dl/bar/0.1.0/download")).unwrap();
2913+
let mut index = repo.index().unwrap();
2914+
index.remove(&bar_path, 0).unwrap();
2915+
index.write().unwrap();
2916+
git::commit(&repo);
2917+
fs::remove_file(&bar_reg_path).unwrap();
2918+
2919+
// With `Cargo.lock` unchanged, it shouldn't have an impact.
2920+
p.cargo("tree")
2921+
.with_stderr("")
2922+
.with_stdout(
2923+
"\
2924+
foo v0.1.0 ([ROOT]/foo)
2925+
└── bar v0.1.0
2926+
",
2927+
)
2928+
.run();
2929+
2930+
// Regenerating Cargo.lock should fail.
2931+
fs::remove_file(p.root().join("Cargo.lock")).unwrap();
2932+
p.cargo("tree")
2933+
.with_stderr(
2934+
"\
2935+
[UPDATING] `dummy-registry` index
2936+
error: no matching package named `bar` found
2937+
location searched: registry `crates-io`
2938+
required by package `foo v0.1.0 ([ROOT]/foo)`
2939+
",
2940+
)
2941+
.with_status(101)
2942+
.run();
2943+
}

0 commit comments

Comments
 (0)