From 4f93debeba2d911083d0466827e3aad2e0be225c Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 20 May 2024 12:28:16 -0400 Subject: [PATCH] Always print JSON output with --format json --- crates/uv/src/commands/pip/list.rs | 15 ++++----- crates/uv/tests/pip_list.rs | 52 +++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/crates/uv/src/commands/pip/list.rs b/crates/uv/src/commands/pip/list.rs index 5b91881479bd..af20ccc6dde2 100644 --- a/crates/uv/src/commands/pip/list.rs +++ b/crates/uv/src/commands/pip/list.rs @@ -65,11 +65,14 @@ pub(crate) fn pip_list( .filter(|dist| !exclude.contains(dist.name())) .sorted_unstable_by(|a, b| a.name().cmp(b.name()).then(a.version().cmp(b.version()))) .collect_vec(); - if results.is_empty() { - return Ok(ExitStatus::Success); - } 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}")?; + } + ListFormat::Columns if results.is_empty() => {} ListFormat::Columns => { // The package name and version are always present. let mut columns = vec![ @@ -111,11 +114,7 @@ pub(crate) fn pip_list( writeln!(printer.stdout(), "{}", elems.join(" ").trim_end())?; } } - ListFormat::Json => { - let rows = results.iter().copied().map(Entry::from).collect_vec(); - let output = serde_json::to_string(&rows)?; - writeln!(printer.stdout(), "{output}")?; - } + ListFormat::Freeze if results.is_empty() => {} ListFormat::Freeze => { for dist in &results { writeln!( diff --git a/crates/uv/tests/pip_list.rs b/crates/uv/tests/pip_list.rs index f9f286b7800d..73cb88c94407 100644 --- a/crates/uv/tests/pip_list.rs +++ b/crates/uv/tests/pip_list.rs @@ -36,12 +36,14 @@ fn install_command(context: &TestContext) -> Command { } #[test] -fn list_empty() { +fn list_empty_columns() { let context = TestContext::new("3.12"); uv_snapshot!(Command::new(get_bin()) .arg("pip") .arg("list") + .arg("--format") + .arg("columns") .arg("--cache-dir") .arg(context.cache_dir.path()) .env("VIRTUAL_ENV", context.venv.as_os_str()) @@ -56,6 +58,53 @@ fn list_empty() { ); } +#[test] +fn list_empty_freeze() { + let context = TestContext::new("3.12"); + + uv_snapshot!(Command::new(get_bin()) + .arg("pip") + .arg("list") + .arg("--format") + .arg("freeze") + .arg("--cache-dir") + .arg(context.cache_dir.path()) + .env("VIRTUAL_ENV", context.venv.as_os_str()) + .env("UV_NO_WRAP", "1") + .current_dir(&context.temp_dir), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + "### + ); +} + +#[test] +fn list_empty_json() { + let context = TestContext::new("3.12"); + + uv_snapshot!(Command::new(get_bin()) + .arg("pip") + .arg("list") + .arg("--format") + .arg("json") + .arg("--cache-dir") + .arg(context.cache_dir.path()) + .env("VIRTUAL_ENV", context.venv.as_os_str()) + .env("UV_NO_WRAP", "1") + .current_dir(&context.temp_dir), @r###" + success: true + exit_code: 0 + ----- stdout ----- + [] + + ----- stderr ----- + "### + ); +} + #[test] fn list_single_no_editable() -> Result<()> { let context = TestContext::new("3.12"); @@ -451,6 +500,7 @@ fn list_format_json() { success: true exit_code: 0 ----- stdout ----- + [] ----- stderr ----- "###