/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
278.1.13 by John Arbash Meinel
minor cleanup
21
from bzrlib import tests
487.2.5 by Aaron Bentley
Test successful merge
22
from bzrlib.merge_directive import MergeDirective
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.4 by Aaron Bentley
Add tests for DiffController
124
125
class TestDiffController(tests.TestCaseWithTransport):
126
127
    def get_controller(self):
128
        window = MockWindow()
129
        return DiffController('load-path', eg_diff.splitlines(True), window)
130
131
    def test_get_diff_sections(self):
132
        controller = self.get_controller()
133
        controller = DiffController('.', eg_diff.splitlines(True),
134
                                    controller.window)
135
        sections = list(controller.get_diff_sections())
136
        self.assertEqual('Complete Diff', sections[0][0])
137
        self.assertIs(None, sections[0][1])
138
        self.assertEqual(eg_diff, sections[0][2])
139
140
        self.assertEqual('tests/test_diff.py', sections[1][0])
141
        self.assertEqual('tests/test_diff.py', sections[1][1])
142
        self.assertEqual(''.join(eg_diff.splitlines(True)[1:]),
143
                         sections[1][2])
144
145
    def test_initialize_window(self):
146
        controller = self.get_controller()
147
        controller.initialize_window(controller.window)
148
        self.assertEqual(2, len(controller.window.diff.sections))
149
        self.assertEqual('load-path - diff', controller.window.title)
150
151
    def test_perform_save(self):
152
        self.build_tree_contents([('load-path', 'foo')])
153
        controller = self.get_controller()
154
        controller.perform_save(None)
155
        self.assertFileEqual('foo', 'save-path')
156
157
487.2.5 by Aaron Bentley
Test successful merge
158
class TestMergeDirectiveController(tests.TestCaseWithTransport):
159
487.2.7 by Aaron Bentley
Add more merge tests
160
    def make_this_other_directive(self):
487.2.5 by Aaron Bentley
Test successful merge
161
        this = self.make_branch_and_tree('this')
162
        this.commit('first commit')
163
        other = this.bzrdir.sprout('other').open_workingtree()
487.2.7 by Aaron Bentley
Add more merge tests
164
        self.build_tree_contents([('other/foo', 'bar')])
165
        other.add('foo')
487.2.5 by Aaron Bentley
Test successful merge
166
        other.commit('second commit')
167
        other.lock_write()
168
        try:
169
            directive = MergeDirective.from_objects(other.branch.repository,
170
                                                    other.last_revision(), 0,
171
                                                    0, 'this')
172
        finally:
173
            other.unlock()
487.2.7 by Aaron Bentley
Add more merge tests
174
        return this, other, directive
175
176
    def make_merged_window(self, directive):
487.2.5 by Aaron Bentley
Test successful merge
177
        window = MockWindow()
178
        controller = MergeDirectiveController('directive', directive, window)
179
        controller.perform_merge(window)
487.2.7 by Aaron Bentley
Add more merge tests
180
        return window
181
182
    def test_perform_merge_success(self):
183
        this, other, directive = self.make_this_other_directive()
184
        window = self.make_merged_window(directive)
487.2.5 by Aaron Bentley
Test successful merge
185
        self.assertTrue(window.merge_successful)
487.2.6 by Aaron Bentley
Ensure MergeController sets pending merges and updates files
186
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
487.2.7 by Aaron Bentley
Add more merge tests
187
        self.assertFileEqual('bar', 'this/foo')
188
189
    def test_perform_merge_conflicts(self):
190
        this, other, directive = self.make_this_other_directive()
191
        self.build_tree_contents([('this/foo', 'bar')])
192
        this.add('foo')
193
        this.commit('message')
194
        window = self.make_merged_window(directive)
195
        self.assertFalse(window.merge_successful)
196
        self.assertTrue(window.conflicts)
197
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
198
        self.assertFileEqual('bar', 'this/foo')
487.2.5 by Aaron Bentley
Test successful merge
199
200
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
201
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
202
203
    def assertStatusEqual(self, expected, tree):
450 by Aaron Bentley
Update to use new Tree.iter_changes
204
        values = iter_changes_to_status(tree.basis_tree(), tree)
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
205
        self.assertEqual(expected, values)
206
207
    def test_status_added(self):
208
        tree = self.make_branch_and_tree('tree')
209
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
210
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
211
212
        self.assertStatusEqual(
213
            [('a-id', 'a', 'added', 'a'),
214
             ('b-id', 'b', 'added', 'b/'),
215
             ('c-id', 'b/c', 'added', 'b/c'),
216
            ], tree)
217
218
    def test_status_renamed(self):
219
        tree = self.make_branch_and_tree('tree')
220
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
221
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
222
        rev_id1 = tree.commit('one')
223
224
        tree.rename_one('b', 'd')
225
        tree.rename_one('a', 'd/a')
226
227
        self.assertStatusEqual(
228
            [('b-id', 'd', 'renamed', 'b/ => d/'),
229
             ('a-id', 'd/a', 'renamed', 'a => d/a'),
230
            ], tree)
231
232
    def test_status_modified(self):
233
        tree = self.make_branch_and_tree('tree')
234
        self.build_tree(['tree/a'])
235
        tree.add(['a'], ['a-id'])
236
        rev_id1 = tree.commit('one')
237
238
        self.build_tree_contents([('tree/a', 'new contents for a\n')])
239
240
        self.assertStatusEqual(
241
            [('a-id', 'a', 'modified', 'a'),
242
            ], tree)
243
244
    def test_status_renamed_and_modified(self):
245
        tree = self.make_branch_and_tree('tree')
246
        self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
247
        tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
248
        rev_id1 = tree.commit('one')
249
250
        tree.rename_one('b', 'd')
251
        tree.rename_one('a', 'd/a')
252
        self.build_tree_contents([('tree/d/a', 'new contents for a\n'),
253
                                  ('tree/d/c', 'new contents for c\n'),
254
                                 ])
255
        # 'c' is not considered renamed, because only its parent was moved, it
256
        # stayed in the same directory
257
258
        self.assertStatusEqual(
259
            [('b-id', 'd', 'renamed', 'b/ => d/'),
260
             ('a-id', 'd/a', 'renamed and modified', 'a => d/a'),
261
             ('c-id', 'd/c', 'modified', 'd/c'),
262
            ], tree)
263
264
    def test_status_kind_changed(self):
265
        tree = self.make_branch_and_tree('tree')
266
        self.build_tree(['tree/a', 'tree/b'])
267
        tree.add(['a', 'b'], ['a-id', 'b-id'])
268
        tree.commit('one')
269
270
        os.remove('tree/a')
271
        self.build_tree(['tree/a/'])
272
        # XXX:  This is technically valid, and the file list handles it fine,
273
        #       but 'show_diff_trees()' does not, so we skip this part of the
274
        #       test for now.
275
        # tree.rename_one('b', 'c')
276
        # os.remove('tree/c')
277
        # self.build_tree(['tree/c/'])
278
279
        self.assertStatusEqual(
280
            [('a-id', 'a', 'kind changed', 'a => a/'),
281
            # ('b-id', 'c', True, 'b => c/', 'renamed and modified'),
282
            ], tree)
283
284
    def test_status_removed(self):
285
        tree = self.make_branch_and_tree('tree')
286
        self.build_tree(['tree/a', 'tree/b/'])
287
        tree.add(['a', 'b'], ['a-id', 'b-id'])
288
        tree.commit('one')
289
290
        os.remove('tree/a')
291
        tree.remove('b', force=True)
292
293
        self.assertStatusEqual(
294
            [('a-id', 'a', 'removed', 'a'),
295
             ('b-id', 'b', 'removed', 'b/'),
296
            ], tree)