ยปCore Development>Code coverage>Lib/distutils/tests/test_filelist.py

Python code coverage for Lib/distutils/tests/test_filelist.py

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