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

33 statements  

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

1"""Tests for fixable issue hints 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_fixable_hint_shown_in_check_mode( 

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 hint about fixable issues shown in check mode. 

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 = "[*] 5 fixable with the `--fix` flag" 

33 

34 print_tool_result( 

35 console_output_func=mock_console, 

36 success_func=mock_success, 

37 tool_name="ruff", 

38 output=output, 

39 issues_count=5, 

40 action="check", 

41 ) 

42 

43 yellow_texts = [ 

44 t for t, kwargs in console_output if kwargs.get("color") == "yellow" 

45 ] 

46 assert_that( 

47 any("5 formatting/linting issue(s)" in t for t in yellow_texts), 

48 ).is_true() 

49 assert_that(any("lintro format" in t for t in yellow_texts)).is_true() 

50 

51 

52def test_fixable_hint_sums_multiple_matches( 

53 console_capture_with_kwargs: tuple[ 

54 Callable[..., None], 

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

56 ], 

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

58) -> None: 

59 """Verify multiple fixable counts are summed from output. 

60 

61 Args: 

62 console_capture_with_kwargs: Mock console output capture with kwargs. 

63 success_capture: Mock success message capture. 

64 """ 

65 mock_console, console_output = console_capture_with_kwargs 

66 mock_success, _ = success_capture 

67 

68 output = "[*] 3 fixable\n[*] 2 fixable" 

69 

70 print_tool_result( 

71 console_output_func=mock_console, 

72 success_func=mock_success, 

73 tool_name="ruff", 

74 output=output, 

75 issues_count=5, 

76 action="check", 

77 ) 

78 

79 yellow_texts = [ 

80 t for t, kwargs in console_output if kwargs.get("color") == "yellow" 

81 ] 

82 assert_that( 

83 any("5 formatting/linting issue(s)" in t for t in yellow_texts), 

84 ).is_true() 

85 

86 

87def test_fixable_hint_uses_raw_output_for_meta( 

88 console_capture_with_kwargs: tuple[ 

89 Callable[..., None], 

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

91 ], 

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

93) -> None: 

94 """Verify raw_output_for_meta used for fixable detection. 

95 

96 Args: 

97 console_capture_with_kwargs: Mock console output capture with kwargs. 

98 success_capture: Mock success message capture. 

99 """ 

100 mock_console, console_output = console_capture_with_kwargs 

101 mock_success, _ = success_capture 

102 

103 print_tool_result( 

104 console_output_func=mock_console, 

105 success_func=mock_success, 

106 tool_name="ruff", 

107 output="Formatted output without fixable info", 

108 issues_count=3, 

109 raw_output_for_meta="[*] 3 fixable", 

110 action="check", 

111 ) 

112 

113 yellow_texts = [ 

114 t for t, kwargs in console_output if kwargs.get("color") == "yellow" 

115 ] 

116 assert_that( 

117 any("3 formatting/linting issue(s)" in t for t in yellow_texts), 

118 ).is_true() 

119 

120 

121def test_no_fixable_hint_in_fix_action( 

122 console_capture_with_kwargs: tuple[ 

123 Callable[..., None], 

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

125 ], 

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

127) -> None: 

128 """Verify fixable hint not shown when already running fix action. 

129 

130 Args: 

131 console_capture_with_kwargs: Mock console output capture with kwargs. 

132 success_capture: Mock success message capture. 

133 """ 

134 mock_console, console_output = console_capture_with_kwargs 

135 mock_success, _ = success_capture 

136 

137 output = "[*] 5 fixable" 

138 

139 print_tool_result( 

140 console_output_func=mock_console, 

141 success_func=mock_success, 

142 tool_name="ruff", 

143 output=output, 

144 issues_count=5, 

145 action=Action.FIX, 

146 ) 

147 

148 yellow_texts = [ 

149 t for t, kwargs in console_output if kwargs.get("color") == "yellow" 

150 ] 

151 assert_that(any("lintro format" in t for t in yellow_texts)).is_false()