Skip to content

Commit 1900a18

Browse files
committed
push
1 parent ab0bdd1 commit 1900a18

3 files changed

Lines changed: 119 additions & 5 deletions

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ expected output
210210
You can also register a runner as the default for one or more fence languages:
211211

212212
```python
213-
@pytest_markdown_docs._runners.register_runner(languages=("text",))
213+
@pytest_markdown_docs._runners.register_runner(default_for=("text",))
214214
class TextRunner(pytest_markdown_docs._runners.DefaultRunner):
215215
def runtest(self, test, args):
216216
assert "expected output" in test.source
@@ -224,6 +224,17 @@ expected output
224224
```
225225
````
226226

227+
Runner selection uses this order:
228+
229+
1. A fence with `runner:<name>` uses that named runner.
230+
2. A fence whose language is listed in `default_for` uses that runner.
231+
3. Built-in Python fences (`py`, `python`, `python3`) use the global default
232+
runner.
233+
234+
This means `default_for=("python",)` affects only ```` ```python ```` fences,
235+
while `default=True` changes the global default used by Python fences that do
236+
not have a more specific runner.
237+
227238
### Compatibility with Material for MkDocs
228239

229240
Material for Mkdocs is not compatible with the default syntax.

src/pytest_markdown_docs/_runners.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def repr_failure(
3333
def register_runner(
3434
*,
3535
default: bool = False,
36-
languages: typing.Collection[str] = (),
36+
default_for: typing.Collection[str] = (),
3737
):
3838
"""Decorator for adding custom runners
3939
@@ -49,7 +49,7 @@ def decorator(r: RUNNER_TYPE) -> RUNNER_TYPE:
4949
_registered_runners[r.__name__] = runner
5050
if default:
5151
_default_runner = runner
52-
for language in languages:
52+
for language in default_for:
5353
_registered_language_runner_names[language] = r.__name__
5454
return r
5555

tests/plugin_test.py

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,8 @@ def test_language_runner_collects_non_python_fence(testdir):
526526
import pytest_markdown_docs
527527
import pytest_markdown_docs._runners
528528
529-
@pytest_markdown_docs._runners.register_runner(languages=("text",))
530-
class LanguageTextRunner(pytest_markdown_docs._runners.DefaultRunner):
529+
@pytest_markdown_docs._runners.register_runner(default_for=("text",))
530+
class DefaultForTextRunner(pytest_markdown_docs._runners.DefaultRunner):
531531
def runtest(self, test, args):
532532
pytest_markdown_docs.language_text_fence = test.source.strip()
533533
"""
@@ -549,6 +549,109 @@ def runtest(self, test, args):
549549
delattr(pytest_markdown_docs, "language_text_fence")
550550

551551

552+
def test_default_for_python_language_does_not_replace_global_default(testdir):
553+
import pytest_markdown_docs._runners
554+
555+
pytest_markdown_docs._runners._registered_language_runner_names.pop("python", None)
556+
if hasattr(pytest_markdown_docs, "default_for_python_runs"):
557+
delattr(pytest_markdown_docs, "default_for_python_runs")
558+
559+
testdir.makeconftest(
560+
"""
561+
import pytest_markdown_docs
562+
import pytest_markdown_docs._runners
563+
564+
@pytest_markdown_docs._runners.register_runner(default_for=("python",))
565+
class DefaultForPythonRunner(pytest_markdown_docs._runners.DefaultRunner):
566+
def runtest(self, test, args):
567+
runs = getattr(pytest_markdown_docs, "default_for_python_runs", [])
568+
runs.append(test.source.strip())
569+
pytest_markdown_docs.default_for_python_runs = runs
570+
"""
571+
)
572+
testdir.makefile(
573+
".md",
574+
"""
575+
```python
576+
handled by language default
577+
```
578+
579+
```py
580+
handled by global default
581+
```
582+
""",
583+
)
584+
result = testdir.runpytest("--markdown-docs")
585+
result.assert_outcomes(passed=1, failed=1)
586+
assert getattr(pytest_markdown_docs, "default_for_python_runs", None) == [
587+
"handled by language default"
588+
]
589+
pytest_markdown_docs._runners._registered_language_runner_names.pop("python", None)
590+
delattr(pytest_markdown_docs, "default_for_python_runs")
591+
592+
593+
def test_explicit_runner_takes_precedence_over_default_for(testdir):
594+
if hasattr(pytest_markdown_docs, "selected_text_runner"):
595+
delattr(pytest_markdown_docs, "selected_text_runner")
596+
597+
testdir.makeconftest(
598+
"""
599+
import pytest_markdown_docs
600+
import pytest_markdown_docs._runners
601+
602+
@pytest_markdown_docs._runners.register_runner(default_for=("text",))
603+
class DefaultForTextRunner(pytest_markdown_docs._runners.DefaultRunner):
604+
def runtest(self, test, args):
605+
pytest_markdown_docs.selected_text_runner = "default_for"
606+
607+
@pytest_markdown_docs._runners.register_runner()
608+
class ExplicitTextRunner(pytest_markdown_docs._runners.DefaultRunner):
609+
def runtest(self, test, args):
610+
pytest_markdown_docs.selected_text_runner = "explicit"
611+
"""
612+
)
613+
testdir.makefile(
614+
".md",
615+
"""
616+
```text runner:ExplicitTextRunner
617+
hello from explicit text
618+
```
619+
""",
620+
)
621+
result = testdir.runpytest("--markdown-docs")
622+
result.assert_outcomes(passed=1)
623+
assert getattr(pytest_markdown_docs, "selected_text_runner", None) == "explicit"
624+
delattr(pytest_markdown_docs, "selected_text_runner")
625+
626+
627+
def test_notest_takes_precedence_over_default_for(testdir):
628+
if hasattr(pytest_markdown_docs, "default_for_notest_fence"):
629+
delattr(pytest_markdown_docs, "default_for_notest_fence")
630+
631+
testdir.makeconftest(
632+
"""
633+
import pytest_markdown_docs
634+
import pytest_markdown_docs._runners
635+
636+
@pytest_markdown_docs._runners.register_runner(default_for=("text",))
637+
class DefaultForTextRunner(pytest_markdown_docs._runners.DefaultRunner):
638+
def runtest(self, test, args):
639+
pytest_markdown_docs.default_for_notest_fence = test.source.strip()
640+
"""
641+
)
642+
testdir.makefile(
643+
".md",
644+
"""
645+
```text notest
646+
this should not run
647+
```
648+
""",
649+
)
650+
result = testdir.runpytest("--markdown-docs")
651+
result.assert_outcomes(passed=0)
652+
assert not hasattr(pytest_markdown_docs, "default_for_notest_fence")
653+
654+
552655
def test_multiple_runner_options_error_for_non_python_fence(testdir):
553656
testdir.makefile(
554657
".md",

0 commit comments

Comments
 (0)