Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa2cb3a

Browse files
committed
Increase test coverage for packaging.manifest (#11751).
Patch by Justin Love.
1 parent c822f08 commit aa2cb3a

2 files changed

Lines changed: 199 additions & 2 deletions

File tree

Lib/packaging/manifest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ def exclude_pattern(self, pattern, anchor=True, prefix=None,
147147

148148
def _parse_template_line(self, line):
149149
words = line.split()
150-
if len(words) == 1:
150+
if len(words) == 1 and words[0] not in (
151+
'include', 'exclude', 'global-include', 'global-exclude',
152+
'recursive-include', 'recursive-exclude', 'graft', 'prune'):
151153
# no action given, let's use the default 'include'
152154
words.insert(0, 'include')
153155

Lib/packaging/tests/test_manifest.py

Lines changed: 196 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Tests for packaging.manifest."""
22
import os
3+
import re
34
import logging
45
from io import StringIO
5-
from packaging.manifest import Manifest
6+
from packaging.errors import PackagingTemplateError
7+
from packaging.manifest import Manifest, _translate_pattern, _glob_to_re
68

79
from packaging.tests import unittest, support
810

@@ -34,6 +36,12 @@ def tearDown(self):
3436
os.chdir(self.cwd)
3537
super(ManifestTestCase, self).tearDown()
3638

39+
def assertNoWarnings(self):
40+
self.assertEqual(self.get_logs(logging.WARNING), [])
41+
42+
def assertWarnings(self):
43+
self.assertGreater(len(self.get_logs(logging.WARNING)), 0)
44+
3745
def test_manifest_reader(self):
3846
tmpdir = self.mkdtemp()
3947
MANIFEST = os.path.join(tmpdir, 'MANIFEST.in')
@@ -69,6 +77,193 @@ def test_default_actions(self):
6977
manifest.read_template(content)
7078
self.assertEqual(['README', 'file1'], manifest.files)
7179

80+
def test_glob_to_re(self):
81+
# simple cases
82+
self.assertEqual(_glob_to_re('foo*'), 'foo[^/]*\\Z(?ms)')
83+
self.assertEqual(_glob_to_re('foo?'), 'foo[^/]\\Z(?ms)')
84+
self.assertEqual(_glob_to_re('foo??'), 'foo[^/][^/]\\Z(?ms)')
85+
86+
# special cases
87+
self.assertEqual(_glob_to_re(r'foo\\*'), r'foo\\\\[^/]*\Z(?ms)')
88+
self.assertEqual(_glob_to_re(r'foo\\\*'), r'foo\\\\\\[^/]*\Z(?ms)')
89+
self.assertEqual(_glob_to_re('foo????'), r'foo[^/][^/][^/][^/]\Z(?ms)')
90+
self.assertEqual(_glob_to_re(r'foo\\??'), r'foo\\\\[^/][^/]\Z(?ms)')
91+
92+
def test_remove_duplicates(self):
93+
manifest = Manifest()
94+
manifest.files = ['a', 'b', 'a', 'g', 'c', 'g']
95+
# files must be sorted beforehand
96+
manifest.sort()
97+
manifest.remove_duplicates()
98+
self.assertEqual(manifest.files, ['a', 'b', 'c', 'g'])
99+
100+
def test_translate_pattern(self):
101+
# blackbox test of a private function
102+
103+
# not regex
104+
pattern = _translate_pattern('a', anchor=True, is_regex=False)
105+
self.assertTrue(hasattr(pattern, 'search'))
106+
107+
# is a regex
108+
regex = re.compile('a')
109+
pattern = _translate_pattern(regex, anchor=True, is_regex=True)
110+
self.assertEqual(pattern, regex)
111+
112+
# plain string flagged as regex
113+
pattern = _translate_pattern('a', anchor=True, is_regex=True)
114+
self.assertTrue(hasattr(pattern, 'search'))
115+
116+
# glob support
117+
pattern = _translate_pattern('*.py', anchor=True, is_regex=False)
118+
self.assertTrue(pattern.search('filelist.py'))
119+
120+
def test_exclude_pattern(self):
121+
# return False if no match
122+
manifest = Manifest()
123+
self.assertFalse(manifest.exclude_pattern('*.py'))
124+
125+
# return True if files match
126+
manifest = Manifest()
127+
manifest.files = ['a.py', 'b.py']
128+
self.assertTrue(manifest.exclude_pattern('*.py'))
129+
130+
# test excludes
131+
manifest = Manifest()
132+
manifest.files = ['a.py', 'a.txt']
133+
manifest.exclude_pattern('*.py')
134+
self.assertEqual(manifest.files, ['a.txt'])
135+
136+
def test_include_pattern(self):
137+
# return False if no match
138+
manifest = Manifest()
139+
manifest.allfiles = []
140+
self.assertFalse(manifest._include_pattern('*.py'))
141+
142+
# return True if files match
143+
manifest = Manifest()
144+
manifest.allfiles = ['a.py', 'b.txt']
145+
self.assertTrue(manifest._include_pattern('*.py'))
146+
147+
# test * matches all files
148+
manifest = Manifest()
149+
self.assertIsNone(manifest.allfiles)
150+
manifest.allfiles = ['a.py', 'b.txt']
151+
manifest._include_pattern('*')
152+
self.assertEqual(manifest.allfiles, ['a.py', 'b.txt'])
153+
154+
def test_process_template(self):
155+
# invalid lines
156+
manifest = Manifest()
157+
for action in ('include', 'exclude', 'global-include',
158+
'global-exclude', 'recursive-include',
159+
'recursive-exclude', 'graft', 'prune'):
160+
self.assertRaises(PackagingTemplateError,
161+
manifest._process_template_line, action)
162+
163+
# implicit include
164+
manifest = Manifest()
165+
manifest.allfiles = ['a.py', 'b.txt', 'd/c.py']
166+
167+
manifest._process_template_line('*.py')
168+
self.assertEqual(manifest.files, ['a.py'])
169+
self.assertNoWarnings()
170+
171+
# include
172+
manifest = Manifest()
173+
manifest.allfiles = ['a.py', 'b.txt', 'd/c.py']
174+
175+
manifest._process_template_line('include *.py')
176+
self.assertEqual(manifest.files, ['a.py'])
177+
self.assertNoWarnings()
178+
179+
manifest._process_template_line('include *.rb')
180+
self.assertEqual(manifest.files, ['a.py'])
181+
self.assertWarnings()
182+
183+
# exclude
184+
manifest = Manifest()
185+
manifest.files = ['a.py', 'b.txt', 'd/c.py']
186+
187+
manifest._process_template_line('exclude *.py')
188+
self.assertEqual(manifest.files, ['b.txt', 'd/c.py'])
189+
self.assertNoWarnings()
190+
191+
manifest._process_template_line('exclude *.rb')
192+
self.assertEqual(manifest.files, ['b.txt', 'd/c.py'])
193+
self.assertWarnings()
194+
195+
# global-include
196+
manifest = Manifest()
197+
manifest.allfiles = ['a.py', 'b.txt', 'd/c.py']
198+
199+
manifest._process_template_line('global-include *.py')
200+
self.assertEqual(manifest.files, ['a.py', 'd/c.py'])
201+
self.assertNoWarnings()
202+
203+
manifest._process_template_line('global-include *.rb')
204+
self.assertEqual(manifest.files, ['a.py', 'd/c.py'])
205+
self.assertWarnings()
206+
207+
# global-exclude
208+
manifest = Manifest()
209+
manifest.files = ['a.py', 'b.txt', 'd/c.py']
210+
211+
manifest._process_template_line('global-exclude *.py')
212+
self.assertEqual(manifest.files, ['b.txt'])
213+
self.assertNoWarnings()
214+
215+
manifest._process_template_line('global-exclude *.rb')
216+
self.assertEqual(manifest.files, ['b.txt'])
217+
self.assertWarnings()
218+
219+
# recursive-include
220+
manifest = Manifest()
221+
manifest.allfiles = ['a.py', 'd/b.py', 'd/c.txt', 'd/d/e.py']
222+
223+
manifest._process_template_line('recursive-include d *.py')
224+
self.assertEqual(manifest.files, ['d/b.py', 'd/d/e.py'])
225+
self.assertNoWarnings()
226+
227+
manifest._process_template_line('recursive-include e *.py')
228+
self.assertEqual(manifest.files, ['d/b.py', 'd/d/e.py'])
229+
self.assertWarnings()
230+
231+
# recursive-exclude
232+
manifest = Manifest()
233+
manifest.files = ['a.py', 'd/b.py', 'd/c.txt', 'd/d/e.py']
234+
235+
manifest._process_template_line('recursive-exclude d *.py')
236+
self.assertEqual(manifest.files, ['a.py', 'd/c.txt'])
237+
self.assertNoWarnings()
238+
239+
manifest._process_template_line('recursive-exclude e *.py')
240+
self.assertEqual(manifest.files, ['a.py', 'd/c.txt'])
241+
self.assertWarnings()
242+
243+
# graft
244+
manifest = Manifest()
245+
manifest.allfiles = ['a.py', 'd/b.py', 'd/d/e.py', 'f/f.py']
246+
247+
manifest._process_template_line('graft d')
248+
self.assertEqual(manifest.files, ['d/b.py', 'd/d/e.py'])
249+
self.assertNoWarnings()
250+
251+
manifest._process_template_line('graft e')
252+
self.assertEqual(manifest.files, ['d/b.py', 'd/d/e.py'])
253+
self.assertWarnings()
254+
255+
# prune
256+
manifest = Manifest()
257+
manifest.files = ['a.py', 'd/b.py', 'd/d/e.py', 'f/f.py']
258+
259+
manifest._process_template_line('prune d')
260+
self.assertEqual(manifest.files, ['a.py', 'f/f.py'])
261+
self.assertNoWarnings()
262+
263+
manifest._process_template_line('prune e')
264+
self.assertEqual(manifest.files, ['a.py', 'f/f.py'])
265+
self.assertWarnings()
266+
72267

73268
def test_suite():
74269
return unittest.makeSuite(ManifestTestCase)

0 commit comments

Comments
 (0)