From cedb5e4aec51c61638f63ddce2423e5dfc2a4ec5 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 5 Apr 2024 13:10:23 -0400 Subject: [PATCH] Add a test for `--offline` direct URLs (#2837) --- crates/uv/tests/pip_compile.rs | 85 ++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 4 deletions(-) diff --git a/crates/uv/tests/pip_compile.rs b/crates/uv/tests/pip_compile.rs index bf26d9ffb..91252ac7c 100644 --- a/crates/uv/tests/pip_compile.rs +++ b/crates/uv/tests/pip_compile.rs @@ -4319,9 +4319,9 @@ fn matching_index_urls_requirements_txt() -> Result<()> { Ok(()) } -/// Resolve without network access via the `--offline` flag. +/// Resolve a registry package without network access via the `--offline` flag. #[test] -fn offline() -> Result<()> { +fn offline_registry() -> Result<()> { let context = TestContext::new("3.12"); let requirements_in = context.temp_dir.child("requirements.in"); requirements_in.write_str("black==23.10.1")?; @@ -4397,8 +4397,8 @@ fn offline() -> Result<()> { Ok(()) } -/// Resolve without network access via the `--offline` flag, using `--find-links` for an HTML -/// registry. +/// Resolve a package without network access via the `--offline` flag, using `--find-links` for an +/// HTML registry. #[test] fn offline_find_links() -> Result<()> { let context = TestContext::new("3.12"); @@ -4449,6 +4449,61 @@ fn offline_find_links() -> Result<()> { Ok(()) } +/// Resolve a direct URL package without network access via the `--offline` flag. +#[test] +fn offline_direct_url() -> Result<()> { + let context = TestContext::new("3.12"); + let requirements_in = context.temp_dir.child("requirements.in"); + requirements_in.write_str("iniconfig @ https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl")?; + + // Resolve with `--offline` with an empty cache. + uv_snapshot!(context.compile() + .arg("requirements.in") + .arg("--offline"), @r###" + success: false + exit_code: 2 + ----- stdout ----- + + ----- stderr ----- + error: Failed to download: iniconfig @ https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl + Caused by: Network connectivity is disabled, but the requested data wasn't found in the cache for: `https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl` + "### + ); + + // Populate the cache. + uv_snapshot!(context.compile() + .arg("requirements.in"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + # This file was autogenerated by uv via the following command: + # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z requirements.in + iniconfig @ https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl + + ----- stderr ----- + Resolved 1 package in [TIME] + "### + ); + + // Resolve with `--offline` with a populated cache. + uv_snapshot!(context.compile() + .arg("requirements.in") + .arg("--offline"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + # This file was autogenerated by uv via the following command: + # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z requirements.in --offline + iniconfig @ https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl + + ----- stderr ----- + Resolved 1 package in [TIME] + "### + ); + + Ok(()) +} + /// Resolve nested `-r` requirements files with relative paths. #[test] fn compile_relative_subfile() -> Result<()> { @@ -5847,6 +5902,28 @@ fn no_stream() -> Result<()> { Ok(()) } +/// Resolve a direct URL package with a URL that doesn't exist (i.e., returns a 404). +#[test] +fn not_found_direct_url() -> Result<()> { + let context = TestContext::new("3.12"); + let requirements_in = context.temp_dir.child("requirements.in"); + requirements_in.write_str("iniconfig @ https://files.pythonhosted.org/packages/ef/a6/fake/iniconfig-2.0.0-py3-none-any.whl")?; + + uv_snapshot!(context.compile() + .arg("requirements.in"), @r###" + success: false + exit_code: 2 + ----- stdout ----- + + ----- stderr ----- + error: Failed to download: iniconfig @ https://files.pythonhosted.org/packages/ef/a6/fake/iniconfig-2.0.0-py3-none-any.whl + Caused by: HTTP status client error (404 Not Found) for url (https://files.pythonhosted.org/packages/ef/a6/fake/iniconfig-2.0.0-py3-none-any.whl) + "### + ); + + Ok(()) +} + /// Raise an error when a direct URL dependency's `Requires-Python` constraint is not met. #[test] fn requires_python_direct_url() -> Result<()> {