From 915bd38f32cac392aa1860b07cdfb68b08629db6 Mon Sep 17 00:00:00 2001 From: Zanie Date: Sat, 3 Feb 2024 10:52:59 -0600 Subject: [PATCH] Add `--no-index` test cases to `pip sync` --- crates/puffin/tests/pip_sync.rs | 90 +++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/crates/puffin/tests/pip_sync.rs b/crates/puffin/tests/pip_sync.rs index f0898a362bb9..6f231b246437 100644 --- a/crates/puffin/tests/pip_sync.rs +++ b/crates/puffin/tests/pip_sync.rs @@ -42,6 +42,19 @@ fn command(context: &TestContext) -> Command { command } +/// Create a `pip uninstall` command with options shared across scenarios. +fn uninstall_command(context: &TestContext) -> Command { + let mut command = Command::new(get_bin()); + command + .arg("pip") + .arg("uninstall") + .arg("--cache-dir") + .arg(context.cache_dir.path()) + .env("VIRTUAL_ENV", context.venv.as_os_str()) + .current_dir(&context.temp_dir); + command +} + #[test] fn missing_requirements_txt() { let context = TestContext::new("3.12"); @@ -802,6 +815,83 @@ fn install_no_binary() -> Result<()> { Ok(()) } +/// Attempt to install a package without using a remote index. +#[test] +fn install_no_index() -> Result<()> { + let context = TestContext::new("3.12"); + + let requirements_txt = context.temp_dir.child("requirements.txt"); + requirements_txt.touch()?; + requirements_txt.write_str("MarkupSafe==2.1.3")?; + + puffin_snapshot!(command(&context) + .arg("requirements.txt") + .arg("--no-index") + .arg("--strict"), @r###" + success: false + exit_code: 2 + ----- stdout ----- + + ----- stderr ----- + error: markupsafe isn't available locally, but making network requests to registries was banned. + "### + ); + + context.assert_command("import markupsafe").failure(); + + Ok(()) +} + +/// Attempt to install a package without using a remote index +/// after a previous successful installation. +#[test] +fn install_no_index_cached() -> Result<()> { + let context = TestContext::new("3.12"); + + let requirements_txt = context.temp_dir.child("requirements.txt"); + requirements_txt.touch()?; + requirements_txt.write_str("MarkupSafe==2.1.3")?; + + puffin_snapshot!(command(&context) + .arg("requirements.txt") + .arg("--strict"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 1 package in [TIME] + Downloaded 1 package in [TIME] + Installed 1 package in [TIME] + + markupsafe==2.1.3 + "### + ); + + context.assert_command("import markupsafe").success(); + + uninstall_command(&context) + .arg("markupsafe") + .assert() + .success(); + + puffin_snapshot!(command(&context) + .arg("requirements.txt") + .arg("--no-index") + .arg("--strict"), @r###" + success: false + exit_code: 2 + ----- stdout ----- + + ----- stderr ----- + error: markupsafe isn't available locally, but making network requests to registries was banned. + "### + ); + + context.assert_command("import markupsafe").failure(); + + Ok(()) +} + #[test] fn warn_on_yanked_version() -> Result<()> { let context = TestContext::new("3.12");