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

Skip to content

Commit af2ffd7

Browse files
committed
Cleanups in distutils tests.
- Actually check the contents of the file created by bdist_dumb. - Don’t use “RECORD” as filename for non-PEP 376 record file - Don’t start method name with “_test”, it smells like a disabled test method instead of an helper method - Fix some idioms (assertIn, addCleanup)
1 parent 91d5193 commit af2ffd7

3 files changed

Lines changed: 32 additions & 25 deletions

File tree

Lib/distutils/tests/test_bdist_dumb.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Tests for distutils.command.bdist_dumb."""
22

3-
import unittest
4-
import sys
53
import os
4+
import imp
5+
import sys
6+
import zipfile
7+
import unittest
68
from test.support import run_unittest
79

810
from distutils.core import Distribution
@@ -72,15 +74,24 @@ def test_simple_built(self):
7274

7375
# see what we have
7476
dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
75-
base = "%s.%s" % (dist.get_fullname(), cmd.plat_name)
77+
base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name)
7678
if os.name == 'os2':
7779
base = base.replace(':', '-')
7880

79-
wanted = ['%s.zip' % base]
80-
self.assertEqual(dist_created, wanted)
81+
self.assertEqual(dist_created, [base])
8182

8283
# now let's check what we have in the zip file
83-
# XXX to be done
84+
fp = zipfile.ZipFile(os.path.join('dist', base))
85+
try:
86+
contents = fp.namelist()
87+
finally:
88+
fp.close()
89+
90+
contents = sorted(os.path.basename(fn) for fn in contents)
91+
wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2],
92+
'foo.%s.pyc' % imp.get_tag(),
93+
'foo.py']
94+
self.assertEqual(contents, sorted(wanted))
8495

8596
def test_suite():
8697
return unittest.makeSuite(BuildDumbTestCase)

Lib/distutils/tests/test_install.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,34 +87,32 @@ def _expanduser(path):
8787
self.old_expand = os.path.expanduser
8888
os.path.expanduser = _expanduser
8989

90-
try:
91-
# this is the actual test
92-
self._test_user_site()
93-
finally:
90+
def cleanup():
9491
site.USER_BASE = self.old_user_base
9592
site.USER_SITE = self.old_user_site
9693
install_module.USER_BASE = self.old_user_base
9794
install_module.USER_SITE = self.old_user_site
9895
os.path.expanduser = self.old_expand
9996

100-
def _test_user_site(self):
97+
self.addCleanup(cleanup)
98+
10199
for key in ('nt_user', 'unix_user', 'os2_home'):
102-
self.assertTrue(key in INSTALL_SCHEMES)
100+
self.assertIn(key, INSTALL_SCHEMES)
103101

104102
dist = Distribution({'name': 'xx'})
105103
cmd = install(dist)
106104

107105
# making sure the user option is there
108106
options = [name for name, short, lable in
109107
cmd.user_options]
110-
self.assertTrue('user' in options)
108+
self.assertIn('user', options)
111109

112110
# setting a value
113111
cmd.user = 1
114112

115113
# user base and site shouldn't be created yet
116-
self.assertTrue(not os.path.exists(self.user_base))
117-
self.assertTrue(not os.path.exists(self.user_site))
114+
self.assertFalse(os.path.exists(self.user_base))
115+
self.assertFalse(os.path.exists(self.user_site))
118116

119117
# let's run finalize
120118
cmd.ensure_finalized()
@@ -123,8 +121,8 @@ def _test_user_site(self):
123121
self.assertTrue(os.path.exists(self.user_base))
124122
self.assertTrue(os.path.exists(self.user_site))
125123

126-
self.assertTrue('userbase' in cmd.config_vars)
127-
self.assertTrue('usersite' in cmd.config_vars)
124+
self.assertIn('userbase', cmd.config_vars)
125+
self.assertIn('usersite', cmd.config_vars)
128126

129127
def test_handle_extra_path(self):
130128
dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'})
@@ -178,14 +176,13 @@ def test_finalize_options(self):
178176
def test_record(self):
179177
install_dir = self.mkdtemp()
180178
project_dir, dist = self.create_dist(scripts=['hello'])
181-
self.addCleanup(os.chdir, os.getcwd())
182179
os.chdir(project_dir)
183180
self.write_file('hello', "print('o hai')")
184181

185182
cmd = install(dist)
186183
dist.command_obj['install'] = cmd
187184
cmd.root = install_dir
188-
cmd.record = os.path.join(project_dir, 'RECORD')
185+
cmd.record = os.path.join(project_dir, 'filelist')
189186
cmd.ensure_finalized()
190187
cmd.run()
191188

@@ -204,7 +201,6 @@ def test_record_extensions(self):
204201
install_dir = self.mkdtemp()
205202
project_dir, dist = self.create_dist(ext_modules=[
206203
Extension('xx', ['xxmodule.c'])])
207-
self.addCleanup(os.chdir, os.getcwd())
208204
os.chdir(project_dir)
209205
support.copy_xxmodule_c(project_dir)
210206

@@ -216,7 +212,7 @@ def test_record_extensions(self):
216212
dist.command_obj['install'] = cmd
217213
dist.command_obj['build_ext'] = buildextcmd
218214
cmd.root = install_dir
219-
cmd.record = os.path.join(project_dir, 'RECORD')
215+
cmd.record = os.path.join(project_dir, 'filelist')
220216
cmd.ensure_finalized()
221217
cmd.run()
222218

Lib/distutils/tests/test_sdist.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def test_finalize_options(self):
288288
# the following tests make sure there is a nice error message instead
289289
# of a traceback when parsing an invalid manifest template
290290

291-
def _test_template(self, content):
291+
def _check_template(self, content):
292292
dist, cmd = self.get_cmd()
293293
os.chdir(self.tmp_dir)
294294
self.write_file('MANIFEST.in', content)
@@ -299,17 +299,17 @@ def _test_template(self, content):
299299
self.assertEqual(len(warnings), 1)
300300

301301
def test_invalid_template_unknown_command(self):
302-
self._test_template('taunt knights *')
302+
self._check_template('taunt knights *')
303303

304304
def test_invalid_template_wrong_arguments(self):
305305
# this manifest command takes one argument
306-
self._test_template('prune')
306+
self._check_template('prune')
307307

308308
@unittest.skipIf(os.name != 'nt', 'test relevant for Windows only')
309309
def test_invalid_template_wrong_path(self):
310310
# on Windows, trailing slashes are not allowed
311311
# this used to crash instead of raising a warning: #8286
312-
self._test_template('include examples/')
312+
self._check_template('include examples/')
313313

314314
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
315315
def test_get_file_list(self):

0 commit comments

Comments
 (0)