/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to brzlib/tests/test_delta.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from io import StringIO
18
17
import os
 
18
from cStringIO import StringIO
19
19
 
20
 
from .. import (
 
20
from brzlib import (
21
21
    delta as _mod_delta,
22
22
    revision as _mod_revision,
23
23
    tests,
24
24
    )
25
 
from ..tree import TreeChange
26
25
 
27
26
 
28
27
class InstrumentedReporter(object):
29
28
    def __init__(self):
30
29
        self.calls = []
31
30
 
32
 
    def report(self, path, versioned, renamed, copied, modified, exe_change,
 
31
    def report(self, file_id, path, versioned, renamed, modified, exe_change,
33
32
               kind):
34
 
        self.calls.append(
35
 
            (path, versioned, renamed, copied, modified, exe_change, kind))
 
33
        self.calls.append((file_id, path, versioned, renamed, modified,
 
34
                           exe_change, kind))
36
35
 
37
36
 
38
37
class TestReportChanges(tests.TestCase):
39
38
    """Test the new change reporting infrastructure"""
40
39
 
41
 
    def assertReport(self, expected, file_id=b'fid', path='path',
 
40
    def assertReport(self, expected, file_id='fid', path='path',
42
41
                     versioned_change='unchanged', renamed=False,
43
 
                     copied=False, modified='unchanged', exe_change=False,
 
42
                     modified='unchanged', exe_change=False,
44
43
                     kind=('file', 'file'), old_path=None,
45
44
                     unversioned_filter=None, view_info=None):
46
45
        if expected is None:
48
47
        else:
49
48
            expected_lines = [expected]
50
49
        self.assertReportLines(expected_lines, file_id, path,
51
 
                               versioned_change, renamed,
52
 
                               copied, modified, exe_change,
53
 
                               kind, old_path,
54
 
                               unversioned_filter, view_info)
 
50
                     versioned_change, renamed,
 
51
                     modified, exe_change,
 
52
                     kind, old_path,
 
53
                     unversioned_filter, view_info)
55
54
 
56
 
    def assertReportLines(self, expected_lines, file_id=b'fid', path='path',
57
 
                          versioned_change='unchanged', renamed=False, copied=False,
58
 
                          modified='unchanged', exe_change=False,
59
 
                          kind=('file', 'file'), old_path=None,
60
 
                          unversioned_filter=None, view_info=None):
 
55
    def assertReportLines(self, expected_lines, file_id='fid', path='path',
 
56
                     versioned_change='unchanged', renamed=False,
 
57
                     modified='unchanged', exe_change=False,
 
58
                     kind=('file', 'file'), old_path=None,
 
59
                     unversioned_filter=None, view_info=None):
61
60
        result = []
62
 
 
63
61
        def result_line(format, *args):
64
62
            result.append(format % args)
65
 
        reporter = _mod_delta._ChangeReporter(
66
 
            result_line, unversioned_filter=unversioned_filter,
67
 
            view_info=view_info)
68
 
        reporter.report((old_path, path), versioned_change, renamed, copied,
69
 
                        modified, exe_change, kind)
 
63
        reporter = _mod_delta._ChangeReporter(result_line,
 
64
            unversioned_filter=unversioned_filter, view_info=view_info)
 
65
        reporter.report(file_id, (old_path, path), versioned_change, renamed,
 
66
            modified, exe_change, kind)
70
67
        if expected_lines is not None:
71
68
            self.assertEqualDiff('\n'.join(expected_lines), '\n'.join(result))
72
69
        else:
86
83
        self.assertReport('RK  old => path/', renamed=True,
87
84
                          modified='kind changed',
88
85
                          kind=('file', 'directory'), old_path='old')
89
 
 
90
86
    def test_new(self):
91
87
        self.assertReport(' N  path/', modified='created',
92
88
                          kind=(None, 'directory'))
116
112
    def test_unversioned(self):
117
113
        # by default any unversioned file is output
118
114
        self.assertReport('?   subdir/foo~', file_id=None, path='subdir/foo~',
119
 
                          old_path=None, versioned_change='unversioned',
120
 
                          renamed=False, modified='created', exe_change=False,
121
 
                          kind=(None, 'file'))
 
115
            old_path=None, versioned_change='unversioned',
 
116
            renamed=False, modified='created', exe_change=False,
 
117
            kind=(None, 'file'))
122
118
        # but we can choose to filter these. Probably that should be done
123
119
        # close to the tree, but this is a reasonable starting point.
124
120
        self.assertReport(None, file_id=None, path='subdir/foo~',
125
 
                          old_path=None, versioned_change='unversioned',
126
 
                          renamed=False, modified='created', exe_change=False,
127
 
                          kind=(None, 'file'), unversioned_filter=lambda x: True)
 
121
            old_path=None, versioned_change='unversioned',
 
122
            renamed=False, modified='created', exe_change=False,
 
123
            kind=(None, 'file'), unversioned_filter=lambda x:True)
128
124
 
129
125
    def test_missing(self):
130
126
        self.assertReport('+!  missing.c', file_id=None, path='missing.c',
131
 
                          old_path=None, versioned_change='added',
132
 
                          renamed=False, modified='missing', exe_change=False,
133
 
                          kind=(None, None))
 
127
             old_path=None, versioned_change='added',
 
128
             renamed=False, modified='missing', exe_change=False,
 
129
             kind=(None, None))
134
130
 
135
131
    def test_view_filtering(self):
136
132
        # If a file in within the view, it should appear in the output
138
134
            "Operating on whole tree but only reporting on 'my' view.",
139
135
            " M  path"]
140
136
        self.assertReportLines(expected_lines, modified='modified',
141
 
                               view_info=('my', ['path']))
 
137
            view_info=('my',['path']))
142
138
        # If a file in outside the view, it should not appear in the output
143
139
        expected_lines = [
144
140
            "Operating on whole tree but only reporting on 'my' view."]
145
141
        self.assertReportLines(expected_lines, modified='modified',
146
 
                               path="foo", view_info=('my', ['path']))
 
142
            path="foo", view_info=('my',['path']))
147
143
 
148
144
    def assertChangesEqual(self,
149
 
                           file_id=b'fid',
 
145
                           file_id='fid',
150
146
                           paths=('path', 'path'),
151
147
                           content_change=False,
152
148
                           versioned=(True, True),
156
152
                           executable=(False, False),
157
153
                           versioned_change='unchanged',
158
154
                           renamed=False,
159
 
                           copied=False,
160
155
                           modified='unchanged',
161
156
                           exe_change=False):
162
157
        reporter = InstrumentedReporter()
163
 
        _mod_delta.report_changes([
164
 
            TreeChange(
165
 
                file_id, paths, content_change, versioned, parent_id,
166
 
                name, kind, executable, copied)], reporter)
 
158
        _mod_delta.report_changes([(file_id, paths, content_change, versioned,
 
159
            parent_id, name, kind, executable)], reporter)
167
160
        output = reporter.calls[0]
168
 
        self.assertEqual(paths, output[0])
169
 
        self.assertEqual(versioned_change, output[1])
170
 
        self.assertEqual(renamed, output[2])
171
 
        self.assertEqual(copied, output[3])
 
161
        self.assertEqual(file_id, output[0])
 
162
        self.assertEqual(paths, output[1])
 
163
        self.assertEqual(versioned_change, output[2])
 
164
        self.assertEqual(renamed, output[3])
172
165
        self.assertEqual(modified, output[4])
173
166
        self.assertEqual(exe_change, output[5])
174
167
        self.assertEqual(kind, output[6])
175
168
 
176
169
    def test_report_changes(self):
177
170
        """Test change detection of report_changes"""
178
 
        # Ensure no changes are detected by default
 
171
        #Ensure no changes are detected by default
179
172
        self.assertChangesEqual(modified='unchanged', renamed=False,
180
173
                                versioned_change='unchanged',
181
174
                                exe_change=False)
217
210
    def test_report_unversioned(self):
218
211
        """Unversioned entries are reported well."""
219
212
        self.assertChangesEqual(file_id=None, paths=(None, 'full/path'),
220
 
                                content_change=True,
221
 
                                versioned=(False, False),
222
 
                                parent_id=(None, None),
223
 
                                name=(None, 'path'),
224
 
                                kind=(None, 'file'),
225
 
                                executable=(None, False),
226
 
                                versioned_change='unversioned',
227
 
                                renamed=False,
228
 
                                modified='created',
229
 
                                exe_change=False)
 
213
                           content_change=True,
 
214
                           versioned=(False, False),
 
215
                           parent_id=(None, None),
 
216
                           name=(None, 'path'),
 
217
                           kind=(None, 'file'),
 
218
                           executable=(None, False),
 
219
                           versioned_change='unversioned',
 
220
                           renamed=False,
 
221
                           modified='created',
 
222
                           exe_change=False)
230
223
 
231
224
 
232
225
class TestChangesFrom(tests.TestCaseWithTransport):
233
226
 
234
 
    def show_string(self, delta, *args, **kwargs):
 
227
    def show_string(self, delta, *args,  **kwargs):
235
228
        to_file = StringIO()
236
229
        _mod_delta.report_delta(to_file, delta, *args, **kwargs)
237
230
        return to_file.getvalue()
240
233
        """Doing a status when a file has changed kind should work"""
241
234
        tree = self.make_branch_and_tree('.')
242
235
        self.build_tree(['filename'])
243
 
        tree.add('filename', b'file-id')
 
236
        tree.add('filename', 'file-id')
244
237
        tree.commit('added filename')
245
238
        os.unlink('filename')
246
239
        self.build_tree(['filename/'])
247
240
        delta = tree.changes_from(tree.basis_tree())
248
 
        self.assertEqual([('filename', 'file', 'directory')],
249
 
                         [(c.path[1], c.kind[0], c.kind[1]) for c in delta.kind_changed])
 
241
        self.assertEqual([('filename', 'file-id', 'file', 'directory')],
 
242
                         delta.kind_changed)
250
243
        self.assertEqual([], delta.added)
251
244
        self.assertEqual([], delta.removed)
252
245
        self.assertEqual([], delta.renamed)
253
246
        self.assertEqual([], delta.modified)
254
247
        self.assertEqual([], delta.unchanged)
255
248
        self.assertTrue(delta.has_changed())
 
249
        self.assertTrue(delta.touches_file_id('file-id'))
256
250
        self.assertEqual('kind changed:\n  filename (file => directory)\n',
257
251
                         self.show_string(delta))
258
252
        other_delta = _mod_delta.TreeDelta()
259
253
        self.assertNotEqual(other_delta, delta)
260
 
        other_delta.kind_changed = [
261
 
            TreeChange(
262
 
                b'file-id',
263
 
                ('filename', 'filename'), True, (True, True),
264
 
                (tree.path2id(''), tree.path2id('')),
265
 
                ('filename', 'filename'),
266
 
                ('file', 'symlink'), (False, False))]
 
254
        other_delta.kind_changed = [('filename', 'file-id', 'file',
 
255
                                     'symlink')]
267
256
        self.assertNotEqual(other_delta, delta)
268
 
        other_delta.kind_changed = [
269
 
            TreeChange(
270
 
                b'file-id',
271
 
                ('filename', 'filename'), True, (True, True),
272
 
                (tree.path2id(''), tree.path2id('')), ('filename', 'filename'),
273
 
                ('file', 'directory'), (False, False))]
 
257
        other_delta.kind_changed = [('filename', 'file-id', 'file',
 
258
                                     'directory')]
274
259
        self.assertEqual(other_delta, delta)
 
260
        self.assertEqualDiff("TreeDelta(added=[], removed=[], renamed=[],"
 
261
            " kind_changed=[(u'filename', 'file-id', 'file', 'directory')],"
 
262
            " modified=[], unchanged=[], unversioned=[])", repr(delta))
275
263
        self.assertEqual('K  filename (file => directory) file-id\n',
276
264
                         self.show_string(delta, show_ids=True,
277
 
                                          short_status=True))
 
265
                         short_status=True))
278
266
 
279
267
        tree.rename_one('filename', 'dirname')
280
268
        delta = tree.changes_from(tree.basis_tree())
281
269
        self.assertEqual([], delta.kind_changed)
282
270
        # This loses the fact that kind changed, remembering it as a
283
271
        # modification
284
 
        self.assertEqual([TreeChange(
285
 
            b'file-id', ('filename', 'dirname'), True,
286
 
            (True, True), (tree.path2id(''), tree.path2id('')),
287
 
            ('filename', 'dirname'), ('file', 'directory'), (False, False))],
288
 
            delta.renamed)
 
272
        self.assertEqual([('filename', 'dirname', 'file-id', 'directory',
 
273
                           True, False)], delta.renamed)
289
274
        self.assertTrue(delta.has_changed())
 
275
        self.assertTrue(delta.touches_file_id('file-id'))
290
276
 
291
277
 
292
278
class TestDeltaShow(tests.TestCaseWithTransport):
295
281
        # We build the delta from a real tree to avoid depending on internal
296
282
        # implementation details.
297
283
        wt = self.make_branch_and_tree('branch')
298
 
        self.build_tree_contents([('branch/f1', b'1\n'),
299
 
                                  ('branch/f2', b'2\n'),
300
 
                                  ('branch/f3', b'3\n'),
301
 
                                  ('branch/f4', b'4\n'),
302
 
                                  ('branch/f5', b'5\n'),
 
284
        self.build_tree_contents([('branch/f1', '1\n'),
 
285
                                  ('branch/f2', '2\n'),
 
286
                                  ('branch/f3', '3\n'),
 
287
                                  ('branch/f4', '4\n'),
 
288
                                  ('branch/f5', '5\n'),
303
289
                                  ('branch/dir/',),
304
 
                                  ])
 
290
                                 ])
305
291
        wt.add(['f1', 'f2', 'f3', 'f4', 'dir'],
306
 
               [b'f1-id', b'f2-id', b'f3-id', b'f4-id', b'dir-id'])
307
 
        wt.commit('commit one', rev_id=b'1')
 
292
               ['f1-id', 'f2-id', 'f3-id', 'f4-id', 'dir-id'])
 
293
        wt.commit('commit one', rev_id='1')
308
294
 
309
295
        # TODO add rename,removed,etc. here?
310
296
        wt.add('f5')
331
317
        d = wt.changes_from(repo.revision_tree(_mod_revision.NULL_REVISION))
332
318
        return d, long_status, short_status
333
319
 
334
 
    def test_short_status(self):
 
320
    def test_delta_show_short_status_no_filter(self):
335
321
        d, long_status, short_status = self._get_delta()
336
322
        out = StringIO()
337
323
        _mod_delta.report_delta(out, d, short_status=True)
338
324
        self.assertEqual(short_status, out.getvalue())
339
325
 
340
 
    def test_long_status(self):
 
326
    def test_delta_show_long_status_no_filter(self):
341
327
        d, long_status, short_status = self._get_delta()
342
328
        out = StringIO()
343
329
        _mod_delta.report_delta(out, d, short_status=False)
344
330
        self.assertEqual(long_status, out.getvalue())
345
331
 
346
 
    def test_predicate_always(self):
 
332
    def test_delta_show_no_filter(self):
347
333
        d, long_status, short_status = self._get_delta()
348
334
        out = StringIO()
349
 
 
350
 
        def always(path):
 
335
        def not_a_filter(path, file_id):
351
336
            return True
352
 
        _mod_delta.report_delta(out, d, short_status=True, predicate=always)
 
337
        _mod_delta.report_delta(out, d, short_status=True, filter=not_a_filter)
353
338
        self.assertEqual(short_status, out.getvalue())
354
339
 
355
 
    def test_short_status_path_predicate(self):
 
340
    def test_delta_show_short_status_single_file_filter(self):
356
341
        d, long_status, short_status = self._get_delta()
357
342
        out = StringIO()
358
 
 
359
 
        def only_f2(path):
 
343
        def only_f2(path, file_id):
360
344
            return path == 'f2'
361
 
        _mod_delta.report_delta(out, d, short_status=True, predicate=only_f2)
 
345
        _mod_delta.report_delta(out, d, short_status=True, filter=only_f2)
362
346
        self.assertEqual("A  f2\n", out.getvalue())
363
347
 
364
 
    def test_long_status_path_predicate(self):
 
348
    def test_delta_show_long_status_single_file_filter(self):
365
349
        d, long_status, short_status = self._get_delta()
366
350
        out = StringIO()
367
 
 
368
 
        def only_f2(path):
 
351
        def only_f2(path, file_id):
369
352
            return path == 'f2'
370
 
        _mod_delta.report_delta(out, d, short_status=False, predicate=only_f2)
 
353
        _mod_delta.report_delta(out, d, short_status=False, filter=only_f2)
371
354
        self.assertEqual("added:\n  f2\n", out.getvalue())
 
355
 
 
356
    def test_delta_show_short_status_single_file_id_filter(self):
 
357
        d, long_status, short_status = self._get_delta()
 
358
        out = StringIO()
 
359
        def only_f2_id(path, file_id):
 
360
            return file_id == 'f2-id'
 
361
        _mod_delta.report_delta(out, d, short_status=True, filter=only_f2_id)
 
362
        self.assertEqual("A  f2\n", out.getvalue())
 
363