~ed.so/duplicity/reuse-passphrase-for-signing-fix

« back to all changes in this revision

Viewing changes to testing/selectiontest.py

  • Committer: bescoto
  • Date: 2002-10-29 01:49:46 UTC
  • Revision ID: vcs-imports@canonical.com-20021029014946-3m4rmm5plom7pl6q
Initial checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import generators
 
2
import re, StringIO, unittest, sys
 
3
sys.path.insert(0, "../src")
 
4
from selection import *
 
5
from lazy import *
 
6
 
 
7
class MatchingTest(unittest.TestCase):
 
8
        """Test matching of file names against various selection functions"""
 
9
        def makeext(self, path): return self.root.new_index(tuple(path.split("/")))
 
10
 
 
11
        def setUp(self):
 
12
                self.root = Path("testfiles/select")
 
13
                self.Select = Select(self.root)
 
14
 
 
15
        def testRegexp(self):
 
16
                """Test regular expression selection func"""
 
17
                sf1 = self.Select.regexp_get_sf(".*\.py", 1)
 
18
                assert sf1(self.makeext("1.py")) == 1
 
19
                assert sf1(self.makeext("usr/foo.py")) == 1
 
20
                assert sf1(self.root.append("1.doc")) == None
 
21
 
 
22
                sf2 = self.Select.regexp_get_sf("hello", 0)
 
23
                assert sf2(Path("hello")) == 0
 
24
                assert sf2(Path("foohello_there")) == 0
 
25
                assert sf2(Path("foo")) == None
 
26
 
 
27
        def testTupleInclude(self):
 
28
                """Test include selection function made from a regular filename"""
 
29
                self.assertRaises(FilePrefixError,
 
30
                                                  self.Select.glob_get_filename_sf, "foo", 1)
 
31
 
 
32
                sf2 = self.Select.glob_get_sf("testfiles/select/usr/local/bin/", 1)
 
33
                assert sf2(self.makeext("usr")) == 1
 
34
                assert sf2(self.makeext("usr/local")) == 1
 
35
                assert sf2(self.makeext("usr/local/bin")) == 1
 
36
                assert sf2(self.makeext("usr/local/doc")) == None
 
37
                assert sf2(self.makeext("usr/local/bin/gzip")) == 1
 
38
                assert sf2(self.makeext("usr/local/bingzip")) == None
 
39
 
 
40
        def testTupleExclude(self):
 
41
                """Test exclude selection function made from a regular filename"""
 
42
                self.assertRaises(FilePrefixError,
 
43
                                                  self.Select.glob_get_filename_sf, "foo", 0)
 
44
 
 
45
                sf2 = self.Select.glob_get_sf("testfiles/select/usr/local/bin/", 0)
 
46
                assert sf2(self.makeext("usr")) == None
 
47
                assert sf2(self.makeext("usr/local")) == None
 
48
                assert sf2(self.makeext("usr/local/bin")) == 0
 
49
                assert sf2(self.makeext("usr/local/doc")) == None
 
50
                assert sf2(self.makeext("usr/local/bin/gzip")) == 0
 
51
                assert sf2(self.makeext("usr/local/bingzip")) == None
 
52
 
 
53
        def testGlobStarInclude(self):
 
54
                """Test a few globbing patterns, including **"""
 
55
                sf1 = self.Select.glob_get_sf("**", 1)
 
56
                assert sf1(self.makeext("foo")) == 1
 
57
                assert sf1(self.makeext("")) == 1
 
58
 
 
59
                sf2 = self.Select.glob_get_sf("**.py", 1)
 
60
                assert sf2(self.makeext("foo")) == 2
 
61
                assert sf2(self.makeext("usr/local/bin")) == 2
 
62
                assert sf2(self.makeext("what/ever.py")) == 1
 
63
                assert sf2(self.makeext("what/ever.py/foo")) == 1
 
64
 
 
65
        def testGlobStarExclude(self):
 
66
                """Test a few glob excludes, including **"""
 
67
                sf1 = self.Select.glob_get_sf("**", 0)
 
68
                assert sf1(self.makeext("/usr/local/bin")) == 0
 
69
 
 
70
                sf2 = self.Select.glob_get_sf("**.py", 0)
 
71
                assert sf2(self.makeext("foo")) == None, sf2(self.makeext("foo"))
 
72
                assert sf2(self.makeext("usr/local/bin")) == None
 
73
                assert sf2(self.makeext("what/ever.py")) == 0
 
74
                assert sf2(self.makeext("what/ever.py/foo")) == 0
 
75
 
 
76
        def testFilelistInclude(self):
 
77
                """Test included filelist"""
 
78
                fp = StringIO.StringIO("""
 
79
testfiles/select/1/2
 
80
testfiles/select/1
 
81
testfiles/select/1/2/3
 
82
testfiles/select/3/3/2""")
 
83
                sf = self.Select.filelist_get_sf(fp, 1, "test")
 
84
                assert sf(self.root) == 1
 
85
                assert sf(self.makeext("1")) == 1
 
86
                assert sf(self.makeext("1/1")) == None
 
87
                assert sf(self.makeext("1/2/3")) == 1
 
88
                assert sf(self.makeext("2/2")) == None
 
89
                assert sf(self.makeext("3")) == 1
 
90
                assert sf(self.makeext("3/3")) == 1
 
91
                assert sf(self.makeext("3/3/3")) == None
 
92
 
 
93
        def testFilelistIncludeNullSep(self):
 
94
                """Test included filelist but with null_separator set"""
 
95
                fp = StringIO.StringIO("""\0testfiles/select/1/2\0testfiles/select/1\0testfiles/select/1/2/3\0testfiles/select/3/3/2\0testfiles/select/hello\nthere\0""")
 
96
                globals.null_separator = 1
 
97
                sf = self.Select.filelist_get_sf(fp, 1, "test")
 
98
                assert sf(self.root) == 1
 
99
                assert sf(self.makeext("1")) == 1
 
100
                assert sf(self.makeext("1/1")) == None
 
101
                assert sf(self.makeext("1/2/3")) == 1
 
102
                assert sf(self.makeext("2/2")) == None
 
103
                assert sf(self.makeext("3")) == 1
 
104
                assert sf(self.makeext("3/3")) == 1
 
105
                assert sf(self.makeext("3/3/3")) == None
 
106
                assert sf(self.makeext("hello\nthere")) == 1
 
107
                globals.null_separator = 1
 
108
 
 
109
        def testFilelistExclude(self):
 
110
                """Test included filelist"""
 
111
                fp = StringIO.StringIO("""
 
112
testfiles/select/1/2
 
113
testfiles/select/1
 
114
this is a badly formed line which should be ignored
 
115
 
 
116
testfiles/select/1/2/3
 
117
testfiles/select/3/3/2""")
 
118
                sf = self.Select.filelist_get_sf(fp, 0, "test")
 
119
                assert sf(self.root) == None
 
120
                assert sf(self.makeext("1")) == 0
 
121
                assert sf(self.makeext("1/1")) == 0
 
122
                assert sf(self.makeext("1/2/3")) == 0
 
123
                assert sf(self.makeext("2/2")) == None
 
124
                assert sf(self.makeext("3")) == None
 
125
                assert sf(self.makeext("3/3/2")) == 0
 
126
                assert sf(self.makeext("3/3/3")) == None
 
127
 
 
128
        def testFilelistInclude2(self):
 
129
                """testFilelistInclude2 - with modifiers"""
 
130
                fp = StringIO.StringIO("""
 
131
testfiles/select/1/1
 
132
- testfiles/select/1/2
 
133
+ testfiles/select/1/3
 
134
- testfiles/select/3""")
 
135
                sf = self.Select.filelist_get_sf(fp, 1, "test1")
 
136
                assert sf(self.makeext("1")) == 1
 
137
                assert sf(self.makeext("1/1")) == 1
 
138
                assert sf(self.makeext("1/1/2")) == None                                 
 
139
                assert sf(self.makeext("1/2")) == 0
 
140
                assert sf(self.makeext("1/2/3")) == 0
 
141
                assert sf(self.makeext("1/3")) == 1
 
142
                assert sf(self.makeext("2")) == None
 
143
                assert sf(self.makeext("3")) == 0
 
144
 
 
145
        def testFilelistExclude2(self):
 
146
                """testFilelistExclude2 - with modifiers"""
 
147
                fp = StringIO.StringIO("""
 
148
testfiles/select/1/1
 
149
- testfiles/select/1/2
 
150
+ testfiles/select/1/3
 
151
- testfiles/select/3""")
 
152
                sf = self.Select.filelist_get_sf(fp, 0, "test1")
 
153
                sf_val1 = sf(self.root)
 
154
                assert sf_val1 == 1 or sf_val1 == None # either is OK
 
155
                sf_val2 = sf(self.makeext("1"))
 
156
                assert sf_val2 == 1 or sf_val2 == None
 
157
                assert sf(self.makeext("1/1")) == 0
 
158
                assert sf(self.makeext("1/1/2")) == 0
 
159
                assert sf(self.makeext("1/2")) == 0
 
160
                assert sf(self.makeext("1/2/3")) == 0
 
161
                assert sf(self.makeext("1/3")) == 1
 
162
                assert sf(self.makeext("2")) == None
 
163
                assert sf(self.makeext("3")) == 0
 
164
 
 
165
        def testGlobRE(self):
 
166
                """testGlobRE - test translation of shell pattern to regular exp"""
 
167
                assert self.Select.glob_to_re("hello") == "hello"
 
168
                assert self.Select.glob_to_re(".e?ll**o") == "\\.e[^/]ll.*o"
 
169
                r = self.Select.glob_to_re("[abc]el[^de][!fg]h")
 
170
                assert r == "[abc]el[^de][^fg]h", r
 
171
                r = self.Select.glob_to_re("/usr/*/bin/")
 
172
                assert r == "\\/usr\\/[^/]*\\/bin\\/", r
 
173
                assert self.Select.glob_to_re("[a.b/c]") == "[a.b/c]"
 
174
                r = self.Select.glob_to_re("[a*b-c]e[!]]")
 
175
                assert r == "[a*b-c]e[^]]", r
 
176
 
 
177
        def testGlobSFException(self):
 
178
                """testGlobSFException - see if globbing errors returned"""
 
179
                self.assertRaises(GlobbingError, self.Select.glob_get_normal_sf,
 
180
                                                  "testfiles/select/hello//there", 1)
 
181
                self.assertRaises(FilePrefixError,
 
182
                                                  self.Select.glob_get_sf, "testfiles/whatever", 1)
 
183
                self.assertRaises(FilePrefixError,
 
184
                                                  self.Select.glob_get_sf, "testfiles/?hello", 0)
 
185
                assert self.Select.glob_get_normal_sf("**", 1)
 
186
 
 
187
        def testIgnoreCase(self):
 
188
                """testIgnoreCase - try a few expressions with ignorecase:"""
 
189
                sf = self.Select.glob_get_sf("ignorecase:testfiles/SeLect/foo/bar", 1)
 
190
                assert sf(self.makeext("FOO/BAR")) == 1
 
191
                assert sf(self.makeext("foo/bar")) == 1
 
192
                assert sf(self.makeext("fOo/BaR")) == 1
 
193
                self.assertRaises(FilePrefixError, self.Select.glob_get_sf,
 
194
                                                  "ignorecase:tesfiles/sect/foo/bar", 1)
 
195
                                                  
 
196
        def testRoot(self):
 
197
                """testRoot - / may be a counterexample to several of these.."""
 
198
                root = Path("/")
 
199
                select = Select(root)
 
200
 
 
201
                assert select.glob_get_sf("/", 1)(root) == 1
 
202
                assert select.glob_get_sf("/foo", 1)(root) == 1
 
203
                assert select.glob_get_sf("/foo/bar", 1)(root) == 1
 
204
                assert select.glob_get_sf("/", 0)(root) == 0
 
205
                assert select.glob_get_sf("/foo", 0)(root) == None
 
206
 
 
207
                assert select.glob_get_sf("**.py", 1)(root) == 2
 
208
                assert select.glob_get_sf("**", 1)(root) == 1
 
209
                assert select.glob_get_sf("ignorecase:/", 1)(root) == 1
 
210
                assert select.glob_get_sf("**.py", 0)(root) == None
 
211
                assert select.glob_get_sf("**", 0)(root) == 0
 
212
                assert select.glob_get_sf("/foo/*", 0)(root) == None
 
213
 
 
214
                select.filelist_get_sf(StringIO.StringIO("/"), 1, "test")(root) == 1
 
215
                select.filelist_get_sf(StringIO.StringIO("/foo/bar"), 1,
 
216
                                                           "test")(root) == 1
 
217
                select.filelist_get_sf(StringIO.StringIO("/"), 0, "test")(root) == 0
 
218
                select.filelist_get_sf(StringIO.StringIO("/foo/bar"), 0,
 
219
                                                           "test")(root) == None
 
220
 
 
221
        def testOtherFilesystems(self):
 
222
                """Test to see if --exclude-other-filesystems works correctly"""
 
223
                root = Path("/")
 
224
                select = Select(root)
 
225
                sf = select.other_filesystems_get_sf(0)
 
226
                assert sf(root) is None
 
227
                assert sf(Path("/usr/bin")) is None, \
 
228
                           "Assumption: /usr/bin is on the same filesystem as /"
 
229
                assert sf(Path("/proc")) == 0, \
 
230
                           "Assumption: /proc is on a different filesystem"
 
231
                assert sf(Path("/boot")) == 0, \
 
232
                           "Assumption: /boot is on a different filesystem"
 
233
 
 
234
class ParseArgsTest(unittest.TestCase):
 
235
        """Test argument parsing"""
 
236
        root = None
 
237
        def ParseTest(self, tuplelist, indicies):
 
238
                """No error if running select on tuple goes over indicies"""
 
239
                if not self.root: self.root = Path("testfiles/select")
 
240
                self.Select = Select(self.root)
 
241
                self.Select.ParseArgs(tuplelist)
 
242
                self.Select.set_iter()
 
243
                assert Iter.equal(Iter.map(lambda path: path.index, self.Select),
 
244
                                                  iter(indicies), verbose = 1)
 
245
 
 
246
        def testParse(self):
 
247
                """Test just one include, all exclude"""
 
248
                self.ParseTest([("--include", "testfiles/select/1/1"),
 
249
                                                ("--exclude", "**")],
 
250
                                           [(), ('1',), ("1", "1"), ("1", '1', '1'),
 
251
                                                         ('1', '1', '2'), ('1', '1', '3')])
 
252
                
 
253
        def testParse2(self):
 
254
                """Test three level include/exclude"""
 
255
                self.ParseTest([("--exclude", "testfiles/select/1/1/1"),
 
256
                                                           ("--include", "testfiles/select/1/1"),
 
257
                                                           ("--exclude", "testfiles/select/1"),
 
258
                                                           ("--exclude", "**")],
 
259
                                           [(), ('1',), ('1', '1'), ('1', '1', '2'),
 
260
                                                ('1', '1', '3')])
 
261
 
 
262
        def testGlob(self):
 
263
                """Test globbing expression"""
 
264
                self.ParseTest([("--exclude", "**[3-5]"),
 
265
                                                ("--include", "testfiles/select/1"),
 
266
                                                ("--exclude", "**")],
 
267
                                           [(), ('1',), ('1', '1'),
 
268
                                                ('1', '1', '1'), ('1', '1', '2'),
 
269
                                                ('1', '2'), ('1', '2', '1'), ('1', '2', '2')])
 
270
                self.ParseTest([("--include", "testfiles/select**/2"),
 
271
                                                ("--exclude", "**")],
 
272
                                           [(), ('1',), ('1', '1'),
 
273
                                                ('1', '1', '2'),
 
274
                                                ('1', '2'),
 
275
                                                ('1', '2', '1'), ('1', '2', '2'), ('1', '2', '3'),
 
276
                                                ('1', '3'),
 
277
                                                ('1', '3', '2'),
 
278
                                                ('2',), ('2', '1'),
 
279
                                                ('2', '1', '1'), ('2', '1', '2'), ('2', '1', '3'),
 
280
                                                ('2', '2'),
 
281
                                                ('2', '2', '1'), ('2', '2', '2'), ('2', '2', '3'),
 
282
                                                ('2', '3'),
 
283
                                                ('2', '3', '1'), ('2', '3', '2'), ('2', '3', '3'),
 
284
                                                ('3',), ('3', '1'),
 
285
                                                ('3', '1', '2'),
 
286
                                                ('3', '2'),
 
287
                                                ('3', '2', '1'), ('3', '2', '2'), ('3', '2', '3'),
 
288
                                                ('3', '3'),
 
289
                                                ('3', '3', '2')])
 
290
 
 
291
        def testGlob2(self):
 
292
                """Test more globbing functions"""
 
293
                self.ParseTest([("--include", "testfiles/select/*foo*/p*"),
 
294
                                                ("--exclude", "**")],
 
295
                                           [(), ('efools',), ('efools', 'ping'),
 
296
                                                ('foobar',), ('foobar', 'pong')])
 
297
                self.ParseTest([("--exclude", "testfiles/select/1/1/*"),
 
298
                                                ("--exclude", "testfiles/select/1/2/**"),
 
299
                                                ("--exclude", "testfiles/select/1/3**"),
 
300
                                                ("--include", "testfiles/select/1"),
 
301
                                                ("--exclude", "**")],
 
302
                                           [(), ('1',), ('1', '1'), ('1', '2')])
 
303
 
 
304
        def testAlternateRoot(self):
 
305
                """Test select with different root"""
 
306
                self.root = Path("testfiles/select/1")
 
307
                self.ParseTest([("--exclude", "testfiles/select/1/[23]")],
 
308
                                           [(), ('1',), ('1', '1'), ('1', '2'), ('1', '3')])
 
309
 
 
310
                self.root = Path("/")
 
311
                self.ParseTest([("--exclude", "/home/*"),
 
312
                                                ("--include", "/home"),
 
313
                                                ("--exclude", "/")],
 
314
                                           [(), ("home",)])
 
315
 
 
316
if __name__ == "__main__": unittest.main()