Skip to content

Commit

Permalink
Always print JSON output with --format json
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed May 20, 2024
1 parent 95c9621 commit 4f93deb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
15 changes: 7 additions & 8 deletions crates/uv/src/commands/pip/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand Down Expand Up @@ -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!(
Expand Down
52 changes: 51 additions & 1 deletion crates/uv/tests/pip_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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");
Expand Down Expand Up @@ -451,6 +500,7 @@ fn list_format_json() {
success: true
exit_code: 0
----- stdout -----
[]
----- stderr -----
"###
Expand Down

0 comments on commit 4f93deb

Please sign in to comment.