1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2007 Adeodato Simó <dato@net.com.org.es>
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.
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.
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
18
from cStringIO import StringIO
27
from bzrlib.tests.features import UnicodeFilenameFeature
28
except ImportError: # bzr < 2.5
29
from bzrlib.tests import UnicodeFilenameFeature
30
from bzrlib.merge_directive import MergeDirective2
32
from bzrlib.plugins.gtk.diff import (
35
iter_changes_to_status,
36
MergeDirectiveController,
39
=== modified file 'tests/test_diff.py'
40
--- tests/test_diff.py 2008-03-11 13:18:28 +0000
41
+++ tests/test_diff.py 2008-05-08 22:44:02 +0000
44
from bzrlib import tests
46
-from bzrlib.plugins.gtk.diff import DiffView, iter_changes_to_status
47
+from bzrlib.plugins.gtk.diff import (
50
+ iter_changes_to_status,
54
class TestDiffViewSimple(tests.TestCase):
57
class TestDiffViewSimple(tests.TestCase):
59
def test_parse_colordiffrc(self):
63
# now a comment and a blank line
66
# another comment preceded by whitespace
71
'diffstuff': '#ffff00',
73
parsed_colors = DiffView.parse_colordiffrc(StringIO(colordiffrc))
74
self.assertEqual(colors, parsed_colors)
77
class TestDiffView(tests.TestCaseWithTransport):
79
def test_unicode(self):
80
self.requireFeature(UnicodeFilenameFeature)
82
tree = self.make_branch_and_tree('tree')
83
self.build_tree([u'tree/\u03a9'])
84
tree.add([u'\u03a9'], ['omega-id'])
87
view.set_trees(tree, tree.basis_tree())
90
start, end = buf.get_bounds()
91
text = buf.get_text(start, end, True)
92
self.assertContainsRe(text,
93
"=== added file '\xce\xa9'\n"
94
'--- .*\t1970-01-01 00:00:00 \\+0000\n'
95
r'\+\+\+ .*\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d [+-]\d\d\d\d\n'
97
'\\+contents of tree/\xce\xa9\n'
102
class MockDiffWidget(object):
104
def set_diff_text_sections(self, sections):
105
self.sections = list(sections)
108
class MockWindow(object):
111
self.diff = MockDiffWidget()
112
self.merge_successful = False
114
def set_title(self, title):
117
def _get_save_path(self, basename):
120
def _get_merge_target(self):
126
def _merge_successful(self):
127
self.merge_successful = True
129
def _conflicts(self):
130
self.conflicts = True
132
def _handle_error(self, e):
133
self.handled_error = e
136
class TestDiffController(tests.TestCaseWithTransport):
138
def get_controller(self):
139
window = MockWindow()
140
return DiffController('load-path', eg_diff.splitlines(True), window)
142
def test_get_diff_sections(self):
143
controller = self.get_controller()
144
controller = DiffController('.', eg_diff.splitlines(True),
146
sections = list(controller.get_diff_sections())
147
self.assertEqual('Complete Diff', sections[0][0])
148
self.assertIs(None, sections[0][1])
149
self.assertEqual(eg_diff, sections[0][2])
151
self.assertEqual('tests/test_diff.py', sections[1][0])
152
self.assertEqual('tests/test_diff.py', sections[1][1])
153
self.assertEqual(''.join(eg_diff.splitlines(True)[1:]),
156
def test_initialize_window(self):
157
controller = self.get_controller()
158
controller.initialize_window(controller.window)
159
self.assertEqual(2, len(controller.window.diff.sections))
160
self.assertEqual('load-path - diff', controller.window.title)
162
def test_perform_save(self):
163
self.build_tree_contents([('load-path', 'foo')])
164
controller = self.get_controller()
165
controller.perform_save(None)
166
self.assertFileEqual('foo', 'save-path')
169
class TestMergeDirectiveController(tests.TestCaseWithTransport):
171
def make_this_other_directive(self):
172
this = self.make_branch_and_tree('this')
173
this.commit('first commit')
174
other = this.bzrdir.sprout('other').open_workingtree()
175
self.build_tree_contents([('other/foo', 'bar')])
177
other.commit('second commit')
180
directive = MergeDirective2.from_objects(other.branch.repository,
181
other.last_revision(), 0,
185
return this, other, directive
187
def make_merged_window(self, directive):
188
window = MockWindow()
189
controller = MergeDirectiveController('directive', directive, window)
190
controller.perform_merge(window)
193
def test_perform_merge_success(self):
194
this, other, directive = self.make_this_other_directive()
195
window = self.make_merged_window(directive)
196
self.assertTrue(window.merge_successful)
197
self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
198
self.assertFileEqual('bar', 'this/foo')
200
def test_perform_merge_conflicts(self):
201
this, other, directive = self.make_this_other_directive()
202
self.build_tree_contents([('this/foo', 'bar')])
204
this.commit('message')
205
window = self.make_merged_window(directive)
206
self.assertFalse(window.merge_successful)
207
self.assertTrue(window.conflicts)
208
self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
209
self.assertFileEqual('bar', 'this/foo')
211
def test_perform_merge_uncommitted_changes(self):
212
this, other, directive = self.make_this_other_directive()
213
self.build_tree_contents([('this/foo', 'bar')])
215
window = self.make_merged_window(directive)
216
self.assertIsInstance(window.handled_error, errors.UncommittedChanges)
219
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
221
def assertStatusEqual(self, expected, tree):
222
values = iter_changes_to_status(tree.basis_tree(), tree)
223
self.assertEqual(expected, values)
225
def test_status_added(self):
226
tree = self.make_branch_and_tree('tree')
227
self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
228
tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
230
self.assertStatusEqual(
231
[('a-id', 'a', 'added', 'a'),
232
('b-id', 'b', 'added', 'b/'),
233
('c-id', 'b/c', 'added', 'b/c'),
236
def test_status_renamed(self):
237
tree = self.make_branch_and_tree('tree')
238
self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
239
tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
240
rev_id1 = tree.commit('one')
242
tree.rename_one('b', 'd')
243
tree.rename_one('a', 'd/a')
245
self.assertStatusEqual(
246
[('b-id', 'd', 'renamed', 'b/ => d/'),
247
('a-id', 'd/a', 'renamed', 'a => d/a'),
250
def test_status_modified(self):
251
tree = self.make_branch_and_tree('tree')
252
self.build_tree(['tree/a'])
253
tree.add(['a'], ['a-id'])
254
rev_id1 = tree.commit('one')
256
self.build_tree_contents([('tree/a', 'new contents for a\n')])
258
self.assertStatusEqual(
259
[('a-id', 'a', 'modified', 'a'),
262
def test_status_renamed_and_modified(self):
263
tree = self.make_branch_and_tree('tree')
264
self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
265
tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
266
rev_id1 = tree.commit('one')
268
tree.rename_one('b', 'd')
269
tree.rename_one('a', 'd/a')
270
self.build_tree_contents([('tree/d/a', 'new contents for a\n'),
271
('tree/d/c', 'new contents for c\n'),
273
# 'c' is not considered renamed, because only its parent was moved, it
274
# stayed in the same directory
276
self.assertStatusEqual(
277
[('b-id', 'd', 'renamed', 'b/ => d/'),
278
('a-id', 'd/a', 'renamed and modified', 'a => d/a'),
279
('c-id', 'd/c', 'modified', 'd/c'),
282
def test_status_kind_changed(self):
283
tree = self.make_branch_and_tree('tree')
284
self.build_tree(['tree/a', 'tree/b'])
285
tree.add(['a', 'b'], ['a-id', 'b-id'])
289
self.build_tree(['tree/a/'])
290
# XXX: This is technically valid, and the file list handles it fine,
291
# but 'show_diff_trees()' does not, so we skip this part of the
293
# tree.rename_one('b', 'c')
294
# os.remove('tree/c')
295
# self.build_tree(['tree/c/'])
297
self.assertStatusEqual(
298
[('a-id', 'a', 'kind changed', 'a => a/'),
299
# ('b-id', 'c', True, 'b => c/', 'renamed and modified'),
302
def test_status_removed(self):
303
tree = self.make_branch_and_tree('tree')
304
self.build_tree(['tree/a', 'tree/b/'])
305
tree.add(['a', 'b'], ['a-id', 'b-id'])
309
tree.remove('b', force=True)
311
self.assertStatusEqual(
312
[('a-id', 'a', 'removed', 'a'),
313
('b-id', 'b', 'removed', 'b/'),
316
def test_status_missing_file(self):
317
this = self.make_branch_and_tree('this')
318
self.build_tree(['this/foo'])
319
this.add(['foo'], ['foo-id'])
322
other = this.bzrdir.sprout('other').open_workingtree()
324
os.remove('this/foo')
325
this.remove('foo', force=True)
326
this.commit('remove')
328
f = open('other/foo', 'wt')
330
f.write('Modified\n')
333
other.commit('modified')
335
this.merge_from_branch(other.branch)
336
conflicts.resolve(this)
338
self.assertStatusEqual(
339
[('foo-id', 'foo.OTHER', 'missing', 'foo.OTHER'),],
342
def test_status_missing_directory(self):
343
this = self.make_branch_and_tree('this')
344
self.build_tree(['this/foo/', 'this/foo/bar'])
345
this.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
348
other = this.bzrdir.sprout('other').open_workingtree()
350
os.remove('this/foo/bar')
352
this.remove('foo', force=True)
353
this.commit('remove')
355
f = open('other/foo/bar', 'wt')
357
f.write('Modified\n')
360
other.commit('modified')
362
this.merge_from_branch(other.branch)
363
conflicts.resolve(this)
365
self.assertStatusEqual(
366
[('foo-id', u'foo', 'added', u'foo/'),
367
('bar-id', u'foo/bar.OTHER', 'missing', u'foo/bar.OTHER'),],