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
self.requireFeature(tests.UnicodeFilenameFeature)
74
tree = self.make_branch_and_tree('tree')
75
self.build_tree([u'tree/\u03a9'])
76
tree.add([u'\u03a9'], ['omega-id'])
79
view.set_trees(tree, tree.basis_tree())
82
start, end = buf.get_bounds()
83
text = buf.get_text(start, end)
84
self.assertContainsRe(text,
85
"=== added file '\xce\xa9'\n"
86
'--- .*\t1970-01-01 00:00:00 \\+0000\n'
87
r'\+\+\+ .*\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d [+-]\d\d\d\d\n'
89
'\\+contents of tree/\xce\xa9\n'
94
class MockDiffWidget(object):
96
def set_diff_text_sections(self, sections):
97
self.sections = list(sections)
100
class MockWindow(object):
102
self.diff = MockDiffWidget()
103
self.merge_successful = False
105
def set_title(self, title):
108
def _get_save_path(self, basename):
111
def _get_merge_target(self):
117
def _merge_successful(self):
118
self.merge_successful = True
120
def _conflicts(self):
121
self.conflicts = True
123
def _handle_error(self, e):
124
self.handled_error = e
127
class TestDiffController(tests.TestCaseWithTransport):
129
def get_controller(self):
130
window = MockWindow()
131
return DiffController('load-path', eg_diff.splitlines(True), window)
133
def test_get_diff_sections(self):
134
controller = self.get_controller()
135
controller = DiffController('.', eg_diff.splitlines(True),
137
sections = list(controller.get_diff_sections())
138
self.assertEqual('Complete Diff', sections[0][0])
139
self.assertIs(None, sections[0][1])
140
self.assertEqual(eg_diff, sections[0][2])
142
self.assertEqual('tests/test_diff.py', sections[1][0])
143
self.assertEqual('tests/test_diff.py', sections[1][1])
144
self.assertEqual(''.join(eg_diff.splitlines(True)[1:]),
147
def test_initialize_window(self):
148
controller = self.get_controller()
149
controller.initialize_window(controller.window)
150
self.assertEqual(2, len(controller.window.diff.sections))
151
self.assertEqual('load-path - diff', controller.window.title)
153
def test_perform_save(self):
154
self.build_tree_contents([('load-path', 'foo')])
155
controller = self.get_controller()
156
controller.perform_save(None)
157
self.assertFileEqual('foo', 'save-path')
160
class TestMergeDirectiveController(tests.TestCaseWithTransport):
162
def make_this_other_directive(self):
163
this = self.make_branch_and_tree('this')
164
this.commit('first commit')
165
other = this.bzrdir.sprout('other').open_workingtree()
166
self.build_tree_contents([('other/foo', 'bar')])
168
other.commit('second commit')
171
directive = MergeDirective2.from_objects(other.branch.repository,
172
other.last_revision(), 0,
176
return this, other, directive
178
def make_merged_window(self, directive):
179
window = MockWindow()
180
controller = MergeDirectiveController('directive', directive, window)
181
controller.perform_merge(window)
184
def test_perform_merge_success(self):
185
this, other, directive = self.make_this_other_directive()
186
window = self.make_merged_window(directive)
187
self.assertTrue(window.merge_successful)
188
self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
189
self.assertFileEqual('bar', 'this/foo')
191
def test_perform_merge_conflicts(self):
192
this, other, directive = self.make_this_other_directive()
193
self.build_tree_contents([('this/foo', 'bar')])
195
this.commit('message')
196
window = self.make_merged_window(directive)
197
self.assertFalse(window.merge_successful)
198
self.assertTrue(window.conflicts)
199
self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
200
self.assertFileEqual('bar', 'this/foo')
202
def test_perform_merge_uncommitted_changes(self):
203
this, other, directive = self.make_this_other_directive()
204
self.build_tree_contents([('this/foo', 'bar')])
206
window = self.make_merged_window(directive)
207
self.assertIsInstance(window.handled_error, errors.UncommittedChanges)
210
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
212
def assertStatusEqual(self, expected, tree):
213
values = iter_changes_to_status(tree.basis_tree(), tree)
214
self.assertEqual(expected, values)
216
def test_status_added(self):
217
tree = self.make_branch_and_tree('tree')
218
self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
219
tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
221
self.assertStatusEqual(
222
[('a-id', 'a', 'added', 'a'),
223
('b-id', 'b', 'added', 'b/'),
224
('c-id', 'b/c', 'added', 'b/c'),
227
def test_status_renamed(self):
228
tree = self.make_branch_and_tree('tree')
229
self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
230
tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
231
rev_id1 = tree.commit('one')
233
tree.rename_one('b', 'd')
234
tree.rename_one('a', 'd/a')
236
self.assertStatusEqual(
237
[('b-id', 'd', 'renamed', 'b/ => d/'),
238
('a-id', 'd/a', 'renamed', 'a => d/a'),
241
def test_status_modified(self):
242
tree = self.make_branch_and_tree('tree')
243
self.build_tree(['tree/a'])
244
tree.add(['a'], ['a-id'])
245
rev_id1 = tree.commit('one')
247
self.build_tree_contents([('tree/a', 'new contents for a\n')])
249
self.assertStatusEqual(
250
[('a-id', 'a', 'modified', 'a'),
253
def test_status_renamed_and_modified(self):
254
tree = self.make_branch_and_tree('tree')
255
self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
256
tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
257
rev_id1 = tree.commit('one')
259
tree.rename_one('b', 'd')
260
tree.rename_one('a', 'd/a')
261
self.build_tree_contents([('tree/d/a', 'new contents for a\n'),
262
('tree/d/c', 'new contents for c\n'),
264
# 'c' is not considered renamed, because only its parent was moved, it
265
# stayed in the same directory
267
self.assertStatusEqual(
268
[('b-id', 'd', 'renamed', 'b/ => d/'),
269
('a-id', 'd/a', 'renamed and modified', 'a => d/a'),
270
('c-id', 'd/c', 'modified', 'd/c'),
273
def test_status_kind_changed(self):
274
tree = self.make_branch_and_tree('tree')
275
self.build_tree(['tree/a', 'tree/b'])
276
tree.add(['a', 'b'], ['a-id', 'b-id'])
280
self.build_tree(['tree/a/'])
281
# XXX: This is technically valid, and the file list handles it fine,
282
# but 'show_diff_trees()' does not, so we skip this part of the
284
# tree.rename_one('b', 'c')
285
# os.remove('tree/c')
286
# self.build_tree(['tree/c/'])
288
self.assertStatusEqual(
289
[('a-id', 'a', 'kind changed', 'a => a/'),
290
# ('b-id', 'c', True, 'b => c/', 'renamed and modified'),
293
def test_status_removed(self):
294
tree = self.make_branch_and_tree('tree')
295
self.build_tree(['tree/a', 'tree/b/'])
296
tree.add(['a', 'b'], ['a-id', 'b-id'])
300
tree.remove('b', force=True)
302
self.assertStatusEqual(
303
[('a-id', 'a', 'removed', 'a'),
304
('b-id', 'b', 'removed', 'b/'),