/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-12-21 20:20:37 UTC
  • mto: This revision was merged to the branch mainline in revision 422.
  • Revision ID: jelmer@samba.org-20071221202037-6zvbwefvrte3e2zy
Fix URL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
from cStringIO import StringIO
19
19
import os
20
20
 
21
 
from bzrlib import (
22
 
    conflicts,
23
 
    errors,
24
 
    tests,
25
 
    )
26
 
from bzrlib.merge_directive import MergeDirective2
27
 
 
28
 
from bzrlib.plugins.gtk.diff import (
29
 
    DiffController,
30
 
    DiffView,
31
 
    iter_changes_to_status,
32
 
    MergeDirectiveController,
33
 
    )
34
 
eg_diff = """\
35
 
=== modified file 'tests/test_diff.py'
36
 
--- tests/test_diff.py  2008-03-11 13:18:28 +0000
37
 
+++ tests/test_diff.py  2008-05-08 22:44:02 +0000
38
 
@@ -20,7 +20,11 @@
39
 
 
40
 
 from bzrlib import tests
41
 
 
42
 
-from bzrlib.plugins.gtk.diff import DiffView, iter_changes_to_status
43
 
+from bzrlib.plugins.gtk.diff import (
44
 
+    DiffController,
45
 
+    DiffView,
46
 
+    iter_changes_to_status,
47
 
+    )
48
 
 
49
 
 
50
 
 class TestDiffViewSimple(tests.TestCase):
51
 
"""
 
21
from bzrlib import tests
 
22
 
 
23
from bzrlib.plugins.gtk.diff import DiffView, _iter_changes_to_status
 
24
 
52
25
 
53
26
class TestDiffViewSimple(tests.TestCase):
54
27
 
73
46
class TestDiffView(tests.TestCaseWithTransport):
74
47
 
75
48
    def test_unicode(self):
76
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
49
        from bzrlib.tests.test_diff import UnicodeFilename
 
50
        self.requireFeature(UnicodeFilename)
77
51
 
78
52
        tree = self.make_branch_and_tree('tree')
79
53
        self.build_tree([u'tree/\u03a9'])
84
58
        view.show_diff(None)
85
59
        buf = view.buffer
86
60
        start, end = buf.get_bounds()
87
 
        text = buf.get_text(start, end, True)
 
61
        text = buf.get_text(start, end)
88
62
        self.assertContainsRe(text,
89
63
            "=== added file '\xce\xa9'\n"
90
64
            '--- .*\t1970-01-01 00:00:00 \\+0000\n'
95
69
            )
96
70
 
97
71
 
98
 
class MockDiffWidget(object):
99
 
 
100
 
    def set_diff_text_sections(self, sections):
101
 
        self.sections = list(sections)
102
 
 
103
 
 
104
 
class MockWindow(object):
105
 
 
106
 
    def __init__(self):
107
 
        self.diff = MockDiffWidget()
108
 
        self.merge_successful = False
109
 
 
110
 
    def set_title(self, title):
111
 
        self.title = title
112
 
 
113
 
    def _get_save_path(self, basename):
114
 
        return 'save-path'
115
 
 
116
 
    def _get_merge_target(self):
117
 
        return 'this'
118
 
 
119
 
    def destroy(self):
120
 
        pass
121
 
 
122
 
    def _merge_successful(self):
123
 
        self.merge_successful = True
124
 
 
125
 
    def _conflicts(self):
126
 
        self.conflicts = True
127
 
 
128
 
    def _handle_error(self, e):
129
 
        self.handled_error = e
130
 
 
131
 
 
132
 
class TestDiffController(tests.TestCaseWithTransport):
133
 
 
134
 
    def get_controller(self):
135
 
        window = MockWindow()
136
 
        return DiffController('load-path', eg_diff.splitlines(True), window)
137
 
 
138
 
    def test_get_diff_sections(self):
139
 
        controller = self.get_controller()
140
 
        controller = DiffController('.', eg_diff.splitlines(True),
141
 
                                    controller.window)
142
 
        sections = list(controller.get_diff_sections())
143
 
        self.assertEqual('Complete Diff', sections[0][0])
144
 
        self.assertIs(None, sections[0][1])
145
 
        self.assertEqual(eg_diff, sections[0][2])
146
 
 
147
 
        self.assertEqual('tests/test_diff.py', sections[1][0])
148
 
        self.assertEqual('tests/test_diff.py', sections[1][1])
149
 
        self.assertEqual(''.join(eg_diff.splitlines(True)[1:]),
150
 
                         sections[1][2])
151
 
 
152
 
    def test_initialize_window(self):
153
 
        controller = self.get_controller()
154
 
        controller.initialize_window(controller.window)
155
 
        self.assertEqual(2, len(controller.window.diff.sections))
156
 
        self.assertEqual('load-path - diff', controller.window.title)
157
 
 
158
 
    def test_perform_save(self):
159
 
        self.build_tree_contents([('load-path', 'foo')])
160
 
        controller = self.get_controller()
161
 
        controller.perform_save(None)
162
 
        self.assertFileEqual('foo', 'save-path')
163
 
 
164
 
 
165
 
class TestMergeDirectiveController(tests.TestCaseWithTransport):
166
 
 
167
 
    def make_this_other_directive(self):
168
 
        this = self.make_branch_and_tree('this')
169
 
        this.commit('first commit')
170
 
        other = this.bzrdir.sprout('other').open_workingtree()
171
 
        self.build_tree_contents([('other/foo', 'bar')])
172
 
        other.add('foo')
173
 
        other.commit('second commit')
174
 
        other.lock_write()
175
 
        try:
176
 
            directive = MergeDirective2.from_objects(other.branch.repository,
177
 
                                                     other.last_revision(), 0,
178
 
                                                     0, 'this')
179
 
        finally:
180
 
            other.unlock()
181
 
        return this, other, directive
182
 
 
183
 
    def make_merged_window(self, directive):
184
 
        window = MockWindow()
185
 
        controller = MergeDirectiveController('directive', directive, window)
186
 
        controller.perform_merge(window)
187
 
        return window
188
 
 
189
 
    def test_perform_merge_success(self):
190
 
        this, other, directive = self.make_this_other_directive()
191
 
        window = self.make_merged_window(directive)
192
 
        self.assertTrue(window.merge_successful)
193
 
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
194
 
        self.assertFileEqual('bar', 'this/foo')
195
 
 
196
 
    def test_perform_merge_conflicts(self):
197
 
        this, other, directive = self.make_this_other_directive()
198
 
        self.build_tree_contents([('this/foo', 'bar')])
199
 
        this.add('foo')
200
 
        this.commit('message')
201
 
        window = self.make_merged_window(directive)
202
 
        self.assertFalse(window.merge_successful)
203
 
        self.assertTrue(window.conflicts)
204
 
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
205
 
        self.assertFileEqual('bar', 'this/foo')
206
 
 
207
 
    def test_perform_merge_uncommitted_changes(self):
208
 
        this, other, directive = self.make_this_other_directive()
209
 
        self.build_tree_contents([('this/foo', 'bar')])
210
 
        this.add('foo')
211
 
        window = self.make_merged_window(directive)
212
 
        self.assertIsInstance(window.handled_error, errors.UncommittedChanges)
213
 
 
214
 
 
215
72
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
216
73
 
217
74
    def assertStatusEqual(self, expected, tree):
218
 
        values = iter_changes_to_status(tree.basis_tree(), tree)
 
75
        values = _iter_changes_to_status(tree.basis_tree(), tree)
219
76
        self.assertEqual(expected, values)
220
77
 
221
78
    def test_status_added(self):
308
165
            [('a-id', 'a', 'removed', 'a'),
309
166
             ('b-id', 'b', 'removed', 'b/'),
310
167
            ], tree)
311
 
 
312
 
    def test_status_missing_file(self):
313
 
        this = self.make_branch_and_tree('this')
314
 
        self.build_tree(['this/foo'])
315
 
        this.add(['foo'], ['foo-id'])
316
 
        this.commit('add')
317
 
 
318
 
        other = this.bzrdir.sprout('other').open_workingtree()
319
 
 
320
 
        os.remove('this/foo')
321
 
        this.remove('foo', force=True)
322
 
        this.commit('remove')
323
 
 
324
 
        f = open('other/foo', 'wt')
325
 
        try:
326
 
            f.write('Modified\n')
327
 
        finally:
328
 
            f.close()
329
 
        other.commit('modified')
330
 
 
331
 
        this.merge_from_branch(other.branch)
332
 
        conflicts.resolve(this)
333
 
 
334
 
        self.assertStatusEqual(
335
 
            [('foo-id', 'foo.OTHER', 'missing', 'foo.OTHER'),],
336
 
            this)
337
 
 
338
 
    def test_status_missing_directory(self):
339
 
        this = self.make_branch_and_tree('this')
340
 
        self.build_tree(['this/foo/', 'this/foo/bar'])
341
 
        this.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
342
 
        this.commit('add')
343
 
 
344
 
        other = this.bzrdir.sprout('other').open_workingtree()
345
 
 
346
 
        os.remove('this/foo/bar')
347
 
        os.rmdir('this/foo')
348
 
        this.remove('foo', force=True)
349
 
        this.commit('remove')
350
 
 
351
 
        f = open('other/foo/bar', 'wt')
352
 
        try:
353
 
            f.write('Modified\n')
354
 
        finally:
355
 
            f.close()
356
 
        other.commit('modified')
357
 
 
358
 
        this.merge_from_branch(other.branch)
359
 
        conflicts.resolve(this)
360
 
 
361
 
        self.assertStatusEqual(
362
 
            [('foo-id', u'foo', 'added', u'foo/'),
363
 
             ('bar-id', u'foo/bar.OTHER', 'missing', u'foo/bar.OTHER'),],
364
 
            this)