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
21
from bzrlib import errors, tests
22
from bzrlib.merge_directive import MergeDirective2
24
from bzrlib.plugins.gtk.diff import (
27
iter_changes_to_status,
28
MergeDirectiveController,
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
36
from bzrlib import tests
38
-from bzrlib.plugins.gtk.diff import DiffView, iter_changes_to_status
39
+from bzrlib.plugins.gtk.diff import (
42
+ iter_changes_to_status,
46
class TestDiffViewSimple(tests.TestCase):
49
class TestDiffViewSimple(tests.TestCase):
51
def test_parse_colordiffrc(self):
55
# now a comment and a blank line
58
# another comment preceded by whitespace
63
'diffstuff': '#ffff00',
65
parsed_colors = DiffView.parse_colordiffrc(StringIO(colordiffrc))
66
self.assertEqual(colors, parsed_colors)
69
class TestDiffView(tests.TestCaseWithTransport):
71
def test_unicode(self):
72
from bzrlib.tests.test_diff import UnicodeFilename
73
self.requireFeature(UnicodeFilename)
75
tree = self.make_branch_and_tree('tree')
76
self.build_tree([u'tree/\u03a9'])
77
tree.add([u'\u03a9'], ['omega-id'])
80
view.set_trees(tree, tree.basis_tree())
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'
90
'\\+contents of tree/\xce\xa9\n'
95
class MockDiffWidget(object):
97
def set_diff_text_sections(self, sections):
98
self.sections = list(sections)
101
class MockWindow(object):
103
self.diff = MockDiffWidget()
104
self.merge_successful = False
106
def set_title(self, title):
109
def _get_save_path(self, basename):
112
def _get_merge_target(self):
118
def _merge_successful(self):
119
self.merge_successful = True
121
def _conflicts(self):
122
self.conflicts = True
124
def _handle_error(self, e):
125
self.handled_error = e
128
class TestDiffController(tests.TestCaseWithTransport):
130
def get_controller(self):
131
window = MockWindow()
132
return DiffController('load-path', eg_diff.splitlines(True), window)
134
def test_get_diff_sections(self):
135
controller = self.get_controller()
136
controller = DiffController('.', eg_diff.splitlines(True),
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])
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:]),
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)
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')
161
class TestMergeDirectiveController(tests.TestCaseWithTransport):
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')])
169
other.commit('second commit')
172
directive = MergeDirective2.from_objects(other.branch.repository,
173
other.last_revision(), 0,
177
return this, other, directive
179
def make_merged_window(self, directive):
180
window = MockWindow()
181
controller = MergeDirectiveController('directive', directive, window)
182
controller.perform_merge(window)
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')
192
def test_perform_merge_conflicts(self):
193
this, other, directive = self.make_this_other_directive()
194
self.build_tree_contents([('this/foo', 'bar')])
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')
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')])
207
window = self.make_merged_window(directive)
208
self.assertIsInstance(window.handled_error, errors.UncommittedChanges)
211
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
213
def assertStatusEqual(self, expected, tree):
214
values = iter_changes_to_status(tree.basis_tree(), tree)
215
self.assertEqual(expected, values)
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'])
222
self.assertStatusEqual(
223
[('a-id', 'a', 'added', 'a'),
224
('b-id', 'b', 'added', 'b/'),
225
('c-id', 'b/c', 'added', 'b/c'),
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')
234
tree.rename_one('b', 'd')
235
tree.rename_one('a', 'd/a')
237
self.assertStatusEqual(
238
[('b-id', 'd', 'renamed', 'b/ => d/'),
239
('a-id', 'd/a', 'renamed', 'a => d/a'),
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')
248
self.build_tree_contents([('tree/a', 'new contents for a\n')])
250
self.assertStatusEqual(
251
[('a-id', 'a', 'modified', 'a'),
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')
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'),
265
# 'c' is not considered renamed, because only its parent was moved, it
266
# stayed in the same directory
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'),
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'])
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
285
# tree.rename_one('b', 'c')
286
# os.remove('tree/c')
287
# self.build_tree(['tree/c/'])
289
self.assertStatusEqual(
290
[('a-id', 'a', 'kind changed', 'a => a/'),
291
# ('b-id', 'c', True, 'b => c/', 'renamed and modified'),
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'])
301
tree.remove('b', force=True)
303
self.assertStatusEqual(
304
[('a-id', 'a', 'removed', 'a'),
305
('b-id', 'b', 'removed', 'b/'),