~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_delta.py

Merged bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
17
from bzrlib import (
 
18
    delta,
 
19
    inventory,
 
20
    tests,
 
21
    )
 
22
 
 
23
 
 
24
class InstrumentedReporter(object):
 
25
    def __init__(self):
 
26
        self.calls = []
 
27
 
 
28
    def report(self, file_id, path, versioned, renamed, modified, exe_change,
 
29
               kind):
 
30
        self.calls.append((file_id, path, versioned, renamed, modified,
 
31
                           exe_change, kind))
 
32
 
 
33
 
 
34
class TestReportChanges(tests.TestCase):
 
35
    """Test the new change reporting infrastructure"""
 
36
 
 
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):
 
41
        result = []
 
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,
 
47
                                            inv.root.file_id))
 
48
        reporter = delta.ChangeReporter(inv, result_line)
 
49
        reporter.report(file_id, path, versioned_change, renamed, modified,
 
50
                         exe_change, kind)
 
51
        self.assertEqualDiff(expected, result[0])
 
52
 
 
53
    def test_rename(self):
 
54
        self.assertReport('R   old => path', renamed=True, old_path='old')
 
55
        self.assertReport('    path')
 
56
 
 
57
    def test_kind(self):
 
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')
 
65
    def test_new(self):
 
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'))
 
76
 
 
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'))
 
84
 
 
85
    def test_modification(self):
 
86
        self.assertReport(' M  path', modified='modified')
 
87
        self.assertReport(' M* path', modified='modified', exe_change=True)
 
88
 
 
89
    def assertChangesEqual(self,
 
90
                           file_id='fid',
 
91
                           path='path',
 
92
                           content_change=False,
 
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',
 
99
                           renamed=False,
 
100
                           modified='unchanged',
 
101
                           exe_change=False):
 
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])
 
113
 
 
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',
 
119
                                exe_change=False)
 
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
 
133
        # a regular file.
 
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'))
 
140
 
 
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),
 
147
                                kind=(None, 'file'))
 
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))