PageRenderTime 136ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/distutils/tests/test_filelist.py

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