Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 19 additions & 11 deletions src/alr/alr-commands.adb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ with Alire.Solutions;
with Alire.Toolchains;
with Alire.Utils.Did_You_Mean;
with Alire.Utils.Tables;
with Alire.Utils.User_Input;

with Alr.Commands.Action;
with Alr.Commands.Build;
Expand Down Expand Up @@ -185,7 +186,8 @@ package body Alr.Commands is
Long_Switch => "--format?",
Argument => "FORMAT",
Help =>
"Use structured output for tables (JSON, TOML, YAML)");
"Use structured output for tables (JSON, TOML, YAML)."
& " Implies -n and -q.");

Define_Switch (Config,
No_Color'Access,
Expand Down Expand Up @@ -515,24 +517,30 @@ package body Alr.Commands is
Alire.Utils.Did_You_Mean.Upper_Case);

begin
if Structured_Format.all /= "unset" then
Alire.Utils.Tables.Structured_Output := True;
else
-- Do nothing if there is no `--format` argument (or in the unlikely
-- event `--formatunset` is passed)
Comment thread
Seb-MCaw marked this conversation as resolved.
if Structured_Format.all = "unset" then
return;
end if;

if Format_Str /= "" and then not Is_Valid (Format_Str) then
Reportaise_Wrong_Arguments
("Unknown argument in --format" & Structured_Format.all
& "." & Suggest (Format_Str));
end if;
-- Enable structured output
Alire.Utils.Tables.Structured_Output := True;

if Format_Str /= "" then
-- Set the output format, defaulting to JSON if unspecified.
if Format_Str = "" then
Alire.Utils.Tables.Structured_Output_Format := JSON;
elsif Is_Valid (Format_Str) then
Alire.Utils.Tables.Structured_Output_Format
:= Alire.Utils.Tables.Formats'Value (Format_Str);
else
Alire.Utils.Tables.Structured_Output_Format := JSON;
Reportaise_Wrong_Arguments
("Unknown argument in --format" & Structured_Format.all
& "." & Suggest (Format_Str));
end if;

-- Disable any additional messages/prompts which can render the
-- output unparsable
Alire.Utils.User_Input.Enable_Silent_Running;
end Set_Structured_Output;

use all type Alire.Platforms.Operating_Systems;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = "1.1"
33 changes: 33 additions & 0 deletions testsuite/tests/misc/structured-output-always-quiet/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Verify that structured output is not broken by extraneous output."""

import json
from drivers.alr import alr_settings_set, init_local_crate, run_alr
from drivers.asserts import assert_substring, assert_eq, assert_not_substring


# Load an an old (but compatible) index with the associated warning enabled.
Comment thread
Seb-MCaw marked this conversation as resolved.
Outdated
alr_settings_set("warning.old_index", "true")
json_search = run_alr("--format=json", "search", "--list", quiet=False)
plain_search = run_alr("search", "--list", quiet=False)

# The warning should be included in the plain output, but not in the formatted
# output, which should parse as valid JSON.
assert_substring("is older than the newest supported by alr", plain_search.out)
assert_not_substring("is older than the newest supported by alr", json_search.out)
parsed_search = json.loads(json_search.out)
assert_eq(["hello", "libhello"], [c["name"] for c in parsed_search])


# Run `alr show` on a local crate with no toolchain configured and the toolchain
# assistant enabled.
init_local_crate(name="local_crate_name")
alr_settings_set("toolchain.assistant", "true")
p = run_alr("--format=json", "show", quiet=False)

# The toolchain assistant should not output anything that breaks JSON parsing.
assert_not_substring("toolchain", p.out)
parsed_show = json.loads(p.out)
assert_eq("local_crate_name", parsed_show["name"])


print("SUCCESS")
6 changes: 6 additions & 0 deletions testsuite/tests/misc/structured-output-always-quiet/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
driver: python-script
indexes:
outdated_index:
in_fixtures: false
basic_index:
in_fixtures: true
Loading