Always print JSON output with --format json (#3671)

## Summary

Closes https://github.com/astral-sh/uv/issues/3670.

## Test Plan

```
uv on  charlie/list:main
❯ cargo run pip list --editable
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/uv pip list --editable`
uv on  charlie/list:main
❯ cargo run pip list --editable --format json
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.16s
     Running `target/debug/uv pip list --editable --format json`
[]
uv on  charlie/list:main
❯ cargo run pip list --editable --format freeze
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/uv pip list --editable --format freeze`
uv on  charlie/list:main
❯ cargo run pip list --editable --format columns
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/uv pip list --editable --format columns`
```
This commit is contained in:
Charlie Marsh 2024-05-20 12:39:31 -04:00 committed by GitHub
parent 95c9621541
commit 223980e4bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 58 additions and 9 deletions

View file

@ -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!(

View file

@ -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 -----
"###