1 | n/a | """Tests for distutils.filelist.""" |
---|
2 | n/a | import os |
---|
3 | n/a | import re |
---|
4 | n/a | import unittest |
---|
5 | n/a | from distutils import debug |
---|
6 | n/a | from distutils.log import WARN |
---|
7 | n/a | from distutils.errors import DistutilsTemplateError |
---|
8 | n/a | from distutils.filelist import glob_to_re, translate_pattern, FileList |
---|
9 | n/a | from distutils import filelist |
---|
10 | n/a | |
---|
11 | n/a | import test.support |
---|
12 | n/a | from test.support import captured_stdout, run_unittest |
---|
13 | n/a | from distutils.tests import support |
---|
14 | n/a | |
---|
15 | n/a | MANIFEST_IN = """\ |
---|
16 | n/a | include ok |
---|
17 | n/a | include xo |
---|
18 | n/a | exclude xo |
---|
19 | n/a | include foo.tmp |
---|
20 | n/a | include buildout.cfg |
---|
21 | n/a | global-include *.x |
---|
22 | n/a | global-include *.txt |
---|
23 | n/a | global-exclude *.tmp |
---|
24 | n/a | recursive-include f *.oo |
---|
25 | n/a | recursive-exclude global *.x |
---|
26 | n/a | graft dir |
---|
27 | n/a | prune dir3 |
---|
28 | n/a | """ |
---|
29 | n/a | |
---|
30 | n/a | |
---|
31 | n/a | def make_local_path(s): |
---|
32 | n/a | """Converts '/' in a string to os.sep""" |
---|
33 | n/a | return s.replace('/', os.sep) |
---|
34 | n/a | |
---|
35 | n/a | |
---|
36 | n/a | class FileListTestCase(support.LoggingSilencer, |
---|
37 | n/a | unittest.TestCase): |
---|
38 | n/a | |
---|
39 | n/a | def assertNoWarnings(self): |
---|
40 | n/a | self.assertEqual(self.get_logs(WARN), []) |
---|
41 | n/a | self.clear_logs() |
---|
42 | n/a | |
---|
43 | n/a | def assertWarnings(self): |
---|
44 | n/a | self.assertGreater(len(self.get_logs(WARN)), 0) |
---|
45 | n/a | self.clear_logs() |
---|
46 | n/a | |
---|
47 | n/a | def test_glob_to_re(self): |
---|
48 | n/a | sep = os.sep |
---|
49 | n/a | if os.sep == '\\': |
---|
50 | n/a | sep = re.escape(os.sep) |
---|
51 | n/a | |
---|
52 | n/a | for glob, regex in ( |
---|
53 | n/a | # simple cases |
---|
54 | n/a | ('foo*', r'(?s:foo[^%(sep)s]*)\Z'), |
---|
55 | n/a | ('foo?', r'(?s:foo[^%(sep)s])\Z'), |
---|
56 | n/a | ('foo??', r'(?s:foo[^%(sep)s][^%(sep)s])\Z'), |
---|
57 | n/a | # special cases |
---|
58 | n/a | (r'foo\\*', r'(?s:foo\\\\[^%(sep)s]*)\Z'), |
---|
59 | n/a | (r'foo\\\*', r'(?s:foo\\\\\\[^%(sep)s]*)\Z'), |
---|
60 | n/a | ('foo????', r'(?s:foo[^%(sep)s][^%(sep)s][^%(sep)s][^%(sep)s])\Z'), |
---|
61 | n/a | (r'foo\\??', r'(?s:foo\\\\[^%(sep)s][^%(sep)s])\Z')): |
---|
62 | n/a | regex = regex % {'sep': sep} |
---|
63 | n/a | self.assertEqual(glob_to_re(glob), regex) |
---|
64 | n/a | |
---|
65 | n/a | def test_process_template_line(self): |
---|
66 | n/a | # testing all MANIFEST.in template patterns |
---|
67 | n/a | file_list = FileList() |
---|
68 | n/a | l = make_local_path |
---|
69 | n/a | |
---|
70 | n/a | # simulated file list |
---|
71 | n/a | file_list.allfiles = ['foo.tmp', 'ok', 'xo', 'four.txt', |
---|
72 | n/a | 'buildout.cfg', |
---|
73 | n/a | # filelist does not filter out VCS directories, |
---|
74 | n/a | # it's sdist that does |
---|
75 | n/a | l('.hg/last-message.txt'), |
---|
76 | n/a | l('global/one.txt'), |
---|
77 | n/a | l('global/two.txt'), |
---|
78 | n/a | l('global/files.x'), |
---|
79 | n/a | l('global/here.tmp'), |
---|
80 | n/a | l('f/o/f.oo'), |
---|
81 | n/a | l('dir/graft-one'), |
---|
82 | n/a | l('dir/dir2/graft2'), |
---|
83 | n/a | l('dir3/ok'), |
---|
84 | n/a | l('dir3/sub/ok.txt'), |
---|
85 | n/a | ] |
---|
86 | n/a | |
---|
87 | n/a | for line in MANIFEST_IN.split('\n'): |
---|
88 | n/a | if line.strip() == '': |
---|
89 | n/a | continue |
---|
90 | n/a | file_list.process_template_line(line) |
---|
91 | n/a | |
---|
92 | n/a | wanted = ['ok', |
---|
93 | n/a | 'buildout.cfg', |
---|
94 | n/a | 'four.txt', |
---|
95 | n/a | l('.hg/last-message.txt'), |
---|
96 | n/a | l('global/one.txt'), |
---|
97 | n/a | l('global/two.txt'), |
---|
98 | n/a | l('f/o/f.oo'), |
---|
99 | n/a | l('dir/graft-one'), |
---|
100 | n/a | l('dir/dir2/graft2'), |
---|
101 | n/a | ] |
---|
102 | n/a | |
---|
103 | n/a | self.assertEqual(file_list.files, wanted) |
---|
104 | n/a | |
---|
105 | n/a | def test_debug_print(self): |
---|
106 | n/a | file_list = FileList() |
---|
107 | n/a | with captured_stdout() as stdout: |
---|
108 | n/a | file_list.debug_print('xxx') |
---|
109 | n/a | self.assertEqual(stdout.getvalue(), '') |
---|
110 | n/a | |
---|
111 | n/a | debug.DEBUG = True |
---|
112 | n/a | try: |
---|
113 | n/a | with captured_stdout() as stdout: |
---|
114 | n/a | file_list.debug_print('xxx') |
---|
115 | n/a | self.assertEqual(stdout.getvalue(), 'xxx\n') |
---|
116 | n/a | finally: |
---|
117 | n/a | debug.DEBUG = False |
---|
118 | n/a | |
---|
119 | n/a | def test_set_allfiles(self): |
---|
120 | n/a | file_list = FileList() |
---|
121 | n/a | files = ['a', 'b', 'c'] |
---|
122 | n/a | file_list.set_allfiles(files) |
---|
123 | n/a | self.assertEqual(file_list.allfiles, files) |
---|
124 | n/a | |
---|
125 | n/a | def test_remove_duplicates(self): |
---|
126 | n/a | file_list = FileList() |
---|
127 | n/a | file_list.files = ['a', 'b', 'a', 'g', 'c', 'g'] |
---|
128 | n/a | # files must be sorted beforehand (sdist does it) |
---|
129 | n/a | file_list.sort() |
---|
130 | n/a | file_list.remove_duplicates() |
---|
131 | n/a | self.assertEqual(file_list.files, ['a', 'b', 'c', 'g']) |
---|
132 | n/a | |
---|
133 | n/a | def test_translate_pattern(self): |
---|
134 | n/a | # not regex |
---|
135 | n/a | self.assertTrue(hasattr( |
---|
136 | n/a | translate_pattern('a', anchor=True, is_regex=False), |
---|
137 | n/a | 'search')) |
---|
138 | n/a | |
---|
139 | n/a | # is a regex |
---|
140 | n/a | regex = re.compile('a') |
---|
141 | n/a | self.assertEqual( |
---|
142 | n/a | translate_pattern(regex, anchor=True, is_regex=True), |
---|
143 | n/a | regex) |
---|
144 | n/a | |
---|
145 | n/a | # plain string flagged as regex |
---|
146 | n/a | self.assertTrue(hasattr( |
---|
147 | n/a | translate_pattern('a', anchor=True, is_regex=True), |
---|
148 | n/a | 'search')) |
---|
149 | n/a | |
---|
150 | n/a | # glob support |
---|
151 | n/a | self.assertTrue(translate_pattern( |
---|
152 | n/a | '*.py', anchor=True, is_regex=False).search('filelist.py')) |
---|
153 | n/a | |
---|
154 | n/a | def test_exclude_pattern(self): |
---|
155 | n/a | # return False if no match |
---|
156 | n/a | file_list = FileList() |
---|
157 | n/a | self.assertFalse(file_list.exclude_pattern('*.py')) |
---|
158 | n/a | |
---|
159 | n/a | # return True if files match |
---|
160 | n/a | file_list = FileList() |
---|
161 | n/a | file_list.files = ['a.py', 'b.py'] |
---|
162 | n/a | self.assertTrue(file_list.exclude_pattern('*.py')) |
---|
163 | n/a | |
---|
164 | n/a | # test excludes |
---|
165 | n/a | file_list = FileList() |
---|
166 | n/a | file_list.files = ['a.py', 'a.txt'] |
---|
167 | n/a | file_list.exclude_pattern('*.py') |
---|
168 | n/a | self.assertEqual(file_list.files, ['a.txt']) |
---|
169 | n/a | |
---|
170 | n/a | def test_include_pattern(self): |
---|
171 | n/a | # return False if no match |
---|
172 | n/a | file_list = FileList() |
---|
173 | n/a | file_list.set_allfiles([]) |
---|
174 | n/a | self.assertFalse(file_list.include_pattern('*.py')) |
---|
175 | n/a | |
---|
176 | n/a | # return True if files match |
---|
177 | n/a | file_list = FileList() |
---|
178 | n/a | file_list.set_allfiles(['a.py', 'b.txt']) |
---|
179 | n/a | self.assertTrue(file_list.include_pattern('*.py')) |
---|
180 | n/a | |
---|
181 | n/a | # test * matches all files |
---|
182 | n/a | file_list = FileList() |
---|
183 | n/a | self.assertIsNone(file_list.allfiles) |
---|
184 | n/a | file_list.set_allfiles(['a.py', 'b.txt']) |
---|
185 | n/a | file_list.include_pattern('*') |
---|
186 | n/a | self.assertEqual(file_list.allfiles, ['a.py', 'b.txt']) |
---|
187 | n/a | |
---|
188 | n/a | def test_process_template(self): |
---|
189 | n/a | l = make_local_path |
---|
190 | n/a | # invalid lines |
---|
191 | n/a | file_list = FileList() |
---|
192 | n/a | for action in ('include', 'exclude', 'global-include', |
---|
193 | n/a | 'global-exclude', 'recursive-include', |
---|
194 | n/a | 'recursive-exclude', 'graft', 'prune', 'blarg'): |
---|
195 | n/a | self.assertRaises(DistutilsTemplateError, |
---|
196 | n/a | file_list.process_template_line, action) |
---|
197 | n/a | |
---|
198 | n/a | # include |
---|
199 | n/a | file_list = FileList() |
---|
200 | n/a | file_list.set_allfiles(['a.py', 'b.txt', l('d/c.py')]) |
---|
201 | n/a | |
---|
202 | n/a | file_list.process_template_line('include *.py') |
---|
203 | n/a | self.assertEqual(file_list.files, ['a.py']) |
---|
204 | n/a | self.assertNoWarnings() |
---|
205 | n/a | |
---|
206 | n/a | file_list.process_template_line('include *.rb') |
---|
207 | n/a | self.assertEqual(file_list.files, ['a.py']) |
---|
208 | n/a | self.assertWarnings() |
---|
209 | n/a | |
---|
210 | n/a | # exclude |
---|
211 | n/a | file_list = FileList() |
---|
212 | n/a | file_list.files = ['a.py', 'b.txt', l('d/c.py')] |
---|
213 | n/a | |
---|
214 | n/a | file_list.process_template_line('exclude *.py') |
---|
215 | n/a | self.assertEqual(file_list.files, ['b.txt', l('d/c.py')]) |
---|
216 | n/a | self.assertNoWarnings() |
---|
217 | n/a | |
---|
218 | n/a | file_list.process_template_line('exclude *.rb') |
---|
219 | n/a | self.assertEqual(file_list.files, ['b.txt', l('d/c.py')]) |
---|
220 | n/a | self.assertWarnings() |
---|
221 | n/a | |
---|
222 | n/a | # global-include |
---|
223 | n/a | file_list = FileList() |
---|
224 | n/a | file_list.set_allfiles(['a.py', 'b.txt', l('d/c.py')]) |
---|
225 | n/a | |
---|
226 | n/a | file_list.process_template_line('global-include *.py') |
---|
227 | n/a | self.assertEqual(file_list.files, ['a.py', l('d/c.py')]) |
---|
228 | n/a | self.assertNoWarnings() |
---|
229 | n/a | |
---|
230 | n/a | file_list.process_template_line('global-include *.rb') |
---|
231 | n/a | self.assertEqual(file_list.files, ['a.py', l('d/c.py')]) |
---|
232 | n/a | self.assertWarnings() |
---|
233 | n/a | |
---|
234 | n/a | # global-exclude |
---|
235 | n/a | file_list = FileList() |
---|
236 | n/a | file_list.files = ['a.py', 'b.txt', l('d/c.py')] |
---|
237 | n/a | |
---|
238 | n/a | file_list.process_template_line('global-exclude *.py') |
---|
239 | n/a | self.assertEqual(file_list.files, ['b.txt']) |
---|
240 | n/a | self.assertNoWarnings() |
---|
241 | n/a | |
---|
242 | n/a | file_list.process_template_line('global-exclude *.rb') |
---|
243 | n/a | self.assertEqual(file_list.files, ['b.txt']) |
---|
244 | n/a | self.assertWarnings() |
---|
245 | n/a | |
---|
246 | n/a | # recursive-include |
---|
247 | n/a | file_list = FileList() |
---|
248 | n/a | file_list.set_allfiles(['a.py', l('d/b.py'), l('d/c.txt'), |
---|
249 | n/a | l('d/d/e.py')]) |
---|
250 | n/a | |
---|
251 | n/a | file_list.process_template_line('recursive-include d *.py') |
---|
252 | n/a | self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')]) |
---|
253 | n/a | self.assertNoWarnings() |
---|
254 | n/a | |
---|
255 | n/a | file_list.process_template_line('recursive-include e *.py') |
---|
256 | n/a | self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')]) |
---|
257 | n/a | self.assertWarnings() |
---|
258 | n/a | |
---|
259 | n/a | # recursive-exclude |
---|
260 | n/a | file_list = FileList() |
---|
261 | n/a | file_list.files = ['a.py', l('d/b.py'), l('d/c.txt'), l('d/d/e.py')] |
---|
262 | n/a | |
---|
263 | n/a | file_list.process_template_line('recursive-exclude d *.py') |
---|
264 | n/a | self.assertEqual(file_list.files, ['a.py', l('d/c.txt')]) |
---|
265 | n/a | self.assertNoWarnings() |
---|
266 | n/a | |
---|
267 | n/a | file_list.process_template_line('recursive-exclude e *.py') |
---|
268 | n/a | self.assertEqual(file_list.files, ['a.py', l('d/c.txt')]) |
---|
269 | n/a | self.assertWarnings() |
---|
270 | n/a | |
---|
271 | n/a | # graft |
---|
272 | n/a | file_list = FileList() |
---|
273 | n/a | file_list.set_allfiles(['a.py', l('d/b.py'), l('d/d/e.py'), |
---|
274 | n/a | l('f/f.py')]) |
---|
275 | n/a | |
---|
276 | n/a | file_list.process_template_line('graft d') |
---|
277 | n/a | self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')]) |
---|
278 | n/a | self.assertNoWarnings() |
---|
279 | n/a | |
---|
280 | n/a | file_list.process_template_line('graft e') |
---|
281 | n/a | self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')]) |
---|
282 | n/a | self.assertWarnings() |
---|
283 | n/a | |
---|
284 | n/a | # prune |
---|
285 | n/a | file_list = FileList() |
---|
286 | n/a | file_list.files = ['a.py', l('d/b.py'), l('d/d/e.py'), l('f/f.py')] |
---|
287 | n/a | |
---|
288 | n/a | file_list.process_template_line('prune d') |
---|
289 | n/a | self.assertEqual(file_list.files, ['a.py', l('f/f.py')]) |
---|
290 | n/a | self.assertNoWarnings() |
---|
291 | n/a | |
---|
292 | n/a | file_list.process_template_line('prune e') |
---|
293 | n/a | self.assertEqual(file_list.files, ['a.py', l('f/f.py')]) |
---|
294 | n/a | self.assertWarnings() |
---|
295 | n/a | |
---|
296 | n/a | |
---|
297 | n/a | class FindAllTestCase(unittest.TestCase): |
---|
298 | n/a | @test.support.skip_unless_symlink |
---|
299 | n/a | def test_missing_symlink(self): |
---|
300 | n/a | with test.support.temp_cwd(): |
---|
301 | n/a | os.symlink('foo', 'bar') |
---|
302 | n/a | self.assertEqual(filelist.findall(), []) |
---|
303 | n/a | |
---|
304 | n/a | def test_basic_discovery(self): |
---|
305 | n/a | """ |
---|
306 | n/a | When findall is called with no parameters or with |
---|
307 | n/a | '.' as the parameter, the dot should be omitted from |
---|
308 | n/a | the results. |
---|
309 | n/a | """ |
---|
310 | n/a | with test.support.temp_cwd(): |
---|
311 | n/a | os.mkdir('foo') |
---|
312 | n/a | file1 = os.path.join('foo', 'file1.txt') |
---|
313 | n/a | test.support.create_empty_file(file1) |
---|
314 | n/a | os.mkdir('bar') |
---|
315 | n/a | file2 = os.path.join('bar', 'file2.txt') |
---|
316 | n/a | test.support.create_empty_file(file2) |
---|
317 | n/a | expected = [file2, file1] |
---|
318 | n/a | self.assertEqual(sorted(filelist.findall()), expected) |
---|
319 | n/a | |
---|
320 | n/a | def test_non_local_discovery(self): |
---|
321 | n/a | """ |
---|
322 | n/a | When findall is called with another path, the full |
---|
323 | n/a | path name should be returned. |
---|
324 | n/a | """ |
---|
325 | n/a | with test.support.temp_dir() as temp_dir: |
---|
326 | n/a | file1 = os.path.join(temp_dir, 'file1.txt') |
---|
327 | n/a | test.support.create_empty_file(file1) |
---|
328 | n/a | expected = [file1] |
---|
329 | n/a | self.assertEqual(filelist.findall(temp_dir), expected) |
---|
330 | n/a | |
---|
331 | n/a | |
---|
332 | n/a | def test_suite(): |
---|
333 | n/a | return unittest.TestSuite([ |
---|
334 | n/a | unittest.makeSuite(FileListTestCase), |
---|
335 | n/a | unittest.makeSuite(FindAllTestCase), |
---|
336 | n/a | ]) |
---|
337 | n/a | |
---|
338 | n/a | |
---|
339 | n/a | if __name__ == "__main__": |
---|
340 | n/a | run_unittest(test_suite()) |
---|