Skip to content

Commit 36647a1

Browse files
author
aviat cohen
committed
use mock_questionary_print instead of capsys
1 parent acd7be2 commit 36647a1

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/fabric_cli/utils/fab_ui.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,11 @@ def _print_entries_key_value_list_style(entries: Any) -> None:
513513
fab_constant.ERROR_INVALID_ENTRIES_FORMAT,
514514
)
515515

516-
for entry in _entries:
516+
for i, entry in enumerate(_entries):
517517
for key, value in entry.items():
518518
pretty_key = _format_key_to_convert_to_title_case(key)
519519
print_grey(f"{pretty_key}: {value}", to_stderr=False)
520-
if len(_entries) > 1:
520+
if i < len(_entries) - 1:
521521
print_grey("", to_stderr=False) # Empty line between entries
522522

523523

tests/test_utils/test_fab_ui.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -678,22 +678,20 @@ def test_print_output_format_text_no_result_failure():
678678
assert excinfo.value.status_code == constant.ERROR_INVALID_INPUT
679679

680680

681-
@pytest.mark.skipif(
682-
platform.system() == "Windows",
683-
reason="Failed to run on windows with vscode - no real console",
684-
)
685-
def test_print_entries_key_value_style_success(capsys):
681+
def test_print_entries_key_value_style_success(mock_questionary_print):
686682
"""Test printing entries in key-value format."""
687683

688684
# Test with single dictionary entry
689685
entry = {"logged_in": "true", "account_name": "johndoe@example.com"}
690686
ui._print_entries_key_value_list_style(entry)
691687

692-
captured = capsys.readouterr()
693-
# print_grey outputs to stderr with to_stderr=False, so check stdout
694-
output = captured.out
695-
assert "Logged In: true" in output
696-
assert "Account Name: johndoe@example.com" in output
688+
# Verify the correct formatted output was printed
689+
assert mock_questionary_print.call_count == 2
690+
printed_calls = [call.args[0] for call in mock_questionary_print.call_args_list]
691+
assert "Logged In: true" in printed_calls
692+
assert "Account Name: johndoe@example.com" in printed_calls
693+
694+
mock_questionary_print.reset_mock()
697695

698696
# Test with list of dictionaries
699697
entries = [
@@ -702,19 +700,21 @@ def test_print_entries_key_value_style_success(capsys):
702700
]
703701
ui._print_entries_key_value_list_style(entries)
704702

705-
captured = capsys.readouterr()
706-
output = captured.out
707-
assert "User Name: john" in output
708-
assert "Status: active" in output
709-
assert "User Name: jane" in output
710-
assert "Status: inactive" in output
703+
# Verify output for list of entries (should include empty line between entries, but not after last)
704+
assert mock_questionary_print.call_count == 5 # 2 for john + 1 empty line + 2 for jane
705+
printed_calls = [call.args[0] for call in mock_questionary_print.call_args_list]
706+
assert "User Name: john" in printed_calls
707+
assert "Status: active" in printed_calls
708+
assert "User Name: jane" in printed_calls
709+
assert "Status: inactive" in printed_calls
710+
assert "" in printed_calls # Empty line between entries (but not after the last entry)
711+
712+
mock_questionary_print.reset_mock()
711713

712714
# Test with empty list
713715
ui._print_entries_key_value_list_style([])
714-
captured = capsys.readouterr()
715-
# Should not output anything for empty list
716-
assert captured.err == ""
717-
assert captured.out == ""
716+
# Should not call print for empty list
717+
mock_questionary_print.assert_not_called()
718718

719719

720720
def test_print_entries_key_value_style_invalid_input():

0 commit comments

Comments
 (0)