13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from cStringIO import StringIO
18
from StringIO import StringIO
20
20
from bzrlib import (
21
21
delta as _mod_delta,
22
revision as _mod_revision,
40
40
def assertReport(self, expected, file_id='fid', path='path',
41
41
versioned_change='unchanged', renamed=False,
42
42
modified='unchanged', exe_change=False,
43
kind=('file', 'file'), old_path=None,
44
unversioned_filter=None, view_info=None):
48
expected_lines = [expected]
49
self.assertReportLines(expected_lines, file_id, path,
50
versioned_change, renamed,
53
unversioned_filter, view_info)
55
def assertReportLines(self, expected_lines, file_id='fid', path='path',
56
versioned_change='unchanged', renamed=False,
57
modified='unchanged', exe_change=False,
58
kind=('file', 'file'), old_path=None,
59
unversioned_filter=None, view_info=None):
43
kind=('file', 'file'), old_path=None):
61
45
def result_line(format, *args):
62
46
result.append(format % args)
63
reporter = _mod_delta._ChangeReporter(result_line,
64
unversioned_filter=unversioned_filter, view_info=view_info)
65
reporter.report(file_id, (old_path, path), versioned_change, renamed,
66
modified, exe_change, kind)
67
if expected_lines is not None:
68
for i in range(len(expected_lines)):
69
self.assertEqualDiff(expected_lines[i], result[i])
71
self.assertEqual([], result)
47
inv = inventory.Inventory()
48
if old_path is not None:
49
inv.add(inventory.InventoryFile(file_id, old_path,
51
reporter = _mod_delta.ChangeReporter(inv, result_line)
52
reporter.report(file_id, path, versioned_change, renamed, modified,
54
self.assertEqualDiff(expected, result[0])
73
56
def test_rename(self):
74
57
self.assertReport('R old => path', renamed=True, old_path='old')
79
62
def test_kind(self):
80
63
self.assertReport(' K path => path/', modified='kind changed',
81
kind=('file', 'directory'), old_path='path')
64
kind=('file', 'directory'))
82
65
self.assertReport(' K path/ => path', modified='kind changed',
83
66
kind=('directory', 'file'), old_path='old')
84
67
self.assertReport('RK old => path/', renamed=True,
95
78
modified='created', kind=(None, 'directory'))
96
79
self.assertReport('+M path/', versioned_change='added',
97
80
modified='modified', kind=(None, 'directory'))
81
self.assertReport('+K path => path/', versioned_change='added',
82
modified='kind changed', kind=('file', 'directory'))
99
84
def test_removal(self):
100
85
self.assertReport(' D path/', modified='deleted',
101
86
kind=('directory', None), old_path='old')
102
87
self.assertReport('- path/', versioned_change='removed',
104
88
kind=(None, 'directory'))
105
89
self.assertReport('-D path', versioned_change='removed',
107
90
modified='deleted', kind=('file', 'directory'))
109
92
def test_modification(self):
110
93
self.assertReport(' M path', modified='modified')
111
94
self.assertReport(' M* path', modified='modified', exe_change=True)
113
def test_unversioned(self):
114
# by default any unversioned file is output
115
self.assertReport('? subdir/foo~', file_id=None, path='subdir/foo~',
116
old_path=None, versioned_change='unversioned',
117
renamed=False, modified='created', exe_change=False,
119
# but we can choose to filter these. Probably that should be done
120
# close to the tree, but this is a reasonable starting point.
121
self.assertReport(None, file_id=None, path='subdir/foo~',
122
old_path=None, versioned_change='unversioned',
123
renamed=False, modified='created', exe_change=False,
124
kind=(None, 'file'), unversioned_filter=lambda x:True)
126
def test_view_filtering(self):
127
# If a file in within the view, it should appear in the output
129
"Operating on whole tree but only reporting on 'my' view.",
131
self.assertReportLines(expected_lines, modified='modified',
132
view_info=('my',['path']))
133
# If a file in outside the view, it should not appear in the output
135
"Operating on whole tree but only reporting on 'my' view."]
136
self.assertReportLines(expected_lines, modified='modified',
137
path="foo", view_info=('my',['path']))
139
96
def assertChangesEqual(self,
141
paths=('path', 'path'),
142
99
content_change=False,
143
100
versioned=(True, True),
144
101
parent_id=('pid', 'pid'),
150
107
modified='unchanged',
151
108
exe_change=False):
152
109
reporter = InstrumentedReporter()
153
_mod_delta.report_changes([(file_id, paths, content_change, versioned,
110
_mod_delta.report_changes([(file_id, path, content_change, versioned,
154
111
parent_id, name, kind, executable)], reporter)
155
112
output = reporter.calls[0]
156
113
self.assertEqual(file_id, output[0])
157
self.assertEqual(paths, output[1])
114
self.assertEqual(path, output[1])
158
115
self.assertEqual(versioned_change, output[2])
159
116
self.assertEqual(renamed, output[3])
160
117
self.assertEqual(modified, output[4])
202
159
content_change=True, name=('old', 'new'),
203
160
executable=(False, True))
205
def test_report_unversioned(self):
206
"""Unversioned entries are reported well."""
207
self.assertChangesEqual(file_id=None, paths=(None, 'full/path'),
209
versioned=(False, False),
210
parent_id=(None, None),
213
executable=(None, False),
214
versioned_change='unversioned',
220
class TestChangesFrom(tests.TestCaseWithTransport):
163
class TestChangesFrom (tests.TestCaseWithTransport):
222
165
def show_string(self, delta, *args, **kwargs):
223
166
to_file = StringIO()
224
_mod_delta.report_delta(to_file, delta, *args, **kwargs)
167
delta.show(to_file, *args, **kwargs)
225
168
return to_file.getvalue()
227
170
def test_kind_change(self):
254
197
self.assertEqual(other_delta, delta)
255
198
self.assertEqualDiff("TreeDelta(added=[], removed=[], renamed=[],"
256
199
" kind_changed=[(u'filename', 'file-id', 'file', 'directory')],"
257
" modified=[], unchanged=[], unversioned=[])", repr(delta))
200
" modified=[], unchanged=[])", repr(delta))
258
201
self.assertEqual('K filename (file => directory) file-id\n',
259
202
self.show_string(delta, show_ids=True,
260
203
short_status=True))
268
211
True, False)], delta.renamed)
269
212
self.assertTrue(delta.has_changed())
270
213
self.assertTrue(delta.touches_file_id('file-id'))
273
class TestDeltaShow(tests.TestCaseWithTransport):
275
def _get_delta(self):
276
# We build the delta from a real tree to avoid depending on internal
277
# implementation details.
278
wt = self.make_branch_and_tree('branch')
279
self.build_tree_contents([('branch/f1', '1\n'),
280
('branch/f2', '2\n'),
281
('branch/f3', '3\n'),
282
('branch/f4', '4\n'),
285
wt.add(['f1', 'f2', 'f3', 'f4', 'dir'],
286
['f1-id', 'f2-id', 'f3-id', 'f4-id', 'dir-id'])
287
wt.commit('commit one', rev_id='1')
289
long_status = """added:
296
short_status = """A dir/
303
repo = wt.branch.repository
304
d = wt.changes_from(repo.revision_tree(_mod_revision.NULL_REVISION))
305
return d, long_status, short_status
307
def test_delta_show_short_status_no_filter(self):
308
d, long_status, short_status = self._get_delta()
310
_mod_delta.report_delta(out, d, short_status=True)
311
self.assertEquals(short_status, out.getvalue())
313
def test_delta_show_long_status_no_filter(self):
314
d, long_status, short_status = self._get_delta()
316
_mod_delta.report_delta(out, d, short_status=False)
317
self.assertEquals(long_status, out.getvalue())
319
def test_delta_show_no_filter(self):
320
d, long_status, short_status = self._get_delta()
322
def not_a_filter(path, file_id):
324
_mod_delta.report_delta(out, d, short_status=True, filter=not_a_filter)
325
self.assertEquals(short_status, out.getvalue())
327
def test_delta_show_short_status_single_file_filter(self):
328
d, long_status, short_status = self._get_delta()
330
def only_f2(path, file_id):
332
_mod_delta.report_delta(out, d, short_status=True, filter=only_f2)
333
self.assertEquals("A f2\n", out.getvalue())
335
def test_delta_show_long_status_single_file_filter(self):
336
d, long_status, short_status = self._get_delta()
338
def only_f2(path, file_id):
340
_mod_delta.report_delta(out, d, short_status=False, filter=only_f2)
341
self.assertEquals("added:\n f2\n", out.getvalue())
343
def test_delta_show_short_status_single_file_id_filter(self):
344
d, long_status, short_status = self._get_delta()
346
def only_f2_id(path, file_id):
347
return file_id == 'f2-id'
348
_mod_delta.report_delta(out, d, short_status=True, filter=only_f2_id)
349
self.assertEquals("A f2\n", out.getvalue())