File: diffparse_test.py

package info (click to toggle)
git-cola 4.13.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 6,480 kB
  • sloc: python: 36,938; sh: 304; makefile: 223; xml: 100; tcl: 62
file content (375 lines) | stat: -rw-r--r-- 10,053 bytes parent folder | download | duplicates (2)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""Tests for the diffparse module"""
import pytest

from cola import core
from cola import diffparse

from . import helper


class DiffLinesTestData:
    """Test data used by DiffLines tests"""

    def __init__(self):
        self.parser = diffparse.DiffLines()
        fixture_path = helper.fixture('diff.txt')
        self.text = core.read(fixture_path)


@pytest.fixture
def difflines_data():
    """Return test data for diffparse.DiffLines tests"""
    return DiffLinesTestData()


def test_diff():
    fixture_path = helper.fixture('diff.txt')
    patch = diffparse.Patch.parse('cola/diffparse.py', core.read(fixture_path))
    hunks = patch.hunks

    assert len(hunks) == 3
    assert len(hunks[0].lines) == 23
    assert hunks[0].lines[0] == '@@ -6,10 +6,21 @@ from cola import gitcmds\n'
    assert hunks[0].lines[1] == ' from cola import gitcfg\n'
    assert hunks[0].lines[2] == ' \n'
    assert hunks[0].lines[3] == ' \n'
    assert hunks[0].lines[4] == '+class DiffSource(object):\n'
    assert hunks[0].lines[-1] == (
        r"         self._header_start_re = re.compile('^@@ -(\d+)"
        r" \+(\d+),(\d+) @@.*')"
        '\n'
    )
    assert len(hunks[1].lines) == 18
    assert hunks[1].lines[0] == '@@ -29,13 +40,11 @@ class DiffParser(object):\n'
    assert hunks[1].lines[1] == '         self.diff_sel = []\n'
    assert hunks[1].lines[2] == '         self.selected = []\n'
    assert hunks[1].lines[3] == '         self.filename = filename\n'
    assert hunks[1].lines[4] == (
        '+        self.diff_source = diff_source or DiffSource()\n'
    )
    assert hunks[1].lines[-1] == '         self.header = header\n'

    assert len(hunks[2].lines) == 16
    assert hunks[2].lines[0] == '@@ -43,11 +52,10 @@ class DiffParser(object):\n'
    assert hunks[2].lines[-1] == (
        '         """Writes a new diff corresponding to the user\'s' ' selection."""\n'
    )


def test_diff_at_start():
    fixture_path = helper.fixture('diff-start.txt')
    patch = diffparse.Patch.parse('foo bar/a', core.read(fixture_path))
    hunks = patch.hunks

    assert hunks[0].lines[0] == '@@ -1 +1,4 @@\n'
    assert hunks[-1].lines[-1] == '+c\n'
    assert hunks[0].old_start == 1
    assert hunks[0].old_count == 1
    assert hunks[0].new_start == 1
    assert hunks[0].new_count == 4
    assert patch.extract_subset(1, 3).as_text() == (
        '--- a/foo bar/a\n' '+++ b/foo bar/a\n' '@@ -1 +1,3 @@\n' ' bar\n' '+a\n' '+b\n'
    )
    assert patch.extract_subset(0, 4).as_text() == (
        '--- a/foo bar/a\n'
        '+++ b/foo bar/a\n'
        '@@ -1 +1,4 @@\n'
        ' bar\n'
        '+a\n'
        '+b\n'
        '+c\n'
    )


def test_diff_at_end():
    fixture_path = helper.fixture('diff-end.txt')
    patch = diffparse.Patch.parse('rijndael.js', core.read(fixture_path))
    hunks = patch.hunks

    assert hunks[0].lines[0] == '@@ -1,39 +1 @@\n'
    assert hunks[-1].lines[-1] == (
        "+module.exports = require('./build/Release/rijndael');\n"
    )
    assert hunks[0].old_start == 1
    assert hunks[0].old_count == 39
    assert hunks[0].new_start == 1
    assert hunks[0].new_count == 1


def test_diff_that_empties_file():
    fixture_path = helper.fixture('diff-empty.txt')
    patch = diffparse.Patch.parse('filename', core.read(fixture_path))
    hunks = patch.hunks

    assert hunks[0].lines[0] == '@@ -1,2 +0,0 @@\n'
    assert hunks[-1].lines[-1] == '-second\n'
    assert hunks[0].old_start == 1
    assert hunks[0].old_count == 2
    assert hunks[0].new_start == 0
    assert hunks[0].new_count == 0
    assert patch.extract_subset(1, 1).as_text() == (
        '--- a/filename\n' '+++ b/filename\n' '@@ -1,2 +1 @@\n' '-first\n' ' second\n'
    )
    assert patch.extract_subset(0, 2).as_text() == (
        '--- a/filename\n' '+++ b/filename\n' '@@ -1,2 +0,0 @@\n' '-first\n' '-second\n'
    )


def test_diff_file_removal():
    diff_text = """\
deleted file mode 100755
@@ -1,1 +0,0 @@
-#!/bin/sh
"""
    patch = diffparse.Patch.parse('deleted.txt', diff_text)

    expect = 1
    actual = len(patch.hunks)
    assert expect == actual

    # Selecting the first two lines generate no diff
    expect = ''
    actual = patch.extract_subset(0, 1).as_text()
    assert expect == actual

    # Selecting the last line should generate a line removal
    expect = """\
--- a/deleted.txt
+++ b/deleted.txt
@@ -1 +0,0 @@
-#!/bin/sh
"""
    actual = patch.extract_subset(1, 2).as_text()
    assert expect == actual

    # All three lines should map to the same hunk diff
    actual = patch.extract_hunk(0).as_text()
    assert expect == actual

    actual = patch.extract_hunk(1).as_text()
    assert expect == actual

    actual = patch.extract_hunk(2).as_text()
    assert expect == actual


def test_basic_diff_line_count(difflines_data):
    """Verify the basic line counts"""
    lines = difflines_data.parser.parse(difflines_data.text)
    expect = len(difflines_data.text.splitlines())
    actual = len(lines)
    assert expect == actual


def test_diff_line_count_ranges(difflines_data):
    parser = difflines_data.parser
    lines = parser.parse(difflines_data.text)

    # Diff header
    line = 0
    count = 1
    assert lines[line][0] == parser.DASH
    assert lines[line][1] == parser.DASH
    line += count

    # 3 lines of context
    count = 3
    current_old = 6
    current_new = 6
    for i in range(count):
        assert lines[line + i][0] == current_old + i
        assert lines[line + i][1] == current_new + i
    line += count
    current_old += count
    current_new += count

    # 10 lines of new text
    count = 10
    for i in range(count):
        assert lines[line + i][0] == parser.EMPTY
        assert lines[line + i][1] == current_new + i

    line += count
    current_new += count

    # 3 more lines of context
    count = 3
    for i in range(count):
        assert lines[line + i][0] == current_old + i
        assert lines[line + i][1] == current_new + i
    line += count
    current_new += count
    current_old += count

    # 1 line of removal
    count = 1
    for i in range(count):
        assert lines[line + i][0] == current_old + i
        assert lines[line + i][1] == parser.EMPTY
    line += count
    current_old += count

    # 2 lines of addition
    count = 2
    for i in range(count):
        assert lines[line + i][0] == parser.EMPTY
        assert lines[line + i][1] == current_new + i
    line += count
    current_new += count

    # 3 more lines of context
    count = 3
    for i in range(count):
        assert lines[line + i][0] == current_old + i
        assert lines[line + i][1] == current_new + i
    line += count
    current_new += count
    current_old += count

    # 1 line of header
    count = 1
    for i in range(count):
        assert lines[line + i][0] == parser.DASH
        assert lines[line + i][1] == parser.DASH
    line += count

    # 3 more lines of context
    current_old = 29
    current_new = 40
    count = 3
    for i in range(count):
        assert lines[line + i][0] == current_old + i
        assert lines[line + i][1] == current_new + i
    line += count
    current_new += count
    current_old += count

    expect_max_old = 53
    assert expect_max_old == parser.old.max_value

    expect_max_new = 61
    assert expect_max_new == parser.new.max_value

    assert parser.digits() == 2


def test_diff_line_for_merge(difflines_data):
    """Verify the basic line counts"""
    text = """@@@ -1,23 -1,33 +1,75 @@@
++<<<<<<< upstream
 +
 +Ok
"""
    parser = difflines_data.parser
    lines = parser.parse(text)
    assert len(lines) == 4
    assert len(lines[0]) == 3
    assert len(lines[1]) == 3
    assert len(lines[2]) == 3
    assert len(lines[3]) == 3

    assert lines[0][0] == parser.DASH
    assert lines[0][1] == parser.DASH
    assert lines[0][2] == parser.DASH

    assert lines[1][0] == parser.EMPTY
    assert lines[1][1] == parser.EMPTY
    assert lines[1][2] == 1

    assert lines[2][0] == 1
    assert lines[2][1] == parser.EMPTY
    assert lines[2][2] == 2

    assert lines[3][0] == 2
    assert lines[3][1] == parser.EMPTY
    assert lines[3][2] == 3


def test_diff_line_digits(difflines_data):
    parser = difflines_data.parser

    text = """@@ -1,99 +1,99 @@"""
    parser.parse(text)
    assert parser.digits() == 2

    text = """@@ -2,99 +2,99 @@"""
    parser.parse(text)
    assert parser.digits() == 3


def test_format_basic():
    fmt = diffparse.FormatDigits()
    fmt.set_digits(2)

    expect = '01 99'
    actual = fmt.value(1, 99)
    assert expect == actual


def test_format_reuse():
    fmt = diffparse.FormatDigits()

    fmt.set_digits(3)
    expect = '001 099'
    actual = fmt.value(1, 99)
    assert expect == actual

    fmt.set_digits(4)
    expect = '0001 0099'
    actual = fmt.value(1, 99)
    assert expect == actual


def test_format_special_values():
    fmt = diffparse.FormatDigits(dash='-')
    fmt.set_digits(3)

    expect = '    099'
    actual = fmt.value(fmt.EMPTY, 99)
    assert expect == actual

    expect = '001    '
    actual = fmt.value(1, fmt.EMPTY)
    assert expect == actual

    expect = '       '
    actual = fmt.value(fmt.EMPTY, fmt.EMPTY)
    assert expect == actual

    expect = '--- 001'
    actual = fmt.value(fmt.DASH, 1)
    assert expect == actual

    expect = '099 ---'
    actual = fmt.value(99, fmt.DASH)
    assert expect == actual

    expect = '--- ---'
    actual = fmt.value(fmt.DASH, fmt.DASH)
    assert expect == actual

    expect = '    ---'
    actual = fmt.value(fmt.EMPTY, fmt.DASH)
    assert expect == actual

    expect = '---    '
    actual = fmt.value(fmt.DASH, fmt.EMPTY)
    assert expect == actual


def test_parse_range_str():
    start, count = diffparse.parse_range_str('1,2')
    assert start == 1
    assert count == 2


def test_parse_range_str_single_line():
    start, count = diffparse.parse_range_str('2')
    assert start == 2
    assert count == 1


def test_parse_range_str_empty():
    start, count = diffparse.parse_range_str('0,0')
    assert start == 0
    assert count == 0