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
|
import subprocess
import tempfile
import unittest
from pathlib import Path
from parameterized import parameterized
from moreorless import unified_diff
class ParityTest(unittest.TestCase):
@parameterized.expand( # type: ignore
[
("a", "a"),
("a", "b"),
("a\n", "b"),
("a", "b\n"),
("a\n", "b\n"),
]
)
def test_parity(self, a: str, b: str) -> None:
with tempfile.TemporaryDirectory() as d:
a_path = Path(d) / "a"
a_path.mkdir()
b_path = Path(d) / "b"
b_path.mkdir()
(a_path / "file").write_text(a)
(b_path / "file").write_text(b)
# Notably, diff exits 1 when the files are different :/
# Force the labels because it would otherwise include timestamps.
proc = subprocess.run(
["diff", "--label", "a/file", "--label", "b/file", "-u", "a", "b"],
cwd=d,
encoding="utf-8",
stdout=subprocess.PIPE,
)
if "\n" in proc.stdout:
expected = proc.stdout[proc.stdout.index("\n") + 1 :]
else:
expected = ""
actual = unified_diff(a, b, "file")
self.assertEqual(expected, actual)
def test_absolute_paths(self) -> None:
actual = unified_diff("a\n", "a\nb\n", "/file")
self.assertEqual(
"""\
--- /file
+++ /file
@@ -1 +1,2 @@
a
+b
""",
actual,
)
|