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