Skip to content

Commit

Permalink
Fix unified diff parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
kkpattern committed Feb 4, 2021
1 parent 73290df commit 4d6629d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions tests/casefiles/abc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The Nameless is the origin of Heaven and Earth;
5 changes: 5 additions & 0 deletions tests/casefiles/diff-unified2.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--- abc 2013-01-05 16:56:19.000000000 -0600
+++ efg 2013-01-05 16:56:35.000000000 -0600
@@ -1 +1,2 @@
The Nameless is the origin of Heaven and Earth;
+The named is the mother of all things.
2 changes: 2 additions & 0 deletions tests/casefiles/efg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The Nameless is the origin of Heaven and Earth;
The named is the mother of all things.
29 changes: 29 additions & 0 deletions tests/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def setUp(self):
with open("tests/casefiles/tzu") as f:
self.tzu = f.read().splitlines()

with open("tests/casefiles/abc") as f:
self.abc = f.read().splitlines()

with open("tests/casefiles/efg") as f:
self.efg = f.read().splitlines()

def test_truth(self):
self.assertEqual(type(self.lao), list)
self.assertEqual(type(self.tzu), list)
Expand Down Expand Up @@ -55,6 +61,13 @@ def test_diff_unified(self):
self.assertEqual(_apply(self.lao, diff_text), self.tzu)
self.assertEqual(_apply_r(self.tzu, diff_text), self.lao)

def test_diff_unified2(self):
with open("tests/casefiles/diff-unified2.diff") as f:
diff_text = f.read()

self.assertEqual(_apply(self.abc, diff_text), self.efg)
self.assertEqual(_apply_r(self.efg, diff_text), self.abc)

def test_diff_unified_bad(self):
with open("tests/casefiles/diff-unified-bad.diff") as f:
diff_text = f.read()
Expand Down Expand Up @@ -129,6 +142,22 @@ def test_diff_unified_patchutil(self):
with pytest.raises(exceptions.ApplyException):
_apply([""] + self.lao, diff_text, use_patch=True)

def test_diff_unified2_patchutil(self):
with open("tests/casefiles/diff-unified2.diff") as f:
diff_text = f.read()

if not which("patch"):
raise SkipTest()

self.assertEqual(_apply(self.abc, diff_text, use_patch=True),
(self.efg, None))
self.assertEqual(_apply(self.abc, diff_text, use_patch=True),
(_apply(self.abc, diff_text), None))
self.assertEqual(_apply_r(self.efg, diff_text, use_patch=True),
(self.abc, None))
self.assertEqual(_apply_r(self.efg, diff_text, use_patch=True),
(_apply_r(self.efg, diff_text), None))

def test_diff_rcs(self):
with open("tests/casefiles/diff-rcs.diff") as f:
diff_text = f.read()
Expand Down
28 changes: 28 additions & 0 deletions tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,34 @@ def test_unified_diff(self):
results_main = next(wtp.patch.parse_patch(text))
self.assert_diffs_equal(results_main, expected_main)

def test_unified2_diff(self):
with open(datapath("diff-unified2.diff")) as f:
text = f.read()

# off with your head!
text_diff = "\n".join(text.splitlines()[2:]) + "\n"

expected = [
(None, 2, "The named is the mother of all things."),
]

results = list(wtp.patch.parse_unified_diff(text_diff))
self.assert_diffs_equal(results, expected)

expected_main = diffobj(
header=headerobj(
index_path=None,
old_path="abc",
old_version="2013-01-05 16:56:19.000000000 -0600",
new_path="efg",
new_version="2013-01-05 16:56:35.000000000 -0600",
),
changes=expected,
text=text,
)
results_main = next(wtp.patch.parse_patch(text))
self.assert_diffs_equal(results_main, expected_main)

def test_diff_unified_with_does_not_include_extra_lines(self):
with open("tests/casefiles/diff-unified-blah.diff") as f:
text = f.read()
Expand Down
5 changes: 3 additions & 2 deletions whatthepatch/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,9 @@ def parse_unified_diff(text):
elif kind == "+" and (i != new_len or i == 0):
changes.append(Change(None, new + i, line, hunk_n))
i += 1
elif kind == " " and r != old_len and i != new_len:
changes.append(Change(old + r, new + i, line, hunk_n))
elif kind == " ":
if r != old_len and i != new_len:
changes.append(Change(old + r, new + i, line, hunk_n))
r += 1
i += 1

Expand Down

0 comments on commit 4d6629d

Please sign in to comment.