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 StringIO import StringIO
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):
45
def result_line(format, *args):
46
result.append(format % args)
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])
56
def test_rename(self):
57
self.assertReport('R old => path', renamed=True, old_path='old')
58
self.assertReport(' path')
59
self.assertReport('RN old => path', renamed=True, old_path='old',
60
modified='created', kind=(None, 'file'))
63
self.assertReport(' K path => path/', modified='kind changed',
64
kind=('file', 'directory'))
65
self.assertReport(' K path/ => path', modified='kind changed',
66
kind=('directory', 'file'), old_path='old')
67
self.assertReport('RK old => path/', renamed=True,
68
modified='kind changed',
69
kind=('file', 'directory'), old_path='old')
71
self.assertReport(' N path/', modified='created',
72
kind=(None, 'directory'))
73
self.assertReport('+ path/', versioned_change='added',
74
modified='unchanged', kind=(None, 'directory'))
75
self.assertReport('+ path', versioned_change='added',
76
modified='unchanged', kind=(None, None))
77
self.assertReport('+N path/', versioned_change='added',
78
modified='created', kind=(None, 'directory'))
79
self.assertReport('+M path/', versioned_change='added',
80
modified='modified', kind=(None, 'directory'))
81
self.assertReport('+K path => path/', versioned_change='added',
82
modified='kind changed', kind=('file', 'directory'))
84
def test_removal(self):
85
self.assertReport(' D path/', modified='deleted',
86
kind=('directory', None), old_path='old')
87
self.assertReport('- path/', versioned_change='removed',
88
kind=(None, 'directory'))
89
self.assertReport('-D path', versioned_change='removed',
90
modified='deleted', kind=('file', 'directory'))
92
def test_modification(self):
93
self.assertReport(' M path', modified='modified')
94
self.assertReport(' M* path', modified='modified', exe_change=True)
96
def assertChangesEqual(self,
100
versioned=(True, True),
101
parent_id=('pid', 'pid'),
102
name=('name', 'name'),
103
kind=('file', 'file'),
104
executable=(False, False),
105
versioned_change='unchanged',
107
modified='unchanged',
109
reporter = InstrumentedReporter()
110
_mod_delta.report_changes([(file_id, path, content_change, versioned,
111
parent_id, name, kind, executable)], reporter)
112
output = reporter.calls[0]
113
self.assertEqual(file_id, output[0])
114
self.assertEqual(path, output[1])
115
self.assertEqual(versioned_change, output[2])
116
self.assertEqual(renamed, output[3])
117
self.assertEqual(modified, output[4])
118
self.assertEqual(exe_change, output[5])
119
self.assertEqual(kind, output[6])
121
def test_report_changes(self):
122
"""Test change detection of report_changes"""
123
#Ensure no changes are detected by default
124
self.assertChangesEqual(modified='unchanged', renamed=False,
125
versioned_change='unchanged',
127
self.assertChangesEqual(modified='kind changed',
128
kind=('file', 'directory'))
129
self.assertChangesEqual(modified='created', kind=(None, 'directory'))
130
self.assertChangesEqual(modified='deleted', kind=('directory', None))
131
self.assertChangesEqual(content_change=True, modified='modified')
132
self.assertChangesEqual(renamed=True, name=('old', 'new'))
133
self.assertChangesEqual(renamed=True,
134
parent_id=('old-parent', 'new-parent'))
135
self.assertChangesEqual(versioned_change='added',
136
versioned=(False, True))
137
self.assertChangesEqual(versioned_change='removed',
138
versioned=(True, False))
139
# execute bit is only detected as "changed" if the file is and was
141
self.assertChangesEqual(exe_change=True, executable=(True, False))
142
self.assertChangesEqual(exe_change=False, executable=(True, False),
143
kind=('directory', 'directory'))
144
self.assertChangesEqual(exe_change=False, modified='kind changed',
145
executable=(False, True),
146
kind=('directory', 'file'))
147
self.assertChangesEqual(parent_id=('pid', None))
149
# Now make sure they all work together
150
self.assertChangesEqual(versioned_change='removed',
151
modified='deleted', versioned=(True, False),
152
kind=('directory', None))
153
self.assertChangesEqual(versioned_change='removed',
154
modified='created', versioned=(True, False),
156
self.assertChangesEqual(versioned_change='removed',
157
modified='modified', renamed=True,
158
exe_change=True, versioned=(True, False),
159
content_change=True, name=('old', 'new'),
160
executable=(False, True))
163
class TestChangesFrom (tests.TestCaseWithTransport):
165
def show_string(self, delta, *args, **kwargs):
167
delta.show(to_file, *args, **kwargs)
168
return to_file.getvalue()
170
def test_kind_change(self):
171
"""Doing a status when a file has changed kind should work"""
172
tree = self.make_branch_and_tree('.')
173
self.build_tree(['filename'])
174
tree.add('filename', 'file-id')
175
tree.commit('added filename')
176
os.unlink('filename')
177
self.build_tree(['filename/'])
178
delta = tree.changes_from(tree.basis_tree())
179
self.assertEqual([('filename', 'file-id', 'file', 'directory')],
181
self.assertEqual([], delta.added)
182
self.assertEqual([], delta.removed)
183
self.assertEqual([], delta.renamed)
184
self.assertEqual([], delta.modified)
185
self.assertEqual([], delta.unchanged)
186
self.assertTrue(delta.has_changed())
187
self.assertTrue(delta.touches_file_id('file-id'))
188
self.assertEqual('kind changed:\n filename (file => directory)\n',
189
self.show_string(delta))
190
other_delta = _mod_delta.TreeDelta()
191
self.assertNotEqual(other_delta, delta)
192
other_delta.kind_changed = [('filename', 'file-id', 'file',
194
self.assertNotEqual(other_delta, delta)
195
other_delta.kind_changed = [('filename', 'file-id', 'file',
197
self.assertEqual(other_delta, delta)
198
self.assertEqualDiff("TreeDelta(added=[], removed=[], renamed=[],"
199
" kind_changed=[(u'filename', 'file-id', 'file', 'directory')],"
200
" modified=[], unchanged=[])", repr(delta))
201
self.assertEqual('K filename (file => directory) file-id\n',
202
self.show_string(delta, show_ids=True,
205
tree.rename_one('filename', 'dirname')
206
delta = tree.changes_from(tree.basis_tree())
207
self.assertEqual([], delta.kind_changed)
208
# This loses the fact that kind changed, remembering it as a
210
self.assertEqual([('filename', 'dirname', 'file-id', 'directory',
211
True, False)], delta.renamed)
212
self.assertTrue(delta.has_changed())
213
self.assertTrue(delta.touches_file_id('file-id'))