Skip to content

Commit

Permalink
Merge pull request #46 from volo-zyko/parse_patch_info_lines
Browse files Browse the repository at this point in the history
Implement parsing of lines with extended patch info
  • Loading branch information
matiasb authored Sep 12, 2017
2 parents 3bd0bf1 + 83bcb36 commit 2e3b11a
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 24 deletions.
2 changes: 1 addition & 1 deletion tests/samples/git.diff
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ index c7921f5..8946660 100644
+This is now updated.
+
+This is a new line.

This will stay.
\ No newline at end of file
diff --git a/removed_file b/removed_file
Expand Down
29 changes: 29 additions & 0 deletions tests/samples/sample5.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== modified file 'modified_file1'
--- modified_file1 2013-10-13 23:53:13 +0000
+++ modified_file1 2013-10-13 23:53:26 +0000
@@ -1,5 +1,7 @@
This is the original content.

-This should be updated.
+This is now updated.
+
+This is a new line.

This will stay.
\ No newline at end of file

=== modified file 'modified_file2'
--- modified_file2 2013-10-13 23:53:13 +0000
+++ modified_file2 2013-10-13 23:53:26 +0000
@@ -1,5 +1,7 @@
This is the original content.

-This should be updated.
+This is now updated.
+
+This is a new line.

This will stay.
\ No newline at end of file


38 changes: 38 additions & 0 deletions tests/samples/sample6.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--- /path/to/original ''timestamp''
+++ /path/to/new ''timestamp''
@@ -1,3 +1,9 @@
+This is an important
+notice! It should
+therefore be located at
+the beginning of this
+document!
+
This part of the
document has stayed the
same from version to
@@ -5,16 +11,13 @@
be shown if it doesn't
change. Otherwise, that
would not be helping to
-compress the size of the
-changes.
-
-This paragraph contains
-text that is outdated.
-It will be deleted in the
-near future.
+compress anything.

It is important to spell
-check this dokument. On
+check this document. On
the other hand, a
misspelled word isn't
the end of the world.
this paragraph needs to
be changed. Things can
be added after it.
+
+This paragraph contains
+important new additions
+to this document.
29 changes: 29 additions & 0 deletions tests/samples/sample7.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--- /path/to/original ''timestamp''
+++ /path/to/new ''timestamp''
@@ -1,3 +1,9 @@
+This is an important
+notice! It should
+therefore be located at
+the beginning of this
+document!
+
This part of the
document has stayed the
same from version to
@@ -5,16 +11,13 @@
be shown if it doesn't
change. Otherwise, that
would not be helping to
-compress the size of the
-changes.
-
-This paragraph contains
-text that is outdated.
+compress anything.

It is important to spell
-check this dokument. On
+check this document. On
the other hand, a
misspelled word isn't
the end of the world.
48 changes: 40 additions & 8 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,27 @@ def test_preserve_dos_line_endings(self):
added_unicode_line = res.added_files[0][0][1]
self.assertEqual(added_unicode_line.value, 'holá mundo!\r\n')

def test_preserve_dos_line_endings_empty_line_type(self):
utf8_file = os.path.join(self.samples_dir, 'samples/sample5.diff')
with open(utf8_file, 'rb') as diff_file:
res = PatchSet(diff_file, encoding='utf-8')

# 2 files updated by diff
self.assertEqual(len(res), 2)
modified_unicode_line = res.modified_files[0][0][6]
self.assertEqual(modified_unicode_line.value, '\r\n')
self.assertEqual(modified_unicode_line.line_type, ' ')

modified_unicode_line = res.modified_files[1][0][6]
self.assertEqual(modified_unicode_line.value, '\n')
self.assertEqual(modified_unicode_line.line_type, ' ')

def test_print_hunks_without_gaps(self):
with codecs.open(self.sample_file, 'r', encoding='utf-8') as diff_file:
res = PatchSet(diff_file)
lines = unicode(res).splitlines()
self.assertEqual(lines[12], '@@ -5,16 +11,10 @@ ')
self.assertEqual(lines[31], '@@ -22,3 +22,7 @@ ')
self.assertEqual(lines[12], '@@ -5,16 +11,10 @@')
self.assertEqual(lines[31], '@@ -22,3 +22,7 @@')

def test_parse_sample(self):
"""Parse sample file."""
Expand Down Expand Up @@ -181,20 +196,32 @@ def test_patchset_from_bytes_string(self):
self.assertEqual(ps1, ps2)

def test_patchset_string_input(self):
with codecs.open(self.sample_file, 'r', encoding='utf-8') as diff_file:
diff_data = diff_file.read()
ps1 = PatchSet(diff_data)
with codecs.open(self.sample_file, 'r', encoding='utf-8') as diff_file:
diff_data = diff_file.read()
ps1 = PatchSet(diff_data)

with codecs.open(self.sample_file, 'r', encoding='utf-8') as diff_file:
ps2 = PatchSet(diff_file)
with codecs.open(self.sample_file, 'r', encoding='utf-8') as diff_file:
ps2 = PatchSet(diff_file)

self.assertEqual(ps1, ps2)
self.assertEqual(ps1, ps2)

def test_parse_malformed_diff(self):
"""Parse malformed file."""
with open(self.sample_bad_file) as diff_file:
self.assertRaises(UnidiffParseError, PatchSet, diff_file)

def test_parse_malformed_diff_longer_than_expected(self):
"""Parse malformed file with non-terminated hunk."""
utf8_file = os.path.join(self.samples_dir, 'samples/sample6.diff')
with open(utf8_file, 'r') as diff_file:
self.assertRaises(UnidiffParseError, PatchSet, diff_file)

def test_parse_malformed_diff_shorter_than_expected(self):
"""Parse malformed file with non-terminated hunk."""
utf8_file = os.path.join(self.samples_dir, 'samples/sample7.diff')
with open(utf8_file, 'r') as diff_file:
self.assertRaises(UnidiffParseError, PatchSet, diff_file)

def test_diff_lines_linenos(self):
with open(self.sample_file, 'rb') as diff_file:
res = PatchSet(diff_file, encoding='utf-8')
Expand Down Expand Up @@ -295,3 +322,8 @@ def test_samples(self):

self.assertEqual(res.added, 7)
self.assertEqual(res.removed, 4)

# check that original diffs and those produced
# by unidiff are the same
with codecs.open(file_path, 'r', encoding='utf-8') as diff_file:
self.assertEqual(diff_file.read(), str(res))
6 changes: 4 additions & 2 deletions unidiff/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
# - deleted line
# \ No newline case
RE_HUNK_BODY_LINE = re.compile(
r'^(?P<line_type>[- \n\+\\])(?P<value>.*)', re.DOTALL)
r'^(?P<line_type>[- \+\\])(?P<value>.*)', re.DOTALL)
RE_HUNK_EMPTY_BODY_LINE = re.compile(
r'^(?P<line_type>[- \+\\]?)(?P<value>[\r\n]{1,2})', re.DOTALL)

RE_NO_NEWLINE_MARKER = re.compile(r'^\\ No newline at end of file')

Expand All @@ -53,6 +55,6 @@
LINE_TYPE_ADDED = '+'
LINE_TYPE_REMOVED = '-'
LINE_TYPE_CONTEXT = ' '
LINE_TYPE_EMPTY = '\n'
LINE_TYPE_EMPTY = ''
LINE_TYPE_NO_NEWLINE = '\\'
LINE_VALUE_NO_NEWLINE = ' No newline at end of file'
Loading

0 comments on commit 2e3b11a

Please sign in to comment.