91 lines
3.0 KiB
Python
91 lines
3.0 KiB
Python
# file: tests/test_markdown_tables.py
|
|
# version: 1
|
|
|
|
"""Exercise Markdown table detection, including explicit Markdown examples."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
_SCRIPT = pathlib.Path(__file__).resolve().parents[1] / "scripts/audit_markdown_tables.py"
|
|
|
|
|
|
class MarkdownTableAuditTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
temporary = tempfile.TemporaryDirectory(prefix="pgr-tables-")
|
|
self.addCleanup(temporary.cleanup)
|
|
self.root = pathlib.Path(temporary.name)
|
|
|
|
def source(self, contents: str) -> pathlib.Path:
|
|
path = self.root / "sample.md"
|
|
path.write_text(contents, encoding="utf-8")
|
|
return path
|
|
|
|
def run_audit(self) -> subprocess.CompletedProcess[str]:
|
|
return subprocess.run(
|
|
[sys.executable, "-B", str(_SCRIPT), "sample.md"],
|
|
cwd=self.root, capture_output=True, text=True, check=False,
|
|
)
|
|
|
|
def test_regular_and_fenced_markdown_tables_are_counted(self) -> None:
|
|
self.source(
|
|
"| A | BB |\n"
|
|
"|---|----|\n"
|
|
"| x | y |\n"
|
|
"\n"
|
|
"```markdown\n"
|
|
"| C | DD |\n"
|
|
"|---|----|\n"
|
|
"| z | q |\n"
|
|
"```\n"
|
|
)
|
|
result = self.run_audit()
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertIn("clean (2 table(s), 1 file(s))", result.stdout)
|
|
|
|
def test_misaligned_table_inside_markdown_fence_is_rejected(self) -> None:
|
|
self.source(
|
|
"```markdown\n"
|
|
"| Fichier ou famille | Responsabilité | Règle de modification |\n"
|
|
"|--------------------|----------------|------------------------|\n"
|
|
"| `<path-or-pattern>` | `<rôle durable>` | `<quand/comment modifier>` |\n"
|
|
"```\n"
|
|
)
|
|
result = self.run_audit()
|
|
self.assertEqual(result.returncode, 1)
|
|
self.assertIn("sample.md:2: column 1 is not vertically aligned", result.stderr)
|
|
self.assertIn("column 2 is not vertically aligned", result.stderr)
|
|
self.assertIn("column 3 is not vertically aligned", result.stderr)
|
|
|
|
def test_md_fence_is_audited(self) -> None:
|
|
self.source(
|
|
"```md\n"
|
|
"| A | BB |\n"
|
|
"|---|----|\n"
|
|
"| x | y |\n"
|
|
"```\n"
|
|
)
|
|
result = self.run_audit()
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertIn("clean (1 table(s), 1 file(s))", result.stdout)
|
|
|
|
def test_non_markdown_fence_remains_ignored(self) -> None:
|
|
self.source(
|
|
"```text\n"
|
|
"| Fichier ou famille | Responsabilité |\n"
|
|
"|--------------------|----------------|\n"
|
|
"| x | y |\n"
|
|
"```\n"
|
|
)
|
|
result = self.run_audit()
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertIn("clean (0 table(s), 1 file(s))", result.stdout)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|