Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify pip list and tree to print to stdout regardless of the --quiet flag #8392

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Modify pip list and tree to print to stdout regardless of the --quiet…
… flag
  • Loading branch information
flyaroundme committed Oct 20, 2024
commit dc705211b71ac2973d7c132e16af5c77e3dd3a18
15 changes: 6 additions & 9 deletions crates/uv/src/commands/pip/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use uv_python::{EnvironmentPreference, PythonEnvironment};

use crate::commands::pip::operations::report_target_environment;
use crate::commands::ExitStatus;
use crate::printer::Printer;
use crate::printer::{Printer, Stdout};

/// Enumerate the installed packages in the current environment.
#[allow(clippy::fn_params_excessive_bools)]
Expand Down Expand Up @@ -52,11 +52,13 @@ pub(crate) fn pip_list(
.sorted_unstable_by(|a, b| a.name().cmp(b.name()).then(a.version().cmp(b.version())))
.collect_vec();

// We force output to stdout specifically for `pip list` command (#8379)
let mut forced_stdout = Stdout::Enabled;
match format {
ListFormat::Json => {
let rows = results.iter().copied().map(Entry::from).collect_vec();
let output = serde_json::to_string(&rows)?;
writeln!(printer.stdout(), "{output}")?;
writeln!(forced_stdout, "{output}")?;
}
ListFormat::Columns if results.is_empty() => {}
ListFormat::Columns => {
Expand Down Expand Up @@ -97,18 +99,13 @@ pub(crate) fn pip_list(
}

for elems in MultiZip(columns.iter().map(Column::fmt).collect_vec()) {
writeln!(printer.stdout(), "{}", elems.join(" ").trim_end())?;
writeln!(forced_stdout, "{}", elems.join(" ").trim_end())?;
}
}
ListFormat::Freeze if results.is_empty() => {}
ListFormat::Freeze => {
for dist in &results {
writeln!(
printer.stdout(),
"{}=={}",
dist.name().bold(),
dist.version()
)?;
writeln!(forced_stdout, "{}=={}", dist.name().bold(), dist.version())?;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/uv/src/commands/project/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::commands::pip::loggers::DefaultResolveLogger;
use crate::commands::pip::resolution_markers;
use crate::commands::project::ProjectInterpreter;
use crate::commands::{project, ExitStatus, SharedState};
use crate::printer::Printer;
use crate::printer::{Printer, Stdout};
use crate::settings::ResolverSettings;

/// Run a command.
Expand Down Expand Up @@ -100,7 +100,8 @@ pub(crate) async fn tree(
invert,
);

write!(printer.stdout(), "{tree}")?;
// Making an exclusion specifically for tree subcommand (#8379)
write!(Stdout::Enabled, "{tree}")?;

Ok(ExitStatus::Success)
}
74 changes: 74 additions & 0 deletions crates/uv/tests/it/pip_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,77 @@ Version: 0.1-bulbasaur

Ok(())
}

#[test]
fn list_ignores_quiet_flag_format_freeze() {
let context = TestContext::new("3.12");

// Install the editable package.
uv_snapshot!(context.filters(), context
.pip_install()
.arg("-e")
.arg(context.workspace_root.join("scripts/packages/poetry_editable")), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 4 packages in [TIME]
Prepared 4 packages in [TIME]
Installed 4 packages in [TIME]
+ anyio==4.3.0
+ idna==3.6
+ poetry-editable==0.1.0 (from file://[WORKSPACE]/scripts/packages/poetry_editable)
+ sniffio==1.3.1
"###
);

let filters = context
.filters()
.into_iter()
.chain(vec![(r"\-\-\-\-\-\-+.*", "[UNDERLINE]"), (" +", " ")])
.collect::<Vec<_>>();

uv_snapshot!(filters, context.pip_list()
.arg("--format=freeze")
.arg("--quiet"), @r###"
success: true
exit_code: 0
----- stdout -----
anyio==4.3.0
idna==3.6
poetry-editable==0.1.0
sniffio==1.3.1

----- stderr -----
"###
);

uv_snapshot!(filters, context.pip_list()
.arg("--format=freeze")
.arg("--editable")
.arg("--quiet"), @r###"
success: true
exit_code: 0
----- stdout -----
poetry-editable==0.1.0

----- stderr -----
"###
);

uv_snapshot!(filters, context.pip_list()
.arg("--format=freeze")
.arg("--exclude-editable")
.arg("--quiet"), @r###"
success: true
exit_code: 0
----- stdout -----
anyio==4.3.0
idna==3.6
sniffio==1.3.1

----- stderr -----
"###
);
}
39 changes: 39 additions & 0 deletions crates/uv/tests/it/pip_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1744,3 +1744,42 @@ fn show_version_specifiers_with_package() {
"###
);
}

#[test]
fn print_output_even_with_quite_flag() {
let context = TestContext::new("3.12");

let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str("requests==2.31.0").unwrap();

uv_snapshot!(context
.pip_install()
.arg("-r")
.arg("requirements.txt")
.arg("--strict"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 5 packages in [TIME]
Prepared 5 packages in [TIME]
Installed 5 packages in [TIME]
+ certifi==2024.2.2
+ charset-normalizer==3.3.2
+ idna==3.6
+ requests==2.31.0
+ urllib3==2.2.1
"###
);

context.assert_command("import requests").success();
uv_snapshot!(context.filters(), context.pip_tree().arg("--quiet"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
"###
);
}
Loading