diff --git a/linodecli/overrides.py b/linodecli/overrides.py index dff913877..0e08ea170 100644 --- a/linodecli/overrides.py +++ b/linodecli/overrides.py @@ -152,6 +152,7 @@ def linode_types_with_region_prices( output = Table( header_style="bold", + show_header=output_handler.headers, show_lines=True, ) @@ -169,7 +170,9 @@ def linode_types_with_region_prices( row = [] for h in headers: if h == "region_prices": - sub_table = format_region_prices(linode[h]) + sub_table = format_region_prices( + linode[h], show_header=output_handler.headers + ) row.append(sub_table) elif h in ("price.hourly", "price.monthly"): @@ -201,13 +204,15 @@ def format_prices(prices, data: Dict[str, any]) -> any: return str(data[price_headers[0]][price_headers[1]]) -def format_region_prices(data: Dict[str, any]) -> any: +def format_region_prices( + data: Dict[str, any], *, show_header: bool = True +) -> any: """ Format nested region price entry into a sub-table. """ subheaders = ["id", "hourly", "monthly"] - sub_table = Table(box=box.SIMPLE_HEAVY) + sub_table = Table(box=box.SIMPLE_HEAVY, show_header=show_header) for header in subheaders: sub_table.add_column(header, justify="center") diff --git a/tests/unit/test_overrides.py b/tests/unit/test_overrides.py index f5533b6b6..0b5109a98 100644 --- a/tests/unit/test_overrides.py +++ b/tests/unit/test_overrides.py @@ -1,8 +1,12 @@ import contextlib import io +from argparse import ArgumentParser from unittest.mock import patch +import pytest + from linodecli import OutputMode +from linodecli.arg_helpers import register_args from linodecli.overrides import OUTPUT_OVERRIDES @@ -60,8 +64,14 @@ def patch_func(*a): assert stdout_buf.getvalue() != "line 1\nline 2\n" + @pytest.mark.parametrize("no_headers", [False, True]) def test_types_region_prices_list( - self, mock_cli, list_operation_for_overrides_test + self, + mock_cli, + list_operation_for_overrides_test, + capsys, + monkeypatch, + no_headers, ): response_json = { "data": [ @@ -99,19 +109,23 @@ def test_types_region_prices_list( "results": 1, } - override_signature = ("linodes", "types", OutputMode.table) - list_operation_for_overrides_test.command = "linodes" list_operation_for_overrides_test.action = "types" - mock_cli.output_handler.mode = OutputMode.table - - stdout_buf = io.StringIO() - - with contextlib.redirect_stdout(stdout_buf): - list_operation_for_overrides_test.process_response_json( - response_json, mock_cli.output_handler - ) - - rows = stdout_buf.getvalue().split("\n") - # assert that the overridden table has the new columns - assert len(rows[1].split(rows[1][0])) == 15 + args = ["linodes", "types"] + if no_headers: + args.append("--no-headers") + parsed = register_args(ArgumentParser(add_help=False)).parse_args(args) + mock_cli.output_handler.configure(parsed) + monkeypatch.setenv("COLUMNS", "240") + + list_operation_for_overrides_test.process_response_json( + response_json, mock_cli.output_handler + ) + + cells = capsys.readouterr().out.split() + for value in ("g6-standard-2", "0.03", "20", "us-east", "0.036", "24"): + assert value in cells + price_headers = {"price.hourly", "price.monthly", "hourly", "monthly"} + assert price_headers.intersection(cells) == ( + set() if no_headers else price_headers + )