101 lines
3.3 KiB
Python
101 lines
3.3 KiB
Python
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
|
|
SCRIPT = pathlib.Path(__file__).resolve().parents[1] / "scripts" / "fix_markdown_titles.py"
|
|
|
|
|
|
def run_cli(*args):
|
|
return subprocess.run(
|
|
[sys.executable, str(SCRIPT), *args],
|
|
text=True,
|
|
capture_output=True,
|
|
check=False,
|
|
)
|
|
|
|
|
|
class FixMarkdownTitlesTest(unittest.TestCase):
|
|
def test_shifts_atx_headings_and_leaves_code_blocks(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
target = pathlib.Path(tmp) / "reply.md"
|
|
target.write_text(
|
|
"\n".join(
|
|
[
|
|
"Intro",
|
|
"",
|
|
"# One",
|
|
"## Two ##",
|
|
"```md",
|
|
"# Not a heading",
|
|
"```",
|
|
"> # quoted content",
|
|
" # indented code",
|
|
"###### Six",
|
|
"",
|
|
]
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
result = run_cli(str(target), "--levels", "2")
|
|
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertEqual(
|
|
target.read_text(encoding="utf-8"),
|
|
"\n".join(
|
|
[
|
|
"Intro",
|
|
"",
|
|
"### One",
|
|
"#### Two ##",
|
|
"```md",
|
|
"# Not a heading",
|
|
"```",
|
|
"> # quoted content",
|
|
" # indented code",
|
|
"###### Six",
|
|
"",
|
|
]
|
|
),
|
|
)
|
|
|
|
def test_dry_run_prints_without_writing(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
target = pathlib.Path(tmp) / "reply.md"
|
|
target.write_text("# One\n", encoding="utf-8")
|
|
|
|
result = run_cli(str(target), "--levels", "1", "--dry-run")
|
|
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertEqual(result.stdout, "## One\n")
|
|
self.assertEqual(target.read_text(encoding="utf-8"), "# One\n")
|
|
|
|
def test_output_writes_to_separate_file(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
source = pathlib.Path(tmp) / "reply.md"
|
|
output = pathlib.Path(tmp) / "fixed.md"
|
|
source.write_text("# One\n", encoding="utf-8")
|
|
|
|
result = run_cli(str(source), "--levels", "3", "--output", str(output))
|
|
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertEqual(source.read_text(encoding="utf-8"), "# One\n")
|
|
self.assertEqual(output.read_text(encoding="utf-8"), "#### One\n")
|
|
|
|
def test_rejects_non_positive_levels(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
target = pathlib.Path(tmp) / "reply.md"
|
|
target.write_text("# One\n", encoding="utf-8")
|
|
|
|
result = run_cli(str(target), "--levels", "0")
|
|
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertIn("positive integer", result.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|