Coverage for tests / unit / utils / native_parsers / test_oxfmt_config.py: 100%
43 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Tests for _load_native_tool_config with oxfmt."""
3from __future__ import annotations
5from pathlib import Path
6from unittest.mock import MagicMock
8from assertpy import assert_that
10from lintro.utils.native_parsers import _load_native_tool_config
13def test_load_oxfmt_config_from_oxfmtrc_json(
14 mock_empty_pyproject: MagicMock,
15 temp_cwd: Path,
16) -> None:
17 """Load oxfmt config from .oxfmtrc.json file.
19 Args:
20 mock_empty_pyproject: Mock for empty pyproject.toml.
21 temp_cwd: Temporary current working directory.
22 """
23 config = {"printWidth": 100, "tabWidth": 2}
24 (temp_cwd / ".oxfmtrc.json").write_text('{"printWidth": 100, "tabWidth": 2}')
26 result = _load_native_tool_config("oxfmt")
28 assert_that(result).is_equal_to(config)
31def test_load_oxfmt_config_from_oxfmtrc_jsonc(
32 mock_empty_pyproject: MagicMock,
33 temp_cwd: Path,
34) -> None:
35 """Load oxfmt config from .oxfmtrc.jsonc file with comments.
37 Args:
38 mock_empty_pyproject: Mock for empty pyproject.toml.
39 temp_cwd: Temporary current working directory.
40 """
41 content = """
42 {
43 // Line comment
44 "printWidth": 100,
45 /* Block comment */
46 "tabWidth": 2
47 }
48 """
49 (temp_cwd / ".oxfmtrc.jsonc").write_text(content)
51 result = _load_native_tool_config("oxfmt")
53 assert_that(result["printWidth"]).is_equal_to(100)
54 assert_that(result["tabWidth"]).is_equal_to(2)
57def test_load_oxfmt_config_prefers_json_over_jsonc(
58 mock_empty_pyproject: MagicMock,
59 temp_cwd: Path,
60) -> None:
61 """Prefer .oxfmtrc.json over .oxfmtrc.jsonc when both exist.
63 Args:
64 mock_empty_pyproject: Mock for empty pyproject.toml.
65 temp_cwd: Temporary current working directory.
66 """
67 (temp_cwd / ".oxfmtrc.json").write_text('{"source": "json"}')
68 (temp_cwd / ".oxfmtrc.jsonc").write_text('{"source": "jsonc"}')
70 result = _load_native_tool_config("oxfmt")
72 assert_that(result).is_equal_to({"source": "json"})
75def test_load_oxfmt_config_no_config_file(
76 mock_empty_pyproject: MagicMock,
77 temp_cwd: Path,
78) -> None:
79 """Return empty dict when no oxfmt config file exists.
81 Args:
82 mock_empty_pyproject: Mock for empty pyproject.toml.
83 temp_cwd: Temporary current working directory.
84 """
85 result = _load_native_tool_config("oxfmt")
87 assert_that(result).is_empty()
90def test_load_oxfmt_config_invalid_json(
91 mock_empty_pyproject: MagicMock,
92 temp_cwd: Path,
93) -> None:
94 """Return empty dict when oxfmt config contains invalid JSON.
96 Args:
97 mock_empty_pyproject: Mock for empty pyproject.toml.
98 temp_cwd: Temporary current working directory.
99 """
100 (temp_cwd / ".oxfmtrc.json").write_text("{ invalid json }")
102 result = _load_native_tool_config("oxfmt")
104 assert_that(result).is_empty()
107def test_load_oxfmt_config_non_dict_returns_empty(
108 mock_empty_pyproject: MagicMock,
109 temp_cwd: Path,
110) -> None:
111 """Return empty dict when oxfmt config is not a dict.
113 Args:
114 mock_empty_pyproject: Mock for empty pyproject.toml.
115 temp_cwd: Temporary current working directory.
116 """
117 (temp_cwd / ".oxfmtrc.json").write_text('["not", "a", "dict"]')
119 result = _load_native_tool_config("oxfmt")
121 assert_that(result).is_empty()
124def test_load_oxfmt_config_jsonc_with_block_comments(
125 mock_empty_pyproject: MagicMock,
126 temp_cwd: Path,
127) -> None:
128 """Load oxfmt JSONC config with block comments.
130 Args:
131 mock_empty_pyproject: Mock for empty pyproject.toml.
132 temp_cwd: Temporary current working directory.
133 """
134 content = """
135 {
136 /* This is a block comment
137 spanning multiple lines */
138 "semi": true,
139 "singleQuote": false
140 }
141 """
142 (temp_cwd / ".oxfmtrc.jsonc").write_text(content)
144 result = _load_native_tool_config("oxfmt")
146 assert_that(result["semi"]).is_true()
147 assert_that(result["singleQuote"]).is_false()
150def test_load_oxfmt_config_jsonc_with_trailing_comma(
151 mock_empty_pyproject: MagicMock,
152 temp_cwd: Path,
153) -> None:
154 """JSONC parser strips comments but not trailing commas (JSON spec).
156 Args:
157 mock_empty_pyproject: Mock for empty pyproject.toml.
158 temp_cwd: Temporary current working directory.
159 """
160 # Note: Standard JSON doesn't support trailing commas
161 # JSONC comment stripping works, but trailing comma is still invalid JSON
162 content = """
163 {
164 // comment
165 "printWidth": 80
166 }
167 """
168 (temp_cwd / ".oxfmtrc.jsonc").write_text(content)
170 result = _load_native_tool_config("oxfmt")
172 assert_that(result).is_equal_to({"printWidth": 80})