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

Always print JSON output with --format json #3671

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading