Skip to content

Commit

Permalink
Hide index URLs from header if not emitted (#1835)
Browse files Browse the repository at this point in the history
## Summary

Hey guys! The motivation described in #1834

## Test Plan

Changed snapshot of the existing tests. `--index-url` and
`--extra-index-url` occur pretty often, so no extra testing is required,
imo.
  • Loading branch information
DrJackilD authored Feb 23, 2024
1 parent 0212cb7 commit a7b5c55
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
41 changes: 32 additions & 9 deletions crates/uv/src/commands/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,7 @@ pub(crate) async fn pip_compile(
writeln!(
writer,
"{}",
format!(
"# uv {}",
env::args_os()
.skip(1)
.map(|arg| arg.normalized_display().to_string())
.join(" ")
)
.green()
format!("# {}", cmd(include_index_url)).green()
)?;
}

Expand Down Expand Up @@ -398,13 +391,43 @@ pub(crate) async fn pip_compile(
&resolution,
generate_hashes,
include_annotations,
annotation_style
annotation_style,
)
)?;

Ok(ExitStatus::Success)
}

/// Format the `uv` command used to generate the output file.
fn cmd(include_index_url: bool) -> String {
let args = env::args_os()
.skip(1)
.map(|arg| arg.normalized_display().to_string())
.scan(None, move |skip_next, arg| {
if let Some(true) = skip_next {
// Reset state; skip this iteration.
*skip_next = None;
Some(None)
} else if !include_index_url
&& (arg.starts_with("--extra-index-url=") || arg.starts_with("--index-url="))
{
// Reset state; skip this iteration.
*skip_next = None;
Some(None)
} else if !include_index_url && (arg == "--extra-index-url" || arg == "--index-url") {
// Mark the next item as (to be) skipped.
*skip_next = Some(true);
Some(None)
} else {
// Return the argument.
Some(Some(arg))
}
})
.flatten()
.join(" ");
format!("uv {args}")
}

/// Whether to allow package upgrades.
#[derive(Debug)]
pub(crate) enum Upgrade {
Expand Down
20 changes: 11 additions & 9 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,9 @@ fn compile_python_37() -> Result<()> {
"",
),
]
.into_iter()
.chain(INSTA_FILTERS.to_vec())
.collect();
.into_iter()
.chain(INSTA_FILTERS.to_vec())
.collect();

uv_snapshot!(filters, context.compile()
.arg("requirements.in")
Expand Down Expand Up @@ -2608,7 +2608,7 @@ fn compile_html() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in --cache-dir [CACHE_DIR] --index-url https://download.pytorch.org/whl
# uv pip compile requirements.in --cache-dir [CACHE_DIR]
jinja2==3.1.2
markupsafe==2.1.3
# via jinja2
Expand Down Expand Up @@ -2636,7 +2636,7 @@ fn trailing_slash() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --index-url https://test.pypi.org/simple
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in
jinja2==3.1.2
markupsafe==2.1.3
# via jinja2
Expand All @@ -2654,7 +2654,7 @@ fn trailing_slash() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --index-url https://test.pypi.org/simple/
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in
jinja2==3.1.2
markupsafe==2.1.3
# via jinja2
Expand Down Expand Up @@ -3393,6 +3393,7 @@ fn resolver_legacy() -> Result<()> {
}

/// Emit the `--index-url` and `--extra-index-url` locations.
/// Also, preserve the `--index-url` and `--extra-index-url` flags in the command in the header.
#[test]
fn emit_index_urls() -> Result<()> {
let context = TestContext::new("3.12");
Expand Down Expand Up @@ -3499,7 +3500,8 @@ fn no_index_requirements_txt() -> Result<()> {
}

/// Prefer the `--index-url` from the command line over the `--index-url` in a `requirements.txt`
/// file.
/// file. Also, `--index-url` and `--extra-index-url` should not be presented in the output
/// unless we specify `--emit-index-url`.
#[test]
fn index_url_requirements_txt() -> Result<()> {
let context = TestContext::new("3.12");
Expand All @@ -3514,7 +3516,7 @@ fn index_url_requirements_txt() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --index-url https://pypi.org/simple
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in
tqdm==4.66.1
----- stderr -----
Expand Down Expand Up @@ -3905,7 +3907,7 @@ fn index_url_from_command_line() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --index-url https://pypi.org/simple
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in
anyio==3.7.1
idna==3.4
# via anyio
Expand Down
6 changes: 3 additions & 3 deletions crates/uv/tests/pip_compile_scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn requires_incompatible_python_version_compatible_override() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in --index-url https://test.pypi.org/simple --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.11
# uv pip compile requirements.in --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.11
albatross==1.0.0
----- stderr -----
Expand Down Expand Up @@ -244,7 +244,7 @@ fn requires_incompatible_python_version_compatible_override_no_wheels_available_
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in --index-url https://test.pypi.org/simple --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.11
# uv pip compile requirements.in --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.11
albatross==1.0.0
----- stderr -----
Expand Down Expand Up @@ -464,7 +464,7 @@ fn requires_python_patch_version_override_patch_compatible() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in --index-url https://test.pypi.org/simple --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.8.0
# uv pip compile requirements.in --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.8.0
albatross==1.0.0
----- stderr -----
Expand Down

0 comments on commit a7b5c55

Please sign in to comment.