Coverage for tests / unit / utils / native_parsers / test_markdownlint_config.py: 100%

40 statements  

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

1"""Tests for _load_native_tool_config with markdownlint.""" 

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6from unittest.mock import MagicMock, patch 

7 

8import pytest 

9from assertpy import assert_that 

10 

11from lintro.utils.native_parsers import _load_native_tool_config 

12 

13 

14def test_load_markdownlint_config_from_json( 

15 mock_empty_pyproject: MagicMock, 

16 temp_cwd: Path, 

17) -> None: 

18 """Load markdownlint config from .markdownlint.json file. 

19 

20 Args: 

21 mock_empty_pyproject: Mock for empty pyproject.toml. 

22 temp_cwd: Temporary current working directory. 

23 """ 

24 config_file = temp_cwd / ".markdownlint.json" 

25 config_file.write_text('{"MD013": false}') 

26 result = _load_native_tool_config("markdownlint") 

27 assert_that(result).is_equal_to({"MD013": False}) 

28 

29 

30def test_load_markdownlint_config_from_jsonc( 

31 mock_empty_pyproject: MagicMock, 

32 temp_cwd: Path, 

33) -> None: 

34 """Load markdownlint config from .markdownlint.jsonc with comments. 

35 

36 Args: 

37 mock_empty_pyproject: Mock for empty pyproject.toml. 

38 temp_cwd: Temporary current working directory. 

39 """ 

40 config_file = temp_cwd / ".markdownlint.jsonc" 

41 config_file.write_text( 

42 """{ 

43 // Disable line length 

44 "MD013": false 

45}""", 

46 ) 

47 result = _load_native_tool_config("markdownlint") 

48 assert_that(result).is_equal_to({"MD013": False}) 

49 

50 

51def test_load_markdownlint_config_from_yaml( 

52 mock_empty_pyproject: MagicMock, 

53 temp_cwd: Path, 

54) -> None: 

55 """Load markdownlint config from .markdownlint.yaml file. 

56 

57 Args: 

58 mock_empty_pyproject: Mock for empty pyproject.toml. 

59 temp_cwd: Temporary current working directory. 

60 """ 

61 config_file = temp_cwd / ".markdownlint.yaml" 

62 config_file.write_text("MD013: false\nMD001: true\n") 

63 result = _load_native_tool_config("markdownlint") 

64 assert_that(result).is_equal_to({"MD013": False, "MD001": True}) 

65 

66 

67def test_load_markdownlint_config_yaml_not_installed( 

68 mock_empty_pyproject: MagicMock, 

69 temp_cwd: Path, 

70) -> None: 

71 """Skip YAML config files when yaml module is not available. 

72 

73 Args: 

74 mock_empty_pyproject: Mock for empty pyproject.toml. 

75 temp_cwd: Temporary current working directory. 

76 """ 

77 config_file = temp_cwd / ".markdownlint.yaml" 

78 config_file.write_text("MD013: false\n") 

79 with patch("lintro.utils.native_parsers.yaml", None): 

80 result = _load_native_tool_config("markdownlint") 

81 assert_that(result).is_empty() 

82 

83 

84def test_load_markdownlint_config_yaml_multi_document( 

85 mock_empty_pyproject: MagicMock, 

86 temp_cwd: Path, 

87) -> None: 

88 """Handle multi-document YAML by using the first document. 

89 

90 Args: 

91 mock_empty_pyproject: Mock for empty pyproject.toml. 

92 temp_cwd: Temporary current working directory. 

93 """ 

94 config_file = temp_cwd / ".markdownlint.yaml" 

95 config_file.write_text("MD013: false\n") 

96 with patch("lintro.utils.native_parsers.yaml") as mock_yaml: 

97 mock_yaml.safe_load.return_value = [{"MD013": False}, {"MD001": True}] 

98 result = _load_native_tool_config("markdownlint") 

99 assert_that(result).is_equal_to({"MD013": False}) 

100 

101 

102@pytest.mark.parametrize( 

103 ("filename", "content", "description"), 

104 [ 

105 (".markdownlint.json", "not valid json", "invalid_json"), 

106 (".markdownlint.json", '["invalid"]', "not_a_dict"), 

107 (".markdownlint.yaml", "invalid: yaml: [", "invalid_yaml"), 

108 ], 

109 ids=["invalid_json_syntax", "json_array_not_dict", "invalid_yaml_syntax"], 

110) 

111def test_load_markdownlint_config_edge_cases( 

112 mock_empty_pyproject: MagicMock, 

113 temp_cwd: Path, 

114 filename: str, 

115 content: str, 

116 description: str, 

117) -> None: 

118 """Return empty dict for various invalid markdownlint config scenarios. 

119 

120 Args: 

121 mock_empty_pyproject: Mock for empty pyproject.toml. 

122 temp_cwd: Temporary current working directory. 

123 filename: Name of the config file to create. 

124 content: Content to write to the config file. 

125 description: Description of the test case. 

126 """ 

127 config_file = temp_cwd / filename 

128 config_file.write_text(content) 

129 result = _load_native_tool_config("markdownlint") 

130 assert_that(result).is_empty()