1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
"""Tests for the shell formatters."""
from __future__ import annotations
from textwrap import dedent
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import pytest
from markdown import Markdown
def test_output_markdown(md: Markdown) -> None:
"""Assert Markdown is converted to HTML.
Parameters:
md: A Markdown instance (fixture).
"""
html = md.convert(
dedent(
"""
```sh exec="yes"
echo "**Bold!**"
```
""",
),
)
assert html == "<p><strong>Bold!</strong></p>"
def test_output_html(md: Markdown) -> None:
"""Assert HTML is injected as is.
Parameters:
md: A Markdown instance (fixture).
"""
html = md.convert(
dedent(
"""
```sh exec="yes" html="yes"
echo "**Bold!**"
```
""",
),
)
assert html == "<p>**Bold!**\n</p>"
def test_error_raised(md: Markdown, caplog: pytest.LogCaptureFixture) -> None:
"""Assert errors properly log a warning and return a formatted traceback.
Parameters:
md: A Markdown instance (fixture).
caplog: Pytest fixture to capture logs.
"""
html = md.convert(
dedent(
"""
```sh exec="yes"
echo("wrong syntax")
```
""",
),
)
assert "error" in html
assert "Execution of sh code block exited with unexpected code 2" in caplog.text
def test_return_code(md: Markdown, caplog: pytest.LogCaptureFixture) -> None:
"""Assert return code is used correctly.
Parameters:
md: A Markdown instance (fixture).
caplog: Pytest fixture to capture logs.
"""
html = md.convert(
dedent(
"""
```sh exec="yes" returncode="1"
echo Not in the mood
exit 1
```
""",
),
)
assert "Not in the mood" in html
assert "exited with" not in caplog.text
|