| 1 |
"""Tests for ``config`` module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import logging |
| 6 |
import pathlib |
| 7 |
import re |
| 8 |
import typing as t |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from rstcheck_core import _extras, config |
| 13 |
|
| 14 |
|
| 15 |
def test_report_level_map_matches_numbers() -> None: |
| 16 |
"""Test that the enum's values match the map's ones.""" |
| 17 |
enum_values = [e.value for e in config.ReportLevel] |
| 18 |
map_values = list(config.ReportLevelMap.values()) |
| 19 |
|
| 20 |
assert map_values == enum_values |
| 21 |
|
| 22 |
|
| 23 |
def test_report_level_map_matches_names() -> None: |
| 24 |
"""Test that the enum's name match the map's keys.""" |
| 25 |
enum_names = [e.casefold() for e in config.ReportLevel._member_names_] |
| 26 |
map_keys = list(config.ReportLevelMap.keys()) |
| 27 |
|
| 28 |
assert enum_names == map_keys |
| 29 |
|
| 30 |
|
| 31 |
def test_default_values_for_config_file() -> None: |
| 32 |
"""Test default values of ``RstcheckConfigFile``.""" |
| 33 |
result = config.RstcheckConfigFile() |
| 34 |
|
| 35 |
assert result.report_level is None |
| 36 |
assert result.ignore_directives is None |
| 37 |
assert result.ignore_roles is None |
| 38 |
assert result.ignore_substitutions is None |
| 39 |
assert result.ignore_languages is None |
| 40 |
assert result.ignore_messages is None |
| 41 |
|
| 42 |
|
| 43 |
def test_default_values_for_config() -> None: |
| 44 |
"""Test default values of ``RstcheckConfig``.""" |
| 45 |
result = config.RstcheckConfig() |
| 46 |
|
| 47 |
assert result.report_level is None |
| 48 |
assert result.ignore_directives is None |
| 49 |
assert result.ignore_roles is None |
| 50 |
assert result.ignore_substitutions is None |
| 51 |
assert result.ignore_languages is None |
| 52 |
assert result.ignore_messages is None |
| 53 |
assert result.config_path is None |
| 54 |
assert result.recursive is None |
| 55 |
|
| 56 |
|
| 57 |
class TestSplitStrValidator: |
| 58 |
"""Test ``_split_str_validator`` validator function.""" |
| 59 |
|
| 60 |
@staticmethod |
| 61 |
def test_none_means_default() -> None: |
| 62 |
"""Test ``None`` results in ``None``.""" |
| 63 |
result = config._split_str_validator(None) |
| 64 |
|
| 65 |
assert result is None |
| 66 |
|
| 67 |
@staticmethod |
| 68 |
@pytest.mark.parametrize( |
| 69 |
("string", "split_list"), |
| 70 |
[ |
| 71 |
("value1", ["value1"]), |
| 72 |
("value1,value2", ["value1", "value2"]), |
| 73 |
("value1, value2", ["value1", "value2"]), |
| 74 |
("value1 ,value2", ["value1", "value2"]), |
| 75 |
("value1 , value2", ["value1", "value2"]), |
| 76 |
("value1 ,\n value2", ["value1", "value2"]), |
| 77 |
("value1 ,\n value2\n", ["value1", "value2"]), |
| 78 |
("value1 , value2,", ["value1", "value2"]), |
| 79 |
("value1 , value2 ,", ["value1", "value2"]), |
| 80 |
("value1 , value2 , ", ["value1", "value2"]), |
| 81 |
], |
| 82 |
) |
| 83 |
def test_strings_are_transformed_to_lists(string: str, split_list: list[str]) -> None: |
| 84 |
"""Test strings are split at the ",", trailing commas are ignored and whitespace cleaned.""" |
| 85 |
result = config._split_str_validator(string) |
| 86 |
|
| 87 |
assert result == split_list |
| 88 |
|
| 89 |
@staticmethod |
| 90 |
@pytest.mark.parametrize( |
| 91 |
("string_list", "string_list_cleaned"), |
| 92 |
[ |
| 93 |
(["value1"], ["value1"]), |
| 94 |
(["value1", "value2"], ["value1", "value2"]), |
| 95 |
(["value1", " value2"], ["value1", "value2"]), |
| 96 |
(["value1 ", "value2"], ["value1", "value2"]), |
| 97 |
(["value1 ", " value2"], ["value1", "value2"]), |
| 98 |
], |
| 99 |
) |
| 100 |
def test_string_lists_are_whitespace_cleaned( |
| 101 |
string_list: list[str], string_list_cleaned: list[str] |
| 102 |
) -> None: |
| 103 |
"""Test lists of strings are whitespace cleaned.""" |
| 104 |
result = config._split_str_validator(string_list) |
| 105 |
|
| 106 |
assert result == string_list_cleaned |
| 107 |
|
| 108 |
@staticmethod |
| 109 |
@pytest.mark.parametrize( |
| 110 |
"value", |
| 111 |
[ |
| 112 |
1, |
| 113 |
[1], |
| 114 |
1.1, |
| 115 |
[1.1], |
| 116 |
False, |
| 117 |
[False], |
| 118 |
True, |
| 119 |
[True], |
| 120 |
["foo", 1], |
| 121 |
], |
| 122 |
) |
| 123 |
def test_invalid_settings(value: str) -> None: |
| 124 |
"""Test invalid settings.""" |
| 125 |
with pytest.raises(TypeError, match="^Not a string or list of strings$"): |
| 126 |
config._split_str_validator(value) |
| 127 |
|
| 128 |
|
| 129 |
class TestReportLevelValidatorMethod: |
| 130 |
"""Test ``valid_report_level`` validator method of the ``RstcheckConfig`` class. |
| 131 |
|
| 132 |
It validates the ``report_level`` setting. |
| 133 |
""" |
| 134 |
|
| 135 |
@staticmethod |
| 136 |
def test_none_means_none() -> None: |
| 137 |
"""Test ``None`` results in no report level.""" |
| 138 |
result = config.RstcheckConfigFile(report_level=None) |
| 139 |
|
| 140 |
assert result is not None |
| 141 |
assert result.report_level is None |
| 142 |
|
| 143 |
@staticmethod |
| 144 |
def test_set_level_stays() -> None: |
| 145 |
"""Test set level results in same level.""" |
| 146 |
result = config.RstcheckConfigFile(report_level=config.ReportLevel.SEVERE) |
| 147 |
|
| 148 |
assert result is not None |
| 149 |
assert result.report_level is config.ReportLevel.SEVERE |
| 150 |
|
| 151 |
@staticmethod |
| 152 |
def test_empty_string_means_default() -> None: |
| 153 |
"""Test empty string results in default report level.""" |
| 154 |
result = config.RstcheckConfigFile(report_level="") |
| 155 |
|
| 156 |
assert result is not None |
| 157 |
assert result.report_level is config.ReportLevel.INFO |
| 158 |
|
| 159 |
@staticmethod |
| 160 |
@pytest.mark.parametrize( |
| 161 |
"level", |
| 162 |
[1, 2, 3, 4, 5, "info", "warning", "error", "severe", "none", "NONE", "None", "NoNe"], |
| 163 |
) |
| 164 |
def test_valid_report_levels(level: t.Any) -> None: |
| 165 |
"""Test valid report levels accepted by docutils.""" |
| 166 |
result = config.RstcheckConfigFile(report_level=level) |
| 167 |
|
| 168 |
assert result is not None |
| 169 |
assert result.report_level is not None |
| 170 |
|
| 171 |
@staticmethod |
| 172 |
@pytest.mark.parametrize( |
| 173 |
"level", |
| 174 |
[-1, 0, 1.5, 6, 32, False, True, "information", "warn", "err", "critical", "fatal"], |
| 175 |
) |
| 176 |
def test_invalid_report_levels(level: t.Any) -> None: |
| 177 |
"""Test invalid report levels not accepted by docutils.""" |
| 178 |
with pytest.raises(TypeError, match="^Invalid report level$"): |
| 179 |
config.RstcheckConfigFile(report_level=level) |
| 180 |
|
| 181 |
|
| 182 |
class TestSplitStrValidatorMethod: |
| 183 |
"""Test ``split_str`` validator method of the ``RstcheckConfig`` class. |
| 184 |
|
| 185 |
It validates the |
| 186 |
|
| 187 |
- ``ignore_directives`` |
| 188 |
- ``ignore_roles`` |
| 189 |
- ``ignore_substitutions`` |
| 190 |
- ``ignore_languages`` |
| 191 |
|
| 192 |
settings. |
| 193 |
""" |
| 194 |
|
| 195 |
@staticmethod |
| 196 |
def test_none_means_default() -> None: |
| 197 |
"""Test ``None`` results in unset ignore_messages.""" |
| 198 |
result = config.RstcheckConfigFile( |
| 199 |
ignore_languages=None, |
| 200 |
ignore_directives=None, |
| 201 |
ignore_roles=None, |
| 202 |
ignore_substitutions=None, |
| 203 |
) |
| 204 |
|
| 205 |
assert result is not None |
| 206 |
assert result.ignore_languages is None |
| 207 |
assert result.ignore_directives is None |
| 208 |
assert result.ignore_roles is None |
| 209 |
assert result.ignore_substitutions is None |
| 210 |
|
| 211 |
@staticmethod |
| 212 |
@pytest.mark.parametrize( |
| 213 |
("string", "split_list"), |
| 214 |
[ |
| 215 |
("value1", ["value1"]), |
| 216 |
("value1,value2", ["value1", "value2"]), |
| 217 |
("value1, value2", ["value1", "value2"]), |
| 218 |
("value1 ,value2", ["value1", "value2"]), |
| 219 |
("value1 , value2", ["value1", "value2"]), |
| 220 |
("value1 ,\n value2", ["value1", "value2"]), |
| 221 |
("value1 ,\n value2\n", ["value1", "value2"]), |
| 222 |
("value1 , value2,", ["value1", "value2"]), |
| 223 |
("value1 , value2 ,", ["value1", "value2"]), |
| 224 |
("value1 , value2 , ", ["value1", "value2"]), |
| 225 |
], |
| 226 |
) |
| 227 |
def test_strings_are_transformed_to_lists(string: str, split_list: list[str]) -> None: |
| 228 |
"""Test strings are split at the ",", trailing commas are ignored and whitespace cleaned.""" |
| 229 |
result = config.RstcheckConfigFile( |
| 230 |
ignore_languages=string, |
| 231 |
ignore_directives=string, |
| 232 |
ignore_roles=string, |
| 233 |
ignore_substitutions=string, |
| 234 |
) |
| 235 |
|
| 236 |
assert result is not None |
| 237 |
assert result.ignore_languages == split_list |
| 238 |
assert result.ignore_directives == split_list |
| 239 |
assert result.ignore_roles == split_list |
| 240 |
assert result.ignore_substitutions == split_list |
| 241 |
|
| 242 |
@staticmethod |
| 243 |
@pytest.mark.parametrize( |
| 244 |
("string_list", "string_list_cleaned"), |
| 245 |
[ |
| 246 |
(["value1"], ["value1"]), |
| 247 |
(["value1", "value2"], ["value1", "value2"]), |
| 248 |
(["value1", " value2"], ["value1", "value2"]), |
| 249 |
(["value1 ", "value2"], ["value1", "value2"]), |
| 250 |
(["value1 ", " value2"], ["value1", "value2"]), |
| 251 |
], |
| 252 |
) |
| 253 |
def test_string_lists_are_whitespace_cleaned( |
| 254 |
string_list: list[str], string_list_cleaned: list[str] |
| 255 |
) -> None: |
| 256 |
"""Test lists of strings are whitespace cleaned.""" |
| 257 |
result = config.RstcheckConfigFile( |
| 258 |
ignore_languages=string_list, |
| 259 |
ignore_directives=string_list, |
| 260 |
ignore_roles=string_list, |
| 261 |
ignore_substitutions=string_list, |
| 262 |
) |
| 263 |
|
| 264 |
assert result is not None |
| 265 |
assert result.ignore_languages == string_list_cleaned |
| 266 |
assert result.ignore_directives == string_list_cleaned |
| 267 |
assert result.ignore_roles == string_list_cleaned |
| 268 |
assert result.ignore_substitutions == string_list_cleaned |
| 269 |
|
| 270 |
@staticmethod |
| 271 |
@pytest.mark.parametrize( |
| 272 |
"value", |
| 273 |
[ |
| 274 |
1, |
| 275 |
[1], |
| 276 |
1.1, |
| 277 |
[1.1], |
| 278 |
False, |
| 279 |
[False], |
| 280 |
True, |
| 281 |
[True], |
| 282 |
["foo", 1], |
| 283 |
], |
| 284 |
) |
| 285 |
def test_invalid_settings(value: str) -> None: |
| 286 |
"""Test invalid settings.""" |
| 287 |
with pytest.raises(TypeError, match="^Not a string or list of strings$"): |
| 288 |
config.RstcheckConfigFile( |
| 289 |
ignore_languages=value, |
| 290 |
ignore_directives=value, |
| 291 |
ignore_roles=value, |
| 292 |
ignore_substitutions=value, |
| 293 |
) |
| 294 |
|
| 295 |
|
| 296 |
class TestJoinRegexStrValidatorMethod: |
| 297 |
"""Test ``join_regex_str`` validator method of the ``RstcheckConfig`` class. |
| 298 |
|
| 299 |
It validates the ``ignore_messages`` setting. |
| 300 |
""" |
| 301 |
|
| 302 |
@staticmethod |
| 303 |
def test_none_means_unset() -> None: |
| 304 |
"""Test ``None`` results in unset ignore_messages.""" |
| 305 |
result = config.RstcheckConfigFile(ignore_messages=None) |
| 306 |
|
| 307 |
assert result is not None |
| 308 |
assert result.ignore_messages is None |
| 309 |
|
| 310 |
@staticmethod |
| 311 |
def test_passed_regex_is_kept() -> None: |
| 312 |
"""Test passing a regex.""" |
| 313 |
result = config.RstcheckConfigFile(ignore_messages=re.compile("")) |
| 314 |
|
| 315 |
assert result is not None |
| 316 |
assert isinstance(result.ignore_messages, re.Pattern) |
| 317 |
|
| 318 |
@staticmethod |
| 319 |
def test_strings_are_parsed_as_regex() -> None: |
| 320 |
"""Test strings are parsed as regex.""" |
| 321 |
string = r"\d{4}\.[A-Z]+Test$" |
| 322 |
regex = re.compile(string) |
| 323 |
|
| 324 |
result = config.RstcheckConfigFile(ignore_messages=string) |
| 325 |
|
| 326 |
assert result is not None |
| 327 |
assert result.ignore_messages == regex |
| 328 |
|
| 329 |
@staticmethod |
| 330 |
def test_empty_strings_are_valid() -> None: |
| 331 |
"""Test empty strings are parsed as regex too.""" |
| 332 |
string = "" |
| 333 |
regex = re.compile(string) |
| 334 |
|
| 335 |
result = config.RstcheckConfigFile(ignore_messages=string) |
| 336 |
|
| 337 |
assert result is not None |
| 338 |
assert result.ignore_messages == regex |
| 339 |
|
| 340 |
@staticmethod |
| 341 |
def test_string_list_are_joined_and_parsed_as_regex() -> None: |
| 342 |
"""Test ignore_messages string lists are joined with "|" and parsed as regex.""" |
| 343 |
string_list = [r"\d{4}\.[A-Z]+Test$", r"\d{4}\.[A-Z]+Test2$", r"\d{4}\.[A-Z]+Test3$"] |
| 344 |
full_string = r"\d{4}\.[A-Z]+Test$|\d{4}\.[A-Z]+Test2$|\d{4}\.[A-Z]+Test3$" |
| 345 |
regex = re.compile(full_string) |
| 346 |
|
| 347 |
result = config.RstcheckConfigFile(ignore_messages=string_list) |
| 348 |
|
| 349 |
assert result is not None |
| 350 |
assert result.ignore_messages == regex |
| 351 |
|
| 352 |
@staticmethod |
| 353 |
@pytest.mark.parametrize( |
| 354 |
("string_list", "full_string"), [([""], ""), (["", ""], "|"), ([], "")] |
| 355 |
) |
| 356 |
def test_list_with_empty_contents(string_list: list[str], full_string: str) -> None: |
| 357 |
"""Test list with empty contents are parsed as regex too.""" |
| 358 |
regex = re.compile(full_string) |
| 359 |
|
| 360 |
result = config.RstcheckConfigFile(ignore_messages=string_list) |
| 361 |
|
| 362 |
assert result is not None |
| 363 |
assert result.ignore_messages == regex |
| 364 |
|
| 365 |
@staticmethod |
| 366 |
@pytest.mark.parametrize( |
| 367 |
"value", |
| 368 |
[ |
| 369 |
1, |
| 370 |
[1], |
| 371 |
1.1, |
| 372 |
[1.1], |
| 373 |
False, |
| 374 |
[False], |
| 375 |
True, |
| 376 |
[True], |
| 377 |
["foo", 1], |
| 378 |
], |
| 379 |
) |
| 380 |
def test_invalid_settings(value: str) -> None: |
| 381 |
"""Test invalid ignore_messages settings.""" |
| 382 |
with pytest.raises(TypeError, match="^Not a string or list of strings$"): |
| 383 |
config.RstcheckConfigFile(ignore_messages=value) |
| 384 |
|
| 385 |
|
| 386 |
class TestIniFileLoader: |
| 387 |
"""Test ``load_config_from_ini_file``.""" |
| 388 |
|
| 389 |
@staticmethod |
| 390 |
def test_no_config_on_none() -> None: |
| 391 |
"""Test no config is loaded if NONE is set.""" |
| 392 |
conf_file = pathlib.Path("NONE") |
| 393 |
|
| 394 |
result = config._load_config_from_ini_file(conf_file) |
| 395 |
|
| 396 |
assert result is None |
| 397 |
|
| 398 |
@staticmethod |
| 399 |
def test_missing_file_errors(tmp_path: pathlib.Path) -> None: |
| 400 |
"""Test ``FileNotFoundError`` is raised on missing file.""" |
| 401 |
conf_file = tmp_path / "config.ini" |
| 402 |
|
| 403 |
with pytest.raises(FileNotFoundError): |
| 404 |
config._load_config_from_ini_file(conf_file) |
| 405 |
|
| 406 |
@staticmethod |
| 407 |
def test_not_a_file_errors(tmp_path: pathlib.Path) -> None: |
| 408 |
"""Test ``FileNotFoundError`` is raised when not file.""" |
| 409 |
conf_file = tmp_path |
| 410 |
|
| 411 |
with pytest.raises(FileNotFoundError): |
| 412 |
config._load_config_from_ini_file(conf_file) |
| 413 |
|
| 414 |
@staticmethod |
| 415 |
def test_returns_none_on_missing_section(tmp_path: pathlib.Path) -> None: |
| 416 |
"""Test ``None`` is returned on missing section.""" |
| 417 |
conf_file = tmp_path / "config.ini" |
| 418 |
file_content = "[not-rstcheck]" |
| 419 |
conf_file.write_text(file_content) |
| 420 |
|
| 421 |
result = config._load_config_from_ini_file(conf_file) |
| 422 |
|
| 423 |
assert result is None |
| 424 |
|
| 425 |
@staticmethod |
| 426 |
def test_ignores_unsupported_settings(tmp_path: pathlib.Path) -> None: |
| 427 |
"""Test unsupported settings are ignored.""" |
| 428 |
conf_file = tmp_path / "config.ini" |
| 429 |
file_content = """[rstcheck] |
| 430 |
unsupported_feature=True |
| 431 |
""" |
| 432 |
conf_file.write_text(file_content) |
| 433 |
|
| 434 |
result = config._load_config_from_ini_file(conf_file) |
| 435 |
|
| 436 |
assert result is not None |
| 437 |
|
| 438 |
@staticmethod |
| 439 |
def test_supported_settings_are_loaded(tmp_path: pathlib.Path) -> None: |
| 440 |
"""Test supported settings are loaded.""" |
| 441 |
conf_file = tmp_path / "config.ini" |
| 442 |
file_content = """[rstcheck] |
| 443 |
report_level=3 |
| 444 |
ignore_directives=directive |
| 445 |
ignore_roles=role |
| 446 |
ignore_substitutions=substitution |
| 447 |
ignore_languages=language |
| 448 |
ignore_messages=message |
| 449 |
""" |
| 450 |
conf_file.write_text(file_content) |
| 451 |
regex = re.compile("message") |
| 452 |
|
| 453 |
result = config._load_config_from_ini_file(conf_file) |
| 454 |
|
| 455 |
assert result is not None |
| 456 |
assert result.report_level == config.ReportLevel.ERROR |
| 457 |
assert result.ignore_directives == ["directive"] |
| 458 |
assert result.ignore_roles == ["role"] |
| 459 |
assert result.ignore_substitutions == ["substitution"] |
| 460 |
assert result.ignore_languages == ["language"] |
| 461 |
assert result.ignore_messages == regex |
| 462 |
|
| 463 |
@staticmethod |
| 464 |
def test_multiline_strings(tmp_path: pathlib.Path) -> None: |
| 465 |
"""Test multiline strings are parsed.""" |
| 466 |
conf_file = tmp_path / "config.ini" |
| 467 |
file_content = """[rstcheck] |
| 468 |
ignore_directives=directive |
| 469 |
ignore_roles= |
| 470 |
role1, |
| 471 |
role2, |
| 472 |
role3 |
| 473 |
""" |
| 474 |
conf_file.write_text(file_content) |
| 475 |
|
| 476 |
result = config._load_config_from_ini_file(conf_file) |
| 477 |
|
| 478 |
assert result is not None |
| 479 |
assert result.ignore_directives == ["directive"] |
| 480 |
assert result.ignore_roles == ["role1", "role2", "role3"] |
| 481 |
|
| 482 |
@staticmethod |
| 483 |
def test_multiline_strings_trailing_comma(tmp_path: pathlib.Path) -> None: |
| 484 |
"""Test multiline strings with trailing comma are parsed.""" |
| 485 |
conf_file = tmp_path / "config.ini" |
| 486 |
file_content = """[rstcheck] |
| 487 |
ignore_directives=directive |
| 488 |
ignore_roles= |
| 489 |
role1, |
| 490 |
role2, |
| 491 |
role3, |
| 492 |
""" |
| 493 |
conf_file.write_text(file_content) |
| 494 |
|
| 495 |
result = config._load_config_from_ini_file(conf_file) |
| 496 |
|
| 497 |
assert result is not None |
| 498 |
assert result.ignore_directives == ["directive"] |
| 499 |
assert result.ignore_roles == ["role1", "role2", "role3"] |
| 500 |
|
| 501 |
@staticmethod |
| 502 |
def test_file_with_mixed_supported_settings(tmp_path: pathlib.Path) -> None: |
| 503 |
"""Test mix of supported and unsupported settings.""" |
| 504 |
conf_file = tmp_path / "config.ini" |
| 505 |
file_content = """[rstcheck] |
| 506 |
report_level=3 |
| 507 |
ignore_directives=directive |
| 508 |
unsupported_feature=True |
| 509 |
""" |
| 510 |
conf_file.write_text(file_content) |
| 511 |
|
| 512 |
result = config._load_config_from_ini_file(conf_file) |
| 513 |
|
| 514 |
assert result is not None |
| 515 |
assert result.report_level == config.ReportLevel.ERROR |
| 516 |
assert result.ignore_directives == ["directive"] |
| 517 |
|
| 518 |
@staticmethod |
| 519 |
def test_file_with_mixed_supported_sections(tmp_path: pathlib.Path) -> None: |
| 520 |
"""Test mix of rstcheck and other section.""" |
| 521 |
conf_file = tmp_path / "config.ini" |
| 522 |
file_content = """[rstcheck] |
| 523 |
report_level=3 |
| 524 |
ignore_directives=directive |
| 525 |
unsupported_feature=True |
| 526 |
|
| 527 |
[not-rstcheck] |
| 528 |
report_level=2 |
| 529 |
ignore_directives=not-directive |
| 530 |
unsupported_feature=False |
| 531 |
""" |
| 532 |
conf_file.write_text(file_content) |
| 533 |
|
| 534 |
result = config._load_config_from_ini_file(conf_file) |
| 535 |
|
| 536 |
assert result is not None |
| 537 |
assert result.report_level == config.ReportLevel.ERROR |
| 538 |
assert result.ignore_directives == ["directive"] |
| 539 |
|
| 540 |
@staticmethod |
| 541 |
def test_warning_is_logged_on_missing_section_by_default( |
| 542 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 543 |
) -> None: |
| 544 |
"""Test if a warning is logged on missing section.""" |
| 545 |
conf_file = tmp_path / "config.ini" |
| 546 |
file_content = "[checkrst]" |
| 547 |
conf_file.write_text(file_content) |
| 548 |
|
| 549 |
result = config._load_config_from_ini_file(conf_file) |
| 550 |
|
| 551 |
assert result is None |
| 552 |
assert f"Config file has no [rstcheck] section: '{conf_file}'." in caplog.text |
| 553 |
|
| 554 |
@staticmethod |
| 555 |
def test_warning_on_missing_section_can_be_info( |
| 556 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 557 |
) -> None: |
| 558 |
"""Test if the warning logged on missing section can be an info.""" |
| 559 |
conf_file = tmp_path / "config.ini" |
| 560 |
file_content = "[checkrst]" |
| 561 |
conf_file.write_text(file_content) |
| 562 |
caplog.set_level(logging.INFO) |
| 563 |
|
| 564 |
result = config._load_config_from_ini_file(conf_file, log_missing_section_as_warning=False) |
| 565 |
|
| 566 |
assert result is None |
| 567 |
assert f"Config file has no [rstcheck] section: '{conf_file}'." in caplog.text |
| 568 |
|
| 569 |
@staticmethod |
| 570 |
def test_dont_warning_on_unknown_settings_by_default( |
| 571 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 572 |
) -> None: |
| 573 |
"""Test that no warning is logged on unknown setting by default.""" |
| 574 |
conf_file = tmp_path / "config.ini" |
| 575 |
file_content = """ |
| 576 |
[rstcheck] |
| 577 |
report_level=INFO |
| 578 |
unkwown_setting=true |
| 579 |
""" |
| 580 |
conf_file.write_text(file_content) |
| 581 |
|
| 582 |
result = config._load_config_from_ini_file( |
| 583 |
conf_file, |
| 584 |
log_missing_section_as_warning=False, |
| 585 |
) |
| 586 |
|
| 587 |
assert result is not None |
| 588 |
assert not caplog.text |
| 589 |
|
| 590 |
@staticmethod |
| 591 |
def test_warning_on_unknown_settings_when_set( |
| 592 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 593 |
) -> None: |
| 594 |
"""Test that a warning is logged on unknown setting when activated.""" |
| 595 |
conf_file = tmp_path / "config.ini" |
| 596 |
file_content = """ |
| 597 |
[rstcheck] |
| 598 |
report_level=INFO |
| 599 |
unkwown_setting=true |
| 600 |
""" |
| 601 |
conf_file.write_text(file_content) |
| 602 |
|
| 603 |
result = config._load_config_from_ini_file( |
| 604 |
conf_file, |
| 605 |
log_missing_section_as_warning=False, |
| 606 |
warn_unknown_settings=True, |
| 607 |
) |
| 608 |
|
| 609 |
assert result is not None |
| 610 |
assert ( |
| 611 |
f"Unknown setting(s) ['unkwown_setting'] found in file: '{conf_file}'." in caplog.text |
| 612 |
) |
| 613 |
|
| 614 |
@staticmethod |
| 615 |
def test_no_warning_on_known_settings_when_set( |
| 616 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 617 |
) -> None: |
| 618 |
"""Test that a warning is logged on known settings when activated.""" |
| 619 |
conf_file = tmp_path / "config.ini" |
| 620 |
file_content = """ |
| 621 |
[rstcheck] |
| 622 |
report_level=INFO |
| 623 |
""" |
| 624 |
conf_file.write_text(file_content) |
| 625 |
|
| 626 |
result = config._load_config_from_ini_file( |
| 627 |
conf_file, |
| 628 |
log_missing_section_as_warning=False, |
| 629 |
warn_unknown_settings=True, |
| 630 |
) |
| 631 |
|
| 632 |
assert result is not None |
| 633 |
assert not caplog.text |
| 634 |
|
| 635 |
|
| 636 |
@pytest.mark.skipif(not _extras.TOMLI_INSTALLED, reason="Depends on toml extra.") |
| 637 |
class TestTomlFileLoader: |
| 638 |
"""Test ``load_config_from_toml_file``.""" |
| 639 |
|
| 640 |
@staticmethod |
| 641 |
def test_no_config_on_none() -> None: |
| 642 |
"""Test no config is loaded if NONE is set.""" |
| 643 |
conf_file = pathlib.Path("NONE") |
| 644 |
|
| 645 |
result = config._load_config_from_toml_file(conf_file) |
| 646 |
|
| 647 |
assert result is None |
| 648 |
|
| 649 |
@staticmethod |
| 650 |
def test_wrong_file_suffix_errors(tmp_path: pathlib.Path) -> None: |
| 651 |
"""Test ``ValueError`` is raised on wrong file suffix.""" |
| 652 |
conf_file = tmp_path / "config.ini" |
| 653 |
conf_file.touch() |
| 654 |
|
| 655 |
with pytest.raises(ValueError, match="^File is not a TOML file$"): |
| 656 |
config._load_config_from_toml_file(conf_file) |
| 657 |
|
| 658 |
@staticmethod |
| 659 |
def test_missing_file_errors(tmp_path: pathlib.Path) -> None: |
| 660 |
"""Test ``FileNotFoundError`` is raised on missing file.""" |
| 661 |
conf_file = tmp_path / "config.toml" |
| 662 |
|
| 663 |
with pytest.raises(FileNotFoundError): |
| 664 |
config._load_config_from_toml_file(conf_file) |
| 665 |
|
| 666 |
@staticmethod |
| 667 |
def test_not_a_file_errors(tmp_path: pathlib.Path) -> None: |
| 668 |
"""Test ``FileNotFoundError`` is raised when not file.""" |
| 669 |
conf_file = tmp_path / "config.toml" |
| 670 |
conf_file.mkdir() |
| 671 |
|
| 672 |
with pytest.raises(FileNotFoundError): |
| 673 |
config._load_config_from_toml_file(conf_file) |
| 674 |
|
| 675 |
@staticmethod |
| 676 |
@pytest.mark.parametrize("invalid_section", ["[tool.not-rstcheck]", "[rstcheck]"]) |
| 677 |
def test_returns_none_on_missing_section(tmp_path: pathlib.Path, invalid_section: str) -> None: |
| 678 |
"""Test ``None`` is returned on missing section.""" |
| 679 |
conf_file = tmp_path / "config.toml" |
| 680 |
conf_file.write_text(invalid_section) |
| 681 |
|
| 682 |
result = config._load_config_from_toml_file(conf_file) |
| 683 |
|
| 684 |
assert result is None |
| 685 |
|
| 686 |
@staticmethod |
| 687 |
def test_ignores_unsupported_settings(tmp_path: pathlib.Path) -> None: |
| 688 |
"""Test unsupported settings are ignored.""" |
| 689 |
conf_file = tmp_path / "config.toml" |
| 690 |
file_content = """[tool.rstcheck] |
| 691 |
unsupported_feature = true |
| 692 |
""" |
| 693 |
conf_file.write_text(file_content) |
| 694 |
|
| 695 |
result = config._load_config_from_toml_file(conf_file) |
| 696 |
|
| 697 |
assert result is not None |
| 698 |
|
| 699 |
@staticmethod |
| 700 |
def test_supported_settings_are_loaded(tmp_path: pathlib.Path) -> None: |
| 701 |
"""Test supported settings are loaded.""" |
| 702 |
conf_file = tmp_path / "config.toml" |
| 703 |
file_content = """[tool.rstcheck] |
| 704 |
report_level = 3 |
| 705 |
ignore_directives = ["directive"] |
| 706 |
ignore_roles = ["role"] |
| 707 |
ignore_substitutions = ["substitution"] |
| 708 |
ignore_languages = ["language"] |
| 709 |
ignore_messages = "message" |
| 710 |
""" |
| 711 |
conf_file.write_text(file_content) |
| 712 |
regex = re.compile("message") |
| 713 |
|
| 714 |
result = config._load_config_from_toml_file(conf_file) |
| 715 |
|
| 716 |
assert result is not None |
| 717 |
assert result.report_level == config.ReportLevel.ERROR |
| 718 |
assert result.ignore_directives == ["directive"] |
| 719 |
assert result.ignore_roles == ["role"] |
| 720 |
assert result.ignore_substitutions == ["substitution"] |
| 721 |
assert result.ignore_languages == ["language"] |
| 722 |
assert result.ignore_messages == regex |
| 723 |
|
| 724 |
@staticmethod |
| 725 |
def test_file_with_mixed_supported_settings(tmp_path: pathlib.Path) -> None: |
| 726 |
"""Test mix of supported and unsupported settings.""" |
| 727 |
conf_file = tmp_path / "config.toml" |
| 728 |
file_content = """[tool.rstcheck] |
| 729 |
report_level = 3 |
| 730 |
ignore_directives = ["directive"] |
| 731 |
unsupported_feature = true |
| 732 |
""" |
| 733 |
conf_file.write_text(file_content) |
| 734 |
|
| 735 |
result = config._load_config_from_toml_file(conf_file) |
| 736 |
|
| 737 |
assert result is not None |
| 738 |
assert result.report_level == config.ReportLevel.ERROR |
| 739 |
assert result.ignore_directives == ["directive"] |
| 740 |
|
| 741 |
@staticmethod |
| 742 |
def test_file_with_mixed_supported_sections(tmp_path: pathlib.Path) -> None: |
| 743 |
"""Test mix of rstcheck and other section.""" |
| 744 |
conf_file = tmp_path / "config.toml" |
| 745 |
file_content = """[tool.rstcheck] |
| 746 |
report_level = 3 |
| 747 |
ignore_directives = ["directive"] |
| 748 |
unsupported_feature = true |
| 749 |
|
| 750 |
[tool.not-rstcheck] |
| 751 |
report_level = 2 |
| 752 |
ignore_directives = "not-directive" |
| 753 |
unsupported_feature = false |
| 754 |
""" |
| 755 |
conf_file.write_text(file_content) |
| 756 |
|
| 757 |
result = config._load_config_from_toml_file(conf_file) |
| 758 |
|
| 759 |
assert result is not None |
| 760 |
assert result.report_level == config.ReportLevel.ERROR |
| 761 |
assert result.ignore_directives == ["directive"] |
| 762 |
|
| 763 |
@staticmethod |
| 764 |
@pytest.mark.parametrize( |
| 765 |
("value", "parsed_value"), |
| 766 |
[ |
| 767 |
("none", config.ReportLevel.NONE), |
| 768 |
("ERROR", config.ReportLevel.ERROR), |
| 769 |
("1", config.ReportLevel.INFO), |
| 770 |
("3", config.ReportLevel.ERROR), |
| 771 |
], |
| 772 |
) |
| 773 |
def test_report_level_as_strings( |
| 774 |
tmp_path: pathlib.Path, value: str, parsed_value: config.ReportLevel |
| 775 |
) -> None: |
| 776 |
"""Test report setting with string values.""" |
| 777 |
conf_file = tmp_path / "config.toml" |
| 778 |
file_content = f"""[tool.rstcheck] |
| 779 |
report_level = "{value}" |
| 780 |
""" |
| 781 |
conf_file.write_text(file_content) |
| 782 |
|
| 783 |
result = config._load_config_from_toml_file(conf_file) |
| 784 |
|
| 785 |
assert result is not None |
| 786 |
assert result.report_level == parsed_value |
| 787 |
|
| 788 |
@staticmethod |
| 789 |
@pytest.mark.parametrize( |
| 790 |
("value", "parsed_value"), |
| 791 |
[ |
| 792 |
(1, config.ReportLevel.INFO), |
| 793 |
(3, config.ReportLevel.ERROR), |
| 794 |
(5, config.ReportLevel.NONE), |
| 795 |
], |
| 796 |
) |
| 797 |
def test_report_level_as_int( |
| 798 |
tmp_path: pathlib.Path, value: int, parsed_value: config.ReportLevel |
| 799 |
) -> None: |
| 800 |
"""Test report setting with integer values.""" |
| 801 |
conf_file = tmp_path / "config.toml" |
| 802 |
file_content = f"""[tool.rstcheck] |
| 803 |
report_level = {value} |
| 804 |
""" |
| 805 |
conf_file.write_text(file_content) |
| 806 |
|
| 807 |
result = config._load_config_from_toml_file(conf_file) |
| 808 |
|
| 809 |
assert result is not None |
| 810 |
assert result.report_level == parsed_value |
| 811 |
|
| 812 |
@staticmethod |
| 813 |
def test_ignore_messages_as_str(tmp_path: pathlib.Path) -> None: |
| 814 |
"""Test ignore_messages setting with string value.""" |
| 815 |
conf_file = tmp_path / "config.toml" |
| 816 |
file_content = """[tool.rstcheck] |
| 817 |
ignore_messages = "some-regex" |
| 818 |
""" |
| 819 |
conf_file.write_text(file_content) |
| 820 |
regex = re.compile("some-regex") |
| 821 |
|
| 822 |
result = config._load_config_from_toml_file(conf_file) |
| 823 |
|
| 824 |
assert result is not None |
| 825 |
assert result.ignore_messages == regex |
| 826 |
|
| 827 |
@staticmethod |
| 828 |
def test_ignore_messages_as_list(tmp_path: pathlib.Path) -> None: |
| 829 |
"""Test ignore_messages setting with list of strings value.""" |
| 830 |
conf_file = tmp_path / "config.toml" |
| 831 |
file_content = """[tool.rstcheck] |
| 832 |
ignore_messages = ["some-regex", "another-regex"] |
| 833 |
""" |
| 834 |
conf_file.write_text(file_content) |
| 835 |
regex = re.compile(r"some-regex|another-regex") |
| 836 |
|
| 837 |
result = config._load_config_from_toml_file(conf_file) |
| 838 |
|
| 839 |
assert result is not None |
| 840 |
assert result.ignore_messages == regex |
| 841 |
|
| 842 |
@staticmethod |
| 843 |
def test_warning_is_logged_on_missing_section_by_default( |
| 844 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 845 |
) -> None: |
| 846 |
"""Test if a warning is logged on missing section.""" |
| 847 |
conf_file = tmp_path / "config.toml" |
| 848 |
file_content = "[tool.checkrst]" |
| 849 |
conf_file.write_text(file_content) |
| 850 |
|
| 851 |
result = config._load_config_from_toml_file(conf_file) |
| 852 |
|
| 853 |
assert result is None |
| 854 |
assert f"Config file has no [tool.rstcheck] section: '{conf_file}'." in caplog.text |
| 855 |
|
| 856 |
@staticmethod |
| 857 |
def test_warning_on_missing_section_can_be_info( |
| 858 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 859 |
) -> None: |
| 860 |
"""Test if the warning logged on missing section can be an info.""" |
| 861 |
conf_file = tmp_path / "config.toml" |
| 862 |
file_content = "[tool.checkrst]" |
| 863 |
conf_file.write_text(file_content) |
| 864 |
caplog.set_level(logging.INFO) |
| 865 |
|
| 866 |
result = config._load_config_from_toml_file(conf_file, log_missing_section_as_warning=False) |
| 867 |
|
| 868 |
assert result is None |
| 869 |
assert f"Config file has no [tool.rstcheck] section: '{conf_file}'." in caplog.text |
| 870 |
|
| 871 |
@staticmethod |
| 872 |
def test_dont_warning_on_unknown_settings_by_default( |
| 873 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 874 |
) -> None: |
| 875 |
"""Test that no warning is logged on unknown setting by default.""" |
| 876 |
conf_file = tmp_path / "config.toml" |
| 877 |
file_content = """ |
| 878 |
[tool.rstcheck] |
| 879 |
report_level="INFO" |
| 880 |
unkwown_setting=true |
| 881 |
""" |
| 882 |
conf_file.write_text(file_content) |
| 883 |
|
| 884 |
result = config._load_config_from_toml_file( |
| 885 |
conf_file, |
| 886 |
log_missing_section_as_warning=False, |
| 887 |
) |
| 888 |
|
| 889 |
assert result is not None |
| 890 |
assert not caplog.text |
| 891 |
|
| 892 |
@staticmethod |
| 893 |
def test_warning_on_unknown_settings_when_set( |
| 894 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 895 |
) -> None: |
| 896 |
"""Test that a warning is logged on unknown setting when activated.""" |
| 897 |
conf_file = tmp_path / "config.toml" |
| 898 |
file_content = """ |
| 899 |
[tool.rstcheck] |
| 900 |
report_level="INFO" |
| 901 |
unkwown_setting=true |
| 902 |
""" |
| 903 |
conf_file.write_text(file_content) |
| 904 |
|
| 905 |
result = config._load_config_from_toml_file( |
| 906 |
conf_file, |
| 907 |
log_missing_section_as_warning=False, |
| 908 |
warn_unknown_settings=True, |
| 909 |
) |
| 910 |
|
| 911 |
assert result is not None |
| 912 |
assert ( |
| 913 |
f"Unknown setting(s) ['unkwown_setting'] found in file: '{conf_file}'." in caplog.text |
| 914 |
) |
| 915 |
|
| 916 |
@staticmethod |
| 917 |
def test_no_warning_on_known_settings_when_set( |
| 918 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 919 |
) -> None: |
| 920 |
"""Test that a warning is logged on known settings when activated.""" |
| 921 |
conf_file = tmp_path / "config.toml" |
| 922 |
file_content = """ |
| 923 |
[tool.rstcheck] |
| 924 |
report_level="INFO" |
| 925 |
""" |
| 926 |
conf_file.write_text(file_content) |
| 927 |
|
| 928 |
result = config._load_config_from_toml_file( |
| 929 |
conf_file, |
| 930 |
log_missing_section_as_warning=False, |
| 931 |
warn_unknown_settings=True, |
| 932 |
) |
| 933 |
|
| 934 |
assert result is not None |
| 935 |
assert not caplog.text |
| 936 |
|
| 937 |
|
| 938 |
class TestConfigFileLoader: |
| 939 |
"""Test ``load_config_file``.""" |
| 940 |
|
| 941 |
@staticmethod |
| 942 |
def test_no_config_on_none() -> None: |
| 943 |
"""Test no config is loaded if NONE is set.""" |
| 944 |
conf_file = pathlib.Path("NONE") |
| 945 |
|
| 946 |
result = config.load_config_file(conf_file) |
| 947 |
|
| 948 |
assert result is None |
| 949 |
|
| 950 |
@staticmethod |
| 951 |
@pytest.mark.parametrize("ini_file", [".rstcheck.cfg", "setup.cfg", "config.ini", "config.cfg"]) |
| 952 |
def test_ini_files(tmp_path: pathlib.Path, ini_file: str) -> None: |
| 953 |
"""Test with INI files.""" |
| 954 |
conf_file = tmp_path / ini_file |
| 955 |
file_content = """[rstcheck] |
| 956 |
report_level = 3 |
| 957 |
""" |
| 958 |
conf_file.write_text(file_content) |
| 959 |
|
| 960 |
result = config.load_config_file(conf_file) |
| 961 |
|
| 962 |
assert result is not None |
| 963 |
assert result.report_level == config.ReportLevel.ERROR |
| 964 |
|
| 965 |
@staticmethod |
| 966 |
@pytest.mark.skipif(not _extras.TOMLI_INSTALLED, reason="Depends on toml extra.") |
| 967 |
@pytest.mark.parametrize("toml_file", [".rstcheck.toml", "setup.toml", "config.toml"]) |
| 968 |
def test_toml_files(tmp_path: pathlib.Path, toml_file: str) -> None: |
| 969 |
"""Test with TOML files.""" |
| 970 |
conf_file = tmp_path / toml_file |
| 971 |
file_content = """[tool.rstcheck] |
| 972 |
report_level = 3 |
| 973 |
""" |
| 974 |
conf_file.write_text(file_content) |
| 975 |
|
| 976 |
result = config.load_config_file(conf_file) |
| 977 |
|
| 978 |
assert result is not None |
| 979 |
assert result.report_level == config.ReportLevel.ERROR |
| 980 |
|
| 981 |
|
| 982 |
class TestConfigDirLoader: |
| 983 |
"""Test ``load_config_file_from_dir``.""" |
| 984 |
|
| 985 |
@staticmethod |
| 986 |
def test_no_config_on_none() -> None: |
| 987 |
"""Test no config is loaded if NONE is set.""" |
| 988 |
conf_file = pathlib.Path("NONE") |
| 989 |
|
| 990 |
result = config.load_config_file_from_dir(conf_file) |
| 991 |
|
| 992 |
assert result is None |
| 993 |
|
| 994 |
@staticmethod |
| 995 |
@pytest.mark.parametrize("ini_file", [".rstcheck.cfg", "setup.cfg"]) |
| 996 |
def test_supported_ini_files(tmp_path: pathlib.Path, ini_file: str) -> None: |
| 997 |
"""Test with supported INI files.""" |
| 998 |
conf_file = tmp_path / ini_file |
| 999 |
file_content = """[rstcheck] |
| 1000 |
report_level = 3 |
| 1001 |
""" |
| 1002 |
conf_file.write_text(file_content) |
| 1003 |
|
| 1004 |
result = config.load_config_file_from_dir(tmp_path) |
| 1005 |
|
| 1006 |
assert result is not None |
| 1007 |
assert result.report_level == config.ReportLevel.ERROR |
| 1008 |
|
| 1009 |
@staticmethod |
| 1010 |
@pytest.mark.parametrize("ini_file", ["config.ini", "config.cfg"]) |
| 1011 |
def test_unsupported_ini_files(tmp_path: pathlib.Path, ini_file: str) -> None: |
| 1012 |
"""Test with unsupported INI files.""" |
| 1013 |
conf_file = tmp_path / ini_file |
| 1014 |
file_content = """[rstcheck] |
| 1015 |
report_level = 3 |
| 1016 |
""" |
| 1017 |
conf_file.write_text(file_content) |
| 1018 |
|
| 1019 |
result = config.load_config_file_from_dir(tmp_path) |
| 1020 |
|
| 1021 |
assert result is None |
| 1022 |
|
| 1023 |
@staticmethod |
| 1024 |
@pytest.mark.skipif(not _extras.TOMLI_INSTALLED, reason="Depends on toml extra.") |
| 1025 |
@pytest.mark.parametrize("toml_file", ["pyproject.toml"]) |
| 1026 |
def test_supported_toml_files(tmp_path: pathlib.Path, toml_file: str) -> None: |
| 1027 |
"""Test with supported TOML files.""" |
| 1028 |
conf_file = tmp_path / toml_file |
| 1029 |
file_content = """[tool.rstcheck] |
| 1030 |
report_level = 3 |
| 1031 |
""" |
| 1032 |
conf_file.write_text(file_content) |
| 1033 |
|
| 1034 |
result = config.load_config_file_from_dir(tmp_path) |
| 1035 |
|
| 1036 |
assert result is not None |
| 1037 |
assert result.report_level == config.ReportLevel.ERROR |
| 1038 |
|
| 1039 |
@staticmethod |
| 1040 |
@pytest.mark.skipif(not _extras.TOMLI_INSTALLED, reason="Depends on toml extra.") |
| 1041 |
@pytest.mark.parametrize("toml_file", [".rstcheck.toml", "setup.toml", "config.toml"]) |
| 1042 |
def test_unsupported_toml_files(tmp_path: pathlib.Path, toml_file: str) -> None: |
| 1043 |
"""Test with unsupported TOML files.""" |
| 1044 |
conf_file = tmp_path / toml_file |
| 1045 |
file_content = """[tool.rstcheck] |
| 1046 |
report_level = 3 |
| 1047 |
""" |
| 1048 |
conf_file.write_text(file_content) |
| 1049 |
|
| 1050 |
result = config.load_config_file_from_dir(tmp_path) |
| 1051 |
|
| 1052 |
assert result is None |
| 1053 |
|
| 1054 |
@staticmethod |
| 1055 |
def test_rstcheck_over_setup(tmp_path: pathlib.Path) -> None: |
| 1056 |
"""Test .rstcheck.cfg takes precedence over setup.cfg.""" |
| 1057 |
setup_conf_file = tmp_path / "setup.cfg" |
| 1058 |
setup_file_content = """[rstcheck] |
| 1059 |
report_level = 2 |
| 1060 |
""" |
| 1061 |
setup_conf_file.write_text(setup_file_content) |
| 1062 |
rstcheck_conf_file = tmp_path / ".rstcheck.cfg" |
| 1063 |
rstcheck_file_content = """[rstcheck] |
| 1064 |
report_level = 3 |
| 1065 |
""" |
| 1066 |
rstcheck_conf_file.write_text(rstcheck_file_content) |
| 1067 |
|
| 1068 |
result = config.load_config_file_from_dir(tmp_path) |
| 1069 |
|
| 1070 |
assert result is not None |
| 1071 |
assert result.report_level == config.ReportLevel.ERROR |
| 1072 |
|
| 1073 |
@staticmethod |
| 1074 |
@pytest.mark.skipif(not _extras.TOMLI_INSTALLED, reason="Depends on toml extra.") |
| 1075 |
def test_rstcheck_over_pyproject(tmp_path: pathlib.Path) -> None: |
| 1076 |
"""Test .rstcheck.cfg takes precedence over pyproject.toml.""" |
| 1077 |
pyproject_conf_file = tmp_path / "pyproject.toml" |
| 1078 |
pyproject_file_content = """[tool.rstcheck] |
| 1079 |
report_level = 2 |
| 1080 |
""" |
| 1081 |
pyproject_conf_file.write_text(pyproject_file_content) |
| 1082 |
rstcheck_conf_file = tmp_path / ".rstcheck.cfg" |
| 1083 |
rstcheck_file_content = """[rstcheck] |
| 1084 |
report_level = 3 |
| 1085 |
""" |
| 1086 |
rstcheck_conf_file.write_text(rstcheck_file_content) |
| 1087 |
|
| 1088 |
result = config.load_config_file_from_dir(tmp_path) |
| 1089 |
|
| 1090 |
assert result is not None |
| 1091 |
assert result.report_level == config.ReportLevel.ERROR |
| 1092 |
|
| 1093 |
@staticmethod |
| 1094 |
@pytest.mark.skipif(not _extras.TOMLI_INSTALLED, reason="Depends on toml extra.") |
| 1095 |
def test_pyproject_over_setup(tmp_path: pathlib.Path) -> None: |
| 1096 |
"""Test pyproject.toml takes precedence over setup.cfg.""" |
| 1097 |
setup_conf_file = tmp_path / "setup.cfg" |
| 1098 |
setup_file_content = """[rstcheck] |
| 1099 |
report_level = 2 |
| 1100 |
""" |
| 1101 |
setup_conf_file.write_text(setup_file_content) |
| 1102 |
pyproject_conf_file = tmp_path / "pyproject.toml" |
| 1103 |
pyproject_file_content = """[tool.rstcheck] |
| 1104 |
report_level = 3 |
| 1105 |
""" |
| 1106 |
pyproject_conf_file.write_text(pyproject_file_content) |
| 1107 |
|
| 1108 |
result = config.load_config_file_from_dir(tmp_path) |
| 1109 |
|
| 1110 |
assert result is not None |
| 1111 |
assert result.report_level == config.ReportLevel.ERROR |
| 1112 |
|
| 1113 |
@staticmethod |
| 1114 |
@pytest.mark.skipif(not _extras.TOMLI_INSTALLED, reason="Depends on toml extra.") |
| 1115 |
def test_missing_config_means_next_file_is_checked(tmp_path: pathlib.Path) -> None: |
| 1116 |
"""Test missing config in file results in checking of next file.""" |
| 1117 |
setup_conf_file = tmp_path / "setup.cfg" |
| 1118 |
setup_file_content = """[rstcheck] |
| 1119 |
report_level = 3 |
| 1120 |
""" |
| 1121 |
setup_conf_file.write_text(setup_file_content) |
| 1122 |
rstcheck_conf_file = tmp_path / ".rstcheck.cfg" |
| 1123 |
rstcheck_file_content = """[not-rstcheck] |
| 1124 |
report_level = 2 |
| 1125 |
""" |
| 1126 |
rstcheck_conf_file.write_text(rstcheck_file_content) |
| 1127 |
|
| 1128 |
result = config.load_config_file_from_dir(tmp_path) |
| 1129 |
|
| 1130 |
assert result is not None |
| 1131 |
assert result.report_level == config.ReportLevel.ERROR |
| 1132 |
|
| 1133 |
@staticmethod |
| 1134 |
def test_info_is_logged_on_no_config_found( |
| 1135 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 1136 |
) -> None: |
| 1137 |
"""Test if an info is logged if no config could be found.""" |
| 1138 |
conf_dir = tmp_path |
| 1139 |
caplog.set_level(logging.INFO) |
| 1140 |
|
| 1141 |
result = config.load_config_file_from_dir(conf_dir) |
| 1142 |
|
| 1143 |
assert result is None |
| 1144 |
assert ( |
| 1145 |
f"No config section in supported config files found in directory: '{conf_dir}'." |
| 1146 |
in caplog.text |
| 1147 |
) |
| 1148 |
|
| 1149 |
@staticmethod |
| 1150 |
def test_warning_is_logged_on_no_config_section_in_rstcheck_file( |
| 1151 |
tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 1152 |
) -> None: |
| 1153 |
"""Test if an warning is logged if no config section could be found in .rstcheck.cfg.""" |
| 1154 |
conf_dir = tmp_path |
| 1155 |
conf_file = conf_dir / ".rstcheck.cfg" |
| 1156 |
file_content = "[checkrst]" |
| 1157 |
conf_file.write_text(file_content) |
| 1158 |
|
| 1159 |
result = config.load_config_file_from_dir(conf_dir, log_missing_section_as_warning=False) |
| 1160 |
|
| 1161 |
assert result is None |
| 1162 |
assert f"Config file has no [rstcheck] section: '{conf_file}'." in caplog.text |
| 1163 |
|
| 1164 |
|
| 1165 |
class TestConfigDirTreeLoader: |
| 1166 |
"""Test ``load_config_file_from_dir_tree``.""" |
| 1167 |
|
| 1168 |
@staticmethod |
| 1169 |
def test_no_config_on_none() -> None: |
| 1170 |
"""Test no config is loaded if NONE is set.""" |
| 1171 |
conf_file = pathlib.Path("NONE") |
| 1172 |
|
| 1173 |
result = config.load_config_file_from_dir_tree(conf_file) |
| 1174 |
|
| 1175 |
assert result is None |
| 1176 |
|
| 1177 |
@staticmethod |
| 1178 |
def test_parent_searching(tmp_path: pathlib.Path) -> None: |
| 1179 |
"""Test option to search up the dir tree.""" |
| 1180 |
nested_dir = tmp_path / "nested" |
| 1181 |
nested_dir.mkdir() |
| 1182 |
unsupported_file = nested_dir / "config.cfg" |
| 1183 |
unsupported_file_content = """[rstcheck] |
| 1184 |
report_level = 2 |
| 1185 |
""" |
| 1186 |
unsupported_file.write_text(unsupported_file_content) |
| 1187 |
supported_file = tmp_path / "setup.cfg" |
| 1188 |
supported_file_content = """[rstcheck] |
| 1189 |
report_level = 3 |
| 1190 |
""" |
| 1191 |
supported_file.write_text(supported_file_content) |
| 1192 |
|
| 1193 |
result = config.load_config_file_from_dir_tree(nested_dir) |
| 1194 |
|
| 1195 |
assert result is not None |
| 1196 |
assert result.report_level == config.ReportLevel.ERROR |
| 1197 |
|
| 1198 |
@staticmethod |
| 1199 |
def test_no_file_up_to_root(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path) -> None: |
| 1200 |
"""Test option to search up the dir tree with no file up to root dir.""" |
| 1201 |
root_dir = tmp_path |
| 1202 |
nested_dir = tmp_path / "nested" |
| 1203 |
nested_dir.mkdir() |
| 1204 |
monkeypatch.setattr(pathlib.Path, "parent", root_dir) |
| 1205 |
|
| 1206 |
result = config.load_config_file_from_dir_tree(nested_dir) |
| 1207 |
|
| 1208 |
assert result is None |
| 1209 |
|
| 1210 |
@staticmethod |
| 1211 |
def test_info_is_logged_on_no_config_found( |
| 1212 |
tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 1213 |
) -> None: |
| 1214 |
"""Test if an info is logged if no config could be found.""" |
| 1215 |
conf_dir = tmp_path |
| 1216 |
caplog.set_level(logging.INFO) |
| 1217 |
monkeypatch.setattr(pathlib.Path, "parent", conf_dir) |
| 1218 |
|
| 1219 |
result = config.load_config_file_from_dir_tree(conf_dir) |
| 1220 |
|
| 1221 |
assert result is None |
| 1222 |
assert ( |
| 1223 |
f"No config section in supported config files found in directory tree: '{conf_dir}'." |
| 1224 |
in caplog.text |
| 1225 |
) |
| 1226 |
|
| 1227 |
|
| 1228 |
class TestConfigPathLoader: |
| 1229 |
"""Test ``load_config_file_from_path``.""" |
| 1230 |
|
| 1231 |
@staticmethod |
| 1232 |
def test_raises_on_nonexisting_path() -> None: |
| 1233 |
"""Test raises FileNotFoundError on path that is not a file or directory.""" |
| 1234 |
conf_file = pathlib.Path("does-not-exist-cfg") |
| 1235 |
|
| 1236 |
with pytest.raises(FileNotFoundError, match="Passed config path not found"): |
| 1237 |
config.load_config_file_from_path(conf_file) |
| 1238 |
|
| 1239 |
@staticmethod |
| 1240 |
def test_no_config_on_none() -> None: |
| 1241 |
"""Test no config is loaded if NONE is set.""" |
| 1242 |
conf_file = pathlib.Path("NONE") |
| 1243 |
|
| 1244 |
result = config.load_config_file_from_path(conf_file) |
| 1245 |
|
| 1246 |
assert result is None |
| 1247 |
|
| 1248 |
@staticmethod |
| 1249 |
def test_with_file(tmp_path: pathlib.Path) -> None: |
| 1250 |
"""Test with INI file.""" |
| 1251 |
conf_file = tmp_path / ".rstcheck.cfg" |
| 1252 |
file_content = """[rstcheck] |
| 1253 |
report_level = 3 |
| 1254 |
""" |
| 1255 |
conf_file.write_text(file_content) |
| 1256 |
|
| 1257 |
result = config.load_config_file_from_path(conf_file) |
| 1258 |
|
| 1259 |
assert result is not None |
| 1260 |
assert result.report_level == config.ReportLevel.ERROR |
| 1261 |
|
| 1262 |
@staticmethod |
| 1263 |
def test_with_dir(tmp_path: pathlib.Path) -> None: |
| 1264 |
"""Test with directory.""" |
| 1265 |
conf_file = tmp_path / ".rstcheck.cfg" |
| 1266 |
file_content = """[rstcheck] |
| 1267 |
report_level = 3 |
| 1268 |
""" |
| 1269 |
conf_file.write_text(file_content) |
| 1270 |
|
| 1271 |
result = config.load_config_file_from_path(tmp_path) |
| 1272 |
|
| 1273 |
assert result is not None |
| 1274 |
assert result.report_level == config.ReportLevel.ERROR |
| 1275 |
|
| 1276 |
@staticmethod |
| 1277 |
def test_with_nested_dir(tmp_path: pathlib.Path) -> None: |
| 1278 |
"""Test with nested dir tree.""" |
| 1279 |
nested_dir = tmp_path / "nested" |
| 1280 |
nested_dir.mkdir() |
| 1281 |
conf_file = tmp_path / "setup.cfg" |
| 1282 |
file_content = """[rstcheck] |
| 1283 |
report_level = 3 |
| 1284 |
""" |
| 1285 |
conf_file.write_text(file_content) |
| 1286 |
|
| 1287 |
result = config.load_config_file_from_path(nested_dir, search_dir_tree=True) |
| 1288 |
|
| 1289 |
assert result is not None |
| 1290 |
assert result.report_level == config.ReportLevel.ERROR |
| 1291 |
|
| 1292 |
|
| 1293 |
class TestConfigMerger: |
| 1294 |
"""Test ``merge_configs``.""" |
| 1295 |
|
| 1296 |
@staticmethod |
| 1297 |
def test_default_merge_with_full_config() -> None: |
| 1298 |
"""Test config merging with full config.""" |
| 1299 |
config_base = config.RstcheckConfig(report_level=config.ReportLevel.SEVERE) |
| 1300 |
config_add = config.RstcheckConfig(report_level=config.ReportLevel.ERROR) |
| 1301 |
|
| 1302 |
result = config.merge_configs(config_base, config_add) |
| 1303 |
|
| 1304 |
assert result.report_level == config.ReportLevel.ERROR |
| 1305 |
|
| 1306 |
@staticmethod |
| 1307 |
def test_default_merge_with_file_config() -> None: |
| 1308 |
"""Test config merging with file config.""" |
| 1309 |
config_base = config.RstcheckConfig(report_level=config.ReportLevel.SEVERE) |
| 1310 |
config_add = config.RstcheckConfigFile(report_level=config.ReportLevel.ERROR) |
| 1311 |
|
| 1312 |
result = config.merge_configs(config_base, config_add) |
| 1313 |
|
| 1314 |
assert result.report_level == config.ReportLevel.ERROR |
| 1315 |
|
| 1316 |
@staticmethod |
| 1317 |
def test_default_merge_with_full_config_and_empty_base_config() -> None: |
| 1318 |
"""Test config merging with full config and empty base config.""" |
| 1319 |
config_base = config.RstcheckConfig() |
| 1320 |
config_add = config.RstcheckConfig(report_level=config.ReportLevel.ERROR) |
| 1321 |
|
| 1322 |
result = config.merge_configs(config_base, config_add) |
| 1323 |
|
| 1324 |
assert result.report_level == config.ReportLevel.ERROR |
| 1325 |
|
| 1326 |
@staticmethod |
| 1327 |
def test_default_merge_with_file_config_and_empty_base_config() -> None: |
| 1328 |
"""Test config merging with file config and empty base config.""" |
| 1329 |
config_base = config.RstcheckConfig() |
| 1330 |
config_add = config.RstcheckConfigFile(report_level=config.ReportLevel.ERROR) |
| 1331 |
|
| 1332 |
result = config.merge_configs(config_base, config_add) |
| 1333 |
|
| 1334 |
assert result.report_level == config.ReportLevel.ERROR |
| 1335 |
|
| 1336 |
@staticmethod |
| 1337 |
def test_default_merge_with_full_config_and_changed_dominance() -> None: |
| 1338 |
"""Test config merging with full config and changed dominance.""" |
| 1339 |
config_base = config.RstcheckConfig(report_level=config.ReportLevel.SEVERE) |
| 1340 |
config_add = config.RstcheckConfig(report_level=config.ReportLevel.ERROR) |
| 1341 |
|
| 1342 |
result = config.merge_configs(config_base, config_add, config_add_is_dominant=False) |
| 1343 |
|
| 1344 |
assert result.report_level == config.ReportLevel.SEVERE |
| 1345 |
|
| 1346 |
@staticmethod |
| 1347 |
def test_default_merge_with_file_config_and_changed_dominance() -> None: |
| 1348 |
"""Test config merging with file config and changed dominance.""" |
| 1349 |
config_base = config.RstcheckConfig(report_level=config.ReportLevel.SEVERE) |
| 1350 |
config_add = config.RstcheckConfigFile(report_level=config.ReportLevel.ERROR) |
| 1351 |
|
| 1352 |
result = config.merge_configs(config_base, config_add, config_add_is_dominant=False) |
| 1353 |
|
| 1354 |
assert result.report_level == config.ReportLevel.SEVERE |
| 1355 |
|
| 1356 |
@staticmethod |
| 1357 |
def test_default_merge_with_full_config_and_changed_dominance_and_empty_base_config() -> None: |
| 1358 |
"""Test config merging with full config, changed dominance and empty base config. |
| 1359 |
|
| 1360 |
Test ``None`` gets overwritten by set value. |
| 1361 |
""" |
| 1362 |
config_base = config.RstcheckConfig() |
| 1363 |
config_add = config.RstcheckConfig(report_level=config.ReportLevel.ERROR) |
| 1364 |
|
| 1365 |
result = config.merge_configs(config_base, config_add, config_add_is_dominant=False) |
| 1366 |
|
| 1367 |
assert result.report_level == config.ReportLevel.ERROR |
| 1368 |
|
| 1369 |
@staticmethod |
| 1370 |
def test_default_merge_with_file_config_and_changed_dominance_and_empty_base_config() -> None: |
| 1371 |
"""Test config merging with file config, changed dominance and empty base config. |
| 1372 |
|
| 1373 |
Test ``None`` gets overwritten by set value. |
| 1374 |
""" |
| 1375 |
config_base = config.RstcheckConfig() |
| 1376 |
config_add = config.RstcheckConfigFile(report_level=config.ReportLevel.ERROR) |
| 1377 |
|
| 1378 |
result = config.merge_configs(config_base, config_add, config_add_is_dominant=False) |
| 1379 |
|
| 1380 |
assert result.report_level == config.ReportLevel.ERROR |