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

40 statements  

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

1"""Tests for format/fix action handling in print_tool_result.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING, Any 

6 

7from assertpy import assert_that 

8 

9from lintro.enums.action import Action 

10from lintro.utils.result_formatters import print_tool_result 

11 

12if TYPE_CHECKING: 

13 from collections.abc import Callable 

14 

15 

16def test_format_action_shows_fixed_and_remaining_counts( 

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 both fixed and remaining counts displayed for format action. 

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_output = console_capture_with_kwargs 

30 mock_success, _ = success_capture 

31 

32 output = "Fixed 3 issue(s)\nFound 2 issue(s) that cannot be auto-fixed" 

33 

34 print_tool_result( 

35 console_output_func=mock_console, 

36 success_func=mock_success, 

37 tool_name="black", 

38 output=output, 

39 issues_count=0, 

40 action=Action.FIX, 

41 ) 

42 

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

44 assert_that(any("3 fixed" in t for t in texts)).is_true() 

45 assert_that(any("2 remaining" in t for t in texts)).is_true() 

46 

47 

48def test_format_action_shows_only_fixed_when_no_remaining( 

49 console_capture_with_kwargs: tuple[ 

50 Callable[..., None], 

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

52 ], 

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

54) -> None: 

55 """Verify only fixed count shown when all issues were resolved. 

56 

57 Args: 

58 console_capture_with_kwargs: Mock console output capture with kwargs. 

59 success_capture: Mock success message capture. 

60 """ 

61 mock_console, console_output = console_capture_with_kwargs 

62 mock_success, _ = success_capture 

63 

64 output = "Fixed 5 issue(s)\nFound 0 issue(s) that cannot be auto-fixed" 

65 

66 print_tool_result( 

67 console_output_func=mock_console, 

68 success_func=mock_success, 

69 tool_name="black", 

70 output=output, 

71 issues_count=0, 

72 action=Action.FIX, 

73 ) 

74 

75 green_texts = [t for t, kwargs in console_output if kwargs.get("color") == "green"] 

76 assert_that(any("5 fixed" in t for t in green_texts)).is_true() 

77 

78 

79def test_format_action_shows_remaining_count_in_red( 

80 console_capture_with_kwargs: tuple[ 

81 Callable[..., None], 

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

83 ], 

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

85) -> None: 

86 """Verify remaining issues count shown in red for format action. 

87 

88 Args: 

89 console_capture_with_kwargs: Mock console output capture with kwargs. 

90 success_capture: Mock success message capture. 

91 """ 

92 mock_console, console_output = console_capture_with_kwargs 

93 mock_success, _ = success_capture 

94 

95 output = "Fixed 2 issue(s)\nFound 3 issue(s) that cannot be auto-fixed" 

96 

97 print_tool_result( 

98 console_output_func=mock_console, 

99 success_func=mock_success, 

100 tool_name="black", 

101 output=output, 

102 issues_count=3, 

103 ) 

104 

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

106 assert_that(any("3 remaining" in t for t in red_texts)).is_true() 

107 

108 

109def test_format_action_shows_only_remaining_when_nothing_fixed( 

110 console_capture_with_kwargs: tuple[ 

111 Callable[..., None], 

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

113 ], 

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

115) -> None: 

116 """Verify only remaining count shown when no issues were fixed. 

117 

118 Args: 

119 console_capture_with_kwargs: Mock console output capture with kwargs. 

120 success_capture: Mock success message capture. 

121 """ 

122 mock_console, console_output = console_capture_with_kwargs 

123 mock_success, _ = success_capture 

124 

125 output = "Found 4 issue(s) that cannot be auto-fixed" 

126 

127 print_tool_result( 

128 console_output_func=mock_console, 

129 success_func=mock_success, 

130 tool_name="black", 

131 output=output, 

132 issues_count=4, 

133 ) 

134 

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

136 assert_that(any("4 remaining" in t for t in red_texts)).is_true() 

137 

138 

139def test_fix_action_shows_cannot_autofix_message( 

140 console_capture_with_kwargs: tuple[ 

141 Callable[..., None], 

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

143 ], 

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

145) -> None: 

146 """Verify 'cannot be auto-fixed' message shown for fix action with issues. 

147 

148 Args: 

149 console_capture_with_kwargs: Mock console output capture with kwargs. 

150 success_capture: Mock success message capture. 

151 """ 

152 mock_console, console_output = console_capture_with_kwargs 

153 mock_success, _ = success_capture 

154 

155 print_tool_result( 

156 console_output_func=mock_console, 

157 success_func=mock_success, 

158 tool_name="black", 

159 output="Some output", 

160 issues_count=3, 

161 action=Action.FIX, 

162 ) 

163 

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

165 assert_that(any("cannot be auto-fixed" in t for t in red_texts)).is_true()