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
26
from bzrlib.merge_directive import MergeDirective2
28
from bzrlib.plugins.gtk.diff import (
31
iter_changes_to_status,
32
MergeDirectiveController,
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
40
from bzrlib import tests
42
-from bzrlib.plugins.gtk.diff import DiffView, iter_changes_to_status
43
+from bzrlib.plugins.gtk.diff import (
46
+ iter_changes_to_status,
50
class TestDiffViewSimple(tests.TestCase):
53
class TestDiffViewSimple(tests.TestCase):
55
def test_parse_colordiffrc(self):
59
# now a comment and a blank line
62
# another comment preceded by whitespace
67
'diffstuff': '#ffff00',
69
parsed_colors = DiffView.parse_colordiffrc(StringIO(colordiffrc))
70
self.assertEqual(colors, parsed_colors)
73
class TestDiffView(tests.TestCaseWithTransport):
75
def test_unicode(self):
76
self.requireFeature(tests.UnicodeFilenameFeature)
78
tree = self.make_branch_and_tree('tree')
79
self.build_tree([u'tree/\u03a9'])
80
tree.add([u'\u03a9'], ['omega-id'])
83
view.set_trees(tree, tree.basis_tree())
86
start, end = buf.get_bounds()
87
text = buf.get_text(start, end)
88
self.assertContainsRe(text,
89
"=== added file '\xce\xa9'\n"
90
'--- .*\t1970-01-01 00:00:00 \\+0000\n'
91
r'\+\+\+ .*\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d [+-]\d\d\d\d\n'
93
'\\+contents of tree/\xce\xa9\n'
98
class MockDiffWidget(object):
100
def set_diff_text_sections(self, sections):
101
self.sections = list(sections)
104
class MockWindow(object):
107
self.diff = MockDiffWidget()
108
self.merge_successful = False
110
def set_title(self, title):
113
def _get_save_path(self, basename):
116
def _get_merge_target(self):
122
def _merge_successful(self):
123
self.merge_successful = True
125
def _conflicts(self):
126
self.conflicts = True
128
def _handle_error(self, e):
129
self.handled_error = e
132
class TestDiffController(tests.TestCaseWithTransport):
134
def get_controller(self):
135
window = MockWindow()
136
return DiffController('load-path', eg_diff.splitlines(True), window)
138
def test_get_diff_sections(self):
139
controller = self.get_controller()
140
controller = DiffController('.', eg_diff.splitlines(True),
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])
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:]),
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)
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')
165
class TestMergeDirectiveController(tests.TestCaseWithTransport):
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')])
173
other.commit('second commit')
176
directive = MergeDirective2.from_objects(other.branch.repository,
177
other.last_revision(), 0,
181
return this, other, directive
183
def make_merged_window(self, directive):
184
window = MockWindow()
185
controller = MergeDirectiveController('directive', directive, window)
186
controller.perform_merge(window)
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')
196
def test_perform_merge_conflicts(self):
197
this, other, directive = self.make_this_other_directive()
198
self.build_tree_contents([('this/foo', 'bar')])
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')
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')])
211
window = self.make_merged_window(directive)
212
self.assertIsInstance(window.handled_error, errors.UncommittedChanges)
215
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
217
def assertStatusEqual(self, expected, tree):
218
values = iter_changes_to_status(tree.basis_tree(), tree)
219
self.assertEqual(expected, values)
221
def test_status_added(self):
222
tree = self.make_branch_and_tree('tree')
223
self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
224
tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
226
self.assertStatusEqual(
227
[('a-id', 'a', 'added', 'a'),
228
('b-id', 'b', 'added', 'b/'),
229
('c-id', 'b/c', 'added', 'b/c'),
232
def test_status_renamed(self):
233
tree = self.make_branch_and_tree('tree')
234
self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
235
tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
236
rev_id1 = tree.commit('one')
238
tree.rename_one('b', 'd')
239
tree.rename_one('a', 'd/a')
241
self.assertStatusEqual(
242
[('b-id', 'd', 'renamed', 'b/ => d/'),
243
('a-id', 'd/a', 'renamed', 'a => d/a'),
246
def test_status_modified(self):
247
tree = self.make_branch_and_tree('tree')
248
self.build_tree(['tree/a'])
249
tree.add(['a'], ['a-id'])
250
rev_id1 = tree.commit('one')
252
self.build_tree_contents([('tree/a', 'new contents for a\n')])
254
self.assertStatusEqual(
255
[('a-id', 'a', 'modified', 'a'),
258
def test_status_renamed_and_modified(self):
259
tree = self.make_branch_and_tree('tree')
260
self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
261
tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
262
rev_id1 = tree.commit('one')
264
tree.rename_one('b', 'd')
265
tree.rename_one('a', 'd/a')
266
self.build_tree_contents([('tree/d/a', 'new contents for a\n'),
267
('tree/d/c', 'new contents for c\n'),
269
# 'c' is not considered renamed, because only its parent was moved, it
270
# stayed in the same directory
272
self.assertStatusEqual(
273
[('b-id', 'd', 'renamed', 'b/ => d/'),
274
('a-id', 'd/a', 'renamed and modified', 'a => d/a'),
275
('c-id', 'd/c', 'modified', 'd/c'),
278
def test_status_kind_changed(self):
279
tree = self.make_branch_and_tree('tree')
280
self.build_tree(['tree/a', 'tree/b'])
281
tree.add(['a', 'b'], ['a-id', 'b-id'])
285
self.build_tree(['tree/a/'])
286
# XXX: This is technically valid, and the file list handles it fine,
287
# but 'show_diff_trees()' does not, so we skip this part of the
289
# tree.rename_one('b', 'c')
290
# os.remove('tree/c')
291
# self.build_tree(['tree/c/'])
293
self.assertStatusEqual(
294
[('a-id', 'a', 'kind changed', 'a => a/'),
295
# ('b-id', 'c', True, 'b => c/', 'renamed and modified'),
298
def test_status_removed(self):
299
tree = self.make_branch_and_tree('tree')
300
self.build_tree(['tree/a', 'tree/b/'])
301
tree.add(['a', 'b'], ['a-id', 'b-id'])
305
tree.remove('b', force=True)
307
self.assertStatusEqual(
308
[('a-id', 'a', 'removed', 'a'),
309
('b-id', 'b', 'removed', 'b/'),
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'])
318
other = this.bzrdir.sprout('other').open_workingtree()
320
os.remove('this/foo')
321
this.remove('foo', force=True)
322
this.commit('remove')
324
f = open('other/foo', 'wt')
326
f.write('Modified\n')
329
other.commit('modified')
331
this.merge_from_branch(other.branch)
332
conflicts.resolve(this)
334
self.assertStatusEqual(
335
[('foo-id', 'foo.OTHER', 'missing', 'foo.OTHER'),],
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'])
344
other = this.bzrdir.sprout('other').open_workingtree()
346
os.remove('this/foo/bar')
348
this.remove('foo', force=True)
349
this.commit('remove')
351
f = open('other/foo/bar', 'wt')
353
f.write('Modified\n')
356
other.commit('modified')
358
this.merge_from_branch(other.branch)
359
conflicts.resolve(this)
361
self.assertStatusEqual(
362
[('foo-id', u'foo', 'added', u'foo/'),
363
('bar-id', u'foo/bar.OTHER', 'missing', u'foo/bar.OTHER'),],