Coverage for tests / unit / tools / pydoclint / test_check_method.py: 100%

37 statements  

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

1"""Tests for pydoclint plugin check method.""" 

2 

3from __future__ import annotations 

4 

5import subprocess 

6from pathlib import Path 

7from unittest.mock import patch 

8 

9from assertpy import assert_that 

10 

11from lintro.tools.definitions.pydoclint import PydoclintPlugin 

12 

13 

14def test_check_with_mocked_subprocess_success( 

15 pydoclint_plugin: PydoclintPlugin, 

16 tmp_path: Path, 

17) -> None: 

18 """Check returns success when no issues found. 

19 

20 Args: 

21 pydoclint_plugin: The PydoclintPlugin instance to test. 

22 tmp_path: Temporary directory path for test files. 

23 """ 

24 test_file = tmp_path / "test_module.py" 

25 test_file.write_text('"""Test module."""\n') 

26 

27 with patch( 

28 "lintro.plugins.execution_preparation.verify_tool_version", 

29 return_value=None, 

30 ): 

31 with patch.object( 

32 pydoclint_plugin, 

33 "_run_subprocess", 

34 return_value=(True, ""), 

35 ): 

36 result = pydoclint_plugin.check([str(test_file)], {}) 

37 

38 assert_that(result.success).is_true() 

39 assert_that(result.issues_count).is_equal_to(0) 

40 

41 

42def test_check_with_mocked_subprocess_issues( 

43 pydoclint_plugin: PydoclintPlugin, 

44 tmp_path: Path, 

45) -> None: 

46 """Check returns issues when pydoclint finds problems. 

47 

48 Args: 

49 pydoclint_plugin: The PydoclintPlugin instance to test. 

50 tmp_path: Temporary directory path for test files. 

51 """ 

52 test_file = tmp_path / "test_module.py" 

53 test_file.write_text('def foo():\n """Missing return."""\n return 1\n') 

54 

55 pydoclint_output = ( 

56 f"{test_file}\n" 

57 " 1: DOC201: Function `foo` does not have a " 

58 "return section in docstring" 

59 ) 

60 

61 with patch( 

62 "lintro.plugins.execution_preparation.verify_tool_version", 

63 return_value=None, 

64 ): 

65 with patch.object( 

66 pydoclint_plugin, 

67 "_run_subprocess", 

68 return_value=(False, pydoclint_output), 

69 ): 

70 result = pydoclint_plugin.check([str(test_file)], {}) 

71 

72 assert_that(result.success).is_false() 

73 assert_that(result.issues_count).is_greater_than(0) 

74 

75 

76def test_check_with_timeout( 

77 pydoclint_plugin: PydoclintPlugin, 

78 tmp_path: Path, 

79) -> None: 

80 """Check handles timeout correctly. 

81 

82 Args: 

83 pydoclint_plugin: The PydoclintPlugin instance to test. 

84 tmp_path: Temporary directory path for test files. 

85 """ 

86 test_file = tmp_path / "test_module.py" 

87 test_file.write_text('"""Test module."""\n') 

88 

89 with patch( 

90 "lintro.plugins.execution_preparation.verify_tool_version", 

91 return_value=None, 

92 ): 

93 with patch.object( 

94 pydoclint_plugin, 

95 "_run_subprocess", 

96 side_effect=subprocess.TimeoutExpired(cmd=["pydoclint"], timeout=30), 

97 ): 

98 result = pydoclint_plugin.check([str(test_file)], {}) 

99 

100 assert_that(result.success).is_false() 

101 

102 

103def test_check_with_no_python_files( 

104 pydoclint_plugin: PydoclintPlugin, 

105 tmp_path: Path, 

106) -> None: 

107 """Check returns success when no Python files found. 

108 

109 Args: 

110 pydoclint_plugin: The PydoclintPlugin instance to test. 

111 tmp_path: Temporary directory path for test files. 

112 """ 

113 non_py_file = tmp_path / "test.txt" 

114 non_py_file.write_text("Not a python file") 

115 

116 with patch( 

117 "lintro.plugins.execution_preparation.verify_tool_version", 

118 return_value=None, 

119 ): 

120 result = pydoclint_plugin.check([str(non_py_file)], {}) 

121 

122 assert_that(result.success).is_true() 

123 assert_that(result.output).contains("No .py/.pyi files found")