Coverage for tests / unit / tools / ruff / fix / test_format_option.py: 100%

27 statements  

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

1"""Tests for execute_ruff_fix - Format option scenarios.""" 

2 

3from __future__ import annotations 

4 

5from unittest.mock import MagicMock, patch 

6 

7from assertpy import assert_that 

8 

9from lintro.tools.implementations.ruff.fix import execute_ruff_fix 

10 

11 

12def test_execute_ruff_fix_with_format_enabled( 

13 mock_ruff_tool: MagicMock, 

14 sample_ruff_json_empty_output: str, 

15 sample_ruff_format_check_output: str, 

16) -> None: 

17 """Run format when format option is enabled. 

18 

19 Args: 

20 mock_ruff_tool: Mock RuffTool instance for testing. 

21 sample_ruff_json_empty_output: Sample empty JSON output from ruff. 

22 sample_ruff_format_check_output: Sample format check output from ruff. 

23 """ 

24 mock_ruff_tool.options["format"] = True 

25 

26 with patch( 

27 "lintro.tools.implementations.ruff.fix.walk_files_with_excludes", 

28 ) as mock_walk: 

29 mock_walk.return_value = ["test.py"] 

30 

31 mock_ruff_tool._run_subprocess.side_effect = [ 

32 (True, sample_ruff_json_empty_output), # Initial lint check 

33 (False, sample_ruff_format_check_output), # Format check (2 files) 

34 (True, sample_ruff_json_empty_output), # Lint fix 

35 (True, ""), # Format fix 

36 ] 

37 

38 result = execute_ruff_fix(mock_ruff_tool, ["test.py"]) 

39 

40 assert_that(result.success).is_true() 

41 # 2 format issues were found and fixed 

42 assert_that(result.fixed_issues_count).is_equal_to(2) 

43 

44 

45def test_execute_ruff_fix_format_disabled( 

46 mock_ruff_tool: MagicMock, 

47 sample_ruff_json_empty_output: str, 

48) -> None: 

49 """Skip format when format option is disabled. 

50 

51 Args: 

52 mock_ruff_tool: Mock RuffTool instance for testing. 

53 sample_ruff_json_empty_output: Sample empty JSON output from ruff. 

54 """ 

55 mock_ruff_tool.options["format"] = False 

56 

57 with patch( 

58 "lintro.tools.implementations.ruff.fix.walk_files_with_excludes", 

59 ) as mock_walk: 

60 mock_walk.return_value = ["test.py"] 

61 

62 mock_ruff_tool._run_subprocess.side_effect = [ 

63 (True, sample_ruff_json_empty_output), # Initial check 

64 (True, sample_ruff_json_empty_output), # Fix 

65 ] 

66 

67 execute_ruff_fix(mock_ruff_tool, ["test.py"]) 

68 

69 # Should only call _run_subprocess twice (check and fix), not format 

70 assert_that(mock_ruff_tool._run_subprocess.call_count).is_equal_to(2) 

71 

72 

73def test_execute_ruff_fix_lint_fix_disabled( 

74 mock_ruff_tool: MagicMock, 

75 sample_ruff_json_empty_output: str, 

76) -> None: 

77 """Skip lint fix when lint_fix option is disabled. 

78 

79 Args: 

80 mock_ruff_tool: Mock RuffTool instance for testing. 

81 sample_ruff_json_empty_output: Sample empty JSON output from ruff. 

82 """ 

83 mock_ruff_tool.options["lint_fix"] = False 

84 mock_ruff_tool.options["format"] = False 

85 

86 with patch( 

87 "lintro.tools.implementations.ruff.fix.walk_files_with_excludes", 

88 ) as mock_walk: 

89 mock_walk.return_value = ["test.py"] 

90 

91 mock_ruff_tool._run_subprocess.side_effect = [ 

92 (True, sample_ruff_json_empty_output), # Initial check only 

93 ] 

94 

95 execute_ruff_fix(mock_ruff_tool, ["test.py"]) 

96 

97 # Should only call initial check, not fix 

98 assert_that(mock_ruff_tool._run_subprocess.call_count).is_equal_to(1)