/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-05-19 16:56:46 UTC
  • mfrom: (0.1.25 gannotate)
  • Revision ID: jelmer@samba.org-20060519165646-0d867938fdbc9097
Merge in Dan Loda's gannotate plugin and put it in annotate/

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