| 1 | """Integration test for the main runner.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import io |
| 6 | import pathlib |
| 7 | import re |
| 8 | import sys |
| 9 | import typing as t |
| 10 | |
| 11 | import pytest |
| 12 | |
| 13 | from rstcheck_core import _extras, checker, config |
| 14 | from tests.conftest import EXAMPLES_DIR |
| 15 | |
| 16 | |
| 17 | class TestInput: |
| 18 | """Test file input with good and bad files and piping.""" |
| 19 | |
| 20 | @staticmethod |
| 21 | @pytest.mark.parametrize("test_file", list(EXAMPLES_DIR.glob("good/*.rst"))) |
| 22 | def test_all_good_examples(test_file: pathlib.Path) -> None: |
| 23 | """Test all files in ``testing/examples/good`` are errorless.""" |
| 24 | if sys.platform == "win32" and test_file.name == "bom.rst": |
| 25 | pytest.xfail(reason="BOM test fails for windows") |
| 26 | init_config = config.RstcheckConfig() |
| 27 | |
| 28 | result = checker.check_file(test_file, init_config) |
| 29 | |
| 30 | assert not result |
| 31 | |
| 32 | @staticmethod |
| 33 | @pytest.mark.parametrize("test_file", list(EXAMPLES_DIR.glob("bad/*.rst"))) |
| 34 | def test_all_bad_examples(test_file: pathlib.Path) -> None: |
| 35 | """Test all files in ``testing/examples/bad`` have errors.""" |
| 36 | if sys.platform == "win32" and test_file.name == "bash.rst": |
| 37 | pytest.xfail(reason="Unknown Windows specific wrong result for bash.rst") |
| 38 | init_config = config.RstcheckConfig() |
| 39 | |
| 40 | result = checker.check_file(test_file, init_config) |
| 41 | |
| 42 | assert len(result) > 0 |
| 43 | |
| 44 | @staticmethod |
| 45 | def test_good_example_with_piping(monkeypatch: pytest.MonkeyPatch) -> None: |
| 46 | """Test good example file piped into rstcheck.""" |
| 47 | test_file_pipe = EXAMPLES_DIR / "good" / "rst.rst" |
| 48 | test_file_pipe_content = test_file_pipe.read_text("utf-8") |
| 49 | monkeypatch.setattr(sys, "stdin", io.StringIO(test_file_pipe_content)) |
| 50 | init_config = config.RstcheckConfig() |
| 51 | |
| 52 | result = checker.check_file(pathlib.Path("-"), init_config) |
| 53 | |
| 54 | assert not result |
| 55 | |
| 56 | @staticmethod |
| 57 | def test_bad_example_with_piping(monkeypatch: pytest.MonkeyPatch) -> None: |
| 58 | """Test bad example file piped into rstcheck.""" |
| 59 | test_file_pipe = EXAMPLES_DIR / "bad" / "rst.rst" |
| 60 | test_file_pipe_content = test_file_pipe.read_text("utf-8") |
| 61 | monkeypatch.setattr(sys, "stdin", io.StringIO(test_file_pipe_content)) |
| 62 | init_config = config.RstcheckConfig() |
| 63 | |
| 64 | result = checker.check_file(pathlib.Path("-"), init_config) |
| 65 | |
| 66 | assert len(result) == 1 |
| 67 | |
| 68 | |
| 69 | class TestIgnoreOptions: |
| 70 | """Test ignore_* options and report_level.""" |
| 71 | |
| 72 | @staticmethod |
| 73 | def test_without_report_exits_zero() -> None: |
| 74 | """Test bad example without report is ok.""" |
| 75 | test_file = EXAMPLES_DIR / "bad" / "rst.rst" |
| 76 | init_config = config.RstcheckConfig(report_level="none") |
| 77 | |
| 78 | result = checker.check_file(test_file, init_config) |
| 79 | |
| 80 | assert not result |
| 81 | |
| 82 | @staticmethod |
| 83 | def test_ignore_language_silences_error() -> None: |
| 84 | """Test bad example with ignored language is ok.""" |
| 85 | test_file = EXAMPLES_DIR / "bad" / "cpp.rst" |
| 86 | init_config = config.RstcheckConfig(ignore_languages="cpp") |
| 87 | |
| 88 | result = checker.check_file(test_file, init_config) |
| 89 | |
| 90 | assert not result |
| 91 | |
| 92 | @staticmethod |
| 93 | def test_matching_ignore_msg_exits_zero() -> None: |
| 94 | """Test matching ignore message.""" |
| 95 | test_file = EXAMPLES_DIR / "bad" / "rst.rst" |
| 96 | init_config = config.RstcheckConfig( |
| 97 | ignore_messages=r"(Title .verline & underline mismatch\.$)" |
| 98 | ) |
| 99 | |
| 100 | result = checker.check_file(test_file, init_config) |
| 101 | |
| 102 | assert not result |
| 103 | |
| 104 | @staticmethod |
| 105 | def test_non_matching_ignore_msg_errors() -> None: |
| 106 | """Test non matching ignore message.""" |
| 107 | test_file = EXAMPLES_DIR / "bad" / "rst.rst" |
| 108 | init_config = config.RstcheckConfig(ignore_messages=r"(No match\.$)") |
| 109 | |
| 110 | result = checker.check_file(test_file, init_config) |
| 111 | |
| 112 | assert len(result) == 1 |
| 113 | |
| 114 | @staticmethod |
| 115 | def test_table_substitution_error_fixed_by_ignore() -> None: |
| 116 | """Test that ignored substitutions in tables are correctly handled.""" |
| 117 | test_file = EXAMPLES_DIR / "bad" / "table_substitutions.rst" |
| 118 | init_config = config.RstcheckConfig(ignore_substitutions="FOO_ID,BAR_ID") |
| 119 | |
| 120 | result = checker.check_file(test_file, init_config) |
| 121 | |
| 122 | assert not result |
| 123 | |
| 124 | |
| 125 | class TestWithoutConfigFile: |
| 126 | """Test without config file in dir tree.""" |
| 127 | |
| 128 | @staticmethod |
| 129 | def test_error_without_config_file() -> None: |
| 130 | """Test bad example without set config file and implicit config file shows errors.""" |
| 131 | test_file = EXAMPLES_DIR / "without_configuration" / "bad.rst" |
| 132 | init_config = config.RstcheckConfig() |
| 133 | |
| 134 | result = checker.check_file(test_file, init_config) |
| 135 | |
| 136 | assert len(result) == 6 |
| 137 | |
| 138 | @staticmethod |
| 139 | def test_no_error_with_set_ini_config_file() -> None: |
| 140 | """Test bad example with set INI config file does not error.""" |
| 141 | test_file = EXAMPLES_DIR / "without_configuration" / "bad.rst" |
| 142 | config_file = EXAMPLES_DIR / "with_configuration" / "rstcheck.ini" |
| 143 | file_config = t.cast(config.RstcheckConfigFile, config.load_config_file(config_file)) |
| 144 | init_config = config.RstcheckConfig(config_path=config_file, **file_config.model_dump()) |
| 145 | |
| 146 | result = checker.check_file(test_file, init_config) |
| 147 | |
| 148 | assert not result |
| 149 | |
| 150 | @staticmethod |
| 151 | def test_no_error_with_set_config_dir() -> None: |
| 152 | """Test bad example with set config dir does not error.""" |
| 153 | test_file = EXAMPLES_DIR / "without_configuration" / "bad.rst" |
| 154 | config_dir = EXAMPLES_DIR / "with_configuration" |
| 155 | file_config = t.cast( |
| 156 | config.RstcheckConfigFile, config.load_config_file_from_dir(config_dir) |
| 157 | ) |
| 158 | init_config = config.RstcheckConfig(config_path=config_dir, **file_config.model_dump()) |
| 159 | |
| 160 | result = checker.check_file(test_file, init_config) |
| 161 | |
| 162 | assert not result |
| 163 | |
| 164 | @staticmethod |
| 165 | @pytest.mark.skipif(not _extras.TOMLI_INSTALLED, reason="Depends on toml extra.") |
| 166 | def test_no_error_with_set_toml_config_file() -> None: |
| 167 | """Test bad example with set TOML config file does not error.""" |
| 168 | test_file = EXAMPLES_DIR / "without_configuration" / "bad.rst" |
| 169 | config_file = EXAMPLES_DIR / "with_configuration" / "pyproject.toml" |
| 170 | file_config = t.cast(config.RstcheckConfigFile, config.load_config_file(config_file)) |
| 171 | init_config = config.RstcheckConfig(config_path=config_file, **file_config.model_dump()) |
| 172 | |
| 173 | result = checker.check_file(test_file, init_config) |
| 174 | |
| 175 | assert not result |
| 176 | |
| 177 | |
| 178 | class TestWithConfigFile: |
| 179 | """Test with config file in dir tree.""" |
| 180 | |
| 181 | @staticmethod |
| 182 | def test_file_1_is_bad_without_config() -> None: |
| 183 | """Test bad file ``bad.rst`` without config file is not ok.""" |
| 184 | test_file = EXAMPLES_DIR / "with_configuration" / "bad.rst" |
| 185 | init_config = config.RstcheckConfig(config_path=pathlib.Path("NONE")) |
| 186 | |
| 187 | result = checker.check_file(test_file, init_config) |
| 188 | |
| 189 | assert len(result) == 6 |
| 190 | |
| 191 | @staticmethod |
| 192 | def test_file_2_is_bad_without_config() -> None: |
| 193 | """Test bad file ``bad_rst.rst`` without config file not ok.""" |
| 194 | test_file = EXAMPLES_DIR / "with_configuration" / "bad_rst.rst" |
| 195 | init_config = config.RstcheckConfig(config_path=pathlib.Path("NONE")) |
| 196 | |
| 197 | result = checker.check_file(test_file, init_config) |
| 198 | |
| 199 | assert len(result) == 2 |
| 200 | |
| 201 | @staticmethod |
| 202 | def test_bad_file_1_with_implicit_config_no_errors() -> None: |
| 203 | """Test bad file ``bad.rst`` with implicit config file is ok.""" |
| 204 | test_file = EXAMPLES_DIR / "with_configuration" / "bad.rst" |
| 205 | init_config = config.RstcheckConfig() |
| 206 | |
| 207 | result = checker.check_file(test_file, init_config) |
| 208 | |
| 209 | assert not result |
| 210 | |
| 211 | @staticmethod |
| 212 | def test_bad_file_2_with_implicit_config_some_errors() -> None: |
| 213 | """Test bad file ``bad_rst.rst`` with implicit config file partially ok.""" |
| 214 | test_file = EXAMPLES_DIR / "with_configuration" / "bad_rst.rst" |
| 215 | init_config = config.RstcheckConfig() |
| 216 | |
| 217 | result = checker.check_file(test_file, init_config) |
| 218 | |
| 219 | assert len(result) == 1 |
| 220 | |
| 221 | @staticmethod |
| 222 | def test_bad_file_1_with_explicit_config_no_errors() -> None: |
| 223 | """Test bad file ``bad.rst`` with explicit config file is ok.""" |
| 224 | test_file = EXAMPLES_DIR / "with_configuration" / "bad.rst" |
| 225 | config_file = EXAMPLES_DIR / "with_configuration" / "rstcheck.ini" |
| 226 | file_config = t.cast(config.RstcheckConfigFile, config.load_config_file(config_file)) |
| 227 | init_config = config.RstcheckConfig(config_path=config_file, **file_config.model_dump()) |
| 228 | |
| 229 | result = checker.check_file(test_file, init_config) |
| 230 | |
| 231 | assert not result |
| 232 | |
| 233 | @staticmethod |
| 234 | def test_bad_file_2_with_explicit_config_some_errors() -> None: |
| 235 | """Test bad file ``bad_rst.rst`` with explicit config file partially ok.""" |
| 236 | test_file = EXAMPLES_DIR / "with_configuration" / "bad_rst.rst" |
| 237 | config_file = EXAMPLES_DIR / "with_configuration" / "rstcheck.ini" |
| 238 | file_config = t.cast(config.RstcheckConfigFile, config.load_config_file(config_file)) |
| 239 | init_config = config.RstcheckConfig(config_path=config_file, **file_config.model_dump()) |
| 240 | |
| 241 | result = checker.check_file(test_file, init_config) |
| 242 | |
| 243 | assert len(result) == 1 |
| 244 | |
| 245 | |
| 246 | class TestCustomDirectivesAndRoles: |
| 247 | """Test custom directives and roles.""" |
| 248 | |
| 249 | @staticmethod |
| 250 | def test_custom_directive_and_role() -> None: |
| 251 | """Test file with custom directive and role.""" |
| 252 | test_file = EXAMPLES_DIR / "custom" / "custom_directive_and_role.rst" |
| 253 | init_config = config.RstcheckConfig() |
| 254 | |
| 255 | result = checker.check_file(test_file, init_config) |
| 256 | |
| 257 | assert len(result) == 4 |
| 258 | |
| 259 | @staticmethod |
| 260 | def test_custom_directive_and_role_with_ignore() -> None: |
| 261 | """Test file with custom directive and role and CLI ignores.""" |
| 262 | test_file = EXAMPLES_DIR / "custom" / "custom_directive_and_role.rst" |
| 263 | init_config = config.RstcheckConfig( |
| 264 | ignore_directives="custom-directive", ignore_roles="custom-role" |
| 265 | ) |
| 266 | |
| 267 | result = checker.check_file(test_file, init_config) |
| 268 | |
| 269 | assert not result |
| 270 | |
| 271 | @staticmethod |
| 272 | def test_custom_directive_and_role_with_config_file() -> None: |
| 273 | """Test file with custom directive and role and config file.""" |
| 274 | test_file = EXAMPLES_DIR / "custom" / "custom_directive_and_role.rst" |
| 275 | config_file = EXAMPLES_DIR / "custom" / "rstcheck.custom.ini" |
| 276 | file_config = t.cast(config.RstcheckConfigFile, config.load_config_file(config_file)) |
| 277 | init_config = config.RstcheckConfig(config_path=config_file, **file_config.model_dump()) |
| 278 | |
| 279 | result = checker.check_file(test_file, init_config) |
| 280 | |
| 281 | assert not result |
| 282 | |
| 283 | |
| 284 | class TestSphinx: |
| 285 | """Test integration with sphinx.""" |
| 286 | |
| 287 | @staticmethod |
| 288 | @pytest.mark.skipif(_extras.SPHINX_INSTALLED, reason="Test without sphinx extra.") |
| 289 | def test_sphinx_role_erros_without_sphinx() -> None: |
| 290 | """Test sphinx example errors without sphinx.""" |
| 291 | test_file = EXAMPLES_DIR / "sphinx" / "good.rst" |
| 292 | init_config = config.RstcheckConfig() |
| 293 | |
| 294 | result = checker.check_file(test_file, init_config) |
| 295 | |
| 296 | assert len(result) == 2 |
| 297 | |
| 298 | @staticmethod |
| 299 | @pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 300 | def test_sphinx_role_exits_zero_with_sphinx() -> None: |
| 301 | """Test sphinx example does not error with sphinx.""" |
| 302 | test_file = EXAMPLES_DIR / "sphinx" / "good.rst" |
| 303 | init_config = config.RstcheckConfig() |
| 304 | |
| 305 | result = checker.check_file(test_file, init_config) |
| 306 | |
| 307 | assert not result |
| 308 | |
| 309 | |
| 310 | class TestInlineIgnoreComments: |
| 311 | """Test inline config comments to ignore things.""" |
| 312 | |
| 313 | @staticmethod |
| 314 | def test_bad_example_has_issues() -> None: |
| 315 | """Test all issues are found on bad example.""" |
| 316 | test_file = EXAMPLES_DIR / "inline_config" / "without_inline_ignore.rst" |
| 317 | init_config = config.RstcheckConfig() |
| 318 | |
| 319 | result = checker.check_file(test_file, init_config) |
| 320 | |
| 321 | assert len(result) == 6 |
| 322 | err_msgs = " ".join([e["message"] for e in result]) |
| 323 | assert "custom-directive" in err_msgs |
| 324 | assert "custom-role" in err_msgs |
| 325 | assert "python" in err_msgs |
| 326 | assert "unmatched-substitution" in err_msgs |
| 327 | |
| 328 | @staticmethod |
| 329 | def test_bad_example_has_no_issues_with_inline_ignores() -> None: |
| 330 | """Test no issues are found on bad example with ignore comments.""" |
| 331 | test_file = EXAMPLES_DIR / "inline_config" / "with_inline_ignore.rst" |
| 332 | init_config = config.RstcheckConfig() |
| 333 | |
| 334 | result = checker.check_file(test_file, init_config) |
| 335 | |
| 336 | assert not result |
| 337 | |
| 338 | |
| 339 | class TestInlineFlowControlComments: |
| 340 | """Test inline flow control comments to e.g. skip things.""" |
| 341 | |
| 342 | @staticmethod |
| 343 | @pytest.mark.skipif(sys.version_info[0:2] > (3, 9), reason="Requires python3.9 or lower") |
| 344 | def test_bad_example_has_only_one_issue_pre310() -> None: |
| 345 | """Test only one issue is detected for two same code-blocks. |
| 346 | |
| 347 | One code-block has skip comment. |
| 348 | """ |
| 349 | test_file = EXAMPLES_DIR / "inline_config" / "with_inline_skip_code_block.rst" |
| 350 | init_config = config.RstcheckConfig() |
| 351 | |
| 352 | result = checker.check_file(test_file, init_config) |
| 353 | |
| 354 | assert len(result) == 1 |
| 355 | err_msgs = " ".join([e["message"] for e in result]) |
| 356 | assert len(re.findall(r"unexpected EOF while parsing", err_msgs)) == 1 |
| 357 | |
| 358 | @staticmethod |
| 359 | @pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires python3.10 or higher") |
| 360 | def test_bad_example_has_only_one_issue() -> None: |
| 361 | """Test only one issue is detected for two same code-blocks. |
| 362 | |
| 363 | One code-block has skip comment. |
| 364 | """ |
| 365 | test_file = EXAMPLES_DIR / "inline_config" / "with_inline_skip_code_block.rst" |
| 366 | init_config = config.RstcheckConfig() |
| 367 | |
| 368 | result = checker.check_file(test_file, init_config) |
| 369 | |
| 370 | assert len(result) == 1 |
| 371 | err_msgs = " ".join([e["message"] for e in result]) |
| 372 | assert len(re.findall(r"'\(' was never closed", err_msgs)) == 1 |
| 373 | |
| 374 | @staticmethod |
| 375 | @pytest.mark.skipif(sys.version_info[0:2] > (3, 9), reason="Requires python3.9 or lower") |
| 376 | def test_nested_bad_example_has_only_one_issue_pre310() -> None: |
| 377 | """Test only one issue is detected for two same nested code-blocks. |
| 378 | |
| 379 | One code-block has skip comment. |
| 380 | """ |
| 381 | test_file = EXAMPLES_DIR / "inline_config" / "with_nested_inline_skip_code_block.rst" |
| 382 | init_config = config.RstcheckConfig() |
| 383 | |
| 384 | result = checker.check_file(test_file, init_config) |
| 385 | |
| 386 | assert len(result) == 1 |
| 387 | err_msgs = " ".join([e["message"] for e in result]) |
| 388 | assert len(re.findall(r"unexpected EOF while parsing", err_msgs)) == 1 |
| 389 | |
| 390 | @staticmethod |
| 391 | @pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires python3.10 or higher") |
| 392 | def test_nested_bad_example_has_only_one_issue() -> None: |
| 393 | """Test only one issue is detected for two same nested code-blocks. |
| 394 | |
| 395 | One code-block has skip comment. |
| 396 | """ |
| 397 | test_file = EXAMPLES_DIR / "inline_config" / "with_nested_inline_skip_code_block.rst" |
| 398 | init_config = config.RstcheckConfig() |
| 399 | |
| 400 | result = checker.check_file(test_file, init_config) |
| 401 | |
| 402 | assert len(result) == 1 |
| 403 | err_msgs = " ".join([e["message"] for e in result]) |
| 404 | assert len(re.findall(r"'\(' was never closed", err_msgs)) == 1 |