1
from __future__ import generators
2
import re, StringIO, unittest, sys
3
sys.path.insert(0, "../src")
4
from selection import *
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("/")))
12
self.root = Path("testfiles/select")
13
self.Select = Select(self.root)
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
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
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)
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
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)
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
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
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
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
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
76
def testFilelistInclude(self):
77
"""Test included filelist"""
78
fp = StringIO.StringIO("""
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
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
109
def testFilelistExclude(self):
110
"""Test included filelist"""
111
fp = StringIO.StringIO("""
114
this is a badly formed line which should be ignored
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
128
def testFilelistInclude2(self):
129
"""testFilelistInclude2 - with modifiers"""
130
fp = StringIO.StringIO("""
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
145
def testFilelistExclude2(self):
146
"""testFilelistExclude2 - with modifiers"""
147
fp = StringIO.StringIO("""
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
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
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)
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)
197
"""testRoot - / may be a counterexample to several of these.."""
199
select = Select(root)
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
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
214
select.filelist_get_sf(StringIO.StringIO("/"), 1, "test")(root) == 1
215
select.filelist_get_sf(StringIO.StringIO("/foo/bar"), 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
221
def testOtherFilesystems(self):
222
"""Test to see if --exclude-other-filesystems works correctly"""
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"
234
class ParseArgsTest(unittest.TestCase):
235
"""Test argument parsing"""
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)
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')])
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'),
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'),
275
('1', '2', '1'), ('1', '2', '2'), ('1', '2', '3'),
279
('2', '1', '1'), ('2', '1', '2'), ('2', '1', '3'),
281
('2', '2', '1'), ('2', '2', '2'), ('2', '2', '3'),
283
('2', '3', '1'), ('2', '3', '2'), ('2', '3', '3'),
287
('3', '2', '1'), ('3', '2', '2'), ('3', '2', '3'),
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')])
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')])
310
self.root = Path("/")
311
self.ParseTest([("--exclude", "/home/*"),
312
("--include", "/home"),
316
if __name__ == "__main__": unittest.main()