~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to fai/arch/tests/test_output.py

  • Committer: Robert Collins
  • Date: 2005-09-14 11:27:20 UTC
  • mto: (147.2.6) (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: robertc@robertcollins.net-20050914112720-c66a21de86eafa6e
trim fai cribbage

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# arch-tag: 3fe5d578-07de-406e-950e-6bd26dc93af0
3
 
# Copyright (C) 2004--2005 Canonical Limited
4
 
#                    Author: David Allouche <david@allouche.net>
5
 
#
6
 
#    This program is free software; you can redistribute it and/or modify
7
 
#    it under the terms of the GNU General Public License as published by
8
 
#    the Free Software Foundation; either version 2 of the License, or
9
 
#    (at your option) any later version.
10
 
#
11
 
#    This program is distributed in the hope that it will be useful,
12
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#    GNU General Public License for more details.
15
 
#
16
 
#    You should have received a copy of the GNU General Public License
17
 
#    along with this program; if not, write to the Free Software
18
 
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
 
20
 
"""Test cases for output parsing.
21
 
 
22
 
Unit tests for output classifiers and integration tests with actual
23
 
output.
24
 
"""
25
 
 
26
 
import unittest
27
 
import arch
28
 
import framework
29
 
from framework import TestCase
30
 
 
31
 
 
32
 
class Classifiers(unittest.TestCase):
33
 
 
34
 
    tests = []
35
 
 
36
 
    def _classify_chatter_helper(self, classify):
37
 
        lines = ['* chatty', 'witty']
38
 
        classifier = classify(lines)
39
 
        chatter = classifier.next()
40
 
        self.assertEqual('chatty', chatter.text)
41
 
        self.assertEqual('* chatty', str(chatter))
42
 
        self.assertEqual(arch.Chatter, type(chatter))
43
 
        non_chatter = classifier.next()
44
 
        self.assert_(non_chatter is lines[1])
45
 
        self.assertRaises(StopIteration, classifier.next)
46
 
 
47
 
    def classify_chatter(self):
48
 
        """classify_chatter works"""
49
 
        self._classify_chatter_helper(arch.classify_chatter)
50
 
    tests.append('classify_chatter')
51
 
 
52
 
    def classify_changes(self):
53
 
        """classify_changes classifies chatter correctly"""
54
 
        self._classify_chatter_helper(arch.classify_changeset_creation)
55
 
    tests.append('classify_changes')
56
 
 
57
 
    def _classify_tree_change_helper(self, line, type_,
58
 
                                     nametype, name, **kwargs):
59
 
        classifier = arch.classify_changeset_creation([line])
60
 
        change = classifier.next()
61
 
        self.assertRaises(StopIteration, classifier.next)
62
 
        self.assertEqual(type_, type(change))
63
 
        self.assertEqual(line, str(change))
64
 
        self.assertEqual(name, change.name)
65
 
        self.assertEqual(nametype, type(change.name))
66
 
        for k, v in kwargs.items():
67
 
            # do not use getattr so backtraces show the attribute name
68
 
            if k == 'oldname': self.assertEqual(v, change.oldname)
69
 
            elif k == 'binary': self.assertEqual(v, change.binary)
70
 
            elif k == 'directory': self.assertEqual(v, change.directory)
71
 
            else: raise AssertionError('bad argument: %s=%r' % (k, v))
72
 
        for attr in ('oldname', 'binary', 'directory'):
73
 
            if attr not in kwargs:
74
 
                def thunk(): getattr(change, attr)
75
 
                self.assertRaises(AttributeError, thunk)
76
 
 
77
 
    def _classify_merge_outcome_helper(self, line, type_,
78
 
                                       nametype, name, directory):
79
 
        classifier = arch.classify_changeset_application([line])
80
 
        change = classifier.next()
81
 
        self.assertRaises(StopIteration, classifier.next)
82
 
        self.assertEqual(type_, type(change))
83
 
        self.assertEqual(line, str(change))
84
 
        self.assertEqual(name, change.name)
85
 
        self.assertEqual(nametype, type(change.name))
86
 
        self.assertEqual(directory , change.directory)
87
 
 
88
 
 
89
 
class ClassifyFileAddition(Classifiers):
90
 
    tests = []
91
 
    def classify_changes_addition(self):
92
 
         """classify_changes classifies FileAddition"""
93
 
         type_ = arch.FileAddition
94
 
         self._classify_tree_change_helper(
95
 
             'A  foo', type_, arch.FileName, 'foo', directory=False)
96
 
         self._classify_tree_change_helper(
97
 
             'A/ foo', type_, arch.DirName, 'foo', directory=True)
98
 
         self._classify_tree_change_helper(
99
 
             'A  fo\(sp)o', type_, arch.FileName, 'fo o', directory=False)
100
 
         self._classify_tree_change_helper(
101
 
             'A/ fo\(sp)o', type_, arch.DirName, 'fo o', directory=True)
102
 
    tests.append('classify_changes_addition')
103
 
 
104
 
 
105
 
class ClassifyFileDeletion(Classifiers):
106
 
    tests = []
107
 
    def classify_changes_deletion(self):
108
 
         """classify_changes classifies FileDeletion"""
109
 
         type_ = arch.FileDeletion
110
 
         self._classify_tree_change_helper(
111
 
             'D  foo', type_, arch.FileName, 'foo', directory=False)
112
 
         self._classify_tree_change_helper(
113
 
             'D/ foo', type_, arch.DirName, 'foo', directory=True)
114
 
         self._classify_tree_change_helper(
115
 
             'D  fo\(sp)o', type_, arch.FileName, 'fo o', directory=False)
116
 
         self._classify_tree_change_helper(
117
 
             'D/ fo\(sp)o', type_, arch.DirName, 'fo o', directory=True)
118
 
    tests.append('classify_changes_deletion')
119
 
 
120
 
 
121
 
class ClassifyFileModification(Classifiers):
122
 
    tests = []
123
 
    def classify_changes_modification(self):
124
 
         """classify_changes classifies FileModification"""
125
 
         type_ = arch.FileModification
126
 
         self._classify_tree_change_helper(
127
 
             'M  foo', type_, arch.FileName, 'foo', binary=False,
128
 
             directory=False)
129
 
         self._classify_tree_change_helper(
130
 
             'Mb foo', type_, arch.FileName, 'foo', binary=True,
131
 
             directory=False)
132
 
         self._classify_tree_change_helper(
133
 
             'M  fo\(sp)o', type_, arch.FileName, 'fo o', binary=False,
134
 
             directory=False)
135
 
         self._classify_tree_change_helper(
136
 
             'Mb fo\(sp)o', type_, arch.FileName, 'fo o', binary=True,
137
 
             directory=False)
138
 
    tests.append('classify_changes_modification')
139
 
 
140
 
 
141
 
class ClassifyFilePermissionChange(Classifiers):
142
 
    tests = []
143
 
    def classify_changes_permissions(self):
144
 
        """classify_changes classifies FilePermissionsChange"""
145
 
        type_ = arch.FilePermissionsChange
146
 
        self._classify_tree_change_helper(
147
 
            '-- foo', type_, arch.FileName, 'foo', directory=False)
148
 
        self._classify_tree_change_helper(
149
 
            '-/ foo', type_, arch.DirName, 'foo', directory=True)
150
 
        self._classify_tree_change_helper(
151
 
            '-- fo\(sp)o', type_, arch.FileName, 'fo o', directory=False)
152
 
        self._classify_tree_change_helper(
153
 
            '-/ fo\(sp)o', type_, arch.DirName, 'fo o', directory=True)
154
 
    tests.append('classify_changes_permissions')
155
 
 
156
 
 
157
 
class ClassifyFileRename(Classifiers):
158
 
    tests = []
159
 
    def classify_changes_rename(self):
160
 
        """classify_changes classifies FileRename"""
161
 
        type_ = arch.FileRename
162
 
        self._classify_tree_change_helper(
163
 
            '=> bar\tfoo', type_, arch.FileName, 'foo',
164
 
            directory=False, oldname='bar')
165
 
        self._classify_tree_change_helper(
166
 
            '/> bar\tfoo', type_, arch.DirName, 'foo',
167
 
            directory=True, oldname='bar')
168
 
        self._classify_tree_change_helper(
169
 
            '=> b\(sp)ar\tfo\(sp)o', type_, arch.FileName, 'fo o',
170
 
            directory=False, oldname='b ar')
171
 
        self._classify_tree_change_helper(
172
 
            '/> b\(sp)ar\tfo\(sp)o', type_, arch.DirName, 'fo o',
173
 
            directory=True, oldname='b ar')
174
 
    tests.append('classify_changes_rename')
175
 
 
176
 
 
177
 
class ClassifySymLinkModification(Classifiers):
178
 
    tests = []
179
 
    def classify_changes_symlink(self):
180
 
        """classify_changes classifies SymlinkModification"""
181
 
        self._classify_tree_change_helper(
182
 
            '-> foo', arch.SymlinkModification, arch.FileName, 'foo',
183
 
            directory=False)
184
 
        self._classify_tree_change_helper(
185
 
            '-> fo\(sp)o', arch.SymlinkModification, arch.FileName, 'fo o',
186
 
            directory=False)
187
 
    tests.append('classify_changes_symlink')
188
 
 
189
 
 
190
 
class ClassifyPatchConflict(Classifiers):
191
 
    tests = []
192
 
    def classify_changes_conflict(self):
193
 
        """classify_changes classifies PatchConflict"""
194
 
        self._classify_merge_outcome_helper(
195
 
            'C   foo', arch.PatchConflict, arch.FileName, 'foo',
196
 
            directory=False)
197
 
        self._classify_merge_outcome_helper(
198
 
            'C   fo\(sp)o', arch.PatchConflict, arch.FileName, 'fo o',
199
 
            directory=False)
200
 
    tests.append('classify_changes_conflict')
201
 
 
202
 
 
203
 
def test_suite(limit=()):
204
 
    classes = (
205
 
        'Classifiers',
206
 
        'ClassifyFileAddition',
207
 
        'ClassifyFileDeletion',
208
 
        'ClassifyFileModification',
209
 
        'ClassifyFilePermissionChange',
210
 
        'ClassifyFileRename',
211
 
        'ClassifySymLinkModification',
212
 
        'ClassifyPatchConflict',
213
 
        )
214
 
    return framework.make_test_suite(globals(), classes, limit)
215
 
 
216
 
def main(argv):
217
 
    return framework.run_test_suite(argv, test_suite)
218
 
 
219
 
if __name__ == "__main__":
220
 
    sys.exit(main(sys.argv))