/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to tests/test_diff.py

Commit messages never contain config options

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright (C) 2007 Adeodato Simó <dato@net.com.org.es>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
import os
 
19
 
 
20
from gi.repository import Gtk
 
21
 
 
22
from bzrlib import (
 
23
    conflicts,
 
24
    errors,
 
25
    tests,
 
26
    )
 
27
try:
 
28
    from bzrlib.tests.features import UnicodeFilenameFeature
 
29
except ImportError: # bzr < 2.5
 
30
    from bzrlib.tests import UnicodeFilenameFeature
 
31
from bzrlib.merge_directive import MergeDirective2
 
32
 
 
33
from bzrlib.plugins.gtk.diff import (
 
34
    DiffController,
 
35
    DiffFileView,
 
36
    DiffView,
 
37
    DiffWidget,
 
38
    DiffWindow,
 
39
    have_gtksourceview,
 
40
    iter_changes_to_status,
 
41
    MergeDirectiveController,
 
42
    )
 
43
from bzrlib.plugins.gtk.tests import MockMethod
 
44
 
 
45
 
 
46
eg_diff = """\
 
47
=== modified file 'tests/test_diff.py'
 
48
--- tests/test_diff.py  2008-03-11 13:18:28 +0000
 
49
+++ tests/test_diff.py  2008-05-08 22:44:02 +0000
 
50
@@ -20,7 +20,11 @@
 
51
 
 
52
 from bzrlib import tests
 
53
 
 
54
-from bzrlib.plugins.gtk.diff import DiffView, iter_changes_to_status
 
55
+from bzrlib.plugins.gtk.diff import (
 
56
+    DiffController,
 
57
+    DiffView,
 
58
+    iter_changes_to_status,
 
59
+    )
 
60
 
 
61
 
 
62
 class TestDiffViewSimple(tests.TestCase):
 
63
"""
 
64
 
 
65
 
 
66
class FakeDiffFileView(DiffFileView):
 
67
 
 
68
    SHOW_WIDGETS = False
 
69
 
 
70
 
 
71
class DiffFileViewTestCase(tests.TestCase):
 
72
 
 
73
    def test_init_construct(self):
 
74
        view = FakeDiffFileView()
 
75
        self.assertIsNot(None, view.buffer)
 
76
        self.assertIsNot(None, view.sourceview)
 
77
        self.assertIs(False, view.sourceview.props.editable)
 
78
        font_desc = view.sourceview.get_style_context().get_font(
 
79
            Gtk.StateFlags.NORMAL)
 
80
        self.assertIn('Monospace', font_desc.get_family())
 
81
 
 
82
    def test_init_construct_have_gtksourceview(self):
 
83
        if not have_gtksourceview:
 
84
            return
 
85
        view = FakeDiffFileView()
 
86
        self.assertEqual('Diff', view.buffer.get_language().get_name())
 
87
        self.assertIs(True, view.buffer.get_highlight_syntax())
 
88
 
 
89
 
 
90
class TestDiffView(tests.TestCaseWithTransport):
 
91
 
 
92
    def test_unicode(self):
 
93
        self.requireFeature(UnicodeFilenameFeature)
 
94
 
 
95
        tree = self.make_branch_and_tree('tree')
 
96
        self.build_tree([u'tree/\u03a9'])
 
97
        tree.add([u'\u03a9'], ['omega-id'])
 
98
 
 
99
        view = DiffView()
 
100
        view.set_trees(tree, tree.basis_tree())
 
101
        view.show_diff(None)
 
102
        buf = view.buffer
 
103
        start, end = buf.get_bounds()
 
104
        text = buf.get_text(start, end, True)
 
105
        self.assertContainsRe(text,
 
106
            "=== added file '\xce\xa9'\n"
 
107
            '--- .*\t1970-01-01 00:00:00 \\+0000\n'
 
108
            r'\+\+\+ .*\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d [+-]\d\d\d\d\n'
 
109
            '@@ -0,0 \\+1,1 @@\n'
 
110
            '\\+contents of tree/\xce\xa9\n'
 
111
            '\n'
 
112
            )
 
113
 
 
114
 
 
115
 
 
116
class FakeDiffWidget(DiffWidget):
 
117
 
 
118
    SHOW_WIDGETS = False
 
119
 
 
120
 
 
121
class TestDiffWidget(tests.TestCaseWithTransport):
 
122
 
 
123
    def test_treeview_cursor_cb(self):
 
124
        widget = FakeDiffWidget()
 
125
        widget.set_diff_text_sections(
 
126
            [('', None, 'patch1'), ('a', 'a', 'patch2')])
 
127
        widget.treeview.set_cursor(Gtk.TreePath(path=1), None, False)
 
128
        widget._treeview_cursor_cb(None)
 
129
        self.assertTrue('patch2', widget.diff_view.buffer.props.text)
 
130
 
 
131
    def test_treeview_cursor_cb_with_destroyed_treeview(self):
 
132
        widget = FakeDiffWidget()
 
133
        widget.set_diff_text_sections(
 
134
            [('', None, 'patch1'), ('a', 'a', 'patch2')])
 
135
        MockMethod.bind(self, widget.diff_view, 'show_diff')
 
136
        widget.treeview.destroy()
 
137
        widget._treeview_cursor_cb(None)
 
138
        self.assertFalse(widget.diff_view.show_diff.called)
 
139
 
 
140
 
 
141
class FakeDiffWindow(DiffWindow):
 
142
 
 
143
    SHOW_WIDGETS = False
 
144
 
 
145
 
 
146
class DiffWindowTestCase(tests.TestCaseWithTransport):
 
147
 
 
148
    def test_init(self):
 
149
        window = DiffWindow()
 
150
        self.assertEqual('bzr diff', window.props.title)
 
151
        self.assertEqual(0, window.props.border_width)
 
152
 
 
153
    def test_init_construct_without_operations(self):
 
154
        window = DiffWindow()
 
155
        widgets = window.vbox.get_children()
 
156
        self.assertEqual(2, len(widgets))
 
157
        self.assertIsInstance(widgets[0], Gtk.MenuBar)
 
158
        self.assertIsInstance(widgets[1], DiffWidget)
 
159
 
 
160
    def test_init_construct_with_operations(self):
 
161
        method = MockMethod()
 
162
        window = DiffWindow(operations=[('title', method)])
 
163
        widgets = window.vbox.get_children()
 
164
        self.assertEqual(3, len(widgets))
 
165
        self.assertIsInstance(widgets[0], Gtk.MenuBar)
 
166
        self.assertIsInstance(widgets[1], Gtk.HButtonBox)
 
167
        self.assertIsInstance(widgets[2], DiffWidget)
 
168
 
 
169
    def test_get_menu_bar(self):
 
170
        window = DiffWindow()
 
171
        menu_bar = window._get_menu_bar()
 
172
        self.assertIsNot(None, menu_bar)
 
173
        menus = menu_bar.get_children()
 
174
        self.assertEqual(1, len(menus))
 
175
        self.assertEqual('_View', menus[0].props.label)
 
176
        sub_menu = menus[0].get_submenu()
 
177
        self.assertIsNot(None, sub_menu)
 
178
        items = sub_menu.get_children()
 
179
        self.assertEqual(1, len(items))
 
180
        menus[0].get_submenu().get_children()[0].props.label
 
181
        self.assertEqual('Wrap _Long Lines', items[0].props.label)
 
182
 
 
183
    def test_get_button_bar_with_none(self):
 
184
        window = DiffWindow()
 
185
        self.assertIs(None, window._get_button_bar(None))
 
186
 
 
187
    def test_get_button_bar_with_operations(self):
 
188
        window = DiffWindow()
 
189
        method = MockMethod()
 
190
        button_bar = window._get_button_bar([('title', method)])
 
191
        self.assertIsNot(None, button_bar)
 
192
        buttons = button_bar.get_children()
 
193
        self.assertEqual(1, len(buttons))
 
194
        self.assertEqual('title', buttons[0].props.label)
 
195
        buttons[0].emit('clicked')
 
196
        self.assertIs(True, method.called)
 
197
 
 
198
 
 
199
 
 
200
class MockDiffWidget(object):
 
201
 
 
202
    def set_diff_text_sections(self, sections):
 
203
        self.sections = list(sections)
 
204
 
 
205
 
 
206
class MockWindow(object):
 
207
 
 
208
    def __init__(self):
 
209
        self.diff = MockDiffWidget()
 
210
        self.merge_successful = False
 
211
 
 
212
    def set_title(self, title):
 
213
        self.title = title
 
214
 
 
215
    def _get_save_path(self, basename):
 
216
        return 'save-path'
 
217
 
 
218
    def _get_merge_target(self):
 
219
        return 'this'
 
220
 
 
221
    def destroy(self):
 
222
        pass
 
223
 
 
224
    def _merge_successful(self):
 
225
        self.merge_successful = True
 
226
 
 
227
    def _conflicts(self):
 
228
        self.conflicts = True
 
229
 
 
230
    def _handle_error(self, e):
 
231
        self.handled_error = e
 
232
 
 
233
 
 
234
class TestDiffController(tests.TestCaseWithTransport):
 
235
 
 
236
    def get_controller(self):
 
237
        window = MockWindow()
 
238
        return DiffController('load-path', eg_diff.splitlines(True), window)
 
239
 
 
240
    def test_get_diff_sections(self):
 
241
        controller = self.get_controller()
 
242
        controller = DiffController('.', eg_diff.splitlines(True),
 
243
                                    controller.window)
 
244
        sections = list(controller.get_diff_sections())
 
245
        self.assertEqual('Complete Diff', sections[0][0])
 
246
        self.assertIs(None, sections[0][1])
 
247
        self.assertEqual(eg_diff, sections[0][2])
 
248
 
 
249
        self.assertEqual('tests/test_diff.py', sections[1][0])
 
250
        self.assertEqual('tests/test_diff.py', sections[1][1])
 
251
        self.assertEqual(''.join(eg_diff.splitlines(True)[1:]),
 
252
                         sections[1][2])
 
253
 
 
254
    def test_initialize_window(self):
 
255
        controller = self.get_controller()
 
256
        controller.initialize_window(controller.window)
 
257
        self.assertEqual(2, len(controller.window.diff.sections))
 
258
        self.assertEqual('load-path - diff', controller.window.title)
 
259
 
 
260
    def test_perform_save(self):
 
261
        self.build_tree_contents([('load-path', 'foo')])
 
262
        controller = self.get_controller()
 
263
        controller.perform_save(None)
 
264
        self.assertFileEqual('foo', 'save-path')
 
265
 
 
266
 
 
267
class TestMergeDirectiveController(tests.TestCaseWithTransport):
 
268
 
 
269
    def make_this_other_directive(self):
 
270
        this = self.make_branch_and_tree('this')
 
271
        this.commit('first commit')
 
272
        other = this.bzrdir.sprout('other').open_workingtree()
 
273
        self.build_tree_contents([('other/foo', 'bar')])
 
274
        other.add('foo')
 
275
        other.commit('second commit')
 
276
        other.lock_write()
 
277
        try:
 
278
            directive = MergeDirective2.from_objects(other.branch.repository,
 
279
                                                     other.last_revision(), 0,
 
280
                                                     0, 'this')
 
281
        finally:
 
282
            other.unlock()
 
283
        return this, other, directive
 
284
 
 
285
    def make_merged_window(self, directive):
 
286
        window = MockWindow()
 
287
        controller = MergeDirectiveController('directive', directive, window)
 
288
        controller.perform_merge(window)
 
289
        return window
 
290
 
 
291
    def test_perform_merge_success(self):
 
292
        this, other, directive = self.make_this_other_directive()
 
293
        window = self.make_merged_window(directive)
 
294
        self.assertTrue(window.merge_successful)
 
295
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
 
296
        self.assertFileEqual('bar', 'this/foo')
 
297
 
 
298
    def test_perform_merge_conflicts(self):
 
299
        this, other, directive = self.make_this_other_directive()
 
300
        self.build_tree_contents([('this/foo', 'bar')])
 
301
        this.add('foo')
 
302
        this.commit('message')
 
303
        window = self.make_merged_window(directive)
 
304
        self.assertFalse(window.merge_successful)
 
305
        self.assertTrue(window.conflicts)
 
306
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
 
307
        self.assertFileEqual('bar', 'this/foo')
 
308
 
 
309
    def test_perform_merge_uncommitted_changes(self):
 
310
        this, other, directive = self.make_this_other_directive()
 
311
        self.build_tree_contents([('this/foo', 'bar')])
 
312
        this.add('foo')
 
313
        window = self.make_merged_window(directive)
 
314
        self.assertIsInstance(window.handled_error, errors.UncommittedChanges)
 
315
 
 
316
 
 
317
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
 
318
 
 
319
    def assertStatusEqual(self, expected, tree):
 
320
        values = iter_changes_to_status(tree.basis_tree(), tree)
 
321
        self.assertEqual(expected, values)
 
322
 
 
323
    def test_status_added(self):
 
324
        tree = self.make_branch_and_tree('tree')
 
325
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
 
326
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
 
327
 
 
328
        self.assertStatusEqual(
 
329
            [('a-id', 'a', 'added', 'a'),
 
330
             ('b-id', 'b', 'added', 'b/'),
 
331
             ('c-id', 'b/c', 'added', 'b/c'),
 
332
            ], tree)
 
333
 
 
334
    def test_status_renamed(self):
 
335
        tree = self.make_branch_and_tree('tree')
 
336
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
 
337
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
 
338
        rev_id1 = tree.commit('one')
 
339
 
 
340
        tree.rename_one('b', 'd')
 
341
        tree.rename_one('a', 'd/a')
 
342
 
 
343
        self.assertStatusEqual(
 
344
            [('b-id', 'd', 'renamed', 'b/ => d/'),
 
345
             ('a-id', 'd/a', 'renamed', 'a => d/a'),
 
346
            ], tree)
 
347
 
 
348
    def test_status_modified(self):
 
349
        tree = self.make_branch_and_tree('tree')
 
350
        self.build_tree(['tree/a'])
 
351
        tree.add(['a'], ['a-id'])
 
352
        rev_id1 = tree.commit('one')
 
353
 
 
354
        self.build_tree_contents([('tree/a', 'new contents for a\n')])
 
355
 
 
356
        self.assertStatusEqual(
 
357
            [('a-id', 'a', 'modified', 'a'),
 
358
            ], tree)
 
359
 
 
360
    def test_status_renamed_and_modified(self):
 
361
        tree = self.make_branch_and_tree('tree')
 
362
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
 
363
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
 
364
        rev_id1 = tree.commit('one')
 
365
 
 
366
        tree.rename_one('b', 'd')
 
367
        tree.rename_one('a', 'd/a')
 
368
        self.build_tree_contents([('tree/d/a', 'new contents for a\n'),
 
369
                                  ('tree/d/c', 'new contents for c\n'),
 
370
                                 ])
 
371
        # 'c' is not considered renamed, because only its parent was moved, it
 
372
        # stayed in the same directory
 
373
 
 
374
        self.assertStatusEqual(
 
375
            [('b-id', 'd', 'renamed', 'b/ => d/'),
 
376
             ('a-id', 'd/a', 'renamed and modified', 'a => d/a'),
 
377
             ('c-id', 'd/c', 'modified', 'd/c'),
 
378
            ], tree)
 
379
 
 
380
    def test_status_kind_changed(self):
 
381
        tree = self.make_branch_and_tree('tree')
 
382
        self.build_tree(['tree/a', 'tree/b'])
 
383
        tree.add(['a', 'b'], ['a-id', 'b-id'])
 
384
        tree.commit('one')
 
385
 
 
386
        os.remove('tree/a')
 
387
        self.build_tree(['tree/a/'])
 
388
        # XXX:  This is technically valid, and the file list handles it fine,
 
389
        #       but 'show_diff_trees()' does not, so we skip this part of the
 
390
        #       test for now.
 
391
        # tree.rename_one('b', 'c')
 
392
        # os.remove('tree/c')
 
393
        # self.build_tree(['tree/c/'])
 
394
 
 
395
        self.assertStatusEqual(
 
396
            [('a-id', 'a', 'kind changed', 'a => a/'),
 
397
            # ('b-id', 'c', True, 'b => c/', 'renamed and modified'),
 
398
            ], tree)
 
399
 
 
400
    def test_status_removed(self):
 
401
        tree = self.make_branch_and_tree('tree')
 
402
        self.build_tree(['tree/a', 'tree/b/'])
 
403
        tree.add(['a', 'b'], ['a-id', 'b-id'])
 
404
        tree.commit('one')
 
405
 
 
406
        os.remove('tree/a')
 
407
        tree.remove('b', force=True)
 
408
 
 
409
        self.assertStatusEqual(
 
410
            [('a-id', 'a', 'removed', 'a'),
 
411
             ('b-id', 'b', 'removed', 'b/'),
 
412
            ], tree)
 
413
 
 
414
    def test_status_missing_file(self):
 
415
        this = self.make_branch_and_tree('this')
 
416
        self.build_tree(['this/foo'])
 
417
        this.add(['foo'], ['foo-id'])
 
418
        this.commit('add')
 
419
 
 
420
        other = this.bzrdir.sprout('other').open_workingtree()
 
421
 
 
422
        os.remove('this/foo')
 
423
        this.remove('foo', force=True)
 
424
        this.commit('remove')
 
425
 
 
426
        f = open('other/foo', 'wt')
 
427
        try:
 
428
            f.write('Modified\n')
 
429
        finally:
 
430
            f.close()
 
431
        other.commit('modified')
 
432
 
 
433
        this.merge_from_branch(other.branch)
 
434
        conflicts.resolve(this)
 
435
 
 
436
        self.assertStatusEqual(
 
437
            [('foo-id', 'foo.OTHER', 'missing', 'foo.OTHER'),],
 
438
            this)
 
439
 
 
440
    def test_status_missing_directory(self):
 
441
        this = self.make_branch_and_tree('this')
 
442
        self.build_tree(['this/foo/', 'this/foo/bar'])
 
443
        this.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
 
444
        this.commit('add')
 
445
 
 
446
        other = this.bzrdir.sprout('other').open_workingtree()
 
447
 
 
448
        os.remove('this/foo/bar')
 
449
        os.rmdir('this/foo')
 
450
        this.remove('foo', force=True)
 
451
        this.commit('remove')
 
452
 
 
453
        f = open('other/foo/bar', 'wt')
 
454
        try:
 
455
            f.write('Modified\n')
 
456
        finally:
 
457
            f.close()
 
458
        other.commit('modified')
 
459
 
 
460
        this.merge_from_branch(other.branch)
 
461
        conflicts.resolve(this)
 
462
 
 
463
        self.assertStatusEqual(
 
464
            [('foo-id', u'foo', 'added', u'foo/'),
 
465
             ('bar-id', u'foo/bar.OTHER', 'missing', u'foo/bar.OTHER'),],
 
466
            this)