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
24
class InstrumentedReporter(object):
28
def report(self, file_id, path, versioned, renamed, modified, exe_change,
30
self.calls.append((file_id, path, versioned, renamed, modified,
34
class TestReportChanges(tests.TestCase):
35
"""Test the new change reporting infrastructure"""
37
def assertReport(self, expected, file_id='fid', path='path',
38
versioned_change='unchanged', renamed=False,
39
modified='unchanged', exe_change=False,
40
kind=('file', 'file'), old_path=None):
42
def result_line(format, *args):
43
result.append(format % args)
44
inv = inventory.Inventory()
45
if old_path is not None:
46
inv.add(inventory.InventoryFile(file_id, old_path,
48
reporter = delta.ChangeReporter(inv, result_line)
49
reporter.report(file_id, path, versioned_change, renamed, modified,
51
self.assertEqualDiff(expected, result[0])
53
def test_rename(self):
54
self.assertReport('R old => path', renamed=True, old_path='old')
55
self.assertReport(' path')
58
self.assertReport(' K path => path/', modified='kind changed',
59
kind=('file', 'directory'))
60
self.assertReport(' K path/ => path', modified='kind changed',
61
kind=('directory', 'file'), old_path='old')
62
self.assertReport('RK old => path/', renamed=True,
63
modified='kind changed',
64
kind=('file', 'directory'), old_path='old')
66
self.assertReport(' N path/', modified='created',
67
kind=(None, 'directory'))
68
self.assertReport('+ path/', versioned_change='added',
69
modified='unchanged', kind=(None, 'directory'))
70
self.assertReport('+N path/', versioned_change='added',
71
modified='created', kind=(None, 'directory'))
72
self.assertReport('+M path/', versioned_change='added',
73
modified='modified', kind=(None, 'directory'))
74
self.assertReport('+K path => path/', versioned_change='added',
75
modified='kind changed', kind=('file', 'directory'))
77
def test_removal(self):
78
self.assertReport(' D path/', modified='deleted',
79
kind=('directory', None), old_path='old')
80
self.assertReport('- path/', versioned_change='removed',
81
kind=(None, 'directory'))
82
self.assertReport('-D path', versioned_change='removed',
83
modified='deleted', kind=('file', 'directory'))
85
def test_modification(self):
86
self.assertReport(' M path', modified='modified')
87
self.assertReport(' M* path', modified='modified', exe_change=True)
89
def assertChangesEqual(self,
93
versioned=(True, True),
94
parent_id=('pid', 'pid'),
95
name=('name', 'name'),
96
kind=('file', 'file'),
97
executable=(False, False),
98
versioned_change='unchanged',
100
modified='unchanged',
102
reporter = InstrumentedReporter()
103
delta.report_changes([(file_id, path, content_change, versioned,
104
parent_id, name, kind, executable)], reporter)
105
output = reporter.calls[0]
106
self.assertEqual(file_id, output[0])
107
self.assertEqual(path, output[1])
108
self.assertEqual(versioned_change, output[2])
109
self.assertEqual(renamed, output[3])
110
self.assertEqual(modified, output[4])
111
self.assertEqual(exe_change, output[5])
112
self.assertEqual(kind, output[6])
114
def test_report_changes(self):
115
"""Test change detection of report_changes"""
116
#Ensure no changes are detected by default
117
self.assertChangesEqual(modified='unchanged', renamed=False,
118
versioned_change='unchanged',
120
self.assertChangesEqual(modified='kind changed',
121
kind=('file', 'directory'))
122
self.assertChangesEqual(modified='created', kind=(None, 'directory'))
123
self.assertChangesEqual(modified='deleted', kind=('directory', None))
124
self.assertChangesEqual(content_change=True, modified='modified')
125
self.assertChangesEqual(renamed=True, name=('old', 'new'))
126
self.assertChangesEqual(renamed=True,
127
parent_id=('old-parent', 'new-parent'))
128
self.assertChangesEqual(versioned_change='added',
129
versioned=(False, True))
130
self.assertChangesEqual(versioned_change='removed',
131
versioned=(True, False))
132
# execute bit is only detected as "changed" if the file is and was
134
self.assertChangesEqual(exe_change=True, executable=(True, False))
135
self.assertChangesEqual(exe_change=False, executable=(True, False),
136
kind=('directory', 'directory'))
137
self.assertChangesEqual(exe_change=False, modified='kind changed',
138
executable=(False, True),
139
kind=('directory', 'file'))
141
# Now make sure they all work together
142
self.assertChangesEqual(versioned_change='removed',
143
modified='deleted', versioned=(True, False),
144
kind=('directory', None))
145
self.assertChangesEqual(versioned_change='removed',
146
modified='created', versioned=(True, False),
148
self.assertChangesEqual(versioned_change='removed',
149
modified='modified', renamed=True,
150
exe_change=True, versioned=(True, False),
151
content_change=True, name=('old', 'new'),
152
executable=(False, True))