UNPKG

8.27 kBtext/coffeescriptView Raw
1expect = require 'expect.js'
2parse = require '../index'
3
4describe 'diff parser', ->
5 it 'should parse null', ->
6 expect(parse(null).length).to.be(0)
7
8 it 'should parse empty string', ->
9 expect(parse('').length).to.be(0)
10
11 it 'should parse whitespace', ->
12 expect(parse(' ').length).to.be(0)
13
14 it 'should parse simple git-like diff', ->
15 diff = """
16diff --git a/file b/file
17index 123..456 789
18--- a/file
19+++ b/file
20@@ -1,2 +1,2 @@
21- line1
22+ line2
23"""
24 files = parse diff
25 expect(files.length).to.be(1)
26 file = files[0]
27 expect(file.from).to.be('file')
28 expect(file.to).to.be('file')
29 expect(file.chunks.length).to.be(1)
30 chunk = file.chunks[0]
31 expect(chunk.content).to.be('@@ -1,2 +1,2 @@')
32 expect(chunk.changes.length).to.be(2)
33 expect(chunk.changes[0].content).to.be('- line1')
34 expect(chunk.changes[1].content).to.be('+ line2')
35
36 it 'should parse diff with new file mode line', ->
37 diff = """
38diff --git a/test b/test
39new file mode 100644
40index 0000000..db81be4
41--- /dev/null
42+++ b/test
43@@ -0,0 +1,2 @@
44+line1
45+line2
46"""
47 files = parse diff
48 expect(files.length).to.be(1)
49 file = files[0]
50 expect(file.new).to.be.true
51 expect(file.from).to.be('/dev/null')
52 expect(file.to).to.be('test')
53 expect(file.chunks[0].content).to.be('@@ -0,0 +1,2 @@')
54 expect(file.chunks[0].changes.length).to.be(2)
55 expect(file.chunks[0].changes[0].content).to.be('+line1')
56 expect(file.chunks[0].changes[1].content).to.be('+line2')
57
58 it 'should parse diff with deleted file mode line', ->
59 diff = """
60diff --git a/test b/test
61deleted file mode 100644
62index db81be4..0000000
63--- b/test
64+++ /dev/null
65@@ -1,2 +0,0 @@
66-line1
67-line2
68"""
69 files = parse diff
70 expect(files.length).to.be(1)
71 file = files[0]
72 expect(file.deleted).to.be.true
73 expect(file.from).to.be('test')
74 expect(file.to).to.be('/dev/null')
75 expect(file.chunks[0].content).to.be('@@ -1,2 +0,0 @@')
76 expect(file.chunks[0].changes.length).to.be(2)
77 expect(file.chunks[0].changes[0].content).to.be('-line1')
78 expect(file.chunks[0].changes[1].content).to.be('-line2')
79
80 it 'should parse diff with single line files', ->
81 diff = """
82diff --git a/file1 b/file1
83deleted file mode 100644
84index db81be4..0000000
85--- b/file1
86+++ /dev/null
87@@ -1 +0,0 @@
88-line1
89diff --git a/file2 b/file2
90new file mode 100644
91index 0000000..db81be4
92--- /dev/null
93+++ b/file2
94@@ -0,0 +1 @@
95+line1
96"""
97 files = parse diff
98 expect(files.length).to.be(2)
99 file = files[0]
100 expect(file.deleted).to.be.true
101 expect(file.from).to.be('file1')
102 expect(file.to).to.be('/dev/null')
103 expect(file.chunks[0].content).to.be('@@ -1 +0,0 @@')
104 expect(file.chunks[0].changes.length).to.be(1)
105 expect(file.chunks[0].changes[0].content).to.be('-line1')
106 expect(file.chunks[0].changes[0].type).to.be('del')
107 file = files[1]
108 expect(file.new).to.be.true
109 expect(file.from).to.be('/dev/null')
110 expect(file.to).to.be('file2')
111 expect(file.chunks[0].content).to.be('@@ -0,0 +1 @@')
112 expect(file.chunks[0].newLines).to.be(0)
113 expect(file.chunks[0].changes.length).to.be(1)
114 expect(file.chunks[0].changes[0].content).to.be('+line1')
115 expect(file.chunks[0].changes[0].type).to.be('add')
116
117 it 'should parse multiple files in diff', ->
118 diff = """
119diff --git a/file1 b/file1
120index 123..456 789
121--- a/file1
122+++ b/file1
123@@ -1,2 +1,2 @@
124- line1
125+ line2
126diff --git a/file2 b/file2
127index 123..456 789
128--- a/file2
129+++ b/file2
130@@ -1,3 +1,3 @@
131- line1
132+ line2
133"""
134 files = parse diff
135 expect(files.length).to.be(2)
136 file = files[0]
137 expect(file.from).to.be('file1')
138 expect(file.to).to.be('file1')
139 expect(file.chunks[0].content).to.be('@@ -1,2 +1,2 @@')
140 expect(file.chunks[0].changes.length).to.be(2)
141 expect(file.chunks[0].changes[0].content).to.be('- line1')
142 expect(file.chunks[0].changes[1].content).to.be('+ line2')
143 file = files[1]
144 expect(file.from).to.be('file2')
145 expect(file.to).to.be('file2')
146 expect(file.chunks[0].content).to.be('@@ -1,3 +1,3 @@')
147 expect(file.chunks[0].changes.length).to.be(2)
148 expect(file.chunks[0].changes[0].content).to.be('- line1')
149 expect(file.chunks[0].changes[1].content).to.be('+ line2')
150
151 it 'should parse diff with EOF flag', ->
152 diff = """
153diff --git a/file1 b/file1
154index 123..456 789
155--- a/file1
156+++ b/file1
157@@ -1,2 +1,2 @@
158- line1
159+ line2
160\\ No newline at end of file
161"""
162 files = parse diff
163 expect(files.length).to.be(1)
164 file = files[0]
165 expect(file.from).to.be('file1')
166 expect(file.to).to.be('file1')
167 chunk = file.chunks[0]
168 expect(chunk.content).to.be('@@ -1,2 +1,2 @@')
169 expect(chunk.changes.length).to.be(3)
170 expect(chunk.changes[0].content).to.be('- line1')
171 expect(chunk.changes[1].content).to.be('+ line2')
172 expect(chunk.changes[2].type).to.be('add')
173 expect(chunk.changes[2].content).to.be('\\ No newline at end of file')
174
175 it 'should parse gnu sample diff', ->
176 diff = """
177--- lao 2002-02-21 23:30:39.942229878 -0800
178+++ tzu 2002-02-21 23:30:50.442260588 -0800
179@@ -1,7 +1,6 @@
180-The Way that can be told of is not the eternal Way;
181-The name that can be named is not the eternal name.
182 The Nameless is the origin of Heaven and Earth;
183-The Named is the mother of all things.
184+The named is the mother of all things.
185+
186 Therefore let there always be non-being,
187 so we may see their subtlety,
188And let there always be being,
189@@ -9,3 +8,6 @@
190 The two are the same,
191 But after they are produced,
192 they have different names.
193+They both may be called deep and profound.
194+Deeper and more profound,
195+The door of all subtleties!
196"""
197 files = parse diff
198 expect(files.length).to.be(1)
199 file = files[0]
200 expect(file.from).to.be('lao')
201 expect(file.to).to.be('tzu')
202 expect(file.chunks.length).to.be(2)
203 chunk0 = file.chunks[0]
204 expect(chunk0.oldStart).to.be(1)
205 expect(chunk0.oldLines).to.be(7)
206 expect(chunk0.newStart).to.be(1)
207 expect(chunk0.newLines).to.be(6)
208 chunk1 = file.chunks[1]
209 expect(chunk1.oldStart).to.be(9)
210 expect(chunk1.oldLines).to.be(3)
211 expect(chunk1.newStart).to.be(8)
212 expect(chunk1.newLines).to.be(6)
213
214 it 'should parse hg diff output', ->
215 diff = """
216diff -r 514fc757521e lib/parsers.coffee
217--- a/lib/parsers.coffee Thu Jul 09 00:56:36 2015 +0200
218+++ b/lib/parsers.coffee Fri Jul 10 16:23:43 2015 +0200
219@@ -43,6 +43,9 @@
220 files[file] = { added: added, deleted: deleted }
221 files
222
223+ diff: (out) ->
224+ files = {}
225+
226 module.exports = Parsers
227
228 module.exports.version = (out) ->
229"""
230 files = parse diff
231 expect(files.length).to.be(1)
232 file = files[0]
233 expect(file.chunks[0].content).to.be('@@ -43,6 +43,9 @@')
234 expect(file.from).to.be('lib/parsers.coffee')
235 expect(file.to).to.be('lib/parsers.coffee')
236
237 it 'should parse svn diff output', ->
238 diff = """
239Index: new.txt
240===================================================================
241--- new.txt (revision 0)
242+++ new.txt (working copy)
243@@ -0,0 +1 @@
244+test
245Index: text.txt
246===================================================================
247--- text.txt (revision 6)
248+++ text.txt (working copy)
249@@ -1,7 +1,5 @@
250-This part of the
251-document has stayed the
252-same from version to
253-version. It shouldn't
254+This is an important
255+notice! It shouldn't
256 be shown if it doesn't
257 change. Otherwise, that
258 would not be helping to
259"""
260 files = parse diff
261 expect(files.length).to.be(2)
262 file = files[0]
263 expect(file.from).to.be('new.txt')
264 expect(file.to).to.be('new.txt')
265 expect(file.chunks[0].changes.length).to.be(1)
266
267 it 'should parse file names for n new empty file', ->
268 diff = """
269diff --git a/newFile.txt b/newFile.txt
270new file mode 100644
271index 0000000..e6a2e28
272"""
273 files = parse diff
274 expect(files.length).to.be(1)
275 file = files[0]
276 expect(file.from).to.be('/dev/null')
277 expect(file.to).to.be('newFile.txt')
278
279 it 'should parse file names for a deleted file', ->
280 diff = """
281diff --git a/deletedFile.txt b/deletedFile.txt
282deleted file mode 100644
283index e6a2e28..0000000
284"""
285 files = parse diff
286 expect(files.length).to.be(1)
287 file = files[0]
288 expect(file.from).to.be('deletedFile.txt')
289 expect(file.to).to.be('/dev/null')