Skip to content
Open
Changes from all 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
57 changes: 57 additions & 0 deletions tests/test_console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pytest
from unittest.mock import patch, MagicMock

from fastman.console import Output, Style, Icons

class TestOutput:

@patch("fastman.console.HAS_RICH", True)
@patch("fastman.console.console")
@patch("fastman.console.logger.info")
def test_success_with_rich(self, mock_logger, mock_console):
Output.success("Rich success message", icon=True, prefix="[PRE]")

# Verify console print was called correctly
mock_console.print.assert_called_once_with(f"[success]{Icons.SUCCESS} [/success][PRE] Rich success message")

# Verify logger was called
mock_logger.assert_called_once_with("Rich success message")

@patch("fastman.console.HAS_RICH", False)
@patch("fastman.console.logger.info")
def test_success_without_rich(self, mock_logger, capsys):
Output.success("Normal success message", icon=True, prefix="[PRE]")

# Verify stdout
captured = capsys.readouterr()
expected_out = f"{Style.BRIGHT_GREEN}{Icons.SUCCESS} {Style.RESET}[PRE] Normal success message\n"
assert captured.out == expected_out

# Verify logger
mock_logger.assert_called_once_with("Normal success message")

@patch("fastman.console.HAS_RICH", False)
@patch("fastman.console.logger.info")
@patch("builtins.print", side_effect=[UnicodeEncodeError("ascii", "", 0, 1, "mock"), None])
def test_success_without_rich_unicode_fallback(self, mock_print, mock_logger, capsys):
# We patch print with a side_effect that raises UnicodeEncodeError on the first call,
# then succeeds on the second (fallback) call.
# But wait, capsys won't capture patched print if we do it this way easily, so let's verify mock_print calls.
Output.success("Unicode fallback message", icon=True, prefix="[PRE]")

# Verify fallback print logic
assert mock_print.call_count == 2
mock_print.assert_any_call(f"{Style.GREEN}[OK]{Style.RESET} [PRE] Unicode fallback message")
Comment on lines +36 to +44

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Clean up stray developer comment and remove unused capsys parameter.

Lines 37–39 read like an in-progress internal thought ("But wait, capsys won't capture patched print if we do it this way easily, so let's verify mock_print calls.") that should not land in the committed test. Additionally, capsys is declared on line 36 but never used — builtins.print is mocked, so the parameter is dead. Both should be removed for clarity.

🧹 Proposed cleanup
-    `@patch`("fastman.console.HAS_RICH", False)
-    `@patch`("fastman.console.logger.info")
-    `@patch`("builtins.print", side_effect=[UnicodeEncodeError("ascii", "", 0, 1, "mock"), None])
-    def test_success_without_rich_unicode_fallback(self, mock_print, mock_logger, capsys):
-        # We patch print with a side_effect that raises UnicodeEncodeError on the first call,
-        # then succeeds on the second (fallback) call.
-        # But wait, capsys won't capture patched print if we do it this way easily, so let's verify mock_print calls.
-        Output.success("Unicode fallback message", icon=True, prefix="[PRE]")
+    `@patch`("fastman.console.HAS_RICH", False)
+    `@patch`("fastman.console.logger.info")
+    `@patch`("builtins.print", side_effect=[UnicodeEncodeError("ascii", "", 0, 1, "mock"), None])
+    def test_success_without_rich_unicode_fallback(self, mock_print, mock_logger):
+        # First print() raises UnicodeEncodeError; the fallback branch should invoke print() a second time.
+        Output.success("Unicode fallback message", icon=True, prefix="[PRE]")
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/test_console.py` around lines 36 - 44, Remove the stray developer
comment inside the test_success_without_rich_unicode_fallback test and delete
the unused capsys parameter from the test signature; the function should only
take the mocked fixtures (mock_print, mock_logger) since builtins.print is
mocked and capsys is not used—leave the assertions and calls to Output.success
and mock_print intact.


# Verify logger
mock_logger.assert_called_once_with("Unicode fallback message")

@patch("fastman.console.HAS_RICH", False)
@patch("fastman.console.logger.info")
def test_success_no_icon_no_prefix(self, mock_logger, capsys):
Output.success("Plain message", icon=False, prefix="")

captured = capsys.readouterr()
expected_out = f"{Style.BRIGHT_GREEN}{Style.RESET}Plain message\n"
assert captured.out == expected_out
mock_logger.assert_called_once_with("Plain message")