Coverage for tests / unit / utils / console / test_logger_metadata.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Unit tests for ThreadSafeConsoleLogger metadata and pytest result methods. 

2 

3Tests cover the _print_metadata_messages helper for parsing tool output 

4and the _print_pytest_results helper for displaying test results. 

5""" 

6 

7from __future__ import annotations 

8 

9from unittest.mock import patch 

10 

11import pytest 

12from assertpy import assert_that 

13 

14from lintro.utils.console.logger import ThreadSafeConsoleLogger 

15 

16# ============================================================================= 

17# Metadata Message Tests 

18# ============================================================================= 

19 

20 

21@pytest.mark.parametrize( 

22 ("raw_output", "expected_substring"), 

23 [ 

24 pytest.param("5 fixable with ruff", "5 auto-fixable", id="fixable-count"), 

25 pytest.param("0 fixable issues", "No issues found", id="zero-fixable"), 

26 pytest.param( 

27 "Some issues cannot be auto-fixed", 

28 "cannot be auto-fixed", 

29 id="unfixable", 

30 ), 

31 pytest.param( 

32 "file.py would reformat", 

33 "would be reformatted", 

34 id="would-reformat", 

35 ), 

36 pytest.param( 

37 "3 issues fixed successfully", 

38 "were fixed", 

39 id="issues-fixed", 

40 ), 

41 pytest.param("some random output", "No issues found", id="random-output"), 

42 ], 

43) 

44def test_print_metadata_messages_patterns( 

45 logger: ThreadSafeConsoleLogger, 

46 raw_output: str, 

47 expected_substring: str, 

48) -> None: 

49 """Verify _print_metadata_messages handles various output patterns correctly. 

50 

51 Different tool output patterns should be recognized and formatted into 

52 user-friendly informational messages. 

53 

54 Args: 

55 logger: ThreadSafeConsoleLogger instance fixture. 

56 raw_output: The raw output to parse for metadata. 

57 expected_substring: A substring expected in the formatted message. 

58 """ 

59 with patch.object(logger, "console_output") as mock_output: 

60 logger._print_metadata_messages(raw_output) 

61 mock_output.assert_called_once() 

62 call_text = str(mock_output.call_args) 

63 assert_that(call_text).contains(expected_substring) 

64 

65 

66# ============================================================================= 

67# Pytest Results Tests 

68# ============================================================================= 

69 

70 

71def test_print_pytest_results_success_message(logger: ThreadSafeConsoleLogger) -> None: 

72 """Verify _print_pytest_results shows success message when tests pass. 

73 

74 Passing test runs should display a green success indicator with 

75 'All tests passed' message. 

76 

77 Args: 

78 logger: ThreadSafeConsoleLogger instance fixture. 

79 """ 

80 with patch.object(logger, "console_output") as mock_output: 

81 logger._print_pytest_results("test output", success=True) 

82 calls = [str(c) for c in mock_output.call_args_list] 

83 assert_that(any("All tests passed" in c for c in calls)).is_true() 

84 

85 

86def test_print_pytest_results_failure_message(logger: ThreadSafeConsoleLogger) -> None: 

87 """Verify _print_pytest_results shows failure message when tests fail. 

88 

89 Failing test runs should display a red failure indicator with 

90 'Some tests failed' message. 

91 

92 Args: 

93 logger: ThreadSafeConsoleLogger instance fixture. 

94 """ 

95 with patch.object(logger, "console_output") as mock_output: 

96 logger._print_pytest_results("test output", success=False) 

97 calls = [str(c) for c in mock_output.call_args_list] 

98 assert_that(any("Some tests failed" in c for c in calls)).is_true() 

99 

100 

101def test_print_pytest_results_handles_empty_output( 

102 logger: ThreadSafeConsoleLogger, 

103) -> None: 

104 """Verify _print_pytest_results handles empty output gracefully. 

105 

106 Even with empty output, the header and status message should still 

107 be displayed to indicate test completion status. 

108 

109 Args: 

110 logger: ThreadSafeConsoleLogger instance fixture. 

111 """ 

112 with patch.object(logger, "console_output") as mock_output: 

113 logger._print_pytest_results("", success=True) 

114 # Should still print header and status 

115 assert_that(mock_output.call_count).is_greater_than(0) 

116 

117 

118@pytest.mark.parametrize( 

119 "success", 

120 [ 

121 pytest.param(True, id="success"), 

122 pytest.param(False, id="failure"), 

123 ], 

124) 

125def test_print_pytest_results_both_outcomes( 

126 logger: ThreadSafeConsoleLogger, 

127 success: bool, 

128) -> None: 

129 """Verify _print_pytest_results handles both pass and fail outcomes. 

130 

131 Both success and failure cases should produce console output with 

132 appropriate status indicators. 

133 

134 Args: 

135 logger: ThreadSafeConsoleLogger instance fixture. 

136 success: Whether the test run was successful. 

137 """ 

138 with patch.object(logger, "console_output") as mock_output: 

139 logger._print_pytest_results("output", success=success) 

140 assert_that(mock_output.call_count).is_greater_than(0)