/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: 2007-07-15 15:22:29 UTC
  • Revision ID: jelmer@samba.org-20070715152229-clmlen0vpd8d2pzx
Add docstrings, remove unused code.

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 errors, tests
22
 
from bzrlib.merge_directive import MergeDirective2
23
 
 
24
 
from bzrlib.plugins.gtk.diff import (
25
 
    DiffController,
26
 
    DiffView,
27
 
    iter_changes_to_status,
28
 
    MergeDirectiveController,
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
 
"""
48
 
 
49
 
class TestDiffViewSimple(tests.TestCase):
50
 
 
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
 
        }
65
 
        parsed_colors = DiffView.parse_colordiffrc(StringIO(colordiffrc))
66
 
        self.assertEqual(colors, parsed_colors)
67
 
 
68
 
 
69
 
class TestDiffView(tests.TestCaseWithTransport):
70
 
 
71
 
    def test_unicode(self):
72
 
        self.requireFeature(tests.UnicodeFilenameFeature)
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
 
 
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()
103
 
        self.merge_successful = False
104
 
 
105
 
    def set_title(self, title):
106
 
        self.title = title
107
 
 
108
 
    def _get_save_path(self, basename):
109
 
        return 'save-path'
110
 
 
111
 
    def _get_merge_target(self):
112
 
        return 'this'
113
 
 
114
 
    def destroy(self):
115
 
        pass
116
 
 
117
 
    def _merge_successful(self):
118
 
        self.merge_successful = True
119
 
 
120
 
    def _conflicts(self):
121
 
        self.conflicts = True
122
 
 
123
 
    def _handle_error(self, e):
124
 
        self.handled_error = e
125
 
 
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
 
 
160
 
class TestMergeDirectiveController(tests.TestCaseWithTransport):
161
 
 
162
 
    def make_this_other_directive(self):
163
 
        this = self.make_branch_and_tree('this')
164
 
        this.commit('first commit')
165
 
        other = this.bzrdir.sprout('other').open_workingtree()
166
 
        self.build_tree_contents([('other/foo', 'bar')])
167
 
        other.add('foo')
168
 
        other.commit('second commit')
169
 
        other.lock_write()
170
 
        try:
171
 
            directive = MergeDirective2.from_objects(other.branch.repository,
172
 
                                                     other.last_revision(), 0,
173
 
                                                     0, 'this')
174
 
        finally:
175
 
            other.unlock()
176
 
        return this, other, directive
177
 
 
178
 
    def make_merged_window(self, directive):
179
 
        window = MockWindow()
180
 
        controller = MergeDirectiveController('directive', directive, window)
181
 
        controller.perform_merge(window)
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)
187
 
        self.assertTrue(window.merge_successful)
188
 
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
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')
201
 
 
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
 
 
209
 
 
210
 
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
211
 
 
212
 
    def assertStatusEqual(self, expected, tree):
213
 
        values = iter_changes_to_status(tree.basis_tree(), tree)
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)