File: 0001-Preserve-DOS-line-endings.patch

package info (click to toggle)
python-unidiff 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 268 kB
  • ctags: 233
  • sloc: python: 577; makefile: 7; sh: 1
file content (110 lines) | stat: -rw-r--r-- 3,554 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Description: Preserve DOS line endings
Author: SnakE <snake.scaly@gmail.com>
Applied-Upstream: commit:e140033db05d79dfdff54b492a85d11494e81864
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- /dev/null
+++ b/tests/samples/sample4.diff
@@ -0,0 +1,34 @@
+=== added file 'added_file'
+--- added_file	1970-01-01 00:00:00 +0000
++++ added_file	2013-10-13 23:44:04 +0000
+@@ -0,0 +1,4 @@
++This was missing!
++holá mundo!
++
++Only for testing purposes.
+\ No newline at end of file
+
+=== modified file 'modified_file'
+--- modified_file	2013-10-13 23:53:13 +0000
++++ modified_file	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
+
+=== removed file 'removed_file'
+--- removed_file	2013-10-13 23:53:13 +0000
++++ removed_file	1970-01-01 00:00:00 +0000
+@@ -1,3 +0,0 @@
+-This content shouldn't be here.
+-
+-This file will be removed.
+\ No newline at end of file
+
+
--- a/tests/test_parser.py
+++ b/tests/test_parser.py
@@ -34,6 +34,8 @@
 from unidiff.patch import PY2
 from unidiff.errors import UnidiffParseError
 
+if not PY2:
+    unicode = str
 
 class TestUnidiffParser(unittest.TestCase):
     """Tests for Unified Diff Parser."""
@@ -64,7 +66,24 @@
         # 3 files updated by diff
         self.assertEqual(len(res), 3)
         added_unicode_line = res.added_files[0][0][1]
-        self.assertEqual(added_unicode_line.value, 'holá mundo!')
+        self.assertEqual(added_unicode_line.value, 'holá mundo!\n')
+
+    def test_preserve_dos_line_endings(self):
+        utf8_file = os.path.join(self.samples_dir, 'samples/sample4.diff')
+        with open(utf8_file, 'rb') as diff_file:
+            res = PatchSet(diff_file, encoding='utf-8')
+
+        # 3 files updated by diff
+        self.assertEqual(len(res), 3)
+        added_unicode_line = res.added_files[0][0][1]
+        self.assertEqual(added_unicode_line.value, 'holá mundo!\r\n')
+
+    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 @@ ')
 
     def test_parse_sample(self):
         """Parse sample file."""
--- a/unidiff/constants.py
+++ b/unidiff/constants.py
@@ -43,7 +43,7 @@
 # +  added line
 # -  deleted line
 # \  No newline case (ignore)
-RE_HUNK_BODY_LINE = re.compile(r'^(?P<line_type>[- \n\+\\])(?P<value>.*)')
+RE_HUNK_BODY_LINE = re.compile(r'^(?P<line_type>[- \n\+\\])(?P<value>.*)', re.DOTALL)
 
 DEFAULT_ENCODING = 'UTF-8'
 
--- a/unidiff/patch.py
+++ b/unidiff/patch.py
@@ -123,7 +123,7 @@
         head = "@@ -%d,%d +%d,%d @@ %s\n" % (
             self.source_start, self.source_length,
             self.target_start, self.target_length, self.section_header)
-        content = '\n'.join(unicode(line) for line in self)
+        content = ''.join(unicode(line) for line in self)
         return head + content
 
     def append(self, line):
@@ -171,7 +171,7 @@
     def __str__(self):
         source = "--- %s\n" % self.source_file
         target = "+++ %s\n" % self.target_file
-        hunks = '\n'.join(unicode(hunk) for hunk in self)
+        hunks = ''.join(unicode(hunk) for hunk in self)
         return source + target + hunks
 
     def _parse_hunk(self, header, diff, encoding):