Coverage for tests / unit / utils / result_formatters / test_generic_output.py: 100%

29 statements  

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

1"""Tests for generic tool output in print_tool_result.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING, Any 

6 

7import pytest 

8from assertpy import assert_that 

9 

10from lintro.utils.result_formatters import print_tool_result 

11 

12if TYPE_CHECKING: 

13 from collections.abc import Callable 

14 

15 

16def test_no_issues_shows_success_message( 

17 console_capture_with_kwargs: tuple[ 

18 Callable[..., None], 

19 list[tuple[str, dict[str, Any]]], 

20 ], 

21 success_capture: tuple[Callable[[str], None], list[str]], 

22) -> None: 

23 """Verify success message displayed when tool finds no issues. 

24 

25 Args: 

26 console_capture_with_kwargs: Mock console output capture with kwargs. 

27 success_capture: Mock success message capture. 

28 """ 

29 mock_console, _ = console_capture_with_kwargs 

30 mock_success, success_calls = success_capture 

31 

32 print_tool_result( 

33 console_output_func=mock_console, 

34 success_func=mock_success, 

35 tool_name="ruff", 

36 output="", 

37 issues_count=0, 

38 ) 

39 

40 assert_that(success_calls).contains("✓ No issues found.") 

41 

42 

43def test_issues_found_shows_error_message_with_count( 

44 console_capture_with_kwargs: tuple[ 

45 Callable[..., None], 

46 list[tuple[str, dict[str, Any]]], 

47 ], 

48 success_capture: tuple[Callable[[str], None], list[str]], 

49) -> None: 

50 """Verify error message with issue count displayed when issues found. 

51 

52 Args: 

53 console_capture_with_kwargs: Mock console output capture with kwargs. 

54 success_capture: Mock success message capture. 

55 """ 

56 mock_console, console_output = console_capture_with_kwargs 

57 mock_success, _ = success_capture 

58 

59 print_tool_result( 

60 console_output_func=mock_console, 

61 success_func=mock_success, 

62 tool_name="ruff", 

63 output="Some lint errors", 

64 issues_count=5, 

65 ) 

66 

67 red_texts = [t for t, kwargs in console_output if kwargs.get("color") == "red"] 

68 assert_that(any("5 issues" in t for t in red_texts)).is_true() 

69 

70 

71def test_success_false_shows_failure_even_with_zero_issues( 

72 console_capture_with_kwargs: tuple[ 

73 Callable[..., None], 

74 list[tuple[str, dict[str, Any]]], 

75 ], 

76 success_capture: tuple[Callable[[str], None], list[str]], 

77) -> None: 

78 """Verify failure message shown when success=False despite zero issues. 

79 

80 Args: 

81 console_capture_with_kwargs: Mock console output capture with kwargs. 

82 success_capture: Mock success message capture. 

83 """ 

84 mock_console, console_output = console_capture_with_kwargs 

85 mock_success, _ = success_capture 

86 

87 print_tool_result( 

88 console_output_func=mock_console, 

89 success_func=mock_success, 

90 tool_name="ruff", 

91 output="", 

92 issues_count=0, 

93 success=False, 

94 ) 

95 

96 red_texts = [t for t, kwargs in console_output if kwargs.get("color") == "red"] 

97 assert_that(any("failed" in t.lower() for t in red_texts)).is_true() 

98 

99 

100@pytest.mark.parametrize( 

101 ("output_text", "description"), 

102 [ 

103 ("No files to lint", "no files to lint message"), 

104 ("No Python files found to lint", "no Python files found message"), 

105 ], 

106 ids=["no_files_to_lint", "no_python_files_found"], 

107) 

108def test_no_files_processed_warning( 

109 console_capture_with_kwargs: tuple[ 

110 Callable[..., None], 

111 list[tuple[str, dict[str, Any]]], 

112 ], 

113 success_capture: tuple[Callable[[str], None], list[str]], 

114 output_text: str, 

115 description: str, 

116) -> None: 

117 """Verify warning shown when no files were processed. 

118 

119 Args: 

120 console_capture_with_kwargs: Mock console output capture with kwargs. 

121 success_capture: Mock success message capture. 

122 output_text: Tool output text to test. 

123 description: Description of the test case. 

124 """ 

125 mock_console, console_output = console_capture_with_kwargs 

126 mock_success, _ = success_capture 

127 

128 print_tool_result( 

129 console_output_func=mock_console, 

130 success_func=mock_success, 

131 tool_name="ruff", 

132 output=output_text, 

133 issues_count=0, 

134 ) 

135 

136 texts = [t for t, _ in console_output] 

137 assert_that(any("No files processed" in t for t in texts)).described_as( 

138 f"Expected 'No files processed' warning for {description}", 

139 ).is_true()