Skip to content

Commit

Permalink
Rename flag, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Apr 10, 2024
1 parent 5c02973 commit 6305a76
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
20 changes: 11 additions & 9 deletions crates/uv-resolver/src/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ pub struct DisplayResolutionGraph<'a> {
/// requested each package.
include_annotations: bool,
/// Whether to include indexes in the output, to indicate which index was used for each package.
include_indexes: bool,
include_index_annotation: bool,
/// The style of annotation comments, used to indicate the dependencies that requested each
/// package.
annotation_style: AnnotationStyle,
Expand Down Expand Up @@ -536,7 +536,7 @@ impl<'a> DisplayResolutionGraph<'a> {
show_hashes: bool,
include_extras: bool,
include_annotations: bool,
include_indexees: bool,
include_index_annotation: bool,
annotation_style: AnnotationStyle,
) -> DisplayResolutionGraph<'a> {
Self {
Expand All @@ -545,7 +545,7 @@ impl<'a> DisplayResolutionGraph<'a> {
show_hashes,
include_extras,
include_annotations,
include_indexes: include_indexees,
include_index_annotation,
annotation_style,
}
}
Expand Down Expand Up @@ -675,6 +675,8 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
// Determine the annotation comment and separator (between comment and requirement).
let mut annotation = None;

// If enabled, include annotations to indicate the dependencies that requested each
// package (e.g., `# via mypy`).
if self.include_annotations {
// Display all dependencies.
let mut edges = self
Expand Down Expand Up @@ -730,12 +732,12 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
writeln!(f, "{line}")?;
}

if self.include_indexes && node.index().is_some() {
writeln!(
f,
"{}",
format!(" # from {}", node.index().unwrap()).green()
)?;
// If enabled, include indexes to indicate which index was used for each package (e.g.,
// `# from https://pypi.org/simple`).
if self.include_index_annotation {
if let Some(index) = node.index() {
writeln!(f, "{}", format!(" # from {}", index).green())?;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/uv/src/commands/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ pub(crate) async fn pip_compile(
include_extras: bool,
include_annotations: bool,
include_header: bool,
include_indexes: bool,
custom_compile_command: Option<String>,
include_index_url: bool,
include_find_links: bool,
include_marker_expression: bool,
include_index_annotation: bool,
index_locations: IndexLocations,
index_strategy: IndexStrategy,
keyring_provider: KeyringProvider,
Expand Down Expand Up @@ -502,7 +502,7 @@ pub(crate) async fn pip_compile(
generate_hashes,
include_extras,
include_annotations,
include_indexes,
include_index_annotation,
annotation_style,
)
)?;
Expand Down
15 changes: 8 additions & 7 deletions crates/uv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ struct PipCompileArgs {
#[clap(long)]
no_header: bool,

/// Include comment annotations indicating the index of each package.
#[clap(long)]
include_indexes: bool,
/// Choose the style of the annotation comments, which indicate the source of each package.
#[clap(long, default_value_t=AnnotationStyle::Split, value_enum)]
annotation_style: AnnotationStyle,

/// Change header comment to reflect custom command wrapping `uv pip compile`.
#[clap(long, env = "UV_CUSTOM_COMPILE_COMMAND")]
Expand Down Expand Up @@ -499,9 +499,10 @@ struct PipCompileArgs {
#[clap(long, hide = true)]
emit_marker_expression: bool,

/// Choose the style of the annotation comments, which indicate the source of each package.
#[clap(long, default_value_t=AnnotationStyle::Split, value_enum)]
annotation_style: AnnotationStyle,
/// Include comment annotations indicating the index used to resolve each package (e.g.,
/// `# from https://pypi.org/simple`).
#[clap(long)]
emit_index_annotation: bool,

#[command(flatten)]
compat_args: compat::PipCompileCompatArgs,
Expand Down Expand Up @@ -1587,11 +1588,11 @@ async fn run() -> Result<ExitStatus> {
args.no_strip_extras,
!args.no_annotate,
!args.no_header,
args.include_indexes,
args.custom_compile_command,
args.emit_index_url,
args.emit_find_links,
args.emit_marker_expression,
args.emit_index_annotation,
index_urls,
args.index_strategy,
args.keyring_provider,
Expand Down
53 changes: 45 additions & 8 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7335,22 +7335,22 @@ fn compile_index_url_fallback_prefer_primary() -> Result<()> {
Ok(())
}

/// Ensure that `--include-indexes` prints the index URL for each package.
/// Ensure that `--emit-index-annotation` prints the index URL for each package.
#[test]
fn include_indexes_pypi_org_simple() -> Result<()> {
fn emit_index_annotation_pypi_org_simple() -> Result<()> {
let context = TestContext::new("3.12");

let requirements_in = context.temp_dir.child("requirements.in");
requirements_in.write_str("requests")?;

uv_snapshot!(context.compile()
.arg("requirements.in")
.arg("--include-indexes"), @r###"
.arg("--emit-index-annotation"), @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 --include-indexes
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z requirements.in --emit-index-annotation
certifi==2024.2.2
# via requests
# from https://pypi.org/simple
Expand All @@ -7374,9 +7374,46 @@ fn include_indexes_pypi_org_simple() -> Result<()> {
Ok(())
}

/// `--include-indexes` where packages are pulled from two distinct indexes.
/// Ensure that `--emit-index-annotation` plays nicely with `--annotation-style=line`.
#[test]
fn include_indexes_multiple_indexes() -> Result<()> {
fn emit_index_annotation_pypi_org_line() -> Result<()> {
let context = TestContext::new("3.12");

let requirements_in = context.temp_dir.child("requirements.in");
requirements_in.write_str("requests")?;

uv_snapshot!(context.compile()
.arg("requirements.in")
.arg("--emit-index-annotation")
.arg("--annotation-style")
.arg("line"), @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 --emit-index-annotation --annotation-style line
certifi==2024.2.2 # via requests
# from https://pypi.org/simple
charset-normalizer==3.3.2 # via requests
# from https://pypi.org/simple
idna==3.6 # via requests
# from https://pypi.org/simple
requests==2.31.0
# from https://pypi.org/simple
urllib3==2.2.1 # via requests
# from https://pypi.org/simple
----- stderr -----
Resolved 5 packages in [TIME]
"###
);

Ok(())
}

/// `--emit-index-annotation` where packages are pulled from two distinct indexes.
#[test]
fn emit_index_annotation_multiple_indexes() -> Result<()> {
let context = TestContext::new("3.12");

let requirements_in = context.temp_dir.child("requirements.in");
Expand All @@ -7386,12 +7423,12 @@ fn include_indexes_multiple_indexes() -> Result<()> {
.arg("requirements.in")
.arg("--extra-index-url")
.arg("https://test.pypi.org/simple")
.arg("--include-indexes"), @r###"
.arg("--emit-index-annotation"), @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 --include-indexes
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z requirements.in --emit-index-annotation
requests==2.5.4.1
# from https://test.pypi.org/simple
uv==0.1.24
Expand Down

0 comments on commit 6305a76

Please sign in to comment.