/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

  • Committer: Jelmer Vernooij
  • Date: 2006-09-30 10:21:43 UTC
  • Revision ID: jelmer@samba.org-20060930102143-c0ef64d6ca860c21
Merge some files from Olive and bzr-gtk.

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
 
from cStringIO import StringIO
19
 
import os
20
 
 
21
 
from bzrlib import (
22
 
    conflicts,
23
 
    errors,
24
 
    tests,
25
 
    )
26
 
try:
27
 
    from bzrlib.tests.features import UnicodeFilenameFeature
28
 
except ImportError: # bzr < 2.5
29
 
    from bzrlib.tests import UnicodeFilenameFeature
30
 
from bzrlib.merge_directive import MergeDirective2
31
 
 
32
 
from bzrlib.plugins.gtk.diff import (
33
 
    DiffController,
34
 
    DiffView,
35
 
    iter_changes_to_status,
36
 
    MergeDirectiveController,
37
 
    )
38
 
eg_diff = """\
39
 
=== modified file 'tests/test_diff.py'
40
 
--- tests/test_diff.py  2008-03-11 13:18:28 +0000
41
 
+++ tests/test_diff.py  2008-05-08 22:44:02 +0000
42
 
@@ -20,7 +20,11 @@
43
 
 
44
 
 from bzrlib import tests
45
 
 
46
 
-from bzrlib.plugins.gtk.diff import DiffView, iter_changes_to_status
47
 
+from bzrlib.plugins.gtk.diff import (
48
 
+    DiffController,
49
 
+    DiffView,
50
 
+    iter_changes_to_status,
51
 
+    )
52
 
 
53
 
 
54
 
 class TestDiffViewSimple(tests.TestCase):
55
 
"""
56
 
 
57
 
class TestDiffViewSimple(tests.TestCase):
58
 
 
59
 
    def test_parse_colordiffrc(self):
60
 
        colordiffrc = '''\
61
 
newtext=blue
62
 
oldtext = Red
63
 
# now a comment and a blank line
64
 
 
65
 
diffstuff = #ffff00  
66
 
  # another comment preceded by whitespace
67
 
'''
68
 
        colors = {
69
 
                'newtext': 'blue',
70
 
                'oldtext': 'Red',
71
 
                'diffstuff': '#ffff00',
72
 
        }
73
 
        parsed_colors = DiffView.parse_colordiffrc(StringIO(colordiffrc))
74
 
        self.assertEqual(colors, parsed_colors)
75
 
 
76
 
 
77
 
class TestDiffView(tests.TestCaseWithTransport):
78
 
 
79
 
    def test_unicode(self):
80
 
        self.requireFeature(UnicodeFilenameFeature)
81
 
 
82
 
        tree = self.make_branch_and_tree('tree')
83
 
        self.build_tree([u'tree/\u03a9'])
84
 
        tree.add([u'\u03a9'], ['omega-id'])
85
 
 
86
 
        view = DiffView()
87
 
        view.set_trees(tree, tree.basis_tree())
88
 
        view.show_diff(None)
89
 
        buf = view.buffer
90
 
        start, end = buf.get_bounds()
91
 
        text = buf.get_text(start, end, True)
92
 
        self.assertContainsRe(text,
93
 
            "=== added file '\xce\xa9'\n"
94
 
            '--- .*\t1970-01-01 00:00:00 \\+0000\n'
95
 
            r'\+\+\+ .*\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d [+-]\d\d\d\d\n'
96
 
            '@@ -0,0 \\+1,1 @@\n'
97
 
            '\\+contents of tree/\xce\xa9\n'
98
 
            '\n'
99
 
            )
100
 
 
101
 
 
102
 
class MockDiffWidget(object):
103
 
 
104
 
    def set_diff_text_sections(self, sections):
105
 
        self.sections = list(sections)
106
 
 
107
 
 
108
 
class MockWindow(object):
109
 
 
110
 
    def __init__(self):
111
 
        self.diff = MockDiffWidget()
112
 
        self.merge_successful = False
113
 
 
114
 
    def set_title(self, title):
115
 
        self.title = title
116
 
 
117
 
    def _get_save_path(self, basename):
118
 
        return 'save-path'
119
 
 
120
 
    def _get_merge_target(self):
121
 
        return 'this'
122
 
 
123
 
    def destroy(self):
124
 
        pass
125
 
 
126
 
    def _merge_successful(self):
127
 
        self.merge_successful = True
128
 
 
129
 
    def _conflicts(self):
130
 
        self.conflicts = True
131
 
 
132
 
    def _handle_error(self, e):
133
 
        self.handled_error = e
134
 
 
135
 
 
136
 
class TestDiffController(tests.TestCaseWithTransport):
137
 
 
138
 
    def get_controller(self):
139
 
        window = MockWindow()
140
 
        return DiffController('load-path', eg_diff.splitlines(True), window)
141
 
 
142
 
    def test_get_diff_sections(self):
143
 
        controller = self.get_controller()
144
 
        controller = DiffController('.', eg_diff.splitlines(True),
145
 
                                    controller.window)
146
 
        sections = list(controller.get_diff_sections())
147
 
        self.assertEqual('Complete Diff', sections[0][0])
148
 
        self.assertIs(None, sections[0][1])
149
 
        self.assertEqual(eg_diff, sections[0][2])
150
 
 
151
 
        self.assertEqual('tests/test_diff.py', sections[1][0])
152
 
        self.assertEqual('tests/test_diff.py', sections[1][1])
153
 
        self.assertEqual(''.join(eg_diff.splitlines(True)[1:]),
154
 
                         sections[1][2])
155
 
 
156
 
    def test_initialize_window(self):
157
 
        controller = self.get_controller()
158
 
        controller.initialize_window(controller.window)
159
 
        self.assertEqual(2, len(controller.window.diff.sections))
160
 
        self.assertEqual('load-path - diff', controller.window.title)
161
 
 
162
 
    def test_perform_save(self):
163
 
        self.build_tree_contents([('load-path', 'foo')])
164
 
        controller = self.get_controller()
165
 
        controller.perform_save(None)
166
 
        self.assertFileEqual('foo', 'save-path')
167
 
 
168
 
 
169
 
class TestMergeDirectiveController(tests.TestCaseWithTransport):
170
 
 
171
 
    def make_this_other_directive(self):
172
 
        this = self.make_branch_and_tree('this')
173
 
        this.commit('first commit')
174
 
        other = this.bzrdir.sprout('other').open_workingtree()
175
 
        self.build_tree_contents([('other/foo', 'bar')])
176
 
        other.add('foo')
177
 
        other.commit('second commit')
178
 
        other.lock_write()
179
 
        try:
180
 
            directive = MergeDirective2.from_objects(other.branch.repository,
181
 
                                                     other.last_revision(), 0,
182
 
                                                     0, 'this')
183
 
        finally:
184
 
            other.unlock()
185
 
        return this, other, directive
186
 
 
187
 
    def make_merged_window(self, directive):
188
 
        window = MockWindow()
189
 
        controller = MergeDirectiveController('directive', directive, window)
190
 
        controller.perform_merge(window)
191
 
        return window
192
 
 
193
 
    def test_perform_merge_success(self):
194
 
        this, other, directive = self.make_this_other_directive()
195
 
        window = self.make_merged_window(directive)
196
 
        self.assertTrue(window.merge_successful)
197
 
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
198
 
        self.assertFileEqual('bar', 'this/foo')
199
 
 
200
 
    def test_perform_merge_conflicts(self):
201
 
        this, other, directive = self.make_this_other_directive()
202
 
        self.build_tree_contents([('this/foo', 'bar')])
203
 
        this.add('foo')
204
 
        this.commit('message')
205
 
        window = self.make_merged_window(directive)
206
 
        self.assertFalse(window.merge_successful)
207
 
        self.assertTrue(window.conflicts)
208
 
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
209
 
        self.assertFileEqual('bar', 'this/foo')
210
 
 
211
 
    def test_perform_merge_uncommitted_changes(self):
212
 
        this, other, directive = self.make_this_other_directive()
213
 
        self.build_tree_contents([('this/foo', 'bar')])
214
 
        this.add('foo')
215
 
        window = self.make_merged_window(directive)
216
 
        self.assertIsInstance(window.handled_error, errors.UncommittedChanges)
217
 
 
218
 
 
219
 
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
220
 
 
221
 
    def assertStatusEqual(self, expected, tree):
222
 
        values = iter_changes_to_status(tree.basis_tree(), tree)
223
 
        self.assertEqual(expected, values)
224
 
 
225
 
    def test_status_added(self):
226
 
        tree = self.make_branch_and_tree('tree')
227
 
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
228
 
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
229
 
 
230
 
        self.assertStatusEqual(
231
 
            [('a-id', 'a', 'added', 'a'),
232
 
             ('b-id', 'b', 'added', 'b/'),
233
 
             ('c-id', 'b/c', 'added', 'b/c'),
234
 
            ], tree)
235
 
 
236
 
    def test_status_renamed(self):
237
 
        tree = self.make_branch_and_tree('tree')
238
 
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
239
 
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
240
 
        rev_id1 = tree.commit('one')
241
 
 
242
 
        tree.rename_one('b', 'd')
243
 
        tree.rename_one('a', 'd/a')
244
 
 
245
 
        self.assertStatusEqual(
246
 
            [('b-id', 'd', 'renamed', 'b/ => d/'),
247
 
             ('a-id', 'd/a', 'renamed', 'a => d/a'),
248
 
            ], tree)
249
 
 
250
 
    def test_status_modified(self):
251
 
        tree = self.make_branch_and_tree('tree')
252
 
        self.build_tree(['tree/a'])
253
 
        tree.add(['a'], ['a-id'])
254
 
        rev_id1 = tree.commit('one')
255
 
 
256
 
        self.build_tree_contents([('tree/a', 'new contents for a\n')])
257
 
 
258
 
        self.assertStatusEqual(
259
 
            [('a-id', 'a', 'modified', 'a'),
260
 
            ], tree)
261
 
 
262
 
    def test_status_renamed_and_modified(self):
263
 
        tree = self.make_branch_and_tree('tree')
264
 
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
265
 
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
266
 
        rev_id1 = tree.commit('one')
267
 
 
268
 
        tree.rename_one('b', 'd')
269
 
        tree.rename_one('a', 'd/a')
270
 
        self.build_tree_contents([('tree/d/a', 'new contents for a\n'),
271
 
                                  ('tree/d/c', 'new contents for c\n'),
272
 
                                 ])
273
 
        # 'c' is not considered renamed, because only its parent was moved, it
274
 
        # stayed in the same directory
275
 
 
276
 
        self.assertStatusEqual(
277
 
            [('b-id', 'd', 'renamed', 'b/ => d/'),
278
 
             ('a-id', 'd/a', 'renamed and modified', 'a => d/a'),
279
 
             ('c-id', 'd/c', 'modified', 'd/c'),
280
 
            ], tree)
281
 
 
282
 
    def test_status_kind_changed(self):
283
 
        tree = self.make_branch_and_tree('tree')
284
 
        self.build_tree(['tree/a', 'tree/b'])
285
 
        tree.add(['a', 'b'], ['a-id', 'b-id'])
286
 
        tree.commit('one')
287
 
 
288
 
        os.remove('tree/a')
289
 
        self.build_tree(['tree/a/'])
290
 
        # XXX:  This is technically valid, and the file list handles it fine,
291
 
        #       but 'show_diff_trees()' does not, so we skip this part of the
292
 
        #       test for now.
293
 
        # tree.rename_one('b', 'c')
294
 
        # os.remove('tree/c')
295
 
        # self.build_tree(['tree/c/'])
296
 
 
297
 
        self.assertStatusEqual(
298
 
            [('a-id', 'a', 'kind changed', 'a => a/'),
299
 
            # ('b-id', 'c', True, 'b => c/', 'renamed and modified'),
300
 
            ], tree)
301
 
 
302
 
    def test_status_removed(self):
303
 
        tree = self.make_branch_and_tree('tree')
304
 
        self.build_tree(['tree/a', 'tree/b/'])
305
 
        tree.add(['a', 'b'], ['a-id', 'b-id'])
306
 
        tree.commit('one')
307
 
 
308
 
        os.remove('tree/a')
309
 
        tree.remove('b', force=True)
310
 
 
311
 
        self.assertStatusEqual(
312
 
            [('a-id', 'a', 'removed', 'a'),
313
 
             ('b-id', 'b', 'removed', 'b/'),
314
 
            ], tree)
315
 
 
316
 
    def test_status_missing_file(self):
317
 
        this = self.make_branch_and_tree('this')
318
 
        self.build_tree(['this/foo'])
319
 
        this.add(['foo'], ['foo-id'])
320
 
        this.commit('add')
321
 
 
322
 
        other = this.bzrdir.sprout('other').open_workingtree()
323
 
 
324
 
        os.remove('this/foo')
325
 
        this.remove('foo', force=True)
326
 
        this.commit('remove')
327
 
 
328
 
        f = open('other/foo', 'wt')
329
 
        try:
330
 
            f.write('Modified\n')
331
 
        finally:
332
 
            f.close()
333
 
        other.commit('modified')
334
 
 
335
 
        this.merge_from_branch(other.branch)
336
 
        conflicts.resolve(this)
337
 
 
338
 
        self.assertStatusEqual(
339
 
            [('foo-id', 'foo.OTHER', 'missing', 'foo.OTHER'),],
340
 
            this)
341
 
 
342
 
    def test_status_missing_directory(self):
343
 
        this = self.make_branch_and_tree('this')
344
 
        self.build_tree(['this/foo/', 'this/foo/bar'])
345
 
        this.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
346
 
        this.commit('add')
347
 
 
348
 
        other = this.bzrdir.sprout('other').open_workingtree()
349
 
 
350
 
        os.remove('this/foo/bar')
351
 
        os.rmdir('this/foo')
352
 
        this.remove('foo', force=True)
353
 
        this.commit('remove')
354
 
 
355
 
        f = open('other/foo/bar', 'wt')
356
 
        try:
357
 
            f.write('Modified\n')
358
 
        finally:
359
 
            f.close()
360
 
        other.commit('modified')
361
 
 
362
 
        this.merge_from_branch(other.branch)
363
 
        conflicts.resolve(this)
364
 
 
365
 
        self.assertStatusEqual(
366
 
            [('foo-id', u'foo', 'added', u'foo/'),
367
 
             ('bar-id', u'foo/bar.OTHER', 'missing', u'foo/bar.OTHER'),],
368
 
            this)