/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
232.1.4 by Adeodato Simó
Add a test for parse_colordiffrc, and fix the function to handle empty lines.
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
from cStringIO import StringIO
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
19
import os
232.1.4 by Adeodato Simó
Add a test for parse_colordiffrc, and fix the function to handle empty lines.
20
771.1.5 by Curtis Hovey
Added test for success.
21
from gi.repository import Gtk
22
613.1.1 by Vincent Ladeuil
Fix bug #286834 bu handling 'missing' files corner case.
23
from bzrlib import (
24
    conflicts,
25
    errors,
26
    tests,
27
    )
737 by Jelmer Vernooij
Support new location of UnicodeFilenameFeature in 2.5.
28
try:
29
    from bzrlib.tests.features import UnicodeFilenameFeature
30
except ImportError: # bzr < 2.5
31
    from bzrlib.tests import UnicodeFilenameFeature
493.1.1 by Aaron Bentley
Work around bug for old merge directives in bzr.dev
32
from bzrlib.merge_directive import MergeDirective2
278.1.13 by John Arbash Meinel
minor cleanup
33
487.2.4 by Aaron Bentley
Add tests for DiffController
34
from bzrlib.plugins.gtk.diff import (
35
    DiffController,
36
    DiffView,
771.1.4 by Curtis Hovey
Do not update diff_view when the treeview is being destroyed.
37
    DiffWidget,
772.2.1 by Curtis Hovey
Added test for DiffWindow._get_button_bar.
38
    DiffWindow,
487.2.4 by Aaron Bentley
Add tests for DiffController
39
    iter_changes_to_status,
487.2.5 by Aaron Bentley
Test successful merge
40
    MergeDirectiveController,
487.2.4 by Aaron Bentley
Add tests for DiffController
41
    )
771.1.4 by Curtis Hovey
Do not update diff_view when the treeview is being destroyed.
42
from bzrlib.plugins.gtk.tests import MockMethod
43
44
487.2.4 by Aaron Bentley
Add tests for DiffController
45
eg_diff = """\
46
=== modified file 'tests/test_diff.py'
47
--- tests/test_diff.py	2008-03-11 13:18:28 +0000
48
+++ tests/test_diff.py	2008-05-08 22:44:02 +0000
49
@@ -20,7 +20,11 @@
50
 
51
 from bzrlib import tests
52
 
53
-from bzrlib.plugins.gtk.diff import DiffView, iter_changes_to_status
54
+from bzrlib.plugins.gtk.diff import (
55
+    DiffController,
56
+    DiffView,
57
+    iter_changes_to_status,
58
+    )
59
 
60
 
61
 class TestDiffViewSimple(tests.TestCase):
62
"""
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
63
64
class TestDiffViewSimple(tests.TestCase):
278.1.13 by John Arbash Meinel
minor cleanup
65
232.1.4 by Adeodato Simó
Add a test for parse_colordiffrc, and fix the function to handle empty lines.
66
    def test_parse_colordiffrc(self):
67
        colordiffrc = '''\
68
newtext=blue
69
oldtext = Red
70
# now a comment and a blank line
71
72
diffstuff = #ffff00  
73
  # another comment preceded by whitespace
74
'''
75
        colors = {
76
                'newtext': 'blue',
77
                'oldtext': 'Red',
78
                'diffstuff': '#ffff00',
79
        }
278.1.12 by John Arbash Meinel
Delay computing the delta, and clean up some of the diff view names.
80
        parsed_colors = DiffView.parse_colordiffrc(StringIO(colordiffrc))
232.1.4 by Adeodato Simó
Add a test for parse_colordiffrc, and fix the function to handle empty lines.
81
        self.assertEqual(colors, parsed_colors)
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
82
83
84
class TestDiffView(tests.TestCaseWithTransport):
85
86
    def test_unicode(self):
737 by Jelmer Vernooij
Support new location of UnicodeFilenameFeature in 2.5.
87
        self.requireFeature(UnicodeFilenameFeature)
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
88
89
        tree = self.make_branch_and_tree('tree')
90
        self.build_tree([u'tree/\u03a9'])
91
        tree.add([u'\u03a9'], ['omega-id'])
92
93
        view = DiffView()
94
        view.set_trees(tree, tree.basis_tree())
95
        view.show_diff(None)
96
        buf = view.buffer
97
        start, end = buf.get_bounds()
734.1.7 by Curtis Hovey
Updated buffer.getText() calls and ModifierType enums.
98
        text = buf.get_text(start, end, True)
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
99
        self.assertContainsRe(text,
100
            "=== added file '\xce\xa9'\n"
101
            '--- .*\t1970-01-01 00:00:00 \\+0000\n'
102
            r'\+\+\+ .*\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d [+-]\d\d\d\d\n'
103
            '@@ -0,0 \\+1,1 @@\n'
104
            '\\+contents of tree/\xce\xa9\n'
105
            '\n'
106
            )
107
108
771.1.4 by Curtis Hovey
Do not update diff_view when the treeview is being destroyed.
109
110
class FakeDiffWidget(DiffWidget):
111
112
    SHOW_WIDGETS = False
113
114
115
class TestDiffWidget(tests.TestCaseWithTransport):
116
771.1.5 by Curtis Hovey
Added test for success.
117
    def test_treeview_cursor_cb(self):
118
        widget = FakeDiffWidget()
119
        widget.set_diff_text_sections(
771.1.6 by Curtis Hovey
Update test to verify that the correct text is shown.
120
            [('', None, 'patch1'), ('a', 'a', 'patch2')])
771.1.5 by Curtis Hovey
Added test for success.
121
        widget.treeview.set_cursor(Gtk.TreePath(path=1), None, False)
122
        widget._treeview_cursor_cb(None)
771.1.6 by Curtis Hovey
Update test to verify that the correct text is shown.
123
        self.assertTrue('patch2', widget.diff_view.buffer.props.text)
771.1.5 by Curtis Hovey
Added test for success.
124
125
    def test_treeview_cursor_cb_with_destroyed_treeview(self):
126
        widget = FakeDiffWidget()
127
        widget.set_diff_text_sections(
771.1.6 by Curtis Hovey
Update test to verify that the correct text is shown.
128
            [('', None, 'patch1'), ('a', 'a', 'patch2')])
771.1.4 by Curtis Hovey
Do not update diff_view when the treeview is being destroyed.
129
        MockMethod.bind(self, widget.diff_view, 'show_diff')
130
        widget.treeview.destroy()
131
        widget._treeview_cursor_cb(None)
132
        self.assertFalse(widget.diff_view.show_diff.called)
133
134
772.2.1 by Curtis Hovey
Added test for DiffWindow._get_button_bar.
135
class FakeDiffWindow(DiffWindow):
136
137
    SHOW_WIDGETS = False
138
139
140
class DiffWindowTestCase(tests.TestCaseWithTransport):
141
772.2.2 by Curtis Hovey
show_all() instead of individual calls.
142
    def test_init(self):
143
        window = DiffWindow()
144
        self.assertEqual('bzr diff', window.props.title)
145
        self.assertEqual(0, window.props.border_width)
146
147
    def test_init_construct_without_operations(self):
148
        window = DiffWindow()
149
        widgets = window.vbox.get_children()
150
        self.assertEqual(2, len(widgets))
151
        self.assertIsInstance(widgets[0], Gtk.MenuBar)
152
        self.assertIsInstance(widgets[1], DiffWidget)
153
154
    def test_init_construct_with_operations(self):
155
        method = MockMethod()
156
        window = DiffWindow(operations=[('title', method)])
157
        widgets = window.vbox.get_children()
158
        self.assertEqual(3, len(widgets))
159
        self.assertIsInstance(widgets[0], Gtk.MenuBar)
160
        self.assertIsInstance(widgets[1], Gtk.HButtonBox)
161
        self.assertIsInstance(widgets[2], DiffWidget)
162
163
    def test_get_menu_bar(self):
164
        window = DiffWindow()
165
        menu_bar = window._get_menu_bar()
166
        self.assertIsNot(None, menu_bar)
167
        menus = menu_bar.get_children()
168
        self.assertEqual(1, len(menus))
169
        self.assertEqual('_View', menus[0].props.label)
170
        sub_menu = menus[0].get_submenu()
171
        self.assertIsNot(None, sub_menu)
172
        items = sub_menu.get_children()
173
        self.assertEqual(1, len(items))
174
        menus[0].get_submenu().get_children()[0].props.label
175
        self.assertEqual('Wrap _Long Lines', items[0].props.label)
176
772.2.1 by Curtis Hovey
Added test for DiffWindow._get_button_bar.
177
    def test_get_button_bar_with_none(self):
178
        window = DiffWindow()
179
        self.assertIs(None, window._get_button_bar(None))
180
181
    def test_get_button_bar_with_operations(self):
182
        window = DiffWindow()
183
        method = MockMethod()
184
        button_bar = window._get_button_bar([('title', method)])
185
        self.assertIsNot(None, button_bar)
186
        buttons = button_bar.get_children()
187
        self.assertEqual(1, len(buttons))
188
        self.assertEqual('title', buttons[0].props.label)
189
        buttons[0].emit('clicked')
190
        self.assertIs(True, method.called)
191
192
772.2.2 by Curtis Hovey
show_all() instead of individual calls.
193
487.2.4 by Aaron Bentley
Add tests for DiffController
194
class MockDiffWidget(object):
195
196
    def set_diff_text_sections(self, sections):
197
        self.sections = list(sections)
198
199
200
class MockWindow(object):
713 by Jelmer Vernooij
Remove some unused imports, fix some formatting.
201
487.2.4 by Aaron Bentley
Add tests for DiffController
202
    def __init__(self):
203
        self.diff = MockDiffWidget()
487.2.7 by Aaron Bentley
Add more merge tests
204
        self.merge_successful = False
487.2.4 by Aaron Bentley
Add tests for DiffController
205
206
    def set_title(self, title):
207
        self.title = title
208
209
    def _get_save_path(self, basename):
210
        return 'save-path'
211
487.2.5 by Aaron Bentley
Test successful merge
212
    def _get_merge_target(self):
487.2.6 by Aaron Bentley
Ensure MergeController sets pending merges and updates files
213
        return 'this'
487.2.5 by Aaron Bentley
Test successful merge
214
215
    def destroy(self):
216
        pass
217
218
    def _merge_successful(self):
219
        self.merge_successful = True
220
487.2.7 by Aaron Bentley
Add more merge tests
221
    def _conflicts(self):
222
        self.conflicts = True
223
487.2.8 by Aaron Bentley
Update error handling to use window
224
    def _handle_error(self, e):
225
        self.handled_error = e
226
487.2.4 by Aaron Bentley
Add tests for DiffController
227
228
class TestDiffController(tests.TestCaseWithTransport):
229
230
    def get_controller(self):
231
        window = MockWindow()
232
        return DiffController('load-path', eg_diff.splitlines(True), window)
233
234
    def test_get_diff_sections(self):
235
        controller = self.get_controller()
236
        controller = DiffController('.', eg_diff.splitlines(True),
237
                                    controller.window)
238
        sections = list(controller.get_diff_sections())
239
        self.assertEqual('Complete Diff', sections[0][0])
240
        self.assertIs(None, sections[0][1])
241
        self.assertEqual(eg_diff, sections[0][2])
242
243
        self.assertEqual('tests/test_diff.py', sections[1][0])
244
        self.assertEqual('tests/test_diff.py', sections[1][1])
245
        self.assertEqual(''.join(eg_diff.splitlines(True)[1:]),
246
                         sections[1][2])
247
248
    def test_initialize_window(self):
249
        controller = self.get_controller()
250
        controller.initialize_window(controller.window)
251
        self.assertEqual(2, len(controller.window.diff.sections))
252
        self.assertEqual('load-path - diff', controller.window.title)
253
254
    def test_perform_save(self):
255
        self.build_tree_contents([('load-path', 'foo')])
256
        controller = self.get_controller()
257
        controller.perform_save(None)
258
        self.assertFileEqual('foo', 'save-path')
259
260
487.2.5 by Aaron Bentley
Test successful merge
261
class TestMergeDirectiveController(tests.TestCaseWithTransport):
262
487.2.7 by Aaron Bentley
Add more merge tests
263
    def make_this_other_directive(self):
487.2.5 by Aaron Bentley
Test successful merge
264
        this = self.make_branch_and_tree('this')
265
        this.commit('first commit')
266
        other = this.bzrdir.sprout('other').open_workingtree()
487.2.7 by Aaron Bentley
Add more merge tests
267
        self.build_tree_contents([('other/foo', 'bar')])
268
        other.add('foo')
487.2.5 by Aaron Bentley
Test successful merge
269
        other.commit('second commit')
270
        other.lock_write()
271
        try:
493.1.1 by Aaron Bentley
Work around bug for old merge directives in bzr.dev
272
            directive = MergeDirective2.from_objects(other.branch.repository,
273
                                                     other.last_revision(), 0,
274
                                                     0, 'this')
487.2.5 by Aaron Bentley
Test successful merge
275
        finally:
276
            other.unlock()
487.2.7 by Aaron Bentley
Add more merge tests
277
        return this, other, directive
278
279
    def make_merged_window(self, directive):
487.2.5 by Aaron Bentley
Test successful merge
280
        window = MockWindow()
281
        controller = MergeDirectiveController('directive', directive, window)
282
        controller.perform_merge(window)
487.2.7 by Aaron Bentley
Add more merge tests
283
        return window
284
285
    def test_perform_merge_success(self):
286
        this, other, directive = self.make_this_other_directive()
287
        window = self.make_merged_window(directive)
487.2.5 by Aaron Bentley
Test successful merge
288
        self.assertTrue(window.merge_successful)
487.2.6 by Aaron Bentley
Ensure MergeController sets pending merges and updates files
289
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
487.2.7 by Aaron Bentley
Add more merge tests
290
        self.assertFileEqual('bar', 'this/foo')
291
292
    def test_perform_merge_conflicts(self):
293
        this, other, directive = self.make_this_other_directive()
294
        self.build_tree_contents([('this/foo', 'bar')])
295
        this.add('foo')
296
        this.commit('message')
297
        window = self.make_merged_window(directive)
298
        self.assertFalse(window.merge_successful)
299
        self.assertTrue(window.conflicts)
300
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
301
        self.assertFileEqual('bar', 'this/foo')
487.2.5 by Aaron Bentley
Test successful merge
302
487.2.8 by Aaron Bentley
Update error handling to use window
303
    def test_perform_merge_uncommitted_changes(self):
304
        this, other, directive = self.make_this_other_directive()
305
        self.build_tree_contents([('this/foo', 'bar')])
306
        this.add('foo')
307
        window = self.make_merged_window(directive)
308
        self.assertIsInstance(window.handled_error, errors.UncommittedChanges)
309
487.2.5 by Aaron Bentley
Test successful merge
310
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
311
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
312
313
    def assertStatusEqual(self, expected, tree):
450 by Aaron Bentley
Update to use new Tree.iter_changes
314
        values = iter_changes_to_status(tree.basis_tree(), tree)
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
315
        self.assertEqual(expected, values)
316
317
    def test_status_added(self):
318
        tree = self.make_branch_and_tree('tree')
319
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
320
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
321
322
        self.assertStatusEqual(
323
            [('a-id', 'a', 'added', 'a'),
324
             ('b-id', 'b', 'added', 'b/'),
325
             ('c-id', 'b/c', 'added', 'b/c'),
326
            ], tree)
327
328
    def test_status_renamed(self):
329
        tree = self.make_branch_and_tree('tree')
330
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
331
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
332
        rev_id1 = tree.commit('one')
333
334
        tree.rename_one('b', 'd')
335
        tree.rename_one('a', 'd/a')
336
337
        self.assertStatusEqual(
338
            [('b-id', 'd', 'renamed', 'b/ => d/'),
339
             ('a-id', 'd/a', 'renamed', 'a => d/a'),
340
            ], tree)
341
342
    def test_status_modified(self):
343
        tree = self.make_branch_and_tree('tree')
344
        self.build_tree(['tree/a'])
345
        tree.add(['a'], ['a-id'])
346
        rev_id1 = tree.commit('one')
347
348
        self.build_tree_contents([('tree/a', 'new contents for a\n')])
349
350
        self.assertStatusEqual(
351
            [('a-id', 'a', 'modified', 'a'),
352
            ], tree)
353
354
    def test_status_renamed_and_modified(self):
355
        tree = self.make_branch_and_tree('tree')
356
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
357
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
358
        rev_id1 = tree.commit('one')
359
360
        tree.rename_one('b', 'd')
361
        tree.rename_one('a', 'd/a')
362
        self.build_tree_contents([('tree/d/a', 'new contents for a\n'),
363
                                  ('tree/d/c', 'new contents for c\n'),
364
                                 ])
365
        # 'c' is not considered renamed, because only its parent was moved, it
366
        # stayed in the same directory
367
368
        self.assertStatusEqual(
369
            [('b-id', 'd', 'renamed', 'b/ => d/'),
370
             ('a-id', 'd/a', 'renamed and modified', 'a => d/a'),
371
             ('c-id', 'd/c', 'modified', 'd/c'),
372
            ], tree)
373
374
    def test_status_kind_changed(self):
375
        tree = self.make_branch_and_tree('tree')
376
        self.build_tree(['tree/a', 'tree/b'])
377
        tree.add(['a', 'b'], ['a-id', 'b-id'])
378
        tree.commit('one')
379
380
        os.remove('tree/a')
381
        self.build_tree(['tree/a/'])
382
        # XXX:  This is technically valid, and the file list handles it fine,
383
        #       but 'show_diff_trees()' does not, so we skip this part of the
384
        #       test for now.
385
        # tree.rename_one('b', 'c')
386
        # os.remove('tree/c')
387
        # self.build_tree(['tree/c/'])
388
389
        self.assertStatusEqual(
390
            [('a-id', 'a', 'kind changed', 'a => a/'),
391
            # ('b-id', 'c', True, 'b => c/', 'renamed and modified'),
392
            ], tree)
393
394
    def test_status_removed(self):
395
        tree = self.make_branch_and_tree('tree')
396
        self.build_tree(['tree/a', 'tree/b/'])
397
        tree.add(['a', 'b'], ['a-id', 'b-id'])
398
        tree.commit('one')
399
400
        os.remove('tree/a')
401
        tree.remove('b', force=True)
402
403
        self.assertStatusEqual(
404
            [('a-id', 'a', 'removed', 'a'),
405
             ('b-id', 'b', 'removed', 'b/'),
406
            ], tree)
613.1.1 by Vincent Ladeuil
Fix bug #286834 bu handling 'missing' files corner case.
407
408
    def test_status_missing_file(self):
409
        this = self.make_branch_and_tree('this')
410
        self.build_tree(['this/foo'])
411
        this.add(['foo'], ['foo-id'])
412
        this.commit('add')
413
414
        other = this.bzrdir.sprout('other').open_workingtree()
415
416
        os.remove('this/foo')
417
        this.remove('foo', force=True)
418
        this.commit('remove')
419
420
        f = open('other/foo', 'wt')
421
        try:
422
            f.write('Modified\n')
423
        finally:
424
            f.close()
425
        other.commit('modified')
426
427
        this.merge_from_branch(other.branch)
428
        conflicts.resolve(this)
429
430
        self.assertStatusEqual(
431
            [('foo-id', 'foo.OTHER', 'missing', 'foo.OTHER'),],
432
            this)
433
434
    def test_status_missing_directory(self):
435
        this = self.make_branch_and_tree('this')
436
        self.build_tree(['this/foo/', 'this/foo/bar'])
437
        this.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
438
        this.commit('add')
439
440
        other = this.bzrdir.sprout('other').open_workingtree()
441
442
        os.remove('this/foo/bar')
443
        os.rmdir('this/foo')
444
        this.remove('foo', force=True)
445
        this.commit('remove')
446
447
        f = open('other/foo/bar', 'wt')
448
        try:
449
            f.write('Modified\n')
450
        finally:
451
            f.close()
452
        other.commit('modified')
453
454
        this.merge_from_branch(other.branch)
455
        conflicts.resolve(this)
456
457
        self.assertStatusEqual(
458
            [('foo-id', u'foo', 'added', u'foo/'),
459
             ('bar-id', u'foo/bar.OTHER', 'missing', u'foo/bar.OTHER'),],
460
            this)