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

31 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 oxlint.""" 

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6from unittest.mock import MagicMock 

7 

8from assertpy import assert_that 

9 

10from lintro.utils.native_parsers import _load_native_tool_config 

11 

12 

13def test_load_oxlint_config_from_oxlintrc_json( 

14 mock_empty_pyproject: MagicMock, 

15 temp_cwd: Path, 

16) -> None: 

17 """Load oxlint config from .oxlintrc.json file. 

18 

19 Args: 

20 mock_empty_pyproject: Mock for empty pyproject.toml. 

21 temp_cwd: Temporary current working directory. 

22 """ 

23 config = {"rules": {"no-debugger": "error"}} 

24 (temp_cwd / ".oxlintrc.json").write_text('{"rules": {"no-debugger": "error"}}') 

25 

26 result = _load_native_tool_config("oxlint") 

27 

28 assert_that(result).is_equal_to(config) 

29 

30 

31def test_load_oxlint_config_from_oxlint_json( 

32 mock_empty_pyproject: MagicMock, 

33 temp_cwd: Path, 

34) -> None: 

35 """Load oxlint config from oxlint.json file (alternative name). 

36 

37 Args: 

38 mock_empty_pyproject: Mock for empty pyproject.toml. 

39 temp_cwd: Temporary current working directory. 

40 """ 

41 config = {"plugins": ["react"], "rules": {"eqeqeq": "warn"}} 

42 (temp_cwd / "oxlint.json").write_text( 

43 '{"plugins": ["react"], "rules": {"eqeqeq": "warn"}}', 

44 ) 

45 

46 result = _load_native_tool_config("oxlint") 

47 

48 assert_that(result).is_equal_to(config) 

49 

50 

51def test_load_oxlint_config_prefers_oxlintrc_json( 

52 mock_empty_pyproject: MagicMock, 

53 temp_cwd: Path, 

54) -> None: 

55 """Prefer .oxlintrc.json over oxlint.json when both exist. 

56 

57 Args: 

58 mock_empty_pyproject: Mock for empty pyproject.toml. 

59 temp_cwd: Temporary current working directory. 

60 """ 

61 (temp_cwd / ".oxlintrc.json").write_text('{"source": "oxlintrc"}') 

62 (temp_cwd / "oxlint.json").write_text('{"source": "oxlint"}') 

63 

64 result = _load_native_tool_config("oxlint") 

65 

66 assert_that(result).is_equal_to({"source": "oxlintrc"}) 

67 

68 

69def test_load_oxlint_config_no_config_file( 

70 mock_empty_pyproject: MagicMock, 

71 temp_cwd: Path, 

72) -> None: 

73 """Return empty dict when no oxlint config file exists. 

74 

75 Args: 

76 mock_empty_pyproject: Mock for empty pyproject.toml. 

77 temp_cwd: Temporary current working directory. 

78 """ 

79 result = _load_native_tool_config("oxlint") 

80 

81 assert_that(result).is_empty() 

82 

83 

84def test_load_oxlint_config_invalid_json( 

85 mock_empty_pyproject: MagicMock, 

86 temp_cwd: Path, 

87) -> None: 

88 """Return empty dict when oxlint config contains invalid JSON. 

89 

90 Args: 

91 mock_empty_pyproject: Mock for empty pyproject.toml. 

92 temp_cwd: Temporary current working directory. 

93 """ 

94 (temp_cwd / ".oxlintrc.json").write_text("{ invalid json }") 

95 

96 result = _load_native_tool_config("oxlint") 

97 

98 assert_that(result).is_empty() 

99 

100 

101def test_load_oxlint_config_non_dict_returns_empty( 

102 mock_empty_pyproject: MagicMock, 

103 temp_cwd: Path, 

104) -> None: 

105 """Return empty dict when oxlint config is not a dict. 

106 

107 Args: 

108 mock_empty_pyproject: Mock for empty pyproject.toml. 

109 temp_cwd: Temporary current working directory. 

110 """ 

111 (temp_cwd / ".oxlintrc.json").write_text('["not", "a", "dict"]') 

112 

113 result = _load_native_tool_config("oxlint") 

114 

115 assert_that(result).is_empty()