/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2225.1.1 by Aaron Bentley
Added revert change display, with tests
1
# Copyright (C) 2007 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
1551.10.6 by Aaron Bentley
Support kind changes in tree deltas
17
import os
18
from StringIO import StringIO
19
2225.1.1 by Aaron Bentley
Added revert change display, with tests
20
from bzrlib import (
1551.10.6 by Aaron Bentley
Support kind changes in tree deltas
21
    delta as _mod_delta,
2225.1.1 by Aaron Bentley
Added revert change display, with tests
22
    inventory,
23
    tests,
24
    )
25
26
2225.1.2 by Aaron Bentley
Ensure that changes are detected correctly
27
class InstrumentedReporter(object):
28
    def __init__(self):
29
        self.calls = []
30
31
    def report(self, file_id, path, versioned, renamed, modified, exe_change,
32
               kind):
33
        self.calls.append((file_id, path, versioned, renamed, modified,
34
                           exe_change, kind))
35
2225.1.4 by Aaron Bentley
PEP8 cleanup
36
2225.1.1 by Aaron Bentley
Added revert change display, with tests
37
class TestReportChanges(tests.TestCase):
2225.1.4 by Aaron Bentley
PEP8 cleanup
38
    """Test the new change reporting infrastructure"""
2225.1.1 by Aaron Bentley
Added revert change display, with tests
39
2225.1.3 by Aaron Bentley
change method names to assertFoo
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):
2225.1.1 by Aaron Bentley
Added revert change display, with tests
44
        result = []
45
        def result_line(format, *args):
46
            result.append(format % args)
47
        inv = inventory.Inventory()
48
        if old_path is not None:
2225.1.2 by Aaron Bentley
Ensure that changes are detected correctly
49
            inv.add(inventory.InventoryFile(file_id, old_path,
2225.1.1 by Aaron Bentley
Added revert change display, with tests
50
                                            inv.root.file_id))
1551.10.6 by Aaron Bentley
Support kind changes in tree deltas
51
        reporter = _mod_delta.ChangeReporter(inv, result_line)
2225.1.2 by Aaron Bentley
Ensure that changes are detected correctly
52
        reporter.report(file_id, path, versioned_change, renamed, modified,
2225.1.1 by Aaron Bentley
Added revert change display, with tests
53
                         exe_change, kind)
54
        self.assertEqualDiff(expected, result[0])
55
56
    def test_rename(self):
2225.1.3 by Aaron Bentley
change method names to assertFoo
57
        self.assertReport('R   old => path', renamed=True, old_path='old')
58
        self.assertReport('    path')
2225.1.1 by Aaron Bentley
Added revert change display, with tests
59
60
    def test_kind(self):
2225.1.3 by Aaron Bentley
change method names to assertFoo
61
        self.assertReport(' K  path => path/', modified='kind changed',
62
                          kind=('file', 'directory'))
63
        self.assertReport(' K  path/ => path', modified='kind changed',
64
                          kind=('directory', 'file'), old_path='old')
65
        self.assertReport('RK  old => path/', renamed=True,
2225.1.5 by Aaron Bentley
Clean up whitespace changes
66
                          modified='kind changed',
2225.1.3 by Aaron Bentley
change method names to assertFoo
67
                          kind=('file', 'directory'), old_path='old')
2225.1.1 by Aaron Bentley
Added revert change display, with tests
68
    def test_new(self):
2225.1.3 by Aaron Bentley
change method names to assertFoo
69
        self.assertReport(' N  path/', modified='created',
70
                          kind=(None, 'directory'))
71
        self.assertReport('+   path/', versioned_change='added',
72
                          modified='unchanged', kind=(None, 'directory'))
73
        self.assertReport('+N  path/', versioned_change='added',
74
                          modified='created', kind=(None, 'directory'))
75
        self.assertReport('+M  path/', versioned_change='added',
76
                          modified='modified', kind=(None, 'directory'))
77
        self.assertReport('+K  path => path/', versioned_change='added',
78
                          modified='kind changed', kind=('file', 'directory'))
2225.1.1 by Aaron Bentley
Added revert change display, with tests
79
80
    def test_removal(self):
2225.1.3 by Aaron Bentley
change method names to assertFoo
81
        self.assertReport(' D  path/', modified='deleted',
82
                          kind=('directory', None), old_path='old')
83
        self.assertReport('-   path/', versioned_change='removed',
84
                          kind=(None, 'directory'))
85
        self.assertReport('-D  path', versioned_change='removed',
86
                          modified='deleted', kind=('file', 'directory'))
2225.1.1 by Aaron Bentley
Added revert change display, with tests
87
88
    def test_modification(self):
2225.1.3 by Aaron Bentley
change method names to assertFoo
89
        self.assertReport(' M  path', modified='modified')
90
        self.assertReport(' M* path', modified='modified', exe_change=True)
2225.1.2 by Aaron Bentley
Ensure that changes are detected correctly
91
2225.1.3 by Aaron Bentley
change method names to assertFoo
92
    def assertChangesEqual(self,
93
                           file_id='fid',
94
                           path='path',
95
                           content_change=False,
96
                           versioned=(True, True),
97
                           parent_id=('pid', 'pid'),
98
                           name=('name', 'name'),
99
                           kind=('file', 'file'),
100
                           executable=(False, False),
101
                           versioned_change='unchanged',
102
                           renamed=False,
103
                           modified='unchanged',
104
                           exe_change=False):
2225.1.2 by Aaron Bentley
Ensure that changes are detected correctly
105
        reporter = InstrumentedReporter()
1551.10.6 by Aaron Bentley
Support kind changes in tree deltas
106
        _mod_delta.report_changes([(file_id, path, content_change, versioned,
107
            parent_id, name, kind, executable)], reporter)
2225.1.2 by Aaron Bentley
Ensure that changes are detected correctly
108
        output = reporter.calls[0]
109
        self.assertEqual(file_id, output[0])
110
        self.assertEqual(path, output[1])
111
        self.assertEqual(versioned_change, output[2])
112
        self.assertEqual(renamed, output[3])
113
        self.assertEqual(modified, output[4])
114
        self.assertEqual(exe_change, output[5])
115
        self.assertEqual(kind, output[6])
116
117
    def test_report_changes(self):
118
        """Test change detection of report_changes"""
119
        #Ensure no changes are detected by default
2225.1.3 by Aaron Bentley
change method names to assertFoo
120
        self.assertChangesEqual(modified='unchanged', renamed=False,
121
                                versioned_change='unchanged',
122
                                exe_change=False)
123
        self.assertChangesEqual(modified='kind changed',
124
                                kind=('file', 'directory'))
125
        self.assertChangesEqual(modified='created', kind=(None, 'directory'))
126
        self.assertChangesEqual(modified='deleted', kind=('directory', None))
127
        self.assertChangesEqual(content_change=True, modified='modified')
128
        self.assertChangesEqual(renamed=True, name=('old', 'new'))
129
        self.assertChangesEqual(renamed=True,
130
                                parent_id=('old-parent', 'new-parent'))
131
        self.assertChangesEqual(versioned_change='added',
132
                                versioned=(False, True))
133
        self.assertChangesEqual(versioned_change='removed',
134
                                versioned=(True, False))
2225.1.2 by Aaron Bentley
Ensure that changes are detected correctly
135
        # execute bit is only detected as "changed" if the file is and was
136
        # a regular file.
2225.1.3 by Aaron Bentley
change method names to assertFoo
137
        self.assertChangesEqual(exe_change=True, executable=(True, False))
138
        self.assertChangesEqual(exe_change=False, executable=(True, False),
139
                                kind=('directory', 'directory'))
140
        self.assertChangesEqual(exe_change=False, modified='kind changed',
141
                                executable=(False, True),
142
                                kind=('directory', 'file'))
2225.1.2 by Aaron Bentley
Ensure that changes are detected correctly
143
144
        # Now make sure they all work together
2225.1.3 by Aaron Bentley
change method names to assertFoo
145
        self.assertChangesEqual(versioned_change='removed',
146
                                modified='deleted', versioned=(True, False),
147
                                kind=('directory', None))
148
        self.assertChangesEqual(versioned_change='removed',
149
                                modified='created', versioned=(True, False),
150
                                kind=(None, 'file'))
151
        self.assertChangesEqual(versioned_change='removed',
152
                                modified='modified', renamed=True,
153
                                exe_change=True, versioned=(True, False),
154
                                content_change=True, name=('old', 'new'),
155
                                executable=(False, True))
1551.10.6 by Aaron Bentley
Support kind changes in tree deltas
156
157
158
class TestChangesFrom (tests.TestCaseWithTransport):
159
160
    def show_string(self, delta, *args,  **kwargs):
161
        to_file = StringIO()
162
        delta.show(to_file, *args, **kwargs)
163
        return to_file.getvalue()
164
165
    def test_kind_change(self):
166
        """Doing a status when a file has changed kind should work"""
167
        tree = self.make_branch_and_tree('.')
168
        self.build_tree(['filename'])
169
        tree.add('filename', 'file-id')
170
        tree.commit('added filename')
171
        os.unlink('filename')
172
        self.build_tree(['filename/'])
173
        delta = tree.changes_from(tree.basis_tree())
174
        self.assertEqual([('filename', 'file-id', 'file', 'directory')],
175
                         delta.kind_changed)
176
        self.assertEqual([], delta.added)
177
        self.assertEqual([], delta.removed)
178
        self.assertEqual([], delta.renamed)
179
        self.assertEqual([], delta.modified)
180
        self.assertEqual([], delta.unchanged)
181
        self.assertTrue(delta.has_changed())
182
        self.assertTrue(delta.touches_file_id('file-id'))
183
        self.assertEqual('kind changed:\n  filename (file => directory)\n',
184
                         self.show_string(delta))
185
        other_delta = _mod_delta.TreeDelta()
186
        self.assertNotEqual(other_delta, delta)
187
        other_delta.kind_changed = [('filename', 'file-id', 'file',
188
                                     'symlink')]
189
        self.assertNotEqual(other_delta, delta)
190
        other_delta.kind_changed = [('filename', 'file-id', 'file',
191
                                     'directory')]
192
        self.assertEqual(other_delta, delta)
193
        self.assertEqualDiff("TreeDelta(added=[], removed=[], renamed=[],"
194
            " kind_changed=[(u'filename', 'file-id', 'file', 'directory')],"
195
            " modified=[], unchanged=[])", repr(delta))
196
        self.assertEqual('K  filename (file => directory) file-id\n',
197
                         self.show_string(delta, show_ids=True,
198
                         short_status=True))
199
200
        tree.rename_one('filename', 'dirname')
201
        delta = tree.changes_from(tree.basis_tree())
202
        self.assertEqual([], delta.kind_changed)
203
        # This loses the fact that kind changed, remembering it as a
204
        # modification
205
        self.assertEqual([('filename', 'dirname', 'file-id', 'directory',
206
                           True, False)], delta.renamed)
207
        self.assertTrue(delta.has_changed())
208
        self.assertTrue(delta.touches_file_id('file-id'))