1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from cStringIO import StringIO
22
revision as _mod_revision,
27
class InstrumentedReporter(object):
31
def report(self, file_id, path, versioned, renamed, modified, exe_change,
33
self.calls.append((file_id, path, versioned, renamed, modified,
37
class TestReportChanges(tests.TestCase):
38
"""Test the new change reporting infrastructure"""
40
def assertReport(self, expected, file_id='fid', path='path',
41
versioned_change='unchanged', renamed=False,
42
modified='unchanged', exe_change=False,
43
kind=('file', 'file'), old_path=None,
44
unversioned_filter=None):
46
def result_line(format, *args):
47
result.append(format % args)
48
reporter = _mod_delta._ChangeReporter(result_line,
49
unversioned_filter=unversioned_filter)
50
reporter.report(file_id, (old_path, path), versioned_change, renamed,
51
modified, exe_change, kind)
52
if expected is not None:
53
self.assertEqualDiff(expected, result[0])
55
self.assertEqual([], result)
57
def test_rename(self):
58
self.assertReport('R old => path', renamed=True, old_path='old')
59
self.assertReport(' path')
60
self.assertReport('RN old => path', renamed=True, old_path='old',
61
modified='created', kind=(None, 'file'))
64
self.assertReport(' K path => path/', modified='kind changed',
65
kind=('file', 'directory'), old_path='path')
66
self.assertReport(' K path/ => path', modified='kind changed',
67
kind=('directory', 'file'), old_path='old')
68
self.assertReport('RK old => path/', renamed=True,
69
modified='kind changed',
70
kind=('file', 'directory'), old_path='old')
72
self.assertReport(' N path/', modified='created',
73
kind=(None, 'directory'))
74
self.assertReport('+ path/', versioned_change='added',
75
modified='unchanged', kind=(None, 'directory'))
76
self.assertReport('+ path', versioned_change='added',
77
modified='unchanged', kind=(None, None))
78
self.assertReport('+N path/', versioned_change='added',
79
modified='created', kind=(None, 'directory'))
80
self.assertReport('+M path/', versioned_change='added',
81
modified='modified', kind=(None, 'directory'))
83
def test_removal(self):
84
self.assertReport(' D path/', modified='deleted',
85
kind=('directory', None), old_path='old')
86
self.assertReport('- path/', versioned_change='removed',
88
kind=(None, 'directory'))
89
self.assertReport('-D path', versioned_change='removed',
91
modified='deleted', kind=('file', 'directory'))
93
def test_modification(self):
94
self.assertReport(' M path', modified='modified')
95
self.assertReport(' M* path', modified='modified', exe_change=True)
97
def test_unversioned(self):
98
# by default any unversioned file is output
99
self.assertReport('? subdir/foo~', file_id=None, path='subdir/foo~',
100
old_path=None, versioned_change='unversioned',
101
renamed=False, modified='created', exe_change=False,
103
# but we can choose to filter these. Probably that should be done
104
# close to the tree, but this is a reasonable starting point.
105
self.assertReport(None, file_id=None, path='subdir/foo~',
106
old_path=None, versioned_change='unversioned',
107
renamed=False, modified='created', exe_change=False,
108
kind=(None, 'file'), unversioned_filter=lambda x:True)
110
def assertChangesEqual(self,
112
paths=('path', 'path'),
113
content_change=False,
114
versioned=(True, True),
115
parent_id=('pid', 'pid'),
116
name=('name', 'name'),
117
kind=('file', 'file'),
118
executable=(False, False),
119
versioned_change='unchanged',
121
modified='unchanged',
123
reporter = InstrumentedReporter()
124
_mod_delta.report_changes([(file_id, paths, content_change, versioned,
125
parent_id, name, kind, executable)], reporter)
126
output = reporter.calls[0]
127
self.assertEqual(file_id, output[0])
128
self.assertEqual(paths, output[1])
129
self.assertEqual(versioned_change, output[2])
130
self.assertEqual(renamed, output[3])
131
self.assertEqual(modified, output[4])
132
self.assertEqual(exe_change, output[5])
133
self.assertEqual(kind, output[6])
135
def test_report_changes(self):
136
"""Test change detection of report_changes"""
137
#Ensure no changes are detected by default
138
self.assertChangesEqual(modified='unchanged', renamed=False,
139
versioned_change='unchanged',
141
self.assertChangesEqual(modified='kind changed',
142
kind=('file', 'directory'))
143
self.assertChangesEqual(modified='created', kind=(None, 'directory'))
144
self.assertChangesEqual(modified='deleted', kind=('directory', None))
145
self.assertChangesEqual(content_change=True, modified='modified')
146
self.assertChangesEqual(renamed=True, name=('old', 'new'))
147
self.assertChangesEqual(renamed=True,
148
parent_id=('old-parent', 'new-parent'))
149
self.assertChangesEqual(versioned_change='added',
150
versioned=(False, True))
151
self.assertChangesEqual(versioned_change='removed',
152
versioned=(True, False))
153
# execute bit is only detected as "changed" if the file is and was
155
self.assertChangesEqual(exe_change=True, executable=(True, False))
156
self.assertChangesEqual(exe_change=False, executable=(True, False),
157
kind=('directory', 'directory'))
158
self.assertChangesEqual(exe_change=False, modified='kind changed',
159
executable=(False, True),
160
kind=('directory', 'file'))
161
self.assertChangesEqual(parent_id=('pid', None))
163
# Now make sure they all work together
164
self.assertChangesEqual(versioned_change='removed',
165
modified='deleted', versioned=(True, False),
166
kind=('directory', None))
167
self.assertChangesEqual(versioned_change='removed',
168
modified='created', versioned=(True, False),
170
self.assertChangesEqual(versioned_change='removed',
171
modified='modified', renamed=True,
172
exe_change=True, versioned=(True, False),
173
content_change=True, name=('old', 'new'),
174
executable=(False, True))
176
def test_report_unversioned(self):
177
"""Unversioned entries are reported well."""
178
self.assertChangesEqual(file_id=None, paths=(None, 'full/path'),
180
versioned=(False, False),
181
parent_id=(None, None),
184
executable=(None, False),
185
versioned_change='unversioned',
191
class TestChangesFrom(tests.TestCaseWithTransport):
193
def show_string(self, delta, *args, **kwargs):
195
delta.show(to_file, *args, **kwargs)
196
return to_file.getvalue()
198
def test_kind_change(self):
199
"""Doing a status when a file has changed kind should work"""
200
tree = self.make_branch_and_tree('.')
201
self.build_tree(['filename'])
202
tree.add('filename', 'file-id')
203
tree.commit('added filename')
204
os.unlink('filename')
205
self.build_tree(['filename/'])
206
delta = tree.changes_from(tree.basis_tree())
207
self.assertEqual([('filename', 'file-id', 'file', 'directory')],
209
self.assertEqual([], delta.added)
210
self.assertEqual([], delta.removed)
211
self.assertEqual([], delta.renamed)
212
self.assertEqual([], delta.modified)
213
self.assertEqual([], delta.unchanged)
214
self.assertTrue(delta.has_changed())
215
self.assertTrue(delta.touches_file_id('file-id'))
216
self.assertEqual('kind changed:\n filename (file => directory)\n',
217
self.show_string(delta))
218
other_delta = _mod_delta.TreeDelta()
219
self.assertNotEqual(other_delta, delta)
220
other_delta.kind_changed = [('filename', 'file-id', 'file',
222
self.assertNotEqual(other_delta, delta)
223
other_delta.kind_changed = [('filename', 'file-id', 'file',
225
self.assertEqual(other_delta, delta)
226
self.assertEqualDiff("TreeDelta(added=[], removed=[], renamed=[],"
227
" kind_changed=[(u'filename', 'file-id', 'file', 'directory')],"
228
" modified=[], unchanged=[], unversioned=[])", repr(delta))
229
self.assertEqual('K filename (file => directory) file-id\n',
230
self.show_string(delta, show_ids=True,
233
tree.rename_one('filename', 'dirname')
234
delta = tree.changes_from(tree.basis_tree())
235
self.assertEqual([], delta.kind_changed)
236
# This loses the fact that kind changed, remembering it as a
238
self.assertEqual([('filename', 'dirname', 'file-id', 'directory',
239
True, False)], delta.renamed)
240
self.assertTrue(delta.has_changed())
241
self.assertTrue(delta.touches_file_id('file-id'))
244
class TestDeltaShow(tests.TestCaseWithTransport):
246
def _get_delta(self):
247
# We build the delta from a real tree to avoid depending on internal
248
# implementation details.
249
wt = self.make_branch_and_tree('branch')
250
self.build_tree_contents([('branch/f1', '1\n'),
251
('branch/f2', '2\n'),
252
('branch/f3', '3\n'),
253
('branch/f4', '4\n'),
256
wt.add(['f1', 'f2', 'f3', 'f4', 'dir'],
257
['f1-id', 'f2-id', 'f3-id', 'f4-id', 'dir-id'])
258
wt.commit('commit one', rev_id='1')
260
long_status = """added:
267
short_status = """A dir/
274
repo = wt.branch.repository
275
d = wt.changes_from(repo.revision_tree(_mod_revision.NULL_REVISION))
276
return d, long_status, short_status
278
def test_delta_show_short_status_no_filter(self):
279
d, long_status, short_status = self._get_delta()
281
d.show(out, short_status=True)
282
self.assertEquals(short_status, out.getvalue())
284
def test_delta_show_long_status_no_filter(self):
285
d, long_status, short_status = self._get_delta()
287
d.show(out, short_status=False)
288
self.assertEquals(long_status, out.getvalue())
290
def test_delta_show_no_filter(self):
291
d, long_status, short_status = self._get_delta()
293
def not_a_filter(path, file_id):
295
d.show(out, short_status=True, filter=not_a_filter)
296
self.assertEquals(short_status, out.getvalue())
298
def test_delta_show_short_status_single_file_filter(self):
299
d, long_status, short_status = self._get_delta()
301
def only_f2(path, file_id):
303
d.show(out, short_status=True, filter=only_f2)
304
self.assertEquals("A f2\n", out.getvalue())
306
def test_delta_show_long_status_single_file_filter(self):
307
d, long_status, short_status = self._get_delta()
309
def only_f2(path, file_id):
311
d.show(out, short_status=False, filter=only_f2)
312
self.assertEquals("added:\n f2\n", out.getvalue())
314
def test_delta_show_short_status_single_file_id_filter(self):
315
d, long_status, short_status = self._get_delta()
317
def only_f2_id(path, file_id):
318
return file_id == 'f2-id'
319
d.show(out, short_status=True, filter=only_f2_id)
320
self.assertEquals("A f2\n", out.getvalue())