fix(cli): fix format_output() to use rich and color renderers instead of JSON fallback
The format_output() function in src/cleveragents/cli/formatting.py had two routing bugs that caused incorrect output for the 'rich' and 'color' formats: 1. The 'rich' format had no explicit dispatch branch and silently fell through to the final JSON fallback, returning raw JSON instead of styled terminal output. Since 'rich' is the default CLI format (per ADR-021), this meant all commands using format_output() (version, info, diagnostics) produced JSON by default. 2. The 'color' format was incorrectly routed to _format_plain() instead of a color-aware renderer, producing plain text with no ANSI color codes. Fix: - Added _format_rich() helper that delegates to RichMaterializer via OutputSession, producing ANSI-styled terminal output consistent with format_output_session(). - Added _format_color() helper that delegates to ColorMaterializer via OutputSession, producing ANSI-colored terminal output. - Added explicit OutputFormat.RICH dispatch in format_output() routing. - Fixed OutputFormat.COLOR dispatch to use _format_color() instead of _format_plain(). Tests: - Updated existing BDD scenario that was validating the buggy behavior (expected JSON for rich format) to now assert correct styled output. - Added new BDD scenarios: 'rich format produces styled terminal output not JSON' and 'color format produces ANSI-colored output not plain text'. - Added Robot Framework integration tests in cli_formats.robot and helper_cli_formats.py verifying end-to-end styled output for both formats. All nox sessions pass: lint, typecheck, unit_tests, security_scan. ISSUES CLOSED: #2921
This commit is contained in:
@@ -93,3 +93,25 @@ All Six Formats Work With Version Command
|
||||
${result}= Run Process ${PYTHON} ${HELPER} global-format-all-six cwd=${WORKSPACE}
|
||||
Should Be Equal As Integers ${result.rc} 0
|
||||
Should Contain ${result.stdout} cli-global-format-all-six-ok
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# format_output() renderer routing tests (Issue #2921)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Format Output Rich Produces Styled Output Not JSON
|
||||
[Documentation] Verify that format_output(data, "rich") produces styled terminal output
|
||||
... and NOT raw JSON (fix for issue #2921: rich format silently fell back to JSON)
|
||||
${result}= Run Process ${PYTHON} ${HELPER} format-output-rich cwd=${WORKSPACE}
|
||||
Log ${result.stdout}
|
||||
Log ${result.stderr}
|
||||
Should Be Equal As Integers ${result.rc} 0
|
||||
Should Contain ${result.stdout} cli-formats-format-output-rich-ok
|
||||
|
||||
Format Output Color Produces ANSI Colored Output Not Plain Text
|
||||
[Documentation] Verify that format_output(data, "color") produces ANSI-colored output
|
||||
... and NOT plain text (fix for issue #2921: color format used plain renderer)
|
||||
${result}= Run Process ${PYTHON} ${HELPER} format-output-color cwd=${WORKSPACE}
|
||||
Log ${result.stdout}
|
||||
Log ${result.stderr}
|
||||
Should Be Equal As Integers ${result.rc} 0
|
||||
Should Contain ${result.stdout} cli-formats-format-output-color-ok
|
||||
|
||||
@@ -214,6 +214,71 @@ def global_format_all_six() -> None:
|
||||
print("cli-global-format-all-six-ok")
|
||||
|
||||
|
||||
def format_output_rich() -> None:
|
||||
"""Verify format_output(data, 'rich') produces styled output, not JSON.
|
||||
|
||||
Regression test for issue #2921: rich format silently fell back to JSON.
|
||||
"""
|
||||
import json as _json
|
||||
|
||||
from cleveragents.cli.formatting import format_output
|
||||
|
||||
data = {"name": "smoke-test", "status": "active", "count": 42}
|
||||
result = format_output(data, "rich")
|
||||
|
||||
# Must NOT be valid JSON (that was the bug)
|
||||
try:
|
||||
_json.loads(result)
|
||||
raise AssertionError(
|
||||
f"format_output(data, 'rich') returned valid JSON — "
|
||||
f"expected styled terminal output, got: {result!r}"
|
||||
)
|
||||
except (_json.JSONDecodeError, ValueError):
|
||||
pass # Expected: rich output is not JSON
|
||||
|
||||
# Must contain the data keys
|
||||
assert "name" in result, f"Rich output missing 'name': {result!r}"
|
||||
assert "status" in result, f"Rich output missing 'status': {result!r}"
|
||||
|
||||
# Must contain ANSI codes or panel header (styled output)
|
||||
has_ansi = "\033[" in result or "\x1b[" in result
|
||||
has_panel = "Output" in result
|
||||
assert has_ansi or has_panel, (
|
||||
f"Rich output has no ANSI codes or panel header: {result!r}"
|
||||
)
|
||||
print("cli-formats-format-output-rich-ok")
|
||||
|
||||
|
||||
def format_output_color() -> None:
|
||||
"""Verify format_output(data, 'color') produces ANSI-colored output, not plain text.
|
||||
|
||||
Regression test for issue #2921: color format used plain renderer instead of color.
|
||||
"""
|
||||
from cleveragents.cli.formatting import format_output
|
||||
|
||||
data = {"name": "smoke-test", "status": "active", "count": 42}
|
||||
result = format_output(data, "color")
|
||||
|
||||
# Must contain the data keys
|
||||
assert "name" in result, f"Color output missing 'name': {result!r}"
|
||||
assert "status" in result, f"Color output missing 'status': {result!r}"
|
||||
|
||||
# Must NOT be plain text only (that was the bug)
|
||||
plain_text = "name: smoke-test\nstatus: active\ncount: 42"
|
||||
assert result != plain_text, (
|
||||
f"format_output(data, 'color') returned plain text — "
|
||||
f"expected ANSI-colored output, got: {result!r}"
|
||||
)
|
||||
|
||||
# Must contain ANSI codes or panel header (styled output)
|
||||
has_ansi = "\033[" in result or "\x1b[" in result
|
||||
has_panel = "Output" in result
|
||||
assert has_ansi or has_panel, (
|
||||
f"Color output has no ANSI codes or panel header: {result!r}"
|
||||
)
|
||||
print("cli-formats-format-output-color-ok")
|
||||
|
||||
|
||||
_COMMANDS = {
|
||||
"action-list-json": action_list_json,
|
||||
"action-show-yaml": action_show_yaml,
|
||||
@@ -226,6 +291,8 @@ _COMMANDS = {
|
||||
"global-format-json-diagnostics": global_format_json_diagnostics,
|
||||
"global-format-shorthand": global_format_shorthand,
|
||||
"global-format-all-six": global_format_all_six,
|
||||
"format-output-rich": format_output_rich,
|
||||
"format-output-color": format_output_color,
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user