Skip to content

Commit 117f115

Browse files
Rishabh00bshahar1
andauthored
Show square brackets in airflowctl table and plain output as typed (#76175)
* Show square brackets in airflowctl table and plain output as typed `airflowctl ... --output table` and `--output plain` passed values to Rich as markup, so a value such as `[/b]` crashed the command with a MarkupError and a value such as `s3://[bucket]/path` silently lost its bracketed part. Render the values literally, as `--output json` and `--output yaml` already do. * Show square brackets in airflowctl table headers as typed Column headers were still passed to Rich as markup, so a key such as `[/b]` raised a MarkupError and `[red]name[/red]` lost its tags in `--output table`. Escape them like the cell values, and run the output tests once per value, including a backslash before a tag. --------- Co-authored-by: Shahar Epstein <[email protected]>
1 parent 4eba351 commit 117f115

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎airflow-ctl/src/airflowctl/ctl/console_formatting.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from rich.box import ASCII_DOUBLE_HEAD
2525
from rich.console import Console
26+
from rich.markup import escape
2627
from rich.syntax import Syntax
2728
from rich.table import Table
2829
from tabulate import tabulate
@@ -74,10 +75,10 @@ def print_as_table(self, data: list[dict]):
7475

7576
table = SimpleTable(show_header=self.show_header)
7677
for col in data[0]:
77-
table.add_column(col)
78+
table.add_column(escape(str(col)))
7879

7980
for row in data:
80-
table.add_row(*(str(d) for d in row.values()))
81+
table.add_row(*(escape(str(d)) for d in row.values()))
8182
self.print(table)
8283

8384
def print_as_plain_table(self, data: list[dict]):
@@ -87,7 +88,7 @@ def print_as_plain_table(self, data: list[dict]):
8788
return
8889
rows = [d.values() for d in data]
8990
output = tabulate(rows, tablefmt="plain", headers=list(data[0]))
90-
self.print(output)
91+
self.print(output, markup=False)
9192

9293
def _normalize_data(self, value: Any, output: str) -> list | str | dict | None:
9394
if isinstance(value, (tuple, list)):

‎airflow-ctl/tests/airflow_ctl/ctl/test_console_formatting.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,32 @@ def test_simple_table_add_column_smoke():
162162
table.add_column("column_1")
163163

164164
assert table.columns[0].header == "column_1"
165+
166+
167+
MARKUP_LIKE_TEXT = pytest.mark.parametrize(
168+
"text",
169+
["s3://[bucket]/path", "[red]not styled[/red]", "a[0-9]+ [/b]", r"path\[red]"],
170+
ids=["unknown-tag", "style-tag", "closing-tag-only", "backslash-before-tag"],
171+
)
172+
173+
174+
@pytest.mark.parametrize("output", ["table", "plain"])
175+
@MARKUP_LIKE_TEXT
176+
def test_print_as_keeps_square_brackets_in_values(output, text):
177+
"""Values that look like Rich markup are shown as-is instead of being dropped or raising."""
178+
console = console_formatting.AirflowConsole(record=True)
179+
180+
console.print_as([{"name": text}], output=output)
181+
182+
assert text in console.export_text()
183+
184+
185+
@pytest.mark.parametrize("output", ["table", "plain"])
186+
@MARKUP_LIKE_TEXT
187+
def test_print_as_keeps_square_brackets_in_headers(output, text):
188+
"""Column headers that look like Rich markup are shown as-is instead of being dropped or raising."""
189+
console = console_formatting.AirflowConsole(record=True)
190+
191+
console.print_as([{text: "value"}], output=output)
192+
193+
assert text in console.export_text()

0 commit comments

Comments
 (0)