|
| 1 | +"""Regression tests for the markdown2 command line interface.""" |
| 2 | + |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +class FileVarsTestCase(unittest.TestCase): |
| 11 | + source = "<!-- -*- markdown-extras: header-ids -*- -->\n# Heading\n" |
| 12 | + script = Path(__file__).resolve().parent.parent / "lib" / "markdown2.py" |
| 13 | + |
| 14 | + def run_cli(self, *args, text=""): |
| 15 | + result = subprocess.run( |
| 16 | + [sys.executable, str(self.script), *map(str, args)], |
| 17 | + input=text, |
| 18 | + text=True, |
| 19 | + capture_output=True, |
| 20 | + timeout=10, |
| 21 | + ) |
| 22 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 23 | + return result.stdout |
| 24 | + |
| 25 | + def test_file_vars_from_stdin(self): |
| 26 | + html = self.run_cli("--use-file-vars", text=self.source) |
| 27 | + self.assertIn('<h1 id="heading">Heading</h1>', html) |
| 28 | + |
| 29 | + def test_file_vars_before_path(self): |
| 30 | + with tempfile.TemporaryDirectory() as directory: |
| 31 | + path = Path(directory) / "input.md" |
| 32 | + path.write_text(self.source, encoding="utf-8") |
| 33 | + html = self.run_cli("--use-file-vars", path) |
| 34 | + self.assertIn('<h1 id="heading">Heading</h1>', html) |
| 35 | + |
| 36 | + def test_file_vars_after_path(self): |
| 37 | + with tempfile.TemporaryDirectory() as directory: |
| 38 | + path = Path(directory) / "input.md" |
| 39 | + path.write_text(self.source, encoding="utf-8") |
| 40 | + html = self.run_cli(path, "--use-file-vars") |
| 41 | + self.assertIn('<h1 id="heading">Heading</h1>', html) |
| 42 | + |
| 43 | + def test_file_vars_with_multiple_paths(self): |
| 44 | + with tempfile.TemporaryDirectory() as directory: |
| 45 | + paths = [ |
| 46 | + Path(directory) / name for name in ("first.md", "second.md") |
| 47 | + ] |
| 48 | + for path in paths: |
| 49 | + path.write_text(self.source, encoding="utf-8") |
| 50 | + html = self.run_cli("--use-file-vars", *paths) |
| 51 | + self.assertEqual(html.count('<h1 id="heading">Heading</h1>'), 2) |
| 52 | + |
| 53 | + def test_file_vars_with_output_option(self): |
| 54 | + with tempfile.TemporaryDirectory() as directory: |
| 55 | + output = Path(directory) / "output.html" |
| 56 | + stdout = self.run_cli( |
| 57 | + "--use-file-vars", "--output", output, text=self.source |
| 58 | + ) |
| 59 | + self.assertEqual(stdout, "") |
| 60 | + html = output.read_text(encoding="utf-8") |
| 61 | + self.assertIn('<h1 id="heading">Heading</h1>', html) |
| 62 | + |
| 63 | + def test_file_vars_are_disabled_by_default(self): |
| 64 | + html = self.run_cli(text=self.source) |
| 65 | + self.assertIn("<h1>Heading</h1>", html) |
| 66 | + self.assertNotIn('id="heading"', html) |
0 commit comments