From 5e6da66c039bb47a18ec3286c121f34a61d7f1d0 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 22 Oct 2022 18:16:46 -0700 Subject: [PATCH] Add test for deleted index entry --- src/cargo/sources/registry/index.rs | 1 - tests/testsuite/registry.rs | 114 ++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/src/cargo/sources/registry/index.rs b/src/cargo/sources/registry/index.rs index 7b896a7db4f..f5ac24944e8 100644 --- a/src/cargo/sources/registry/index.rs +++ b/src/cargo/sources/registry/index.rs @@ -581,7 +581,6 @@ impl Summaries { return Poll::Ready(Ok(cached_summaries)); } LoadResponse::NotFound => { - debug_assert!(cached_summaries.is_none()); if let Err(e) = fs::remove_file(cache_path) { if e.kind() != ErrorKind::NotFound { log::debug!("failed to remove from cache: {}", e); diff --git a/tests/testsuite/registry.rs b/tests/testsuite/registry.rs index 0a3d9b6629b..60c52fd5462 100644 --- a/tests/testsuite/registry.rs +++ b/tests/testsuite/registry.rs @@ -2827,3 +2827,117 @@ internal server error ) .run(); } + +#[cargo_test] +fn deleted_entry() { + // Checks the behavior when a package is removed from the index. + // This is done occasionally on crates.io to handle things like + // copyright takedowns. + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [dependencies] + bar = "0.1" + "#, + ) + .file("src/lib.rs", "") + .build(); + + // First, test removing a single version, but leaving an older version. + Package::new("bar", "0.1.0").publish(); + let bar_path = Path::new("3/b/bar"); + let bar_reg_path = registry_path().join(&bar_path); + let old_index = fs::read_to_string(&bar_reg_path).unwrap(); + Package::new("bar", "0.1.1").publish(); + p.cargo("tree") + .with_stderr( + "\ +[UPDATING] `dummy-registry` index +[DOWNLOADING] crates ... +[DOWNLOADED] bar v0.1.1 (registry `dummy-registry`) +", + ) + .with_stdout( + "\ +foo v0.1.0 ([ROOT]/foo) +└── bar v0.1.1 +", + ) + .run(); + + // Remove 0.1.1 + fs::remove_file(paths::root().join("dl/bar/0.1.1/download")).unwrap(); + let repo = git2::Repository::open(registry_path()).unwrap(); + let mut index = repo.index().unwrap(); + fs::write(&bar_reg_path, &old_index).unwrap(); + index.add_path(&bar_path).unwrap(); + index.write().unwrap(); + git::commit(&repo); + + // With `Cargo.lock` unchanged, it shouldn't have an impact. + p.cargo("tree") + .with_stderr("") + .with_stdout( + "\ +foo v0.1.0 ([ROOT]/foo) +└── bar v0.1.1 +", + ) + .run(); + + // Regenerating Cargo.lock should switch to old version. + fs::remove_file(p.root().join("Cargo.lock")).unwrap(); + p.cargo("tree") + .with_stderr( + "\ +[UPDATING] `dummy-registry` index +[DOWNLOADING] crates ... +[DOWNLOADED] bar v0.1.0 (registry `dummy-registry`) +", + ) + .with_stdout( + "\ +foo v0.1.0 ([ROOT]/foo) +└── bar v0.1.0 +", + ) + .run(); + + // Remove the package entirely. + fs::remove_file(paths::root().join("dl/bar/0.1.0/download")).unwrap(); + let mut index = repo.index().unwrap(); + index.remove(&bar_path, 0).unwrap(); + index.write().unwrap(); + git::commit(&repo); + fs::remove_file(&bar_reg_path).unwrap(); + + // With `Cargo.lock` unchanged, it shouldn't have an impact. + p.cargo("tree") + .with_stderr("") + .with_stdout( + "\ +foo v0.1.0 ([ROOT]/foo) +└── bar v0.1.0 +", + ) + .run(); + + // Regenerating Cargo.lock should fail. + fs::remove_file(p.root().join("Cargo.lock")).unwrap(); + p.cargo("tree") + .with_stderr( + "\ +[UPDATING] `dummy-registry` index +error: no matching package named `bar` found +location searched: registry `crates-io` +required by package `foo v0.1.0 ([ROOT]/foo)` +", + ) + .with_status(101) + .run(); +}