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