/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: Martin Albisetti
  • Author(s): Adrian Wilkins
  • Date: 2008-06-20 03:36:30 UTC
  • Revision ID: argentina@gmail.com-20080620033630-th70nvquna8d0sjc
Fix float division error in gcommit

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
from cStringIO import StringIO
19
 
 
20
 
from bzrlib.plugins.gtk.diff import DiffWindow
21
 
from bzrlib.tests import TestCase
22
 
 
23
 
class TestDiffWindow(TestCase):
 
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
 
24
51
    def test_parse_colordiffrc(self):
25
52
        colordiffrc = '''\
26
53
newtext=blue
35
62
                'oldtext': 'Red',
36
63
                'diffstuff': '#ffff00',
37
64
        }
38
 
        parsed_colors = DiffWindow.parse_colordiffrc(StringIO(colordiffrc))
 
65
        parsed_colors = DiffView.parse_colordiffrc(StringIO(colordiffrc))
39
66
        self.assertEqual(colors, parsed_colors)
 
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
 
 
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()
 
104
        self.merge_successful = False
 
105
 
 
106
    def set_title(self, title):
 
107
        self.title = title
 
108
 
 
109
    def _get_save_path(self, basename):
 
110
        return 'save-path'
 
111
 
 
112
    def _get_merge_target(self):
 
113
        return 'this'
 
114
 
 
115
    def destroy(self):
 
116
        pass
 
117
 
 
118
    def _merge_successful(self):
 
119
        self.merge_successful = True
 
120
 
 
121
    def _conflicts(self):
 
122
        self.conflicts = True
 
123
 
 
124
    def _handle_error(self, e):
 
125
        self.handled_error = e
 
126
 
 
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
 
 
161
class TestMergeDirectiveController(tests.TestCaseWithTransport):
 
162
 
 
163
    def make_this_other_directive(self):
 
164
        this = self.make_branch_and_tree('this')
 
165
        this.commit('first commit')
 
166
        other = this.bzrdir.sprout('other').open_workingtree()
 
167
        self.build_tree_contents([('other/foo', 'bar')])
 
168
        other.add('foo')
 
169
        other.commit('second commit')
 
170
        other.lock_write()
 
171
        try:
 
172
            directive = MergeDirective2.from_objects(other.branch.repository,
 
173
                                                     other.last_revision(), 0,
 
174
                                                     0, 'this')
 
175
        finally:
 
176
            other.unlock()
 
177
        return this, other, directive
 
178
 
 
179
    def make_merged_window(self, directive):
 
180
        window = MockWindow()
 
181
        controller = MergeDirectiveController('directive', directive, window)
 
182
        controller.perform_merge(window)
 
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)
 
188
        self.assertTrue(window.merge_successful)
 
189
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
 
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')
 
202
 
 
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
 
 
210
 
 
211
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
 
212
 
 
213
    def assertStatusEqual(self, expected, tree):
 
214
        values = iter_changes_to_status(tree.basis_tree(), tree)
 
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)