/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
487.2.8 by Aaron Bentley
Update error handling to use window
21
from bzrlib import errors, tests
493.1.1 by Aaron Bentley
Work around bug for old merge directives in bzr.dev
22
from bzrlib.merge_directive import MergeDirective2
278.1.13 by John Arbash Meinel
minor cleanup
23
487.2.4 by Aaron Bentley
Add tests for DiffController
24
from bzrlib.plugins.gtk.diff import (
25
    DiffController,
26
    DiffView,
27
    iter_changes_to_status,
487.2.5 by Aaron Bentley
Test successful merge
28
    MergeDirectiveController,
487.2.4 by Aaron Bentley
Add tests for DiffController
29
    )
30
eg_diff = """\
31
=== modified file 'tests/test_diff.py'
32
--- tests/test_diff.py	2008-03-11 13:18:28 +0000
33
+++ tests/test_diff.py	2008-05-08 22:44:02 +0000
34
@@ -20,7 +20,11 @@
35
 
36
 from bzrlib import tests
37
 
38
-from bzrlib.plugins.gtk.diff import DiffView, iter_changes_to_status
39
+from bzrlib.plugins.gtk.diff import (
40
+    DiffController,
41
+    DiffView,
42
+    iter_changes_to_status,
43
+    )
44
 
45
 
46
 class TestDiffViewSimple(tests.TestCase):
47
"""
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
48
49
class TestDiffViewSimple(tests.TestCase):
278.1.13 by John Arbash Meinel
minor cleanup
50
232.1.4 by Adeodato Simó
Add a test for parse_colordiffrc, and fix the function to handle empty lines.
51
    def test_parse_colordiffrc(self):
52
        colordiffrc = '''\
53
newtext=blue
54
oldtext = Red
55
# now a comment and a blank line
56
57
diffstuff = #ffff00  
58
  # another comment preceded by whitespace
59
'''
60
        colors = {
61
                'newtext': 'blue',
62
                'oldtext': 'Red',
63
                'diffstuff': '#ffff00',
64
        }
278.1.12 by John Arbash Meinel
Delay computing the delta, and clean up some of the diff view names.
65
        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.
66
        self.assertEqual(colors, parsed_colors)
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
67
68
69
class TestDiffView(tests.TestCaseWithTransport):
70
71
    def test_unicode(self):
72
        from bzrlib.tests.test_diff import UnicodeFilename
73
        self.requireFeature(UnicodeFilename)
74
75
        tree = self.make_branch_and_tree('tree')
76
        self.build_tree([u'tree/\u03a9'])
77
        tree.add([u'\u03a9'], ['omega-id'])
78
79
        view = DiffView()
80
        view.set_trees(tree, tree.basis_tree())
81
        view.show_diff(None)
82
        buf = view.buffer
83
        start, end = buf.get_bounds()
84
        text = buf.get_text(start, end)
85
        self.assertContainsRe(text,
86
            "=== added file '\xce\xa9'\n"
87
            '--- .*\t1970-01-01 00:00:00 \\+0000\n'
88
            r'\+\+\+ .*\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d [+-]\d\d\d\d\n'
89
            '@@ -0,0 \\+1,1 @@\n'
90
            '\\+contents of tree/\xce\xa9\n'
91
            '\n'
92
            )
93
94
487.2.4 by Aaron Bentley
Add tests for DiffController
95
class MockDiffWidget(object):
96
97
    def set_diff_text_sections(self, sections):
98
        self.sections = list(sections)
99
100
101
class MockWindow(object):
102
    def __init__(self):
103
        self.diff = MockDiffWidget()
487.2.7 by Aaron Bentley
Add more merge tests
104
        self.merge_successful = False
487.2.4 by Aaron Bentley
Add tests for DiffController
105
106
    def set_title(self, title):
107
        self.title = title
108
109
    def _get_save_path(self, basename):
110
        return 'save-path'
111
487.2.5 by Aaron Bentley
Test successful merge
112
    def _get_merge_target(self):
487.2.6 by Aaron Bentley
Ensure MergeController sets pending merges and updates files
113
        return 'this'
487.2.5 by Aaron Bentley
Test successful merge
114
115
    def destroy(self):
116
        pass
117
118
    def _merge_successful(self):
119
        self.merge_successful = True
120
487.2.7 by Aaron Bentley
Add more merge tests
121
    def _conflicts(self):
122
        self.conflicts = True
123
487.2.8 by Aaron Bentley
Update error handling to use window
124
    def _handle_error(self, e):
125
        self.handled_error = e
126
487.2.4 by Aaron Bentley
Add tests for DiffController
127
128
class TestDiffController(tests.TestCaseWithTransport):
129
130
    def get_controller(self):
131
        window = MockWindow()
132
        return DiffController('load-path', eg_diff.splitlines(True), window)
133
134
    def test_get_diff_sections(self):
135
        controller = self.get_controller()
136
        controller = DiffController('.', eg_diff.splitlines(True),
137
                                    controller.window)
138
        sections = list(controller.get_diff_sections())
139
        self.assertEqual('Complete Diff', sections[0][0])
140
        self.assertIs(None, sections[0][1])
141
        self.assertEqual(eg_diff, sections[0][2])
142
143
        self.assertEqual('tests/test_diff.py', sections[1][0])
144
        self.assertEqual('tests/test_diff.py', sections[1][1])
145
        self.assertEqual(''.join(eg_diff.splitlines(True)[1:]),
146
                         sections[1][2])
147
148
    def test_initialize_window(self):
149
        controller = self.get_controller()
150
        controller.initialize_window(controller.window)
151
        self.assertEqual(2, len(controller.window.diff.sections))
152
        self.assertEqual('load-path - diff', controller.window.title)
153
154
    def test_perform_save(self):
155
        self.build_tree_contents([('load-path', 'foo')])
156
        controller = self.get_controller()
157
        controller.perform_save(None)
158
        self.assertFileEqual('foo', 'save-path')
159
160
487.2.5 by Aaron Bentley
Test successful merge
161
class TestMergeDirectiveController(tests.TestCaseWithTransport):
162
487.2.7 by Aaron Bentley
Add more merge tests
163
    def make_this_other_directive(self):
487.2.5 by Aaron Bentley
Test successful merge
164
        this = self.make_branch_and_tree('this')
165
        this.commit('first commit')
166
        other = this.bzrdir.sprout('other').open_workingtree()
487.2.7 by Aaron Bentley
Add more merge tests
167
        self.build_tree_contents([('other/foo', 'bar')])
168
        other.add('foo')
487.2.5 by Aaron Bentley
Test successful merge
169
        other.commit('second commit')
170
        other.lock_write()
171
        try:
493.1.1 by Aaron Bentley
Work around bug for old merge directives in bzr.dev
172
            directive = MergeDirective2.from_objects(other.branch.repository,
173
                                                     other.last_revision(), 0,
174
                                                     0, 'this')
487.2.5 by Aaron Bentley
Test successful merge
175
        finally:
176
            other.unlock()
487.2.7 by Aaron Bentley
Add more merge tests
177
        return this, other, directive
178
179
    def make_merged_window(self, directive):
487.2.5 by Aaron Bentley
Test successful merge
180
        window = MockWindow()
181
        controller = MergeDirectiveController('directive', directive, window)
182
        controller.perform_merge(window)
487.2.7 by Aaron Bentley
Add more merge tests
183
        return window
184
185
    def test_perform_merge_success(self):
186
        this, other, directive = self.make_this_other_directive()
187
        window = self.make_merged_window(directive)
487.2.5 by Aaron Bentley
Test successful merge
188
        self.assertTrue(window.merge_successful)
487.2.6 by Aaron Bentley
Ensure MergeController sets pending merges and updates files
189
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
487.2.7 by Aaron Bentley
Add more merge tests
190
        self.assertFileEqual('bar', 'this/foo')
191
192
    def test_perform_merge_conflicts(self):
193
        this, other, directive = self.make_this_other_directive()
194
        self.build_tree_contents([('this/foo', 'bar')])
195
        this.add('foo')
196
        this.commit('message')
197
        window = self.make_merged_window(directive)
198
        self.assertFalse(window.merge_successful)
199
        self.assertTrue(window.conflicts)
200
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
201
        self.assertFileEqual('bar', 'this/foo')
487.2.5 by Aaron Bentley
Test successful merge
202
487.2.8 by Aaron Bentley
Update error handling to use window
203
    def test_perform_merge_uncommitted_changes(self):
204
        this, other, directive = self.make_this_other_directive()
205
        self.build_tree_contents([('this/foo', 'bar')])
206
        this.add('foo')
207
        window = self.make_merged_window(directive)
208
        self.assertIsInstance(window.handled_error, errors.UncommittedChanges)
209
487.2.5 by Aaron Bentley
Test successful merge
210
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
211
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
212
213
    def assertStatusEqual(self, expected, tree):
450 by Aaron Bentley
Update to use new Tree.iter_changes
214
        values = iter_changes_to_status(tree.basis_tree(), tree)
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
215
        self.assertEqual(expected, values)
216
217
    def test_status_added(self):
218
        tree = self.make_branch_and_tree('tree')
219
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
220
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
221
222
        self.assertStatusEqual(
223
            [('a-id', 'a', 'added', 'a'),
224
             ('b-id', 'b', 'added', 'b/'),
225
             ('c-id', 'b/c', 'added', 'b/c'),
226
            ], tree)
227
228
    def test_status_renamed(self):
229
        tree = self.make_branch_and_tree('tree')
230
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
231
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
232
        rev_id1 = tree.commit('one')
233
234
        tree.rename_one('b', 'd')
235
        tree.rename_one('a', 'd/a')
236
237
        self.assertStatusEqual(
238
            [('b-id', 'd', 'renamed', 'b/ => d/'),
239
             ('a-id', 'd/a', 'renamed', 'a => d/a'),
240
            ], tree)
241
242
    def test_status_modified(self):
243
        tree = self.make_branch_and_tree('tree')
244
        self.build_tree(['tree/a'])
245
        tree.add(['a'], ['a-id'])
246
        rev_id1 = tree.commit('one')
247
248
        self.build_tree_contents([('tree/a', 'new contents for a\n')])
249
250
        self.assertStatusEqual(
251
            [('a-id', 'a', 'modified', 'a'),
252
            ], tree)
253
254
    def test_status_renamed_and_modified(self):
255
        tree = self.make_branch_and_tree('tree')
256
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
257
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
258
        rev_id1 = tree.commit('one')
259
260
        tree.rename_one('b', 'd')
261
        tree.rename_one('a', 'd/a')
262
        self.build_tree_contents([('tree/d/a', 'new contents for a\n'),
263
                                  ('tree/d/c', 'new contents for c\n'),
264
                                 ])
265
        # 'c' is not considered renamed, because only its parent was moved, it
266
        # stayed in the same directory
267
268
        self.assertStatusEqual(
269
            [('b-id', 'd', 'renamed', 'b/ => d/'),
270
             ('a-id', 'd/a', 'renamed and modified', 'a => d/a'),
271
             ('c-id', 'd/c', 'modified', 'd/c'),
272
            ], tree)
273
274
    def test_status_kind_changed(self):
275
        tree = self.make_branch_and_tree('tree')
276
        self.build_tree(['tree/a', 'tree/b'])
277
        tree.add(['a', 'b'], ['a-id', 'b-id'])
278
        tree.commit('one')
279
280
        os.remove('tree/a')
281
        self.build_tree(['tree/a/'])
282
        # XXX:  This is technically valid, and the file list handles it fine,
283
        #       but 'show_diff_trees()' does not, so we skip this part of the
284
        #       test for now.
285
        # tree.rename_one('b', 'c')
286
        # os.remove('tree/c')
287
        # self.build_tree(['tree/c/'])
288
289
        self.assertStatusEqual(
290
            [('a-id', 'a', 'kind changed', 'a => a/'),
291
            # ('b-id', 'c', True, 'b => c/', 'renamed and modified'),
292
            ], tree)
293
294
    def test_status_removed(self):
295
        tree = self.make_branch_and_tree('tree')
296
        self.build_tree(['tree/a', 'tree/b/'])
297
        tree.add(['a', 'b'], ['a-id', 'b-id'])
298
        tree.commit('one')
299
300
        os.remove('tree/a')
301
        tree.remove('b', force=True)
302
303
        self.assertStatusEqual(
304
            [('a-id', 'a', 'removed', 'a'),
305
             ('b-id', 'b', 'removed', 'b/'),
306
            ], tree)