diff options
Diffstat (limited to 'Lib/distutils/tests')
47 files changed, 1305 insertions, 1892 deletions
diff --git a/Lib/distutils/tests/__init__.py b/Lib/distutils/tests/__init__.py index 1b939cb..697ff84 100644 --- a/Lib/distutils/tests/__init__.py +++ b/Lib/distutils/tests/__init__.py @@ -15,7 +15,7 @@ by import rather than matching pre-defined names. import os import sys import unittest -from test.support import run_unittest +from test.test_support import run_unittest here = os.path.dirname(__file__) or os.curdir diff --git a/Lib/distutils/tests/setuptools_build_ext.py b/Lib/distutils/tests/setuptools_build_ext.py new file mode 100644 index 0000000..21fa9e8 --- /dev/null +++ b/Lib/distutils/tests/setuptools_build_ext.py @@ -0,0 +1,287 @@ +from distutils.command.build_ext import build_ext as _du_build_ext +try: + # Attempt to use Pyrex for building extensions, if available + from Pyrex.Distutils.build_ext import build_ext as _build_ext +except ImportError: + _build_ext = _du_build_ext + +import os, sys +from distutils.file_util import copy_file + +from distutils.tests.setuptools_extension import Library + +from distutils.ccompiler import new_compiler +from distutils.sysconfig import customize_compiler, get_config_var +get_config_var("LDSHARED") # make sure _config_vars is initialized +from distutils.sysconfig import _config_vars +from distutils import log +from distutils.errors import * + +have_rtld = False +use_stubs = False +libtype = 'shared' + +if sys.platform == "darwin": + use_stubs = True +elif os.name != 'nt': + try: + from dl import RTLD_NOW + have_rtld = True + use_stubs = True + except ImportError: + pass + +def if_dl(s): + if have_rtld: + return s + return '' + + + + + + +class build_ext(_build_ext): + def run(self): + """Build extensions in build directory, then copy if --inplace""" + old_inplace, self.inplace = self.inplace, 0 + _build_ext.run(self) + self.inplace = old_inplace + if old_inplace: + self.copy_extensions_to_source() + + def copy_extensions_to_source(self): + build_py = self.get_finalized_command('build_py') + for ext in self.extensions: + fullname = self.get_ext_fullname(ext.name) + filename = self.get_ext_filename(fullname) + modpath = fullname.split('.') + package = '.'.join(modpath[:-1]) + package_dir = build_py.get_package_dir(package) + dest_filename = os.path.join(package_dir,os.path.basename(filename)) + src_filename = os.path.join(self.build_lib,filename) + + # Always copy, even if source is older than destination, to ensure + # that the right extensions for the current Python/platform are + # used. + copy_file( + src_filename, dest_filename, verbose=self.verbose, + dry_run=self.dry_run + ) + if ext._needs_stub: + self.write_stub(package_dir or os.curdir, ext, True) + + + if _build_ext is not _du_build_ext and not hasattr(_build_ext,'pyrex_sources'): + # Workaround for problems using some Pyrex versions w/SWIG and/or 2.4 + def swig_sources(self, sources, *otherargs): + # first do any Pyrex processing + sources = _build_ext.swig_sources(self, sources) or sources + # Then do any actual SWIG stuff on the remainder + return _du_build_ext.swig_sources(self, sources, *otherargs) + + + + def get_ext_filename(self, fullname): + filename = _build_ext.get_ext_filename(self,fullname) + ext = self.ext_map[fullname] + if isinstance(ext,Library): + fn, ext = os.path.splitext(filename) + return self.shlib_compiler.library_filename(fn,libtype) + elif use_stubs and ext._links_to_dynamic: + d,fn = os.path.split(filename) + return os.path.join(d,'dl-'+fn) + else: + return filename + + def initialize_options(self): + _build_ext.initialize_options(self) + self.shlib_compiler = None + self.shlibs = [] + self.ext_map = {} + + def finalize_options(self): + _build_ext.finalize_options(self) + self.extensions = self.extensions or [] + self.check_extensions_list(self.extensions) + self.shlibs = [ext for ext in self.extensions + if isinstance(ext,Library)] + if self.shlibs: + self.setup_shlib_compiler() + for ext in self.extensions: + ext._full_name = self.get_ext_fullname(ext.name) + for ext in self.extensions: + fullname = ext._full_name + self.ext_map[fullname] = ext + ltd = ext._links_to_dynamic = \ + self.shlibs and self.links_to_dynamic(ext) or False + ext._needs_stub = ltd and use_stubs and not isinstance(ext,Library) + filename = ext._file_name = self.get_ext_filename(fullname) + libdir = os.path.dirname(os.path.join(self.build_lib,filename)) + if ltd and libdir not in ext.library_dirs: + ext.library_dirs.append(libdir) + if ltd and use_stubs and os.curdir not in ext.runtime_library_dirs: + ext.runtime_library_dirs.append(os.curdir) + + def setup_shlib_compiler(self): + compiler = self.shlib_compiler = new_compiler( + compiler=self.compiler, dry_run=self.dry_run, force=self.force + ) + if sys.platform == "darwin": + tmp = _config_vars.copy() + try: + # XXX Help! I don't have any idea whether these are right... + _config_vars['LDSHARED'] = "gcc -Wl,-x -dynamiclib -undefined dynamic_lookup" + _config_vars['CCSHARED'] = " -dynamiclib" + _config_vars['SO'] = ".dylib" + customize_compiler(compiler) + finally: + _config_vars.clear() + _config_vars.update(tmp) + else: + customize_compiler(compiler) + + if self.include_dirs is not None: + compiler.set_include_dirs(self.include_dirs) + if self.define is not None: + # 'define' option is a list of (name,value) tuples + for (name,value) in self.define: + compiler.define_macro(name, value) + if self.undef is not None: + for macro in self.undef: + compiler.undefine_macro(macro) + if self.libraries is not None: + compiler.set_libraries(self.libraries) + if self.library_dirs is not None: + compiler.set_library_dirs(self.library_dirs) + if self.rpath is not None: + compiler.set_runtime_library_dirs(self.rpath) + if self.link_objects is not None: + compiler.set_link_objects(self.link_objects) + + # hack so distutils' build_extension() builds a library instead + compiler.link_shared_object = link_shared_object.__get__(compiler) + + + + def get_export_symbols(self, ext): + if isinstance(ext,Library): + return ext.export_symbols + return _build_ext.get_export_symbols(self,ext) + + def build_extension(self, ext): + _compiler = self.compiler + try: + if isinstance(ext,Library): + self.compiler = self.shlib_compiler + _build_ext.build_extension(self,ext) + if ext._needs_stub: + self.write_stub( + self.get_finalized_command('build_py').build_lib, ext + ) + finally: + self.compiler = _compiler + + def links_to_dynamic(self, ext): + """Return true if 'ext' links to a dynamic lib in the same package""" + # XXX this should check to ensure the lib is actually being built + # XXX as dynamic, and not just using a locally-found version or a + # XXX static-compiled version + libnames = dict.fromkeys([lib._full_name for lib in self.shlibs]) + pkg = '.'.join(ext._full_name.split('.')[:-1]+['']) + for libname in ext.libraries: + if pkg+libname in libnames: return True + return False + + def get_outputs(self): + outputs = _build_ext.get_outputs(self) + optimize = self.get_finalized_command('build_py').optimize + for ext in self.extensions: + if ext._needs_stub: + base = os.path.join(self.build_lib, *ext._full_name.split('.')) + outputs.append(base+'.py') + outputs.append(base+'.pyc') + if optimize: + outputs.append(base+'.pyo') + return outputs + + def write_stub(self, output_dir, ext, compile=False): + log.info("writing stub loader for %s to %s",ext._full_name, output_dir) + stub_file = os.path.join(output_dir, *ext._full_name.split('.'))+'.py' + if compile and os.path.exists(stub_file): + raise DistutilsError(stub_file+" already exists! Please delete.") + if not self.dry_run: + f = open(stub_file,'w') + f.write('\n'.join([ + "def __bootstrap__():", + " global __bootstrap__, __file__, __loader__", + " import sys, os, pkg_resources, imp"+if_dl(", dl"), + " __file__ = pkg_resources.resource_filename(__name__,%r)" + % os.path.basename(ext._file_name), + " del __bootstrap__", + " if '__loader__' in globals():", + " del __loader__", + if_dl(" old_flags = sys.getdlopenflags()"), + " old_dir = os.getcwd()", + " try:", + " os.chdir(os.path.dirname(__file__))", + if_dl(" sys.setdlopenflags(dl.RTLD_NOW)"), + " imp.load_dynamic(__name__,__file__)", + " finally:", + if_dl(" sys.setdlopenflags(old_flags)"), + " os.chdir(old_dir)", + "__bootstrap__()", + "" # terminal \n + ])) + f.close() + if compile: + from distutils.util import byte_compile + byte_compile([stub_file], optimize=0, + force=True, dry_run=self.dry_run) + optimize = self.get_finalized_command('install_lib').optimize + if optimize > 0: + byte_compile([stub_file], optimize=optimize, + force=True, dry_run=self.dry_run) + if os.path.exists(stub_file) and not self.dry_run: + os.unlink(stub_file) + + +if use_stubs or os.name=='nt': + # Build shared libraries + # + def link_shared_object(self, objects, output_libname, output_dir=None, + libraries=None, library_dirs=None, runtime_library_dirs=None, + export_symbols=None, debug=0, extra_preargs=None, + extra_postargs=None, build_temp=None, target_lang=None + ): self.link( + self.SHARED_LIBRARY, objects, output_libname, + output_dir, libraries, library_dirs, runtime_library_dirs, + export_symbols, debug, extra_preargs, extra_postargs, + build_temp, target_lang + ) +else: + # Build static libraries everywhere else + libtype = 'static' + + def link_shared_object(self, objects, output_libname, output_dir=None, + libraries=None, library_dirs=None, runtime_library_dirs=None, + export_symbols=None, debug=0, extra_preargs=None, + extra_postargs=None, build_temp=None, target_lang=None + ): + # XXX we need to either disallow these attrs on Library instances, + # or warn/abort here if set, or something... + #libraries=None, library_dirs=None, runtime_library_dirs=None, + #export_symbols=None, extra_preargs=None, extra_postargs=None, + #build_temp=None + + assert output_dir is None # distutils build_ext doesn't pass this + output_dir,filename = os.path.split(output_libname) + basename, ext = os.path.splitext(filename) + if self.library_filename("x").startswith('lib'): + # strip 'lib' prefix; this is kludgy if some platform uses + # a different prefix + basename = basename[3:] + + self.create_static_lib( + objects, basename, output_dir, debug, target_lang + ) diff --git a/Lib/distutils/tests/setuptools_extension.py b/Lib/distutils/tests/setuptools_extension.py new file mode 100644 index 0000000..ec6b690 --- /dev/null +++ b/Lib/distutils/tests/setuptools_extension.py @@ -0,0 +1,51 @@ +from distutils.core import Extension as _Extension +from distutils.core import Distribution as _Distribution + +def _get_unpatched(cls): + """Protect against re-patching the distutils if reloaded + + Also ensures that no other distutils extension monkeypatched the distutils + first. + """ + while cls.__module__.startswith('setuptools'): + cls, = cls.__bases__ + if not cls.__module__.startswith('distutils'): + raise AssertionError( + "distutils has already been patched by %r" % cls + ) + return cls + +_Distribution = _get_unpatched(_Distribution) +_Extension = _get_unpatched(_Extension) + +try: + from Pyrex.Distutils.build_ext import build_ext +except ImportError: + have_pyrex = False +else: + have_pyrex = True + + +class Extension(_Extension): + """Extension that uses '.c' files in place of '.pyx' files""" + + if not have_pyrex: + # convert .pyx extensions to .c + def __init__(self,*args,**kw): + _Extension.__init__(self,*args,**kw) + sources = [] + for s in self.sources: + if s.endswith('.pyx'): + sources.append(s[:-3]+'c') + else: + sources.append(s) + self.sources = sources + +class Library(Extension): + """Just like a regular Extension, but built as a library instead""" + +import sys, distutils.core, distutils.extension +distutils.core.Extension = Extension +distutils.extension.Extension = Extension +if 'distutils.command.build_ext' in sys.modules: + sys.modules['distutils.command.build_ext'].Extension = Extension diff --git a/Lib/distutils/tests/support.py b/Lib/distutils/tests/support.py index 259af88..96dcd94 100644 --- a/Lib/distutils/tests/support.py +++ b/Lib/distutils/tests/support.py @@ -6,17 +6,25 @@ import tempfile import unittest import sysconfig from copy import deepcopy -import test.support +import warnings from distutils import log from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL from distutils.core import Distribution +def capture_warnings(func): + def _capture_warnings(*args, **kw): + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + return func(*args, **kw) + return _capture_warnings + + class LoggingSilencer(object): def setUp(self): - super().setUp() + super(LoggingSilencer, self).setUp() self.threshold = log.set_threshold(log.FATAL) # catching warnings # when log will be replaced by logging @@ -28,18 +36,19 @@ class LoggingSilencer(object): def tearDown(self): log.set_threshold(self.threshold) log.Log._log = self._old_log - super().tearDown() + super(LoggingSilencer, self).tearDown() def _log(self, level, msg, args): if level not in (DEBUG, INFO, WARN, ERROR, FATAL): raise ValueError('%s wrong log level' % str(level)) - if not isinstance(msg, str): - raise TypeError("msg should be str, not '%.200s'" - % (type(msg).__name__)) self.logs.append((level, msg, args)) def get_logs(self, *levels): - return [msg % args for level, msg, args + def _format(msg, args): + if len(args) == 0: + return msg + return msg % args + return [_format(msg, args) for level, msg, args in self.logs if level in levels] def clear_logs(self): @@ -53,7 +62,7 @@ class TempdirManager(object): """ def setUp(self): - super().setUp() + super(TempdirManager, self).setUp() self.old_cwd = os.getcwd() self.tempdirs = [] @@ -61,10 +70,10 @@ class TempdirManager(object): # Restore working dir, for Solaris and derivatives, where rmdir() # on the current directory fails. os.chdir(self.old_cwd) - super().tearDown() + super(TempdirManager, self).tearDown() while self.tempdirs: - tmpdir = self.tempdirs.pop() - test.support.rmtree(tmpdir) + d = self.tempdirs.pop() + shutil.rmtree(d, os.name in ('nt', 'cygwin')) def mkdtemp(self): """Create a temporary directory that will be cleaned up. @@ -129,7 +138,7 @@ class EnvironGuard(object): if os.environ.get(key) != value: os.environ[key] = value - for key in tuple(os.environ.keys()): + for key in os.environ.keys(): if key not in self.old_environ: del os.environ[key] @@ -156,6 +165,8 @@ def copy_xxmodule_c(directory): def _get_xxmodule_path(): + # FIXME when run from regrtest, srcdir seems to be '.', which does not help + # us find the xxmodule.c file srcdir = sysconfig.get_config_var('srcdir') candidates = [ # use installed copy if available @@ -175,9 +186,10 @@ def _get_xxmodule_path(): def fixup_build_ext(cmd): """Function needed to make build_ext tests pass. - When Python was built with --enable-shared on Unix, -L. is not enough to - find libpython<blah>.so, because regrtest runs in a tempdir, not in the - source directory where the .so lives. + When Python was build with --enable-shared on Unix, -L. is not good + enough to find the libpython<blah>.so. This is because regrtest runs + it under a tempdir, not in the top level where the .so lives. By the + time we've gotten here, Python's already been chdir'd to the tempdir. When Python was built with in debug mode on Windows, build_ext commands need their debug attribute set, and it is not done automatically for diff --git a/Lib/distutils/tests/test_archive_util.py b/Lib/distutils/tests/test_archive_util.py index e9aad0e..137100c 100644 --- a/Lib/distutils/tests/test_archive_util.py +++ b/Lib/distutils/tests/test_archive_util.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- """Tests for distutils.archive_util.""" +__revision__ = "$Id$" + import unittest import os import sys @@ -7,13 +9,12 @@ import tarfile from os.path import splitdrive import warnings -from distutils import archive_util from distutils.archive_util import (check_archive_formats, make_tarball, make_zipfile, make_archive, ARCHIVE_FORMATS) from distutils.spawn import find_executable, spawn from distutils.tests import support -from test.support import check_warnings, run_unittest, patch, change_cwd +from test.test_support import check_warnings, run_unittest try: import grp @@ -28,21 +29,11 @@ try: except ImportError: ZIP_SUPPORT = find_executable('zip') +# some tests will fail if zlib is not available try: import zlib - ZLIB_SUPPORT = True -except ImportError: - ZLIB_SUPPORT = False - -try: - import bz2 -except ImportError: - bz2 = None - -try: - import lzma except ImportError: - lzma = None + zlib = None def can_fs_encode(filename): """ @@ -61,47 +52,18 @@ class ArchiveUtilTestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): - @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') - def test_make_tarball(self, name='archive'): - # creating something to tar - tmpdir = self._create_files() - self._make_tarball(tmpdir, name, '.tar.gz') - # trying an uncompressed one - self._make_tarball(tmpdir, name, '.tar', compress=None) - - @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') - def test_make_tarball_gzip(self): - tmpdir = self._create_files() - self._make_tarball(tmpdir, 'archive', '.tar.gz', compress='gzip') - - @unittest.skipUnless(bz2, 'Need bz2 support to run') - def test_make_tarball_bzip2(self): - tmpdir = self._create_files() - self._make_tarball(tmpdir, 'archive', '.tar.bz2', compress='bzip2') - - @unittest.skipUnless(lzma, 'Need lzma support to run') - def test_make_tarball_xz(self): - tmpdir = self._create_files() - self._make_tarball(tmpdir, 'archive', '.tar.xz', compress='xz') - - @unittest.skipUnless(can_fs_encode('årchiv'), - 'File system cannot handle this filename') - def test_make_tarball_latin1(self): - """ - Mirror test_make_tarball, except filename contains latin characters. - """ - self.test_make_tarball('årchiv') # note this isn't a real word + @unittest.skipUnless(zlib, "requires zlib") + def test_make_tarball(self): + self._make_tarball('archive') - @unittest.skipUnless(can_fs_encode('のアーカイブ'), - 'File system cannot handle this filename') - def test_make_tarball_extended(self): - """ - Mirror test_make_tarball, except filename contains extended - characters outside the latin charset. - """ - self.test_make_tarball('のアーカイブ') # japanese for archive + def _make_tarball(self, target_name): + # creating something to tar + tmpdir = self.mkdtemp() + self.write_file([tmpdir, 'file1'], 'xxx') + self.write_file([tmpdir, 'file2'], 'xxx') + os.mkdir(os.path.join(tmpdir, 'sub')) + self.write_file([tmpdir, 'sub', 'file3'], 'xxx') - def _make_tarball(self, tmpdir, target_name, suffix, **kwargs): tmpdir2 = self.mkdtemp() unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0], "source and target should be on same drive") @@ -109,13 +71,27 @@ class ArchiveUtilTestCase(support.TempdirManager, base_name = os.path.join(tmpdir2, target_name) # working with relative paths to avoid tar warnings - with change_cwd(tmpdir): - make_tarball(splitdrive(base_name)[1], 'dist', **kwargs) + old_dir = os.getcwd() + os.chdir(tmpdir) + try: + make_tarball(splitdrive(base_name)[1], '.') + finally: + os.chdir(old_dir) # check if the compressed tarball was created - tarball = base_name + suffix + tarball = base_name + '.tar.gz' + self.assertTrue(os.path.exists(tarball)) + + # trying an uncompressed one + base_name = os.path.join(tmpdir2, target_name) + old_dir = os.getcwd() + os.chdir(tmpdir) + try: + make_tarball(splitdrive(base_name)[1], '.', compress=None) + finally: + os.chdir(old_dir) + tarball = base_name + '.tar' self.assertTrue(os.path.exists(tarball)) - self.assertEqual(self._tarinfo(tarball), self._created_files) def _tarinfo(self, path): tar = tarfile.open(path) @@ -126,10 +102,6 @@ class ArchiveUtilTestCase(support.TempdirManager, finally: tar.close() - _zip_created_files = ['dist/', 'dist/file1', 'dist/file2', - 'dist/sub/', 'dist/sub/file3', 'dist/sub2/'] - _created_files = [p.rstrip('/') for p in _zip_created_files] - def _create_files(self): # creating something to tar tmpdir = self.mkdtemp() @@ -140,15 +112,15 @@ class ArchiveUtilTestCase(support.TempdirManager, os.mkdir(os.path.join(dist, 'sub')) self.write_file([dist, 'sub', 'file3'], 'xxx') os.mkdir(os.path.join(dist, 'sub2')) - return tmpdir - - @unittest.skipUnless(find_executable('tar') and find_executable('gzip') - and ZLIB_SUPPORT, - 'Need the tar, gzip and zlib command to run') - def test_tarfile_vs_tar(self): - tmpdir = self._create_files() tmpdir2 = self.mkdtemp() base_name = os.path.join(tmpdir2, 'archive') + return tmpdir, tmpdir2, base_name + + @unittest.skipUnless(zlib, "Requires zlib") + @unittest.skipUnless(find_executable('tar') and find_executable('gzip'), + 'Need the tar command to run') + def test_tarfile_vs_tar(self): + tmpdir, tmpdir2, base_name = self._create_files() old_dir = os.getcwd() os.chdir(tmpdir) try: @@ -163,7 +135,7 @@ class ArchiveUtilTestCase(support.TempdirManager, # now create another tarball using `tar` tarball2 = os.path.join(tmpdir, 'archive2.tar.gz') tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist'] - gzip_cmd = ['gzip', '-f', '-9', 'archive2.tar'] + gzip_cmd = ['gzip', '-f9', 'archive2.tar'] old_dir = os.getcwd() os.chdir(tmpdir) try: @@ -174,8 +146,7 @@ class ArchiveUtilTestCase(support.TempdirManager, self.assertTrue(os.path.exists(tarball2)) # let's compare both tarballs - self.assertEqual(self._tarinfo(tarball), self._created_files) - self.assertEqual(self._tarinfo(tarball2), self._created_files) + self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2)) # trying an uncompressed one base_name = os.path.join(tmpdir2, 'archive') @@ -202,8 +173,7 @@ class ArchiveUtilTestCase(support.TempdirManager, @unittest.skipUnless(find_executable('compress'), 'The compress program is required') def test_compress_deprecated(self): - tmpdir = self._create_files() - base_name = os.path.join(self.mkdtemp(), 'archive') + tmpdir, tmpdir2, base_name = self._create_files() # using compress and testing the PendingDeprecationWarning old_dir = os.getcwd() @@ -232,107 +202,32 @@ class ArchiveUtilTestCase(support.TempdirManager, self.assertFalse(os.path.exists(tarball)) self.assertEqual(len(w.warnings), 1) - @unittest.skipUnless(ZIP_SUPPORT and ZLIB_SUPPORT, - 'Need zip and zlib support to run') + @unittest.skipUnless(zlib, "Requires zlib") + @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run') def test_make_zipfile(self): # creating something to tar - tmpdir = self._create_files() - base_name = os.path.join(self.mkdtemp(), 'archive') - with change_cwd(tmpdir): - make_zipfile(base_name, 'dist') - - # check if the compressed tarball was created - tarball = base_name + '.zip' - self.assertTrue(os.path.exists(tarball)) - with zipfile.ZipFile(tarball) as zf: - self.assertEqual(sorted(zf.namelist()), self._zip_created_files) - - @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run') - def test_make_zipfile_no_zlib(self): - patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError - - called = [] - zipfile_class = zipfile.ZipFile - def fake_zipfile(*a, **kw): - if kw.get('compression', None) == zipfile.ZIP_STORED: - called.append((a, kw)) - return zipfile_class(*a, **kw) - - patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile) + tmpdir = self.mkdtemp() + self.write_file([tmpdir, 'file1'], 'xxx') + self.write_file([tmpdir, 'file2'], 'xxx') - # create something to tar and compress - tmpdir = self._create_files() - base_name = os.path.join(self.mkdtemp(), 'archive') - with change_cwd(tmpdir): - make_zipfile(base_name, 'dist') + tmpdir2 = self.mkdtemp() + base_name = os.path.join(tmpdir2, 'archive') + make_zipfile(base_name, tmpdir) + # check if the compressed tarball was created tarball = base_name + '.zip' - self.assertEqual(called, - [((tarball, "w"), {'compression': zipfile.ZIP_STORED})]) - self.assertTrue(os.path.exists(tarball)) - with zipfile.ZipFile(tarball) as zf: - self.assertEqual(sorted(zf.namelist()), self._zip_created_files) def test_check_archive_formats(self): self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']), 'xxx') - self.assertIsNone(check_archive_formats(['gztar', 'bztar', 'xztar', - 'ztar', 'tar', 'zip'])) + self.assertEqual(check_archive_formats(['gztar', 'zip']), None) def test_make_archive(self): tmpdir = self.mkdtemp() base_name = os.path.join(tmpdir, 'archive') self.assertRaises(ValueError, make_archive, base_name, 'xxx') - def test_make_archive_cwd(self): - current_dir = os.getcwd() - def _breaks(*args, **kw): - raise RuntimeError() - ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file') - try: - try: - make_archive('xxx', 'xxx', root_dir=self.mkdtemp()) - except: - pass - self.assertEqual(os.getcwd(), current_dir) - finally: - del ARCHIVE_FORMATS['xxx'] - - def test_make_archive_tar(self): - base_dir = self._create_files() - base_name = os.path.join(self.mkdtemp() , 'archive') - res = make_archive(base_name, 'tar', base_dir, 'dist') - self.assertTrue(os.path.exists(res)) - self.assertEqual(os.path.basename(res), 'archive.tar') - self.assertEqual(self._tarinfo(res), self._created_files) - - @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') - def test_make_archive_gztar(self): - base_dir = self._create_files() - base_name = os.path.join(self.mkdtemp() , 'archive') - res = make_archive(base_name, 'gztar', base_dir, 'dist') - self.assertTrue(os.path.exists(res)) - self.assertEqual(os.path.basename(res), 'archive.tar.gz') - self.assertEqual(self._tarinfo(res), self._created_files) - - @unittest.skipUnless(bz2, 'Need bz2 support to run') - def test_make_archive_bztar(self): - base_dir = self._create_files() - base_name = os.path.join(self.mkdtemp() , 'archive') - res = make_archive(base_name, 'bztar', base_dir, 'dist') - self.assertTrue(os.path.exists(res)) - self.assertEqual(os.path.basename(res), 'archive.tar.bz2') - self.assertEqual(self._tarinfo(res), self._created_files) - - @unittest.skipUnless(lzma, 'Need xz support to run') - def test_make_archive_xztar(self): - base_dir = self._create_files() - base_name = os.path.join(self.mkdtemp() , 'archive') - res = make_archive(base_name, 'xztar', base_dir, 'dist') - self.assertTrue(os.path.exists(res)) - self.assertEqual(os.path.basename(res), 'archive.tar.xz') - self.assertEqual(self._tarinfo(res), self._created_files) - + @unittest.skipUnless(zlib, "Requires zlib") def test_make_archive_owner_group(self): # testing make_archive with owner and group, with various combinations # this works even if there's not gid/uid support @@ -342,8 +237,7 @@ class ArchiveUtilTestCase(support.TempdirManager, else: group = owner = 'root' - base_dir = self._create_files() - root_dir = self.mkdtemp() + base_dir, root_dir, base_name = self._create_files() base_name = os.path.join(self.mkdtemp() , 'archive') res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner, group=group) @@ -360,11 +254,10 @@ class ArchiveUtilTestCase(support.TempdirManager, owner='kjhkjhkjg', group='oihohoh') self.assertTrue(os.path.exists(res)) - @unittest.skipUnless(ZLIB_SUPPORT, "Requires zlib") + @unittest.skipUnless(zlib, "Requires zlib") @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") def test_tarfile_root_owner(self): - tmpdir = self._create_files() - base_name = os.path.join(self.mkdtemp(), 'archive') + tmpdir, tmpdir2, base_name = self._create_files() old_dir = os.getcwd() os.chdir(tmpdir) group = grp.getgrgid(0)[0] @@ -387,6 +280,47 @@ class ArchiveUtilTestCase(support.TempdirManager, finally: archive.close() + def test_make_archive_cwd(self): + current_dir = os.getcwd() + def _breaks(*args, **kw): + raise RuntimeError() + ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file') + try: + try: + make_archive('xxx', 'xxx', root_dir=self.mkdtemp()) + except: + pass + self.assertEqual(os.getcwd(), current_dir) + finally: + del ARCHIVE_FORMATS['xxx'] + + @unittest.skipUnless(zlib, "requires zlib") + def test_make_tarball_unicode(self): + """ + Mirror test_make_tarball, except filename is unicode. + """ + self._make_tarball(u'archive') + + @unittest.skipUnless(zlib, "requires zlib") + @unittest.skipUnless(can_fs_encode(u'årchiv'), + 'File system cannot handle this filename') + def test_make_tarball_unicode_latin1(self): + """ + Mirror test_make_tarball, except filename is unicode and contains + latin characters. + """ + self._make_tarball(u'årchiv') # note this isn't a real word + + @unittest.skipUnless(zlib, "requires zlib") + @unittest.skipUnless(can_fs_encode(u'のアーカイブ'), + 'File system cannot handle this filename') + def test_make_tarball_unicode_extended(self): + """ + Mirror test_make_tarball, except filename is unicode and contains + characters outside the latin charset. + """ + self._make_tarball(u'のアーカイブ') # japanese for archive + def test_suite(): return unittest.makeSuite(ArchiveUtilTestCase) diff --git a/Lib/distutils/tests/test_bdist.py b/Lib/distutils/tests/test_bdist.py index 130d8bf..121d099 100644 --- a/Lib/distutils/tests/test_bdist.py +++ b/Lib/distutils/tests/test_bdist.py @@ -1,8 +1,8 @@ """Tests for distutils.command.bdist.""" import os import unittest -from test.support import run_unittest -import warnings + +from test.test_support import run_unittest from distutils.command.bdist import bdist from distutils.tests import support @@ -22,7 +22,7 @@ class BuildTestCase(support.TempdirManager, # what formats does bdist offer? formats = ['bztar', 'gztar', 'msi', 'rpm', 'tar', - 'wininst', 'xztar', 'zip', 'ztar'] + 'wininst', 'zip', 'ztar'] found = sorted(cmd.format_command) self.assertEqual(found, formats) @@ -34,18 +34,13 @@ class BuildTestCase(support.TempdirManager, cmd.ensure_finalized() dist.command_obj['bdist'] = cmd - names = ['bdist_dumb', 'bdist_wininst'] # bdist_rpm does not support --skip-build + names = ['bdist_dumb', 'bdist_wininst'] + # bdist_rpm does not support --skip-build if os.name == 'nt': names.append('bdist_msi') for name in names: - with warnings.catch_warnings(): - warnings.filterwarnings('ignore', 'bdist_wininst command is deprecated', - DeprecationWarning) - subcmd = cmd.get_finalized_command(name) - if getattr(subcmd, '_unsupported', False): - # command is not supported on this build - continue + subcmd = cmd.get_finalized_command(name) self.assertTrue(subcmd.skip_build, '%s should take --skip-build from bdist' % name) diff --git a/Lib/distutils/tests/test_bdist_dumb.py b/Lib/distutils/tests/test_bdist_dumb.py index 01a233b..ef9e681 100644 --- a/Lib/distutils/tests/test_bdist_dumb.py +++ b/Lib/distutils/tests/test_bdist_dumb.py @@ -4,7 +4,14 @@ import os import sys import zipfile import unittest -from test.support import run_unittest +from test.test_support import run_unittest + +# zlib is not used here, but if it's not available +# test_simple_built will fail +try: + import zlib +except ImportError: + zlib = None from distutils.core import Distribution from distutils.command.bdist_dumb import bdist_dumb @@ -19,13 +26,6 @@ setup(name='foo', version='0.1', py_modules=['foo'], """ -try: - import zlib - ZLIB_SUPPORT = True -except ImportError: - ZLIB_SUPPORT = False - - class BuildDumbTestCase(support.TempdirManager, support.LoggingSilencer, support.EnvironGuard, @@ -42,7 +42,7 @@ class BuildDumbTestCase(support.TempdirManager, sys.argv[:] = self.old_sys_argv[1] super(BuildDumbTestCase, self).tearDown() - @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') + @unittest.skipUnless(zlib, "requires zlib") def test_simple_built(self): # let's create a simple package @@ -74,6 +74,8 @@ class BuildDumbTestCase(support.TempdirManager, # see what we have dist_created = os.listdir(os.path.join(pkg_dir, 'dist')) base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name) + if os.name == 'os2': + base = base.replace(':', '-') self.assertEqual(dist_created, [base]) @@ -87,9 +89,24 @@ class BuildDumbTestCase(support.TempdirManager, contents = sorted(filter(None, map(os.path.basename, contents))) wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], 'foo.py'] if not sys.dont_write_bytecode: - wanted.append('foo.%s.pyc' % sys.implementation.cache_tag) + wanted.append('foo.pyc') self.assertEqual(contents, sorted(wanted)) + def test_finalize_options(self): + pkg_dir, dist = self.create_dist() + os.chdir(pkg_dir) + cmd = bdist_dumb(dist) + self.assertEqual(cmd.bdist_dir, None) + cmd.finalize_options() + + # bdist_dir is initialized to bdist_base/dumb if not set + base = cmd.get_finalized_command('bdist').bdist_base + self.assertEqual(cmd.bdist_dir, os.path.join(base, 'dumb')) + + # the format is set to a default value depending on the os.name + default = cmd.default_format[os.name] + self.assertEqual(cmd.format, default) + def test_suite(): return unittest.makeSuite(BuildDumbTestCase) diff --git a/Lib/distutils/tests/test_bdist_msi.py b/Lib/distutils/tests/test_bdist_msi.py index 15d8bdf..f98b7a2 100644 --- a/Lib/distutils/tests/test_bdist_msi.py +++ b/Lib/distutils/tests/test_bdist_msi.py @@ -1,7 +1,7 @@ """Tests for distutils.command.bdist_msi.""" import sys import unittest -from test.support import run_unittest +from test.test_support import run_unittest from distutils.tests import support diff --git a/Lib/distutils/tests/test_bdist_rpm.py b/Lib/distutils/tests/test_bdist_rpm.py index 6453a02..8248c08 100644 --- a/Lib/distutils/tests/test_bdist_rpm.py +++ b/Lib/distutils/tests/test_bdist_rpm.py @@ -3,12 +3,22 @@ import unittest import sys import os -from test.support import run_unittest, requires_zlib +import tempfile +import shutil + +from test.test_support import run_unittest + +try: + import zlib +except ImportError: + zlib = None from distutils.core import Distribution from distutils.command.bdist_rpm import bdist_rpm from distutils.tests import support from distutils.spawn import find_executable +from distutils import spawn +from distutils.errors import DistutilsExecError SETUP_PY = """\ from distutils.core import setup @@ -25,11 +35,6 @@ class BuildRpmTestCase(support.TempdirManager, unittest.TestCase): def setUp(self): - try: - sys.executable.encode("UTF-8") - except UnicodeEncodeError: - raise unittest.SkipTest("sys.executable is not encodable to UTF-8") - super(BuildRpmTestCase, self).setUp() self.old_location = os.getcwd() self.old_sys_argv = sys.argv, sys.argv[:] @@ -44,7 +49,7 @@ class BuildRpmTestCase(support.TempdirManager, # spurious sdtout/stderr output under Mac OS X @unittest.skipUnless(sys.platform.startswith('linux'), 'spurious sdtout/stderr output under Mac OS X') - @requires_zlib + @unittest.skipUnless(zlib, "requires zlib") @unittest.skipIf(find_executable('rpm') is None, 'the rpm command is not found') @unittest.skipIf(find_executable('rpmbuild') is None, @@ -87,7 +92,7 @@ class BuildRpmTestCase(support.TempdirManager, # spurious sdtout/stderr output under Mac OS X @unittest.skipUnless(sys.platform.startswith('linux'), 'spurious sdtout/stderr output under Mac OS X') - @requires_zlib + @unittest.skipUnless(zlib, "requires zlib") # http://bugs.python.org/issue1533164 @unittest.skipIf(find_executable('rpm') is None, 'the rpm command is not found') diff --git a/Lib/distutils/tests/test_bdist_wininst.py b/Lib/distutils/tests/test_bdist_wininst.py index 5c3d025..247e929 100644 --- a/Lib/distutils/tests/test_bdist_wininst.py +++ b/Lib/distutils/tests/test_bdist_wininst.py @@ -1,16 +1,11 @@ """Tests for distutils.command.bdist_wininst.""" -import sys -import platform import unittest -from test.support import run_unittest, check_warnings + +from test.test_support import run_unittest from distutils.command.bdist_wininst import bdist_wininst from distutils.tests import support -@unittest.skipIf(sys.platform == 'win32' and platform.machine() == 'ARM64', - 'bdist_wininst is not supported in this install') -@unittest.skipIf(getattr(bdist_wininst, '_unsupported', False), - 'bdist_wininst is not supported in this install') class BuildWinInstTestCase(support.TempdirManager, support.LoggingSilencer, unittest.TestCase): @@ -21,8 +16,7 @@ class BuildWinInstTestCase(support.TempdirManager, # this test makes sure it works now for every platform # let's create a command pkg_pth, dist = self.create_dist() - with check_warnings(("", DeprecationWarning)): - cmd = bdist_wininst(dist) + cmd = bdist_wininst(dist) cmd.ensure_finalized() # let's run the code that finds the right wininst*.exe file diff --git a/Lib/distutils/tests/test_build.py b/Lib/distutils/tests/test_build.py index b020a5b..eeb8d73 100644 --- a/Lib/distutils/tests/test_build.py +++ b/Lib/distutils/tests/test_build.py @@ -2,7 +2,7 @@ import unittest import os import sys -from test.support import run_unittest +from test.test_support import run_unittest from distutils.command.build import build from distutils.tests import support @@ -27,7 +27,7 @@ class BuildTestCase(support.TempdirManager, # build_platlib is 'build/lib.platform-x.x[-pydebug]' # examples: # build/lib.macosx-10.3-i386-2.7 - plat_spec = '.%s-%d.%d' % (cmd.plat_name, *sys.version_info[:2]) + plat_spec = '.%s-%s' % (cmd.plat_name, sys.version[0:3]) if hasattr(sys, 'gettotalrefcount'): self.assertTrue(cmd.build_platlib.endswith('-pydebug')) plat_spec += '-pydebug' @@ -42,8 +42,7 @@ class BuildTestCase(support.TempdirManager, self.assertEqual(cmd.build_temp, wanted) # build_scripts is build/scripts-x.x - wanted = os.path.join(cmd.build_base, - 'scripts-%d.%d' % sys.version_info[:2]) + wanted = os.path.join(cmd.build_base, 'scripts-' + sys.version[0:3]) self.assertEqual(cmd.build_scripts, wanted) # executable is os.path.normpath(sys.executable) diff --git a/Lib/distutils/tests/test_build_clib.py b/Lib/distutils/tests/test_build_clib.py index 85d0990..d4a1e69 100644 --- a/Lib/distutils/tests/test_build_clib.py +++ b/Lib/distutils/tests/test_build_clib.py @@ -3,7 +3,7 @@ import unittest import os import sys -from test.support import run_unittest, missing_compiler_executable +from test.test_support import run_unittest from distutils.command.build_clib import build_clib from distutils.errors import DistutilsSetupError @@ -116,11 +116,19 @@ class BuildCLibTestCase(support.TempdirManager, cmd.build_temp = build_temp cmd.build_clib = build_temp - # Before we run the command, we want to make sure - # all commands are present on the system. - ccmd = missing_compiler_executable() - if ccmd is not None: - self.skipTest('The %r command is not found' % ccmd) + # before we run the command, we want to make sure + # all commands are present on the system + # by creating a compiler and checking its executables + from distutils.ccompiler import new_compiler + from distutils.sysconfig import customize_compiler + + compiler = new_compiler() + customize_compiler(compiler) + for ccmd in compiler.executables.values(): + if ccmd is None: + continue + if find_executable(ccmd[0]) is None: + self.skipTest('The %r command is not found' % ccmd[0]) # this should work cmd.run() diff --git a/Lib/distutils/tests/test_build_ext.py b/Lib/distutils/tests/test_build_ext.py index 52d36b2..a6d2d2e 100644 --- a/Lib/distutils/tests/test_build_ext.py +++ b/Lib/distutils/tests/test_build_ext.py @@ -1,74 +1,62 @@ import sys import os -from io import StringIO +from StringIO import StringIO import textwrap -from distutils.core import Distribution +from distutils.core import Extension, Distribution from distutils.command.build_ext import build_ext from distutils import sysconfig -from distutils.tests.support import (TempdirManager, LoggingSilencer, - copy_xxmodule_c, fixup_build_ext) -from distutils.extension import Extension -from distutils.errors import ( - CompileError, DistutilsPlatformError, DistutilsSetupError, - UnknownFileError) +from distutils.tests import support +from distutils.errors import (DistutilsSetupError, CompileError, + DistutilsPlatformError) import unittest -from test import support -from test.support.script_helper import assert_python_ok +from test import test_support # http://bugs.python.org/issue4373 # Don't load the xx module more than once. ALREADY_TESTED = False -class BuildExtTestCase(TempdirManager, - LoggingSilencer, +class BuildExtTestCase(support.TempdirManager, + support.LoggingSilencer, + support.EnvironGuard, unittest.TestCase): def setUp(self): - # Create a simple test environment super(BuildExtTestCase, self).setUp() self.tmp_dir = self.mkdtemp() - import site - self.old_user_base = site.USER_BASE - site.USER_BASE = self.mkdtemp() - from distutils.command import build_ext - build_ext.USER_BASE = site.USER_BASE - - # bpo-30132: On Windows, a .pdb file may be created in the current - # working directory. Create a temporary working directory to cleanup - # everything at the end of the test. - change_cwd = support.change_cwd(self.tmp_dir) - change_cwd.__enter__() - self.addCleanup(change_cwd.__exit__, None, None, None) + self.xx_created = False + sys.path.append(self.tmp_dir) + self.addCleanup(sys.path.remove, self.tmp_dir) + if sys.version > "2.6": + import site + self.old_user_base = site.USER_BASE + site.USER_BASE = self.mkdtemp() + from distutils.command import build_ext + build_ext.USER_BASE = site.USER_BASE def tearDown(self): - import site - site.USER_BASE = self.old_user_base - from distutils.command import build_ext - build_ext.USER_BASE = self.old_user_base + if self.xx_created: + test_support.unload('xx') + # XXX on Windows the test leaves a directory + # with xx module in TEMP super(BuildExtTestCase, self).tearDown() - def build_ext(self, *args, **kwargs): - return build_ext(*args, **kwargs) - def test_build_ext(self): - cmd = support.missing_compiler_executable() - if cmd is not None: - self.skipTest('The %r command is not found' % cmd) global ALREADY_TESTED - copy_xxmodule_c(self.tmp_dir) + support.copy_xxmodule_c(self.tmp_dir) + self.xx_created = True xx_c = os.path.join(self.tmp_dir, 'xxmodule.c') xx_ext = Extension('xx', [xx_c]) dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]}) dist.package_dir = self.tmp_dir - cmd = self.build_ext(dist) - fixup_build_ext(cmd) + cmd = build_ext(dist) + support.fixup_build_ext(cmd) cmd.build_lib = self.tmp_dir cmd.build_temp = self.tmp_dir old_stdout = sys.stdout - if not support.verbose: + if not test_support.verbose: # silence compiler output sys.stdout = StringIO() try: @@ -82,38 +70,23 @@ class BuildExtTestCase(TempdirManager, else: ALREADY_TESTED = type(self).__name__ - code = textwrap.dedent(f""" - tmp_dir = {self.tmp_dir!r} - - import sys - import unittest - from test import support - - sys.path.insert(0, tmp_dir) - import xx - - class Tests(unittest.TestCase): - def test_xx(self): - for attr in ('error', 'foo', 'new', 'roj'): - self.assertTrue(hasattr(xx, attr)) - - self.assertEqual(xx.foo(2, 5), 7) - self.assertEqual(xx.foo(13,15), 28) - self.assertEqual(xx.new().demo(), None) - if support.HAVE_DOCSTRINGS: - doc = 'This is a template module just for instruction.' - self.assertEqual(xx.__doc__, doc) - self.assertIsInstance(xx.Null(), xx.Null) - self.assertIsInstance(xx.Str(), xx.Str) + import xx + for attr in ('error', 'foo', 'new', 'roj'): + self.assertTrue(hasattr(xx, attr)) - unittest.main() - """) - assert_python_ok('-c', code) + self.assertEqual(xx.foo(2, 5), 7) + self.assertEqual(xx.foo(13,15), 28) + self.assertEqual(xx.new().demo(), None) + if test_support.HAVE_DOCSTRINGS: + doc = 'This is a template module just for instruction.' + self.assertEqual(xx.__doc__, doc) + self.assertIsInstance(xx.Null(), xx.Null) + self.assertIsInstance(xx.Str(), xx.Str) def test_solaris_enable_shared(self): dist = Distribution({'name': 'xx'}) - cmd = self.build_ext(dist) + cmd = build_ext(dist) old = sys.platform sys.platform = 'sunos' # fooling finalize_options @@ -132,13 +105,15 @@ class BuildExtTestCase(TempdirManager, # make sure we get some library dirs under solaris self.assertGreater(len(cmd.library_dirs), 0) + @unittest.skipIf(sys.version < '2.6', + 'site.USER_SITE was introduced in 2.6') def test_user_site(self): import site dist = Distribution({'name': 'xx'}) - cmd = self.build_ext(dist) + cmd = build_ext(dist) # making sure the user option is there - options = [name for name, short, lable in + options = [name for name, short, label in cmd.user_options] self.assertIn('user', options) @@ -151,58 +126,37 @@ class BuildExtTestCase(TempdirManager, os.mkdir(lib) os.mkdir(incl) - # let's run finalize cmd.ensure_finalized() - # see if include_dirs and library_dirs - # were set + # see if include_dirs and library_dirs were set self.assertIn(lib, cmd.library_dirs) self.assertIn(lib, cmd.rpath) self.assertIn(incl, cmd.include_dirs) - def test_optional_extension(self): - - # this extension will fail, but let's ignore this failure - # with the optional argument. - modules = [Extension('foo', ['xxx'], optional=False)] - dist = Distribution({'name': 'xx', 'ext_modules': modules}) - cmd = self.build_ext(dist) - cmd.ensure_finalized() - self.assertRaises((UnknownFileError, CompileError), - cmd.run) # should raise an error - - modules = [Extension('foo', ['xxx'], optional=True)] - dist = Distribution({'name': 'xx', 'ext_modules': modules}) - cmd = self.build_ext(dist) - cmd.ensure_finalized() - cmd.run() # should pass - def test_finalize_options(self): # Make sure Python's include directories (for Python.h, pyconfig.h, # etc.) are in the include search path. - modules = [Extension('foo', ['xxx'], optional=False)] + modules = [Extension('foo', ['xxx'])] dist = Distribution({'name': 'xx', 'ext_modules': modules}) - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.finalize_options() py_include = sysconfig.get_python_inc() - for p in py_include.split(os.path.pathsep): - self.assertIn(p, cmd.include_dirs) + self.assertIn(py_include, cmd.include_dirs) plat_py_include = sysconfig.get_python_inc(plat_specific=1) - for p in plat_py_include.split(os.path.pathsep): - self.assertIn(p, cmd.include_dirs) + self.assertIn(plat_py_include, cmd.include_dirs) # make sure cmd.libraries is turned into a list # if it's a string - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.libraries = 'my_lib, other_lib lastlib' cmd.finalize_options() self.assertEqual(cmd.libraries, ['my_lib', 'other_lib', 'lastlib']) # make sure cmd.library_dirs is turned into a list # if it's a string - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.library_dirs = 'my_lib_dir%sother_lib_dir' % os.pathsep cmd.finalize_options() self.assertIn('my_lib_dir', cmd.library_dirs) @@ -210,7 +164,7 @@ class BuildExtTestCase(TempdirManager, # make sure rpath is turned into a list # if it's a string - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.rpath = 'one%stwo' % os.pathsep cmd.finalize_options() self.assertEqual(cmd.rpath, ['one', 'two']) @@ -226,37 +180,36 @@ class BuildExtTestCase(TempdirManager, # make sure define is turned into 2-tuples # strings if they are ','-separated strings - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.define = 'one,two' cmd.finalize_options() self.assertEqual(cmd.define, [('one', '1'), ('two', '1')]) # make sure undef is turned into a list of # strings if they are ','-separated strings - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.undef = 'one,two' cmd.finalize_options() self.assertEqual(cmd.undef, ['one', 'two']) # make sure swig_opts is turned into a list - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.swig_opts = None cmd.finalize_options() self.assertEqual(cmd.swig_opts, []) - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.swig_opts = '1 2' cmd.finalize_options() self.assertEqual(cmd.swig_opts, ['1', '2']) def test_check_extensions_list(self): dist = Distribution() - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.finalize_options() #'extensions' option must be a list of Extension instances - self.assertRaises(DistutilsSetupError, - cmd.check_extensions_list, 'foo') + self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, 'foo') # each element of 'ext_modules' option must be an # Extension instance or 2-tuple @@ -298,9 +251,9 @@ class BuildExtTestCase(TempdirManager, self.assertEqual(exts[0].define_macros, [('1', '2')]) def test_get_source_files(self): - modules = [Extension('foo', ['xxx'], optional=False)] + modules = [Extension('foo', ['xxx'])] dist = Distribution({'name': 'xx', 'ext_modules': modules}) - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.ensure_finalized() self.assertEqual(cmd.get_source_files(), ['xxx']) @@ -309,24 +262,21 @@ class BuildExtTestCase(TempdirManager, # should not be overridden by a compiler instance # when the command is run dist = Distribution() - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.compiler = 'unix' cmd.ensure_finalized() cmd.run() self.assertEqual(cmd.compiler, 'unix') def test_get_outputs(self): - cmd = support.missing_compiler_executable() - if cmd is not None: - self.skipTest('The %r command is not found' % cmd) tmp_dir = self.mkdtemp() c_file = os.path.join(tmp_dir, 'foo.c') - self.write_file(c_file, 'void PyInit_foo(void) {}\n') - ext = Extension('foo', [c_file], optional=False) + self.write_file(c_file, 'void initfoo(void) {};\n') + ext = Extension('foo', [c_file]) dist = Distribution({'name': 'xx', 'ext_modules': [ext]}) - cmd = self.build_ext(dist) - fixup_build_ext(cmd) + cmd = build_ext(dist) + support.fixup_build_ext(cmd) cmd.ensure_finalized() self.assertEqual(len(cmd.get_outputs()), 1) @@ -345,17 +295,17 @@ class BuildExtTestCase(TempdirManager, finally: os.chdir(old_wd) self.assertTrue(os.path.exists(so_file)) - ext_suffix = sysconfig.get_config_var('EXT_SUFFIX') - self.assertTrue(so_file.endswith(ext_suffix)) + self.assertEqual(os.path.splitext(so_file)[-1], + sysconfig.get_config_var('SO')) so_dir = os.path.dirname(so_file) self.assertEqual(so_dir, other_tmp_dir) - - cmd.inplace = 0 cmd.compiler = None + cmd.inplace = 0 cmd.run() so_file = cmd.get_outputs()[0] self.assertTrue(os.path.exists(so_file)) - self.assertTrue(so_file.endswith(ext_suffix)) + self.assertEqual(os.path.splitext(so_file)[-1], + sysconfig.get_config_var('SO')) so_dir = os.path.dirname(so_file) self.assertEqual(so_dir, cmd.build_lib) @@ -382,13 +332,9 @@ class BuildExtTestCase(TempdirManager, self.assertEqual(lastdir, 'bar') def test_ext_fullpath(self): - ext = sysconfig.get_config_var('EXT_SUFFIX') - # building lxml.etree inplace - #etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c') - #etree_ext = Extension('lxml.etree', [etree_c]) - #dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]}) + ext = sysconfig.get_config_vars()['SO'] dist = Distribution() - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.inplace = 1 cmd.distribution.package_dir = {'': 'src'} cmd.distribution.packages = ['lxml', 'lxml.html'] @@ -419,6 +365,72 @@ class BuildExtTestCase(TempdirManager, wanted = os.path.join(curdir, 'twisted', 'runner', 'portmap' + ext) self.assertEqual(wanted, path) + def test_build_ext_inplace(self): + etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c') + etree_ext = Extension('lxml.etree', [etree_c]) + dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]}) + cmd = build_ext(dist) + cmd.ensure_finalized() + cmd.inplace = 1 + cmd.distribution.package_dir = {'': 'src'} + cmd.distribution.packages = ['lxml', 'lxml.html'] + curdir = os.getcwd() + ext = sysconfig.get_config_var("SO") + wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext) + path = cmd.get_ext_fullpath('lxml.etree') + self.assertEqual(wanted, path) + + def test_setuptools_compat(self): + import distutils.core, distutils.extension, distutils.command.build_ext + saved_ext = distutils.extension.Extension + try: + # on some platforms, it loads the deprecated "dl" module + test_support.import_module('setuptools_build_ext', deprecated=True) + + # theses import patch Distutils' Extension class + from setuptools_build_ext import build_ext as setuptools_build_ext + from setuptools_extension import Extension + + etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c') + etree_ext = Extension('lxml.etree', [etree_c]) + dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]}) + cmd = setuptools_build_ext(dist) + cmd.ensure_finalized() + cmd.inplace = 1 + cmd.distribution.package_dir = {'': 'src'} + cmd.distribution.packages = ['lxml', 'lxml.html'] + curdir = os.getcwd() + ext = sysconfig.get_config_var("SO") + wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext) + path = cmd.get_ext_fullpath('lxml.etree') + self.assertEqual(wanted, path) + finally: + # restoring Distutils' Extension class otherwise its broken + distutils.extension.Extension = saved_ext + distutils.core.Extension = saved_ext + distutils.command.build_ext.Extension = saved_ext + + def test_build_ext_path_with_os_sep(self): + dist = Distribution({'name': 'UpdateManager'}) + cmd = build_ext(dist) + cmd.ensure_finalized() + ext = sysconfig.get_config_var("SO") + ext_name = os.path.join('UpdateManager', 'fdsend') + ext_path = cmd.get_ext_fullpath(ext_name) + wanted = os.path.join(cmd.build_lib, 'UpdateManager', 'fdsend' + ext) + self.assertEqual(ext_path, wanted) + + @unittest.skipUnless(sys.platform == 'win32', 'these tests require Windows') + def test_build_ext_path_cross_platform(self): + dist = Distribution({'name': 'UpdateManager'}) + cmd = build_ext(dist) + cmd.ensure_finalized() + ext = sysconfig.get_config_var("SO") + # this needs to work even under win32 + ext_name = 'UpdateManager/fdsend' + ext_path = cmd.get_ext_fullpath(ext_name) + wanted = os.path.join(cmd.build_lib, 'UpdateManager', 'fdsend' + ext) + self.assertEqual(ext_path, wanted) @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX') def test_deployment_target_default(self): @@ -495,38 +507,18 @@ class BuildExtTestCase(TempdirManager, 'ext_modules': [deptarget_ext] }) dist.package_dir = self.tmp_dir - cmd = self.build_ext(dist) + cmd = build_ext(dist) cmd.build_lib = self.tmp_dir cmd.build_temp = self.tmp_dir try: - old_stdout = sys.stdout - if not support.verbose: - # silence compiler output - sys.stdout = StringIO() - try: - cmd.ensure_finalized() - cmd.run() - finally: - sys.stdout = old_stdout - + cmd.ensure_finalized() + cmd.run() except CompileError: self.fail("Wrong deployment target during compilation") - -class ParallelBuildExtTestCase(BuildExtTestCase): - - def build_ext(self, *args, **kwargs): - build_ext = super().build_ext(*args, **kwargs) - build_ext.parallel = True - return build_ext - - def test_suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(BuildExtTestCase)) - suite.addTest(unittest.makeSuite(ParallelBuildExtTestCase)) - return suite + return unittest.makeSuite(BuildExtTestCase) if __name__ == '__main__': - support.run_unittest(__name__) + test_support.run_unittest(test_suite()) diff --git a/Lib/distutils/tests/test_build_py.py b/Lib/distutils/tests/test_build_py.py index 0712e92..c4498bc 100644 --- a/Lib/distutils/tests/test_build_py.py +++ b/Lib/distutils/tests/test_build_py.py @@ -2,6 +2,7 @@ import os import sys +import StringIO import unittest from distutils.command.build_py import build_py @@ -9,7 +10,7 @@ from distutils.core import Distribution from distutils.errors import DistutilsFileError from distutils.tests import support -from test.support import run_unittest +from test.test_support import run_unittest class BuildPyTestCase(support.TempdirManager, @@ -52,21 +53,23 @@ class BuildPyTestCase(support.TempdirManager, # This makes sure the list of outputs includes byte-compiled # files for Python modules but not for package data files # (there shouldn't *be* byte-code files for those!). + # self.assertEqual(len(cmd.get_outputs()), 3) pkgdest = os.path.join(destination, "pkg") files = os.listdir(pkgdest) - pycache_dir = os.path.join(pkgdest, "__pycache__") self.assertIn("__init__.py", files) self.assertIn("README.txt", files) + # XXX even with -O, distutils writes pyc, not pyo; bug? if sys.dont_write_bytecode: - self.assertFalse(os.path.exists(pycache_dir)) + self.assertNotIn("__init__.pyc", files) else: - pyc_files = os.listdir(pycache_dir) - self.assertIn("__init__.%s.pyc" % sys.implementation.cache_tag, - pyc_files) + self.assertIn("__init__.pyc", files) def test_empty_package_dir(self): - # See bugs #1668596/#1720897 + # See SF 1668596/1720897. + cwd = os.getcwd() + + # create the distribution files. sources = self.mkdtemp() open(os.path.join(sources, "__init__.py"), "w").close() @@ -75,53 +78,26 @@ class BuildPyTestCase(support.TempdirManager, open(os.path.join(testdir, "testfile"), "w").close() os.chdir(sources) - dist = Distribution({"packages": ["pkg"], - "package_dir": {"pkg": ""}, - "package_data": {"pkg": ["doc/*"]}}) - # script_name need not exist, it just need to be initialized - dist.script_name = os.path.join(sources, "setup.py") - dist.script_args = ["build"] - dist.parse_command_line() + old_stdout = sys.stdout + sys.stdout = StringIO.StringIO() try: - dist.run_commands() - except DistutilsFileError: - self.fail("failed package_data test when package_dir is ''") - - @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled') - def test_byte_compile(self): - project_dir, dist = self.create_dist(py_modules=['boiledeggs']) - os.chdir(project_dir) - self.write_file('boiledeggs.py', 'import antigravity') - cmd = build_py(dist) - cmd.compile = 1 - cmd.build_lib = 'here' - cmd.finalize_options() - cmd.run() - - found = os.listdir(cmd.build_lib) - self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py']) - found = os.listdir(os.path.join(cmd.build_lib, '__pycache__')) - self.assertEqual(found, - ['boiledeggs.%s.pyc' % sys.implementation.cache_tag]) - - @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled') - def test_byte_compile_optimized(self): - project_dir, dist = self.create_dist(py_modules=['boiledeggs']) - os.chdir(project_dir) - self.write_file('boiledeggs.py', 'import antigravity') - cmd = build_py(dist) - cmd.compile = 0 - cmd.optimize = 1 - cmd.build_lib = 'here' - cmd.finalize_options() - cmd.run() - - found = os.listdir(cmd.build_lib) - self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py']) - found = os.listdir(os.path.join(cmd.build_lib, '__pycache__')) - expect = 'boiledeggs.{}.opt-1.pyc'.format(sys.implementation.cache_tag) - self.assertEqual(sorted(found), [expect]) + dist = Distribution({"packages": ["pkg"], + "package_dir": {"pkg": ""}, + "package_data": {"pkg": ["doc/*"]}}) + # script_name need not exist, it just need to be initialized + dist.script_name = os.path.join(sources, "setup.py") + dist.script_args = ["build"] + dist.parse_command_line() + + try: + dist.run_commands() + except DistutilsFileError: + self.fail("failed package_data test when package_dir is ''") + finally: + # Restore state. + os.chdir(cwd) + sys.stdout = old_stdout def test_dir_in_package_data(self): """ @@ -156,7 +132,7 @@ class BuildPyTestCase(support.TempdirManager, def test_dont_write_bytecode(self): # makes sure byte_compile is not used - dist = self.create_dist()[1] + pkg_dir, dist = self.create_dist() cmd = build_py(dist) cmd.compile = 1 cmd.optimize = 1 @@ -168,9 +144,7 @@ class BuildPyTestCase(support.TempdirManager, finally: sys.dont_write_bytecode = old_dont_write_bytecode - self.assertIn('byte-compiling is disabled', - self.logs[0][1] % self.logs[0][2]) - + self.assertIn('byte-compiling is disabled', self.logs[0][1]) def test_suite(): return unittest.makeSuite(BuildPyTestCase) diff --git a/Lib/distutils/tests/test_build_scripts.py b/Lib/distutils/tests/test_build_scripts.py index 954fc76..ad3c1a2 100644 --- a/Lib/distutils/tests/test_build_scripts.py +++ b/Lib/distutils/tests/test_build_scripts.py @@ -5,10 +5,10 @@ import unittest from distutils.command.build_scripts import build_scripts from distutils.core import Distribution -from distutils import sysconfig +import sysconfig from distutils.tests import support -from test.support import run_unittest +from test.test_support import run_unittest class BuildScriptsTestCase(support.TempdirManager, @@ -94,12 +94,12 @@ class BuildScriptsTestCase(support.TempdirManager, # --with-suffix=3`, python is compiled okay but the build scripts # failed when writing the name of the executable old = sysconfig.get_config_vars().get('VERSION') - sysconfig._config_vars['VERSION'] = 4 + sysconfig._CONFIG_VARS['VERSION'] = 4 try: cmd.run() finally: if old is not None: - sysconfig._config_vars['VERSION'] = old + sysconfig._CONFIG_VARS['VERSION'] = old built = os.listdir(target) for name in expected: diff --git a/Lib/distutils/tests/test_ccompiler.py b/Lib/distutils/tests/test_ccompiler.py new file mode 100644 index 0000000..4976098 --- /dev/null +++ b/Lib/distutils/tests/test_ccompiler.py @@ -0,0 +1,103 @@ +"""Tests for distutils.ccompiler.""" +import os +import unittest +from test.test_support import captured_stdout + +from distutils.ccompiler import (gen_lib_options, CCompiler, + get_default_compiler) +from distutils.sysconfig import customize_compiler +from distutils import debug +from distutils.tests import support + +class FakeCompiler(object): + def library_dir_option(self, dir): + return "-L" + dir + + def runtime_library_dir_option(self, dir): + return ["-cool", "-R" + dir] + + def find_library_file(self, dirs, lib, debug=0): + return 'found' + + def library_option(self, lib): + return "-l" + lib + +class CCompilerTestCase(support.EnvironGuard, unittest.TestCase): + + def test_set_executables(self): + class MyCCompiler(CCompiler): + executables = {'compiler': '', 'compiler_cxx': '', 'linker': ''} + + compiler = MyCCompiler() + + # set executable as list + compiler.set_executables(compiler=['env', 'OMPI_MPICC=clang', 'mpicc']) + self.assertEqual(compiler.compiler, ['env', + 'OMPI_MPICC=clang', + 'mpicc']) + + # set executable as string + compiler.set_executables(compiler_cxx='env OMPI_MPICXX=clang++ mpicxx') + self.assertEqual(compiler.compiler_cxx, ['env', + 'OMPI_MPICXX=clang++', + 'mpicxx']) + + # set executable as unicode string + compiler.set_executables(linker=u'env OMPI_MPICXX=clang++ mpiCC') + self.assertEqual(compiler.linker, [u'env', + u'OMPI_MPICXX=clang++', + u'mpiCC']) + + def test_gen_lib_options(self): + compiler = FakeCompiler() + libdirs = ['lib1', 'lib2'] + runlibdirs = ['runlib1'] + libs = [os.path.join('dir', 'name'), 'name2'] + + opts = gen_lib_options(compiler, libdirs, runlibdirs, libs) + wanted = ['-Llib1', '-Llib2', '-cool', '-Rrunlib1', 'found', + '-lname2'] + self.assertEqual(opts, wanted) + + def test_debug_print(self): + + class MyCCompiler(CCompiler): + executables = {} + + compiler = MyCCompiler() + with captured_stdout() as stdout: + compiler.debug_print('xxx') + stdout.seek(0) + self.assertEqual(stdout.read(), '') + + debug.DEBUG = True + try: + with captured_stdout() as stdout: + compiler.debug_print('xxx') + stdout.seek(0) + self.assertEqual(stdout.read(), 'xxx\n') + finally: + debug.DEBUG = False + + @unittest.skipUnless(get_default_compiler() == 'unix', + 'not testing if default compiler is not unix') + def test_customize_compiler(self): + os.environ['AR'] = 'my_ar' + os.environ['ARFLAGS'] = '-arflags' + + # make sure AR gets caught + class compiler: + compiler_type = 'unix' + + def set_executables(self, **kw): + self.exes = kw + + comp = compiler() + customize_compiler(comp) + self.assertEqual(comp.exes['archiver'], 'my_ar -arflags') + +def test_suite(): + return unittest.makeSuite(CCompilerTestCase) + +if __name__ == "__main__": + unittest.main(defaultTest="test_suite") diff --git a/Lib/distutils/tests/test_check.py b/Lib/distutils/tests/test_check.py index e534aca..2e2cf13 100644 --- a/Lib/distutils/tests/test_check.py +++ b/Lib/distutils/tests/test_check.py @@ -1,8 +1,9 @@ +# -*- encoding: utf8 -*- """Tests for distutils.command.check.""" import os import textwrap import unittest -from test.support import run_unittest +from test.test_support import run_unittest from distutils.command.check import check, HAS_DOCUTILS from distutils.tests import support @@ -62,12 +63,12 @@ class CheckTestCase(support.LoggingSilencer, cmd = self._run(metadata, strict=1) self.assertEqual(cmd._warnings, 0) - # now a test with non-ASCII characters - metadata = {'url': 'xxx', 'author': '\u00c9ric', - 'author_email': 'xxx', 'name': 'xxx', - 'version': 'xxx', - 'description': 'Something about esszet \u00df', - 'long_description': 'More things about esszet \u00df'} + # now a test with Unicode entries + metadata = {'url': u'xxx', 'author': u'\u00c9ric', + 'author_email': u'xxx', u'name': 'xxx', + 'version': u'xxx', + 'description': u'Something about esszet \u00df', + 'long_description': u'More things about esszet \u00df'} cmd = self._run(metadata) self.assertEqual(cmd._warnings, 0) @@ -104,7 +105,7 @@ class CheckTestCase(support.LoggingSilencer, **{'strict': 1, 'restructuredtext': 1}) # and non-broken rest, including a non-ASCII character to test #12114 - metadata['long_description'] = 'title\n=====\n\ntest \u00df' + metadata['long_description'] = u'title\n=====\n\ntest \u00df' cmd = self._run(metadata, strict=1, restructuredtext=1) self.assertEqual(cmd._warnings, 0) diff --git a/Lib/distutils/tests/test_clean.py b/Lib/distutils/tests/test_clean.py index c605afd..e84670d 100644 --- a/Lib/distutils/tests/test_clean.py +++ b/Lib/distutils/tests/test_clean.py @@ -1,10 +1,12 @@ """Tests for distutils.command.clean.""" +import sys import os import unittest +import getpass from distutils.command.clean import clean from distutils.tests import support -from test.support import run_unittest +from test.test_support import run_unittest class cleanTestCase(support.TempdirManager, support.LoggingSilencer, diff --git a/Lib/distutils/tests/test_cmd.py b/Lib/distutils/tests/test_cmd.py index cf5197c..51420c9 100644 --- a/Lib/distutils/tests/test_cmd.py +++ b/Lib/distutils/tests/test_cmd.py @@ -1,7 +1,7 @@ """Tests for distutils.cmd.""" import unittest import os -from test.support import captured_stdout, run_unittest +from test.test_support import captured_stdout, run_unittest from distutils.cmd import Command from distutils.dist import Distribution diff --git a/Lib/distutils/tests/test_config.py b/Lib/distutils/tests/test_config.py index 344084a..4e8af0f 100644 --- a/Lib/distutils/tests/test_config.py +++ b/Lib/distutils/tests/test_config.py @@ -1,6 +1,9 @@ """Tests for distutils.pypirc.pypirc.""" +import sys import os import unittest +import tempfile +import shutil from distutils.core import PyPIRCCommand from distutils.core import Distribution @@ -8,7 +11,7 @@ from distutils.log import set_threshold from distutils.log import WARN from distutils.tests import support -from test.support import run_unittest +from test.test_support import run_unittest PYPIRC = """\ [distutils] @@ -16,7 +19,6 @@ PYPIRC = """\ index-servers = server1 server2 - server3 [server1] username:me @@ -27,10 +29,6 @@ username:meagain password: secret realm:acme repository:http://another.pypi/ - -[server3] -username:cbiggles -password:yh^%#rest-of-my-password """ PYPIRC_OLD = """\ @@ -50,17 +48,16 @@ password:xxx """ -class BasePyPIRCCommandTestCase(support.TempdirManager, +class PyPIRCCommandTestCase(support.TempdirManager, support.LoggingSilencer, support.EnvironGuard, unittest.TestCase): def setUp(self): """Patches the environment.""" - super(BasePyPIRCCommandTestCase, self).setUp() + super(PyPIRCCommandTestCase, self).setUp() self.tmp_dir = self.mkdtemp() os.environ['HOME'] = self.tmp_dir - os.environ['USERPROFILE'] = self.tmp_dir self.rc = os.path.join(self.tmp_dir, '.pypirc') self.dist = Distribution() @@ -77,10 +74,7 @@ class BasePyPIRCCommandTestCase(support.TempdirManager, def tearDown(self): """Removes the patch.""" set_threshold(self.old_threshold) - super(BasePyPIRCCommandTestCase, self).tearDown() - - -class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase): + super(PyPIRCCommandTestCase, self).tearDown() def test_server_registration(self): # This test makes sure PyPIRCCommand knows how to: @@ -92,7 +86,8 @@ class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase): cmd = self._cmd(self.dist) config = cmd._read_pypirc() - config = list(sorted(config.items())) + config = config.items() + config.sort() waited = [('password', 'secret'), ('realm', 'pypi'), ('repository', 'https://upload.pypi.org/legacy/'), ('server', 'server1'), ('username', 'me')] @@ -101,7 +96,8 @@ class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase): # old format self.write_file(self.rc, PYPIRC_OLD) config = cmd._read_pypirc() - config = list(sorted(config.items())) + config = config.items() + config.sort() waited = [('password', 'secret'), ('realm', 'pypi'), ('repository', 'https://upload.pypi.org/legacy/'), ('server', 'server-login'), ('username', 'tarek')] @@ -120,20 +116,6 @@ class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase): finally: f.close() - def test_config_interpolation(self): - # using the % character in .pypirc should not raise an error (#20120) - self.write_file(self.rc, PYPIRC) - cmd = self._cmd(self.dist) - cmd.repository = 'server3' - config = cmd._read_pypirc() - - config = list(sorted(config.items())) - waited = [('password', 'yh^%#rest-of-my-password'), ('realm', 'pypi'), - ('repository', 'https://upload.pypi.org/legacy/'), - ('server', 'server3'), ('username', 'cbiggles')] - self.assertEqual(config, waited) - - def test_suite(): return unittest.makeSuite(PyPIRCCommandTestCase) diff --git a/Lib/distutils/tests/test_config_cmd.py b/Lib/distutils/tests/test_config_cmd.py index b735fd3..b2a418e 100644 --- a/Lib/distutils/tests/test_config_cmd.py +++ b/Lib/distutils/tests/test_config_cmd.py @@ -2,7 +2,7 @@ import unittest import os import sys -from test.support import run_unittest, missing_compiler_executable +from test.test_support import run_unittest from distutils.command.config import dump_file, config from distutils.tests import support @@ -39,17 +39,8 @@ class ConfigTestCase(support.LoggingSilencer, @unittest.skipIf(sys.platform == 'win32', "can't test on Windows") def test_search_cpp(self): - import shutil - cmd = missing_compiler_executable(['preprocessor']) - if cmd is not None: - self.skipTest('The %r command is not found' % cmd) pkg_dir, dist = self.create_dist() cmd = config(dist) - cmd._check_compiler() - compiler = cmd.compiler - is_xlc = shutil.which(compiler.preprocessor[0]).startswith("/usr/vac") - if is_xlc: - self.skipTest('xlc: The -E option overrides the -P, -o, and -qsyntaxonly options') # simple pattern searches match = cmd.search_cpp(pattern='xxx', body='/* xxx */') diff --git a/Lib/distutils/tests/test_core.py b/Lib/distutils/tests/test_core.py index 27ce732..e6d4c9b 100644 --- a/Lib/distutils/tests/test_core.py +++ b/Lib/distutils/tests/test_core.py @@ -1,12 +1,12 @@ """Tests for distutils.core.""" -import io +import StringIO import distutils.core import os import shutil import sys -import test.support -from test.support import captured_stdout, run_unittest +import test.test_support +from test.test_support import captured_stdout, run_unittest import unittest from distutils.tests import support from distutils import log @@ -23,27 +23,12 @@ setup() setup_prints_cwd = """\ import os -print(os.getcwd()) +print os.getcwd() from distutils.core import setup setup() """ -setup_does_nothing = """\ -from distutils.core import setup -setup() -""" - - -setup_defines_subclass = """\ -from distutils.core import setup -from distutils.command.install import install as _install - -class install(_install): - sub_commands = _install.sub_commands + ['cmd'] - -setup(cmdclass={'install': install}) -""" class CoreTestCase(support.EnvironGuard, unittest.TestCase): @@ -62,13 +47,13 @@ class CoreTestCase(support.EnvironGuard, unittest.TestCase): super(CoreTestCase, self).tearDown() def cleanup_testfn(self): - path = test.support.TESTFN + path = test.test_support.TESTFN if os.path.isfile(path): os.remove(path) elif os.path.isdir(path): shutil.rmtree(path) - def write_setup(self, text, path=test.support.TESTFN): + def write_setup(self, text, path=test.test_support.TESTFN): f = open(path, "w") try: f.write(text) @@ -82,31 +67,16 @@ class CoreTestCase(support.EnvironGuard, unittest.TestCase): distutils.core.run_setup( self.write_setup(setup_using___file__)) - def test_run_setup_preserves_sys_argv(self): - # Make sure run_setup does not clobber sys.argv - argv_copy = sys.argv.copy() - distutils.core.run_setup( - self.write_setup(setup_does_nothing)) - self.assertEqual(sys.argv, argv_copy) - - def test_run_setup_defines_subclass(self): - # Make sure the script can use __file__; if that's missing, the test - # setup.py script will raise NameError. - dist = distutils.core.run_setup( - self.write_setup(setup_defines_subclass)) - install = dist.get_command_obj('install') - self.assertIn('cmd', install.sub_commands) - def test_run_setup_uses_current_dir(self): # This tests that the setup script is run with the current directory # as its own current directory; this was temporarily broken by a # previous patch when TESTFN did not use the current directory. - sys.stdout = io.StringIO() + sys.stdout = StringIO.StringIO() cwd = os.getcwd() # Create a directory and write the setup.py file there: - os.mkdir(test.support.TESTFN) - setup_py = os.path.join(test.support.TESTFN, "setup.py") + os.mkdir(test.test_support.TESTFN) + setup_py = os.path.join(test.test_support.TESTFN, "setup.py") distutils.core.run_setup( self.write_setup(setup_prints_cwd, path=setup_py)) diff --git a/Lib/distutils/tests/test_cygwinccompiler.py b/Lib/distutils/tests/test_cygwinccompiler.py deleted file mode 100644 index 9dc869d..0000000 --- a/Lib/distutils/tests/test_cygwinccompiler.py +++ /dev/null @@ -1,154 +0,0 @@ -"""Tests for distutils.cygwinccompiler.""" -import unittest -import sys -import os -from io import BytesIO -from test.support import run_unittest - -from distutils import cygwinccompiler -from distutils.cygwinccompiler import (check_config_h, - CONFIG_H_OK, CONFIG_H_NOTOK, - CONFIG_H_UNCERTAIN, get_versions, - get_msvcr) -from distutils.tests import support - -class FakePopen(object): - test_class = None - - def __init__(self, cmd, shell, stdout): - self.cmd = cmd.split()[0] - exes = self.test_class._exes - if self.cmd in exes: - # issue #6438 in Python 3.x, Popen returns bytes - self.stdout = BytesIO(exes[self.cmd]) - else: - self.stdout = os.popen(cmd, 'r') - - -class CygwinCCompilerTestCase(support.TempdirManager, - unittest.TestCase): - - def setUp(self): - super(CygwinCCompilerTestCase, self).setUp() - self.version = sys.version - self.python_h = os.path.join(self.mkdtemp(), 'python.h') - from distutils import sysconfig - self.old_get_config_h_filename = sysconfig.get_config_h_filename - sysconfig.get_config_h_filename = self._get_config_h_filename - self.old_find_executable = cygwinccompiler.find_executable - cygwinccompiler.find_executable = self._find_executable - self._exes = {} - self.old_popen = cygwinccompiler.Popen - FakePopen.test_class = self - cygwinccompiler.Popen = FakePopen - - def tearDown(self): - sys.version = self.version - from distutils import sysconfig - sysconfig.get_config_h_filename = self.old_get_config_h_filename - cygwinccompiler.find_executable = self.old_find_executable - cygwinccompiler.Popen = self.old_popen - super(CygwinCCompilerTestCase, self).tearDown() - - def _get_config_h_filename(self): - return self.python_h - - def _find_executable(self, name): - if name in self._exes: - return name - return None - - def test_check_config_h(self): - - # check_config_h looks for "GCC" in sys.version first - # returns CONFIG_H_OK if found - sys.version = ('2.6.1 (r261:67515, Dec 6 2008, 16:42:21) \n[GCC ' - '4.0.1 (Apple Computer, Inc. build 5370)]') - - self.assertEqual(check_config_h()[0], CONFIG_H_OK) - - # then it tries to see if it can find "__GNUC__" in pyconfig.h - sys.version = 'something without the *CC word' - - # if the file doesn't exist it returns CONFIG_H_UNCERTAIN - self.assertEqual(check_config_h()[0], CONFIG_H_UNCERTAIN) - - # if it exists but does not contain __GNUC__, it returns CONFIG_H_NOTOK - self.write_file(self.python_h, 'xxx') - self.assertEqual(check_config_h()[0], CONFIG_H_NOTOK) - - # and CONFIG_H_OK if __GNUC__ is found - self.write_file(self.python_h, 'xxx __GNUC__ xxx') - self.assertEqual(check_config_h()[0], CONFIG_H_OK) - - def test_get_versions(self): - - # get_versions calls distutils.spawn.find_executable on - # 'gcc', 'ld' and 'dllwrap' - self.assertEqual(get_versions(), (None, None, None)) - - # Let's fake we have 'gcc' and it returns '3.4.5' - self._exes['gcc'] = b'gcc (GCC) 3.4.5 (mingw special)\nFSF' - res = get_versions() - self.assertEqual(str(res[0]), '3.4.5') - - # and let's see what happens when the version - # doesn't match the regular expression - # (\d+\.\d+(\.\d+)*) - self._exes['gcc'] = b'very strange output' - res = get_versions() - self.assertEqual(res[0], None) - - # same thing for ld - self._exes['ld'] = b'GNU ld version 2.17.50 20060824' - res = get_versions() - self.assertEqual(str(res[1]), '2.17.50') - self._exes['ld'] = b'@(#)PROGRAM:ld PROJECT:ld64-77' - res = get_versions() - self.assertEqual(res[1], None) - - # and dllwrap - self._exes['dllwrap'] = b'GNU dllwrap 2.17.50 20060824\nFSF' - res = get_versions() - self.assertEqual(str(res[2]), '2.17.50') - self._exes['dllwrap'] = b'Cheese Wrap' - res = get_versions() - self.assertEqual(res[2], None) - - def test_get_msvcr(self): - - # none - sys.version = ('2.6.1 (r261:67515, Dec 6 2008, 16:42:21) ' - '\n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]') - self.assertEqual(get_msvcr(), None) - - # MSVC 7.0 - sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' - '[MSC v.1300 32 bits (Intel)]') - self.assertEqual(get_msvcr(), ['msvcr70']) - - # MSVC 7.1 - sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' - '[MSC v.1310 32 bits (Intel)]') - self.assertEqual(get_msvcr(), ['msvcr71']) - - # VS2005 / MSVC 8.0 - sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' - '[MSC v.1400 32 bits (Intel)]') - self.assertEqual(get_msvcr(), ['msvcr80']) - - # VS2008 / MSVC 9.0 - sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' - '[MSC v.1500 32 bits (Intel)]') - self.assertEqual(get_msvcr(), ['msvcr90']) - - # unknown - sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' - '[MSC v.1999 32 bits (Intel)]') - self.assertRaises(ValueError, get_msvcr) - -def test_suite(): - return unittest.makeSuite(CygwinCCompilerTestCase) - -if __name__ == '__main__': - run_unittest(test_suite()) diff --git a/Lib/distutils/tests/test_dep_util.py b/Lib/distutils/tests/test_dep_util.py index c6fae39..7510434 100644 --- a/Lib/distutils/tests/test_dep_util.py +++ b/Lib/distutils/tests/test_dep_util.py @@ -1,11 +1,12 @@ """Tests for distutils.dep_util.""" import unittest import os +import time from distutils.dep_util import newer, newer_pairwise, newer_group from distutils.errors import DistutilsFileError from distutils.tests import support -from test.support import run_unittest +from test.test_support import run_unittest class DepUtilTestCase(support.TempdirManager, unittest.TestCase): diff --git a/Lib/distutils/tests/test_dir_util.py b/Lib/distutils/tests/test_dir_util.py index d436cf8..d82d913 100644 --- a/Lib/distutils/tests/test_dir_util.py +++ b/Lib/distutils/tests/test_dir_util.py @@ -2,17 +2,15 @@ import unittest import os import stat +import shutil import sys -from unittest.mock import patch -from distutils import dir_util, errors from distutils.dir_util import (mkpath, remove_tree, create_tree, copy_tree, ensure_relative) from distutils import log from distutils.tests import support -from test.support import run_unittest - +from test.test_support import run_unittest class DirUtilTestCase(support.TempdirManager, unittest.TestCase): @@ -54,7 +52,7 @@ class DirUtilTestCase(support.TempdirManager, unittest.TestCase): self.assertEqual(self._logs, wanted) @unittest.skipIf(sys.platform.startswith('win'), - "This test is only appropriate for POSIX-like systems.") + "This test is only appropriate for POSIX-like systems.") def test_mkpath_with_custom_mode(self): # Get and set the current umask value for testing mode bits. umask = os.umask(0o002) @@ -78,6 +76,7 @@ class DirUtilTestCase(support.TempdirManager, unittest.TestCase): remove_tree(self.root_target, verbose=0) + def test_copy_tree_verbosity(self): mkpath(self.target, verbose=0) @@ -89,8 +88,11 @@ class DirUtilTestCase(support.TempdirManager, unittest.TestCase): mkpath(self.target, verbose=0) a_file = os.path.join(self.target, 'ok.txt') - with open(a_file, 'w') as f: + f = open(a_file, 'w') + try: f.write('some content') + finally: + f.close() wanted = ['copying %s -> %s' % (a_file, self.target2)] copy_tree(self.target, self.target2, verbose=1) @@ -105,8 +107,11 @@ class DirUtilTestCase(support.TempdirManager, unittest.TestCase): a_file = os.path.join(self.target, 'ok.txt') nfs_file = os.path.join(self.target, '.nfs123abc') for f in a_file, nfs_file: - with open(f, 'w') as fh: + fh = open(f, 'w') + try: fh.write('some content') + finally: + fh.close() copy_tree(self.target, self.target2) self.assertEqual(os.listdir(self.target2), ['ok.txt']) @@ -122,16 +127,6 @@ class DirUtilTestCase(support.TempdirManager, unittest.TestCase): self.assertEqual(ensure_relative('c:\\home\\foo'), 'c:home\\foo') self.assertEqual(ensure_relative('home\\foo'), 'home\\foo') - def test_copy_tree_exception_in_listdir(self): - """ - An exception in listdir should raise a DistutilsFileError - """ - with patch("os.listdir", side_effect=OSError()), \ - self.assertRaises(errors.DistutilsFileError): - src = self.tempdirs[-1] - dir_util.copy_tree(src, None) - - def test_suite(): return unittest.makeSuite(DirUtilTestCase) diff --git a/Lib/distutils/tests/test_dist.py b/Lib/distutils/tests/test_dist.py index cc34725..378fe43 100644 --- a/Lib/distutils/tests/test_dist.py +++ b/Lib/distutils/tests/test_dist.py @@ -1,19 +1,17 @@ +# -*- coding: utf8 -*- + """Tests for distutils.dist.""" import os -import io +import StringIO import sys import unittest import warnings import textwrap -from unittest import mock - -from distutils.dist import Distribution, fix_help_options, DistributionMetadata +from distutils.dist import Distribution, fix_help_options from distutils.cmd import Command - -from test.support import ( - TESTFN, captured_stdout, captured_stderr, run_unittest -) +import distutils.dist +from test.test_support import TESTFN, captured_stdout, run_unittest, unlink from distutils.tests import support from distutils import log @@ -23,7 +21,7 @@ class test_dist(Command): user_options = [ ("sample-option=", "S", "help text"), - ] + ] def initialize_options(self): self.sample_option = None @@ -41,8 +39,8 @@ class TestDistribution(Distribution): return self._config_files -class DistributionTestCase(support.LoggingSilencer, - support.TempdirManager, +class DistributionTestCase(support.TempdirManager, + support.LoggingSilencer, support.EnvironGuard, unittest.TestCase): @@ -63,6 +61,28 @@ class DistributionTestCase(support.LoggingSilencer, d.parse_command_line() return d + def test_debug_mode(self): + with open(TESTFN, "w") as f: + f.write("[global]\n") + f.write("command_packages = foo.bar, splat") + self.addCleanup(unlink, TESTFN) + + files = [TESTFN] + sys.argv.append("build") + + with captured_stdout() as stdout: + self.create_distribution(files) + stdout.seek(0) + self.assertEqual(stdout.read(), '') + distutils.dist.DEBUG = True + try: + with captured_stdout() as stdout: + self.create_distribution(files) + stdout.seek(0) + self.assertEqual(stdout.read(), '') + finally: + distutils.dist.DEBUG = False + def test_command_packages_unspecified(self): sys.argv.append("build") d = self.create_distribution() @@ -83,71 +103,13 @@ class DistributionTestCase(support.LoggingSilencer, self.assertIsInstance(cmd, test_dist) self.assertEqual(cmd.sample_option, "sometext") - def test_venv_install_options(self): - sys.argv.append("install") - self.addCleanup(os.unlink, TESTFN) - - fakepath = '/somedir' - - with open(TESTFN, "w") as f: - print(("[install]\n" - "install-base = {0}\n" - "install-platbase = {0}\n" - "install-lib = {0}\n" - "install-platlib = {0}\n" - "install-purelib = {0}\n" - "install-headers = {0}\n" - "install-scripts = {0}\n" - "install-data = {0}\n" - "prefix = {0}\n" - "exec-prefix = {0}\n" - "home = {0}\n" - "user = {0}\n" - "root = {0}").format(fakepath), file=f) - - # Base case: Not in a Virtual Environment - with mock.patch.multiple(sys, prefix='/a', base_prefix='/a') as values: - d = self.create_distribution([TESTFN]) - - option_tuple = (TESTFN, fakepath) - - result_dict = { - 'install_base': option_tuple, - 'install_platbase': option_tuple, - 'install_lib': option_tuple, - 'install_platlib': option_tuple, - 'install_purelib': option_tuple, - 'install_headers': option_tuple, - 'install_scripts': option_tuple, - 'install_data': option_tuple, - 'prefix': option_tuple, - 'exec_prefix': option_tuple, - 'home': option_tuple, - 'user': option_tuple, - 'root': option_tuple, - } - - self.assertEqual( - sorted(d.command_options.get('install').keys()), - sorted(result_dict.keys())) - - for (key, value) in d.command_options.get('install').items(): - self.assertEqual(value, result_dict[key]) - - # Test case: In a Virtual Environment - with mock.patch.multiple(sys, prefix='/a', base_prefix='/b') as values: - d = self.create_distribution([TESTFN]) - - for key in result_dict.keys(): - self.assertNotIn(key, d.command_options.get('install', {})) - def test_command_packages_configfile(self): sys.argv.append("build") self.addCleanup(os.unlink, TESTFN) f = open(TESTFN, "w") try: - print("[global]", file=f) - print("command_packages = foo.bar, splat", file=f) + print >> f, "[global]" + print >> f, "command_packages = foo.bar, splat" finally: f.close() @@ -167,6 +129,33 @@ class DistributionTestCase(support.LoggingSilencer, d = self.create_distribution([TESTFN]) self.assertEqual(d.get_command_packages(), ["distutils.command"]) + def test_write_pkg_file(self): + # Check DistributionMetadata handling of Unicode fields + tmp_dir = self.mkdtemp() + my_file = os.path.join(tmp_dir, 'f') + klass = Distribution + + dist = klass(attrs={'author': u'Mister Café', + 'name': 'my.package', + 'maintainer': u'Café Junior', + 'description': u'Café torréfié', + 'long_description': u'Héhéhé'}) + + # let's make sure the file can be written + # with Unicode fields. they are encoded with + # PKG_INFO_ENCODING + dist.metadata.write_pkg_file(open(my_file, 'w')) + + # regular ascii is of course always usable + dist = klass(attrs={'author': 'Mister Cafe', + 'name': 'my.package', + 'maintainer': 'Cafe Junior', + 'description': 'Cafe torrefie', + 'long_description': 'Hehehe'}) + + my_file2 = os.path.join(tmp_dir, 'f2') + dist.metadata.write_pkg_file(open(my_file2, 'w')) + def test_empty_options(self): # an empty options dictionary should not stay in the # list of attributes @@ -197,13 +186,6 @@ class DistributionTestCase(support.LoggingSilencer, self.assertEqual(dist.metadata.platforms, ['one', 'two']) self.assertEqual(dist.metadata.keywords, ['one', 'two']) - attrs = {'keywords': 'foo bar', - 'platforms': 'foo bar'} - dist = Distribution(attrs=attrs) - dist.finalize_options() - self.assertEqual(dist.metadata.platforms, ['foo bar']) - self.assertEqual(dist.metadata.keywords, ['foo bar']) - def test_get_command_packages(self): dist = Distribution() self.assertEqual(dist.command_packages, None) @@ -223,7 +205,6 @@ class DistributionTestCase(support.LoggingSilencer, kwargs = {'level': 'ok2'} self.assertRaises(ValueError, dist.announce, args, kwargs) - def test_find_config_files_disable(self): # Ticket #1180: Allow user to disable their home config file. temp_home = self.mkdtemp() @@ -241,10 +222,11 @@ class DistributionTestCase(support.LoggingSilencer, old_expander = os.path.expanduser os.path.expanduser = _expander try: - d = Distribution() + d = distutils.dist.Distribution() all_files = d.find_config_files() - d = Distribution(attrs={'script_args': ['--no-user-cfg']}) + d = distutils.dist.Distribution(attrs={'script_args': + ['--no-user-cfg']}) files = d.find_config_files() finally: os.path.expanduser = old_expander @@ -252,6 +234,7 @@ class DistributionTestCase(support.LoggingSilencer, # make sure --no-user-cfg disables the user cfg file self.assertEqual(len(all_files)-1, len(files)) + class MetadataTestCase(support.TempdirManager, support.EnvironGuard, unittest.TestCase): @@ -264,10 +247,34 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard, sys.argv[:] = self.argv[1] super(MetadataTestCase, self).tearDown() - def format_metadata(self, dist): - sio = io.StringIO() - dist.metadata.write_pkg_file(sio) - return sio.getvalue() + def test_classifier(self): + attrs = {'name': 'Boa', 'version': '3.0', + 'classifiers': ['Programming Language :: Python :: 3']} + dist = Distribution(attrs) + meta = self.format_metadata(dist) + self.assertIn('Metadata-Version: 1.1', meta) + + def test_download_url(self): + attrs = {'name': 'Boa', 'version': '3.0', + 'download_url': 'http://example.org/boa'} + dist = Distribution(attrs) + meta = self.format_metadata(dist) + self.assertIn('Metadata-Version: 1.1', meta) + + def test_long_description(self): + long_desc = textwrap.dedent("""\ + example:: + We start here + and continue here + and end here.""") + attrs = {"name": "package", + "version": "1.0", + "long_description": long_desc} + + dist = Distribution(attrs) + meta = self.format_metadata(dist) + meta = meta.replace('\n' + 8 * ' ', '\n') + self.assertIn(long_desc, meta) def test_simple_metadata(self): attrs = {"name": "package", @@ -321,13 +328,6 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard, "version": "1.0", "requires": ["my.pkg (splat)"]}) - def test_requires_to_list(self): - attrs = {"name": "package", - "requires": iter(["other"])} - dist = Distribution(attrs) - self.assertIsInstance(dist.metadata.requires, list) - - def test_obsoletes(self): attrs = {"name": "package", "version": "1.0", @@ -350,90 +350,10 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard, "version": "1.0", "obsoletes": ["my.pkg (splat)"]}) - def test_obsoletes_to_list(self): - attrs = {"name": "package", - "obsoletes": iter(["other"])} - dist = Distribution(attrs) - self.assertIsInstance(dist.metadata.obsoletes, list) - - def test_classifier(self): - attrs = {'name': 'Boa', 'version': '3.0', - 'classifiers': ['Programming Language :: Python :: 3']} - dist = Distribution(attrs) - self.assertEqual(dist.get_classifiers(), - ['Programming Language :: Python :: 3']) - meta = self.format_metadata(dist) - self.assertIn('Metadata-Version: 1.1', meta) - - def test_classifier_invalid_type(self): - attrs = {'name': 'Boa', 'version': '3.0', - 'classifiers': ('Programming Language :: Python :: 3',)} - with captured_stderr() as error: - d = Distribution(attrs) - # should have warning about passing a non-list - self.assertIn('should be a list', error.getvalue()) - # should be converted to a list - self.assertIsInstance(d.metadata.classifiers, list) - self.assertEqual(d.metadata.classifiers, - list(attrs['classifiers'])) - - def test_keywords(self): - attrs = {'name': 'Monty', 'version': '1.0', - 'keywords': ['spam', 'eggs', 'life of brian']} - dist = Distribution(attrs) - self.assertEqual(dist.get_keywords(), - ['spam', 'eggs', 'life of brian']) - - def test_keywords_invalid_type(self): - attrs = {'name': 'Monty', 'version': '1.0', - 'keywords': ('spam', 'eggs', 'life of brian')} - with captured_stderr() as error: - d = Distribution(attrs) - # should have warning about passing a non-list - self.assertIn('should be a list', error.getvalue()) - # should be converted to a list - self.assertIsInstance(d.metadata.keywords, list) - self.assertEqual(d.metadata.keywords, list(attrs['keywords'])) - - def test_platforms(self): - attrs = {'name': 'Monty', 'version': '1.0', - 'platforms': ['GNU/Linux', 'Some Evil Platform']} - dist = Distribution(attrs) - self.assertEqual(dist.get_platforms(), - ['GNU/Linux', 'Some Evil Platform']) - - def test_platforms_invalid_types(self): - attrs = {'name': 'Monty', 'version': '1.0', - 'platforms': ('GNU/Linux', 'Some Evil Platform')} - with captured_stderr() as error: - d = Distribution(attrs) - # should have warning about passing a non-list - self.assertIn('should be a list', error.getvalue()) - # should be converted to a list - self.assertIsInstance(d.metadata.platforms, list) - self.assertEqual(d.metadata.platforms, list(attrs['platforms'])) - - def test_download_url(self): - attrs = {'name': 'Boa', 'version': '3.0', - 'download_url': 'http://example.org/boa'} - dist = Distribution(attrs) - meta = self.format_metadata(dist) - self.assertIn('Metadata-Version: 1.1', meta) - - def test_long_description(self): - long_desc = textwrap.dedent("""\ - example:: - We start here - and continue here - and end here.""") - attrs = {"name": "package", - "version": "1.0", - "long_description": long_desc} - - dist = Distribution(attrs) - meta = self.format_metadata(dist) - meta = meta.replace('\n' + 8 * ' ', '\n') - self.assertIn(long_desc, meta) + def format_metadata(self, dist): + sio = StringIO.StringIO() + dist.metadata.write_pkg_file(sio) + return sio.getvalue() def test_custom_pydistutils(self): # fixes #2166 @@ -463,10 +383,10 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard, # win32-style if sys.platform == 'win32': # home drive should be found - os.environ['USERPROFILE'] = temp_dir + os.environ['HOME'] = temp_dir files = dist.find_config_files() self.assertIn(user_filename, files, - '%r not found in %r' % (user_filename, files)) + '%r not found in %r' % (user_filename, files)) finally: os.remove(user_filename) @@ -490,7 +410,6 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard, if line.strip() != ''] self.assertTrue(output) - def test_read_metadata(self): attrs = {"name": "package", "version": "1.0", @@ -504,7 +423,7 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard, metadata = dist.metadata # write it then reloads it - PKG_INFO = io.StringIO() + PKG_INFO = StringIO.StringIO() metadata.write_pkg_file(PKG_INFO) PKG_INFO.seek(0) metadata.read_pkg_file(PKG_INFO) @@ -518,6 +437,7 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard, self.assertEqual(metadata.obsoletes, None) self.assertEqual(metadata.requires, ['foo']) + def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(DistributionTestCase)) diff --git a/Lib/distutils/tests/test_extension.py b/Lib/distutils/tests/test_extension.py deleted file mode 100644 index e35f273..0000000 --- a/Lib/distutils/tests/test_extension.py +++ /dev/null @@ -1,69 +0,0 @@ -"""Tests for distutils.extension.""" -import unittest -import os -import warnings - -from test.support import check_warnings, run_unittest -from distutils.extension import read_setup_file, Extension - -class ExtensionTestCase(unittest.TestCase): - - def test_read_setup_file(self): - # trying to read a Setup file - # (sample extracted from the PyGame project) - setup = os.path.join(os.path.dirname(__file__), 'Setup.sample') - - exts = read_setup_file(setup) - names = [ext.name for ext in exts] - names.sort() - - # here are the extensions read_setup_file should have created - # out of the file - wanted = ['_arraysurfarray', '_camera', '_numericsndarray', - '_numericsurfarray', 'base', 'bufferproxy', 'cdrom', - 'color', 'constants', 'display', 'draw', 'event', - 'fastevent', 'font', 'gfxdraw', 'image', 'imageext', - 'joystick', 'key', 'mask', 'mixer', 'mixer_music', - 'mouse', 'movie', 'overlay', 'pixelarray', 'pypm', - 'rect', 'rwobject', 'scrap', 'surface', 'surflock', - 'time', 'transform'] - - self.assertEqual(names, wanted) - - def test_extension_init(self): - # the first argument, which is the name, must be a string - self.assertRaises(AssertionError, Extension, 1, []) - ext = Extension('name', []) - self.assertEqual(ext.name, 'name') - - # the second argument, which is the list of files, must - # be a list of strings - self.assertRaises(AssertionError, Extension, 'name', 'file') - self.assertRaises(AssertionError, Extension, 'name', ['file', 1]) - ext = Extension('name', ['file1', 'file2']) - self.assertEqual(ext.sources, ['file1', 'file2']) - - # others arguments have defaults - for attr in ('include_dirs', 'define_macros', 'undef_macros', - 'library_dirs', 'libraries', 'runtime_library_dirs', - 'extra_objects', 'extra_compile_args', 'extra_link_args', - 'export_symbols', 'swig_opts', 'depends'): - self.assertEqual(getattr(ext, attr), []) - - self.assertEqual(ext.language, None) - self.assertEqual(ext.optional, None) - - # if there are unknown keyword options, warn about them - with check_warnings() as w: - warnings.simplefilter('always') - ext = Extension('name', ['file1', 'file2'], chic=True) - - self.assertEqual(len(w.warnings), 1) - self.assertEqual(str(w.warnings[0].message), - "Unknown Extension options: 'chic'") - -def test_suite(): - return unittest.makeSuite(ExtensionTestCase) - -if __name__ == "__main__": - run_unittest(test_suite()) diff --git a/Lib/distutils/tests/test_file_util.py b/Lib/distutils/tests/test_file_util.py index a4e2d02..686ba3c 100644 --- a/Lib/distutils/tests/test_file_util.py +++ b/Lib/distutils/tests/test_file_util.py @@ -1,14 +1,17 @@ """Tests for distutils.file_util.""" import unittest import os -import errno -from unittest.mock import patch +import shutil -from distutils.file_util import move_file, copy_file +from distutils.file_util import move_file, write_file, copy_file from distutils import log from distutils.tests import support -from distutils.errors import DistutilsFileError -from test.support import run_unittest, unlink +from test.test_support import run_unittest + + +requires_os_link = unittest.skipUnless(hasattr(os, "link"), + "test requires os.link()") + class FileUtilTestCase(support.TempdirManager, unittest.TestCase): @@ -60,34 +63,26 @@ class FileUtilTestCase(support.TempdirManager, unittest.TestCase): wanted = ['moving %s -> %s' % (self.source, self.target_dir)] self.assertEqual(self._logs, wanted) - def test_move_file_exception_unpacking_rename(self): - # see issue 22182 - with patch("os.rename", side_effect=OSError("wrong", 1)), \ - self.assertRaises(DistutilsFileError): - with open(self.source, 'w') as fobj: - fobj.write('spam eggs') - move_file(self.source, self.target, verbose=0) - - def test_move_file_exception_unpacking_unlink(self): - # see issue 22182 - with patch("os.rename", side_effect=OSError(errno.EXDEV, "wrong")), \ - patch("os.unlink", side_effect=OSError("wrong", 1)), \ - self.assertRaises(DistutilsFileError): - with open(self.source, 'w') as fobj: - fobj.write('spam eggs') - move_file(self.source, self.target, verbose=0) - + def test_write_file(self): + lines = ['a', 'b', 'c'] + dir = self.mkdtemp() + foo = os.path.join(dir, 'foo') + write_file(foo, lines) + content = [line.strip() for line in open(foo).readlines()] + self.assertEqual(content, lines) + + def test_copy_file(self): + src_dir = self.mkdtemp() + foo = os.path.join(src_dir, 'foo') + write_file(foo, 'content') + dst_dir = self.mkdtemp() + copy_file(foo, dst_dir) + self.assertTrue(os.path.exists(os.path.join(dst_dir, 'foo'))) + + @requires_os_link def test_copy_file_hard_link(self): with open(self.source, 'w') as f: f.write('some content') - # Check first that copy_file() will not fall back on copying the file - # instead of creating the hard link. - try: - os.link(self.source, self.target) - except OSError as e: - self.skipTest('os.link: %s' % e) - else: - unlink(self.target) st = os.stat(self.source) copy_file(self.source, self.target, link='hard') st2 = os.stat(self.source) @@ -97,6 +92,7 @@ class FileUtilTestCase(support.TempdirManager, unittest.TestCase): with open(self.source, 'r') as f: self.assertEqual(f.read(), 'some content') + @requires_os_link def test_copy_file_hard_link_failure(self): # If hard linking fails, copy_file() falls back on copying file # (some special filesystems don't support hard linking even under @@ -104,8 +100,14 @@ class FileUtilTestCase(support.TempdirManager, unittest.TestCase): with open(self.source, 'w') as f: f.write('some content') st = os.stat(self.source) - with patch("os.link", side_effect=OSError(0, "linking unsupported")): + def _os_link(*args): + raise OSError(0, "linking unsupported") + old_link = os.link + os.link = _os_link + try: copy_file(self.source, self.target, link='hard') + finally: + os.link = old_link st2 = os.stat(self.source) st3 = os.stat(self.target) self.assertTrue(os.path.samestat(st, st2), (st, st2)) diff --git a/Lib/distutils/tests/test_filelist.py b/Lib/distutils/tests/test_filelist.py index c71342d..69b88f2 100644 --- a/Lib/distutils/tests/test_filelist.py +++ b/Lib/distutils/tests/test_filelist.py @@ -6,10 +6,8 @@ from distutils import debug from distutils.log import WARN from distutils.errors import DistutilsTemplateError from distutils.filelist import glob_to_re, translate_pattern, FileList -from distutils import filelist -import test.support -from test.support import captured_stdout, run_unittest +from test.test_support import captured_stdout, run_unittest from distutils.tests import support MANIFEST_IN = """\ @@ -51,14 +49,14 @@ class FileListTestCase(support.LoggingSilencer, for glob, regex in ( # simple cases - ('foo*', r'(?s:foo[^%(sep)s]*)\Z'), - ('foo?', r'(?s:foo[^%(sep)s])\Z'), - ('foo??', r'(?s:foo[^%(sep)s][^%(sep)s])\Z'), + ('foo*', r'foo[^%(sep)s]*\Z(?ms)'), + ('foo?', r'foo[^%(sep)s]\Z(?ms)'), + ('foo??', r'foo[^%(sep)s][^%(sep)s]\Z(?ms)'), # special cases - (r'foo\\*', r'(?s:foo\\\\[^%(sep)s]*)\Z'), - (r'foo\\\*', r'(?s:foo\\\\\\[^%(sep)s]*)\Z'), - ('foo????', r'(?s:foo[^%(sep)s][^%(sep)s][^%(sep)s][^%(sep)s])\Z'), - (r'foo\\??', r'(?s:foo\\\\[^%(sep)s][^%(sep)s])\Z')): + (r'foo\\*', r'foo\\\\[^%(sep)s]*\Z(?ms)'), + (r'foo\\\*', r'foo\\\\\\[^%(sep)s]*\Z(?ms)'), + ('foo????', r'foo[^%(sep)s][^%(sep)s][^%(sep)s][^%(sep)s]\Z(?ms)'), + (r'foo\\??', r'foo\\\\[^%(sep)s][^%(sep)s]\Z(?ms)')): regex = regex % {'sep': sep} self.assertEqual(glob_to_re(glob), regex) @@ -294,47 +292,8 @@ class FileListTestCase(support.LoggingSilencer, self.assertWarnings() -class FindAllTestCase(unittest.TestCase): - @test.support.skip_unless_symlink - def test_missing_symlink(self): - with test.support.temp_cwd(): - os.symlink('foo', 'bar') - self.assertEqual(filelist.findall(), []) - - def test_basic_discovery(self): - """ - When findall is called with no parameters or with - '.' as the parameter, the dot should be omitted from - the results. - """ - with test.support.temp_cwd(): - os.mkdir('foo') - file1 = os.path.join('foo', 'file1.txt') - test.support.create_empty_file(file1) - os.mkdir('bar') - file2 = os.path.join('bar', 'file2.txt') - test.support.create_empty_file(file2) - expected = [file2, file1] - self.assertEqual(sorted(filelist.findall()), expected) - - def test_non_local_discovery(self): - """ - When findall is called with another path, the full - path name should be returned. - """ - with test.support.temp_dir() as temp_dir: - file1 = os.path.join(temp_dir, 'file1.txt') - test.support.create_empty_file(file1) - expected = [file1] - self.assertEqual(filelist.findall(temp_dir), expected) - - def test_suite(): - return unittest.TestSuite([ - unittest.makeSuite(FileListTestCase), - unittest.makeSuite(FindAllTestCase), - ]) - + return unittest.makeSuite(FileListTestCase) if __name__ == "__main__": run_unittest(test_suite()) diff --git a/Lib/distutils/tests/test_install.py b/Lib/distutils/tests/test_install.py index 287ab19..397e2a7 100644 --- a/Lib/distutils/tests/test_install.py +++ b/Lib/distutils/tests/test_install.py @@ -5,7 +5,7 @@ import sys import unittest import site -from test.support import captured_stdout, run_unittest +from test.test_support import captured_stdout, run_unittest from distutils import sysconfig from distutils.command.install import install @@ -17,11 +17,12 @@ from distutils.errors import DistutilsOptionError from distutils.extension import Extension from distutils.tests import support -from test import support as test_support def _make_ext_name(modname): - return modname + sysconfig.get_config_var('EXT_SUFFIX') + if os.name == 'nt' and sys.executable.endswith('_d.exe'): + modname += '_d' + return modname + sysconfig.get_config_var('SO') class InstallTestCase(support.TempdirManager, @@ -65,8 +66,9 @@ class InstallTestCase(support.TempdirManager, check_path(cmd.install_scripts, os.path.join(destination, "bin")) check_path(cmd.install_data, destination) + @unittest.skipIf(sys.version < '2.6', + 'site.USER_SITE was introduced in 2.6') def test_user_site(self): - # test install with --user # preparing the environment for the test self.old_user_base = site.USER_BASE self.old_user_site = site.USER_SITE @@ -92,7 +94,7 @@ class InstallTestCase(support.TempdirManager, self.addCleanup(cleanup) - for key in ('nt_user', 'unix_user'): + for key in ('nt_user', 'unix_user', 'os2_home'): self.assertIn(key, INSTALL_SCHEMES) dist = Distribution({'name': 'xx'}) @@ -174,7 +176,7 @@ class InstallTestCase(support.TempdirManager, project_dir, dist = self.create_dist(py_modules=['hello'], scripts=['sayhi']) os.chdir(project_dir) - self.write_file('hello.py', "def main(): print('o hai')") + self.write_file('hello.py', "def main(): print 'o hai'") self.write_file('sayhi', 'from hello import main; main()') cmd = install(dist) @@ -191,15 +193,11 @@ class InstallTestCase(support.TempdirManager, f.close() found = [os.path.basename(line) for line in content.splitlines()] - expected = ['hello.py', 'hello.%s.pyc' % sys.implementation.cache_tag, - 'sayhi', + expected = ['hello.py', 'hello.pyc', 'sayhi', 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]] self.assertEqual(found, expected) def test_record_extensions(self): - cmd = test_support.missing_compiler_executable() - if cmd is not None: - self.skipTest('The %r command is not found' % cmd) install_dir = self.mkdtemp() project_dir, dist = self.create_dist(ext_modules=[ Extension('xx', ['xxmodule.c'])]) diff --git a/Lib/distutils/tests/test_install_data.py b/Lib/distutils/tests/test_install_data.py index 32ab296..4775694 100644 --- a/Lib/distutils/tests/test_install_data.py +++ b/Lib/distutils/tests/test_install_data.py @@ -1,10 +1,12 @@ """Tests for distutils.command.install_data.""" +import sys import os import unittest +import getpass from distutils.command.install_data import install_data from distutils.tests import support -from test.support import run_unittest +from test.test_support import run_unittest class InstallDataTestCase(support.TempdirManager, support.LoggingSilencer, diff --git a/Lib/distutils/tests/test_install_headers.py b/Lib/distutils/tests/test_install_headers.py index 2217b32..b37224b 100644 --- a/Lib/distutils/tests/test_install_headers.py +++ b/Lib/distutils/tests/test_install_headers.py @@ -1,10 +1,12 @@ """Tests for distutils.command.install_headers.""" +import sys import os import unittest +import getpass from distutils.command.install_headers import install_headers from distutils.tests import support -from test.support import run_unittest +from test.test_support import run_unittest class InstallHeadersTestCase(support.TempdirManager, support.LoggingSilencer, diff --git a/Lib/distutils/tests/test_install_lib.py b/Lib/distutils/tests/test_install_lib.py index fda6315..0defbd6 100644 --- a/Lib/distutils/tests/test_install_lib.py +++ b/Lib/distutils/tests/test_install_lib.py @@ -1,15 +1,13 @@ """Tests for distutils.command.install_data.""" -import sys import os -import importlib.util +import sys import unittest from distutils.command.install_lib import install_lib from distutils.extension import Extension from distutils.tests import support from distutils.errors import DistutilsOptionError -from test.support import run_unittest - +from test.test_support import run_unittest class InstallLibTestCase(support.TempdirManager, support.LoggingSilencer, @@ -17,7 +15,7 @@ class InstallLibTestCase(support.TempdirManager, unittest.TestCase): def test_finalize_options(self): - dist = self.create_dist()[1] + pkg_dir, dist = self.create_dist() cmd = install_lib(dist) cmd.finalize_options() @@ -34,65 +32,61 @@ class InstallLibTestCase(support.TempdirManager, cmd.finalize_options() self.assertEqual(cmd.optimize, 2) - @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled') - def test_byte_compile(self): - project_dir, dist = self.create_dist() - os.chdir(project_dir) + def _setup_byte_compile(self): + pkg_dir, dist = self.create_dist() cmd = install_lib(dist) cmd.compile = cmd.optimize = 1 - f = os.path.join(project_dir, 'foo.py') + f = os.path.join(pkg_dir, 'foo.py') self.write_file(f, '# python file') cmd.byte_compile([f]) - pyc_file = importlib.util.cache_from_source('foo.py', optimization='') - pyc_opt_file = importlib.util.cache_from_source('foo.py', - optimization=cmd.optimize) - self.assertTrue(os.path.exists(pyc_file)) - self.assertTrue(os.path.exists(pyc_opt_file)) + return pkg_dir + + @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile not enabled') + def test_byte_compile(self): + pkg_dir = self._setup_byte_compile() + if sys.flags.optimize < 1: + self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyc'))) + else: + self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyo'))) def test_get_outputs(self): - project_dir, dist = self.create_dist() - os.chdir(project_dir) - os.mkdir('spam') + pkg_dir, dist = self.create_dist() cmd = install_lib(dist) # setting up a dist environment cmd.compile = cmd.optimize = 1 - cmd.install_dir = self.mkdtemp() - f = os.path.join(project_dir, 'spam', '__init__.py') - self.write_file(f, '# python package') + cmd.install_dir = pkg_dir + f = os.path.join(pkg_dir, 'foo.py') + self.write_file(f, '# python file') + cmd.distribution.py_modules = [pkg_dir] cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] - cmd.distribution.packages = ['spam'] + cmd.distribution.packages = [pkg_dir] cmd.distribution.script_name = 'setup.py' - # get_outputs should return 4 elements: spam/__init__.py and .pyc, - # foo.import-tag-abiflags.so / foo.pyd - outputs = cmd.get_outputs() - self.assertEqual(len(outputs), 4, outputs) + # get_output should return 4 elements + self.assertGreaterEqual(len(cmd.get_outputs()), 2) def test_get_inputs(self): - project_dir, dist = self.create_dist() - os.chdir(project_dir) - os.mkdir('spam') + pkg_dir, dist = self.create_dist() cmd = install_lib(dist) # setting up a dist environment cmd.compile = cmd.optimize = 1 - cmd.install_dir = self.mkdtemp() - f = os.path.join(project_dir, 'spam', '__init__.py') - self.write_file(f, '# python package') + cmd.install_dir = pkg_dir + f = os.path.join(pkg_dir, 'foo.py') + self.write_file(f, '# python file') + cmd.distribution.py_modules = [pkg_dir] cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] - cmd.distribution.packages = ['spam'] + cmd.distribution.packages = [pkg_dir] cmd.distribution.script_name = 'setup.py' - # get_inputs should return 2 elements: spam/__init__.py and - # foo.import-tag-abiflags.so / foo.pyd - inputs = cmd.get_inputs() - self.assertEqual(len(inputs), 2, inputs) + # get_input should return 2 elements + self.assertEqual(len(cmd.get_inputs()), 2) def test_dont_write_bytecode(self): # makes sure byte_compile is not used - dist = self.create_dist()[1] + pkg_dir, dist = self.create_dist() cmd = install_lib(dist) cmd.compile = 1 cmd.optimize = 1 @@ -104,9 +98,7 @@ class InstallLibTestCase(support.TempdirManager, finally: sys.dont_write_bytecode = old_dont_write_bytecode - self.assertIn('byte-compiling is disabled', - self.logs[0][1] % self.logs[0][2]) - + self.assertIn('byte-compiling is disabled', self.logs[0][1]) def test_suite(): return unittest.makeSuite(InstallLibTestCase) diff --git a/Lib/distutils/tests/test_install_scripts.py b/Lib/distutils/tests/test_install_scripts.py index 1f7b103..9c0ba6d 100644 --- a/Lib/distutils/tests/test_install_scripts.py +++ b/Lib/distutils/tests/test_install_scripts.py @@ -7,7 +7,7 @@ from distutils.command.install_scripts import install_scripts from distutils.core import Distribution from distutils.tests import support -from test.support import run_unittest +from test.test_support import run_unittest class InstallScriptsTestCase(support.TempdirManager, diff --git a/Lib/distutils/tests/test_log.py b/Lib/distutils/tests/test_log.py deleted file mode 100644 index 75cf900..0000000 --- a/Lib/distutils/tests/test_log.py +++ /dev/null @@ -1,46 +0,0 @@ -"""Tests for distutils.log""" - -import io -import sys -import unittest -from test.support import swap_attr, run_unittest - -from distutils import log - -class TestLog(unittest.TestCase): - def test_non_ascii(self): - # Issues #8663, #34421: test that non-encodable text is escaped with - # backslashreplace error handler and encodable non-ASCII text is - # output as is. - for errors in ('strict', 'backslashreplace', 'surrogateescape', - 'replace', 'ignore'): - with self.subTest(errors=errors): - stdout = io.TextIOWrapper(io.BytesIO(), - encoding='cp437', errors=errors) - stderr = io.TextIOWrapper(io.BytesIO(), - encoding='cp437', errors=errors) - old_threshold = log.set_threshold(log.DEBUG) - try: - with swap_attr(sys, 'stdout', stdout), \ - swap_attr(sys, 'stderr', stderr): - log.debug('Dεbug\tMėssãge') - log.fatal('Fαtal\tÈrrōr') - finally: - log.set_threshold(old_threshold) - - stdout.seek(0) - self.assertEqual(stdout.read().rstrip(), - 'Dεbug\tM?ss?ge' if errors == 'replace' else - 'Dεbug\tMssge' if errors == 'ignore' else - 'Dεbug\tM\\u0117ss\\xe3ge') - stderr.seek(0) - self.assertEqual(stderr.read().rstrip(), - 'Fαtal\t?rr?r' if errors == 'replace' else - 'Fαtal\trrr' if errors == 'ignore' else - 'Fαtal\t\\xc8rr\\u014dr') - -def test_suite(): - return unittest.makeSuite(TestLog) - -if __name__ == "__main__": - run_unittest(test_suite()) diff --git a/Lib/distutils/tests/test_msvc9compiler.py b/Lib/distutils/tests/test_msvc9compiler.py index 77a07ef..331c0f5 100644 --- a/Lib/distutils/tests/test_msvc9compiler.py +++ b/Lib/distutils/tests/test_msvc9compiler.py @@ -5,7 +5,7 @@ import os from distutils.errors import DistutilsPlatformError from distutils.tests import support -from test.support import run_unittest +from test.test_support import run_unittest # A manifest with the only assembly reference being the msvcrt assembly, so # should have the assembly completely stripped. Note that although the @@ -127,11 +127,11 @@ class msvc9compilerTestCase(support.TempdirManager, # looking for values that should exist on all # windows registry versions. path = r'Control Panel\Desktop' - v = Reg.get_value(path, 'dragfullwindows') - self.assertIn(v, ('0', '1', '2')) + v = Reg.get_value(path, u'dragfullwindows') + self.assertIn(v, (u'0', u'1', u'2')) - import winreg - HKCU = winreg.HKEY_CURRENT_USER + import _winreg + HKCU = _winreg.HKEY_CURRENT_USER keys = Reg.read_keys(HKCU, 'xxxx') self.assertEqual(keys, None) diff --git a/Lib/distutils/tests/test_msvccompiler.py b/Lib/distutils/tests/test_msvccompiler.py deleted file mode 100644 index 70a9c93..0000000 --- a/Lib/distutils/tests/test_msvccompiler.py +++ /dev/null @@ -1,132 +0,0 @@ -"""Tests for distutils._msvccompiler.""" -import sys -import unittest -import os - -from distutils.errors import DistutilsPlatformError -from distutils.tests import support -from test.support import run_unittest - - -SKIP_MESSAGE = (None if sys.platform == "win32" else - "These tests are only for win32") - -@unittest.skipUnless(SKIP_MESSAGE is None, SKIP_MESSAGE) -class msvccompilerTestCase(support.TempdirManager, - unittest.TestCase): - - def test_no_compiler(self): - import distutils._msvccompiler as _msvccompiler - # makes sure query_vcvarsall raises - # a DistutilsPlatformError if the compiler - # is not found - def _find_vcvarsall(plat_spec): - return None, None - - old_find_vcvarsall = _msvccompiler._find_vcvarsall - _msvccompiler._find_vcvarsall = _find_vcvarsall - try: - self.assertRaises(DistutilsPlatformError, - _msvccompiler._get_vc_env, - 'wont find this version') - finally: - _msvccompiler._find_vcvarsall = old_find_vcvarsall - - def test_compiler_options(self): - import distutils._msvccompiler as _msvccompiler - # suppress path to vcruntime from _find_vcvarsall to - # check that /MT is added to compile options - old_find_vcvarsall = _msvccompiler._find_vcvarsall - def _find_vcvarsall(plat_spec): - return old_find_vcvarsall(plat_spec)[0], None - _msvccompiler._find_vcvarsall = _find_vcvarsall - try: - compiler = _msvccompiler.MSVCCompiler() - compiler.initialize() - - self.assertIn('/MT', compiler.compile_options) - self.assertNotIn('/MD', compiler.compile_options) - finally: - _msvccompiler._find_vcvarsall = old_find_vcvarsall - - def test_vcruntime_copy(self): - import distutils._msvccompiler as _msvccompiler - # force path to a known file - it doesn't matter - # what we copy as long as its name is not in - # _msvccompiler._BUNDLED_DLLS - old_find_vcvarsall = _msvccompiler._find_vcvarsall - def _find_vcvarsall(plat_spec): - return old_find_vcvarsall(plat_spec)[0], __file__ - _msvccompiler._find_vcvarsall = _find_vcvarsall - try: - tempdir = self.mkdtemp() - compiler = _msvccompiler.MSVCCompiler() - compiler.initialize() - compiler._copy_vcruntime(tempdir) - - self.assertTrue(os.path.isfile(os.path.join( - tempdir, os.path.basename(__file__)))) - finally: - _msvccompiler._find_vcvarsall = old_find_vcvarsall - - def test_vcruntime_skip_copy(self): - import distutils._msvccompiler as _msvccompiler - - tempdir = self.mkdtemp() - compiler = _msvccompiler.MSVCCompiler() - compiler.initialize() - dll = compiler._vcruntime_redist - self.assertTrue(os.path.isfile(dll), dll or "<None>") - - compiler._copy_vcruntime(tempdir) - - self.assertFalse(os.path.isfile(os.path.join( - tempdir, os.path.basename(dll))), dll or "<None>") - - def test_get_vc_env_unicode(self): - import distutils._msvccompiler as _msvccompiler - - test_var = 'ṰḖṤṪ┅ṼẨṜ' - test_value = '₃⁴₅' - - # Ensure we don't early exit from _get_vc_env - old_distutils_use_sdk = os.environ.pop('DISTUTILS_USE_SDK', None) - os.environ[test_var] = test_value - try: - env = _msvccompiler._get_vc_env('x86') - self.assertIn(test_var.lower(), env) - self.assertEqual(test_value, env[test_var.lower()]) - finally: - os.environ.pop(test_var) - if old_distutils_use_sdk: - os.environ['DISTUTILS_USE_SDK'] = old_distutils_use_sdk - - def test_get_vc2017(self): - import distutils._msvccompiler as _msvccompiler - - # This function cannot be mocked, so pass it if we find VS 2017 - # and mark it skipped if we do not. - version, path = _msvccompiler._find_vc2017() - if version: - self.assertGreaterEqual(version, 15) - self.assertTrue(os.path.isdir(path)) - else: - raise unittest.SkipTest("VS 2017 is not installed") - - def test_get_vc2015(self): - import distutils._msvccompiler as _msvccompiler - - # This function cannot be mocked, so pass it if we find VS 2015 - # and mark it skipped if we do not. - version, path = _msvccompiler._find_vc2015() - if version: - self.assertGreaterEqual(version, 14) - self.assertTrue(os.path.isdir(path)) - else: - raise unittest.SkipTest("VS 2015 is not installed") - -def test_suite(): - return unittest.makeSuite(msvccompilerTestCase) - -if __name__ == "__main__": - run_unittest(test_suite()) diff --git a/Lib/distutils/tests/test_register.py b/Lib/distutils/tests/test_register.py index e68b0af..ca7d79b 100644 --- a/Lib/distutils/tests/test_register.py +++ b/Lib/distutils/tests/test_register.py @@ -1,18 +1,18 @@ +# -*- encoding: utf8 -*- """Tests for distutils.command.register.""" import os import unittest import getpass -import urllib +import urllib2 import warnings -from test.support import check_warnings, run_unittest +from test.test_support import check_warnings, run_unittest from distutils.command import register as register_module from distutils.command.register import register from distutils.errors import DistutilsSetupError -from distutils.log import INFO -from distutils.tests.test_config import BasePyPIRCCommandTestCase +from distutils.tests.test_config import PyPIRCCommandTestCase try: import docutils @@ -39,7 +39,7 @@ username:tarek password:password """ -class Inputs(object): +class RawInputs(object): """Fakes user inputs.""" def __init__(self, *answers): self.answers = answers @@ -59,20 +59,14 @@ class FakeOpener(object): def __call__(self, *args): return self - def open(self, req, data=None, timeout=None): + def open(self, req): self.reqs.append(req) return self def read(self): - return b'xxx' + return 'xxx' - def getheader(self, name, default=None): - return { - 'content-type': 'text/plain; charset=utf-8', - }.get(name.lower(), default) - - -class RegisterTestCase(BasePyPIRCCommandTestCase): +class RegisterTestCase(PyPIRCCommandTestCase): def setUp(self): super(RegisterTestCase, self).setUp() @@ -81,14 +75,12 @@ class RegisterTestCase(BasePyPIRCCommandTestCase): def _getpass(prompt): return 'password' getpass.getpass = _getpass - urllib.request._opener = None - self.old_opener = urllib.request.build_opener - self.conn = urllib.request.build_opener = FakeOpener() + self.old_opener = urllib2.build_opener + self.conn = urllib2.build_opener = FakeOpener() def tearDown(self): getpass.getpass = self._old_getpass - urllib.request._opener = None - urllib.request.build_opener = self.old_opener + urllib2.build_opener = self.old_opener super(RegisterTestCase, self).tearDown() def _get_cmd(self, metadata=None): @@ -109,7 +101,7 @@ class RegisterTestCase(BasePyPIRCCommandTestCase): # we shouldn't have a .pypirc file yet self.assertFalse(os.path.exists(self.rc)) - # patching input and getpass.getpass + # patching raw_input and getpass.getpass # so register gets happy # # Here's what we are faking : @@ -117,13 +109,13 @@ class RegisterTestCase(BasePyPIRCCommandTestCase): # Username : 'tarek' # Password : 'password' # Save your login (y/N)? : 'y' - inputs = Inputs('1', 'tarek', 'y') - register_module.input = inputs.__call__ + inputs = RawInputs('1', 'tarek', 'y') + register_module.raw_input = inputs.__call__ # let's run the command try: cmd.run() finally: - del register_module.input + del register_module.raw_input # we should have a brand new .pypirc file self.assertTrue(os.path.exists(self.rc)) @@ -141,7 +133,7 @@ class RegisterTestCase(BasePyPIRCCommandTestCase): # if we run the command again def _no_way(prompt=''): raise AssertionError(prompt) - register_module.input = _no_way + register_module.raw_input = _no_way cmd.show_response = 1 cmd.run() @@ -151,10 +143,8 @@ class RegisterTestCase(BasePyPIRCCommandTestCase): self.assertEqual(len(self.conn.reqs), 2) req1 = dict(self.conn.reqs[0].headers) req2 = dict(self.conn.reqs[1].headers) - - self.assertEqual(req1['Content-length'], '1374') - self.assertEqual(req2['Content-length'], '1374') - self.assertIn(b'xxx', self.conn.reqs[1].data) + self.assertEqual(req2['Content-length'], req1['Content-length']) + self.assertIn('xxx', self.conn.reqs[1].data) def test_password_not_in_file(self): @@ -171,38 +161,38 @@ class RegisterTestCase(BasePyPIRCCommandTestCase): def test_registering(self): # this test runs choice 2 cmd = self._get_cmd() - inputs = Inputs('2', 'tarek', 'tarek@ziade.org') - register_module.input = inputs.__call__ + inputs = RawInputs('2', 'tarek', 'tarek@ziade.org') + register_module.raw_input = inputs.__call__ try: # let's run the command cmd.run() finally: - del register_module.input + del register_module.raw_input # we should have send a request self.assertEqual(len(self.conn.reqs), 1) req = self.conn.reqs[0] headers = dict(req.headers) self.assertEqual(headers['Content-length'], '608') - self.assertIn(b'tarek', req.data) + self.assertIn('tarek', req.data) def test_password_reset(self): # this test runs choice 3 cmd = self._get_cmd() - inputs = Inputs('3', 'tarek@ziade.org') - register_module.input = inputs.__call__ + inputs = RawInputs('3', 'tarek@ziade.org') + register_module.raw_input = inputs.__call__ try: # let's run the command cmd.run() finally: - del register_module.input + del register_module.raw_input # we should have send a request self.assertEqual(len(self.conn.reqs), 1) req = self.conn.reqs[0] headers = dict(req.headers) self.assertEqual(headers['Content-length'], '290') - self.assertIn(b'tarek', req.data) + self.assertIn('tarek', req.data) @unittest.skipUnless(docutils is not None, 'needs docutils') def test_strict(self): @@ -219,7 +209,7 @@ class RegisterTestCase(BasePyPIRCCommandTestCase): # metadata are OK but long_description is broken metadata = {'url': 'xxx', 'author': 'xxx', - 'author_email': 'éxéxé', + 'author_email': u'éxéxé', 'name': 'xxx', 'version': 'xxx', 'long_description': 'title\n==\n\ntext'} @@ -233,42 +223,42 @@ class RegisterTestCase(BasePyPIRCCommandTestCase): cmd = self._get_cmd(metadata) cmd.ensure_finalized() cmd.strict = 1 - inputs = Inputs('1', 'tarek', 'y') - register_module.input = inputs.__call__ + inputs = RawInputs('1', 'tarek', 'y') + register_module.raw_input = inputs.__call__ # let's run the command try: cmd.run() finally: - del register_module.input + del register_module.raw_input # strict is not by default cmd = self._get_cmd() cmd.ensure_finalized() - inputs = Inputs('1', 'tarek', 'y') - register_module.input = inputs.__call__ + inputs = RawInputs('1', 'tarek', 'y') + register_module.raw_input = inputs.__call__ # let's run the command try: cmd.run() finally: - del register_module.input + del register_module.raw_input # and finally a Unicode test (bug #12114) - metadata = {'url': 'xxx', 'author': '\u00c9ric', - 'author_email': 'xxx', 'name': 'xxx', - 'version': 'xxx', - 'description': 'Something about esszet \u00df', - 'long_description': 'More things about esszet \u00df'} + metadata = {'url': u'xxx', 'author': u'\u00c9ric', + 'author_email': u'xxx', u'name': 'xxx', + 'version': u'xxx', + 'description': u'Something about esszet \u00df', + 'long_description': u'More things about esszet \u00df'} cmd = self._get_cmd(metadata) cmd.ensure_finalized() cmd.strict = 1 - inputs = Inputs('1', 'tarek', 'y') - register_module.input = inputs.__call__ + inputs = RawInputs('1', 'tarek', 'y') + register_module.raw_input = inputs.__call__ # let's run the command try: cmd.run() finally: - del register_module.input + del register_module.raw_input @unittest.skipUnless(docutils is not None, 'needs docutils') def test_register_invalid_long_description(self): @@ -280,10 +270,9 @@ class RegisterTestCase(BasePyPIRCCommandTestCase): cmd = self._get_cmd(metadata) cmd.ensure_finalized() cmd.strict = True - inputs = Inputs('2', 'tarek', 'tarek@ziade.org') - register_module.input = inputs - self.addCleanup(delattr, register_module, 'input') - + inputs = RawInputs('2', 'tarek', 'tarek@ziade.org') + register_module.raw_input = inputs + self.addCleanup(delattr, register_module, 'raw_input') self.assertRaises(DistutilsSetupError, cmd.run) def test_check_metadata_deprecated(self): @@ -294,28 +283,6 @@ class RegisterTestCase(BasePyPIRCCommandTestCase): cmd.check_metadata() self.assertEqual(len(w.warnings), 1) - def test_list_classifiers(self): - cmd = self._get_cmd() - cmd.list_classifiers = 1 - cmd.run() - results = self.get_logs(INFO) - self.assertEqual(results, ['running check', 'xxx']) - - def test_show_response(self): - # test that the --show-response option return a well formatted response - cmd = self._get_cmd() - inputs = Inputs('1', 'tarek', 'y') - register_module.input = inputs.__call__ - cmd.show_response = 1 - try: - cmd.run() - finally: - del register_module.input - - results = self.get_logs(INFO) - self.assertEqual(results[3], 75 * '-' + '\nxxx\n' + 75 * '-') - - def test_suite(): return unittest.makeSuite(RegisterTestCase) diff --git a/Lib/distutils/tests/test_sdist.py b/Lib/distutils/tests/test_sdist.py index 23db126..c503bd6 100644 --- a/Lib/distutils/tests/test_sdist.py +++ b/Lib/distutils/tests/test_sdist.py @@ -6,13 +6,14 @@ import warnings import zipfile from os.path import join from textwrap import dedent -from test.support import captured_stdout, check_warnings, run_unittest +from test.test_support import captured_stdout, check_warnings, run_unittest +# zlib is not used here, but if it's not available +# the tests that use zipfile may fail try: import zlib - ZLIB_SUPPORT = True except ImportError: - ZLIB_SUPPORT = False + zlib = None try: import grp @@ -21,9 +22,10 @@ try: except ImportError: UID_GID_SUPPORT = False + from distutils.command.sdist import sdist, show_formats from distutils.core import Distribution -from distutils.tests.test_config import BasePyPIRCCommandTestCase +from distutils.tests.test_config import PyPIRCCommandTestCase from distutils.errors import DistutilsOptionError from distutils.spawn import find_executable from distutils.log import WARN @@ -52,7 +54,7 @@ somecode%(sep)sdoc.dat somecode%(sep)sdoc.txt """ -class SDistTestCase(BasePyPIRCCommandTestCase): +class SDistTestCase(PyPIRCCommandTestCase): def setUp(self): # PyPIRCCommandTestCase creates a temp dir already @@ -87,7 +89,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase): cmd.dist_dir = 'dist' return dist, cmd - @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') + @unittest.skipUnless(zlib, "requires zlib") def test_prune_file_list(self): # this test creates a project with some VCS dirs and an NFS rename # file, then launches sdist to check they get pruned on all systems @@ -132,11 +134,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase): 'somecode/', 'somecode/__init__.py'] self.assertEqual(sorted(content), ['fake-1.0/' + x for x in expected]) - @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') - @unittest.skipIf(find_executable('tar') is None, - "The tar command is not found") - @unittest.skipIf(find_executable('gzip') is None, - "The gzip command is not found") + @unittest.skipUnless(zlib, "requires zlib") def test_make_distribution(self): # now building a sdist dist, cmd = self.get_cmd() @@ -165,7 +163,29 @@ class SDistTestCase(BasePyPIRCCommandTestCase): result.sort() self.assertEqual(result, ['fake-1.0.tar', 'fake-1.0.tar.gz']) - @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') + @unittest.skipUnless(zlib, "requires zlib") + def test_unicode_metadata_tgz(self): + """ + Unicode name or version should not break building to tar.gz format. + Reference issue #11638. + """ + + # create the sdist command with unicode parameters + dist, cmd = self.get_cmd({'name': u'fake', 'version': u'1.0'}) + + # create the sdist as gztar and run the command + cmd.formats = ['gztar'] + cmd.ensure_finalized() + cmd.run() + + # The command should have created the .tar.gz file + dist_folder = join(self.tmp_dir, 'dist') + result = os.listdir(dist_folder) + self.assertEqual(result, ['fake-1.0.tar.gz']) + + os.remove(join(dist_folder, 'fake-1.0.tar.gz')) + + @unittest.skipUnless(zlib, "requires zlib") def test_add_defaults(self): # http://bugs.python.org/issue2279 @@ -244,7 +264,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase): f.close() self.assertEqual(manifest, MANIFEST % {'sep': os.sep}) - @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') + @unittest.skipUnless(zlib, "requires zlib") def test_metadata_check_option(self): # testing the `medata-check` option dist, cmd = self.get_cmd(metadata={}) @@ -305,6 +325,54 @@ class SDistTestCase(BasePyPIRCCommandTestCase): cmd.formats = 'supazipa' self.assertRaises(DistutilsOptionError, cmd.finalize_options) + @unittest.skipUnless(zlib, "requires zlib") + @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") + @unittest.skipIf(find_executable('tar') is None, + "The tar command is not found") + @unittest.skipIf(find_executable('gzip') is None, + "The gzip command is not found") + def test_make_distribution_owner_group(self): + # now building a sdist + dist, cmd = self.get_cmd() + + # creating a gztar and specifying the owner+group + cmd.formats = ['gztar'] + cmd.owner = pwd.getpwuid(0)[0] + cmd.group = grp.getgrgid(0)[0] + cmd.ensure_finalized() + cmd.run() + + # making sure we have the good rights + archive_name = join(self.tmp_dir, 'dist', 'fake-1.0.tar.gz') + archive = tarfile.open(archive_name) + try: + for member in archive.getmembers(): + self.assertEqual(member.uid, 0) + self.assertEqual(member.gid, 0) + finally: + archive.close() + + # building a sdist again + dist, cmd = self.get_cmd() + + # creating a gztar + cmd.formats = ['gztar'] + cmd.ensure_finalized() + cmd.run() + + # making sure we have the good rights + archive_name = join(self.tmp_dir, 'dist', 'fake-1.0.tar.gz') + archive = tarfile.open(archive_name) + + # note that we are not testing the group ownership here + # because, depending on the platforms and the container + # rights (see #7408) + try: + for member in archive.getmembers(): + self.assertEqual(member.uid, os.getuid()) + finally: + archive.close() + # the following tests make sure there is a nice error message instead # of a traceback when parsing an invalid manifest template @@ -331,7 +399,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase): # this used to crash instead of raising a warning: #8286 self._check_template('include examples/') - @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') + @unittest.skipUnless(zlib, "requires zlib") def test_get_file_list(self): # make sure MANIFEST is recalculated dist, cmd = self.get_cmd() @@ -373,7 +441,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase): self.assertEqual(len(manifest2), 6) self.assertIn('doc2.txt', manifest2[-1]) - @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') + @unittest.skipUnless(zlib, "requires zlib") def test_manifest_marker(self): # check that autogenerated MANIFESTs have a marker dist, cmd = self.get_cmd() @@ -390,7 +458,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase): self.assertEqual(manifest[0], '# file GENERATED by distutils, do NOT edit') - @unittest.skipUnless(ZLIB_SUPPORT, "Need zlib support to run") + @unittest.skipUnless(zlib, 'requires zlib') def test_manifest_comments(self): # make sure comments don't cause exceptions or wrong includes contents = dedent("""\ @@ -407,7 +475,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase): cmd.run() self.assertEqual(cmd.filelist.files, ['good.py']) - @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') + @unittest.skipUnless(zlib, "requires zlib") def test_manual_manifest(self): # check that a MANIFEST without a marker is left alone dist, cmd = self.get_cmd() @@ -437,54 +505,6 @@ class SDistTestCase(BasePyPIRCCommandTestCase): self.assertEqual(sorted(filenames), ['fake-1.0', 'fake-1.0/PKG-INFO', 'fake-1.0/README.manual']) - @unittest.skipUnless(ZLIB_SUPPORT, "requires zlib") - @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") - @unittest.skipIf(find_executable('tar') is None, - "The tar command is not found") - @unittest.skipIf(find_executable('gzip') is None, - "The gzip command is not found") - def test_make_distribution_owner_group(self): - # now building a sdist - dist, cmd = self.get_cmd() - - # creating a gztar and specifying the owner+group - cmd.formats = ['gztar'] - cmd.owner = pwd.getpwuid(0)[0] - cmd.group = grp.getgrgid(0)[0] - cmd.ensure_finalized() - cmd.run() - - # making sure we have the good rights - archive_name = join(self.tmp_dir, 'dist', 'fake-1.0.tar.gz') - archive = tarfile.open(archive_name) - try: - for member in archive.getmembers(): - self.assertEqual(member.uid, 0) - self.assertEqual(member.gid, 0) - finally: - archive.close() - - # building a sdist again - dist, cmd = self.get_cmd() - - # creating a gztar - cmd.formats = ['gztar'] - cmd.ensure_finalized() - cmd.run() - - # making sure we have the good rights - archive_name = join(self.tmp_dir, 'dist', 'fake-1.0.tar.gz') - archive = tarfile.open(archive_name) - - # note that we are not testing the group ownership here - # because, depending on the platforms and the container - # rights (see #7408) - try: - for member in archive.getmembers(): - self.assertEqual(member.uid, os.getuid()) - finally: - archive.close() - def test_suite(): return unittest.makeSuite(SDistTestCase) diff --git a/Lib/distutils/tests/test_spawn.py b/Lib/distutils/tests/test_spawn.py index f9ae69e..061a72f 100644 --- a/Lib/distutils/tests/test_spawn.py +++ b/Lib/distutils/tests/test_spawn.py @@ -2,14 +2,13 @@ import os import stat import sys +import time import unittest -from unittest import mock -from test.support import run_unittest, unix_shell +from test.support import captured_stdout, run_unittest from test import support as test_support -from distutils.spawn import find_executable from distutils.spawn import _nt_quote_args -from distutils.spawn import spawn +from distutils.spawn import spawn, find_executable from distutils.errors import DistutilsExecError from distutils.tests import support @@ -34,25 +33,27 @@ class SpawnTestCase(support.TempdirManager, # creating something executable # through the shell that returns 1 - if sys.platform != 'win32': + if os.name == 'posix': exe = os.path.join(tmpdir, 'foo.sh') - self.write_file(exe, '#!%s\nexit 1' % unix_shell) + self.write_file(exe, '#!/bin/sh\nexit 1') + os.chmod(exe, 0777) else: exe = os.path.join(tmpdir, 'foo.bat') self.write_file(exe, 'exit 1') - os.chmod(exe, 0o777) + os.chmod(exe, 0777) self.assertRaises(DistutilsExecError, spawn, [exe]) # now something that works - if sys.platform != 'win32': + if os.name == 'posix': exe = os.path.join(tmpdir, 'foo.sh') - self.write_file(exe, '#!%s\nexit 0' % unix_shell) + self.write_file(exe, '#!/bin/sh\nexit 0') + os.chmod(exe, 0777) else: exe = os.path.join(tmpdir, 'foo.bat') self.write_file(exe, 'exit 0') - os.chmod(exe, 0o777) + os.chmod(exe, 0777) spawn([exe]) # should work without any error def test_find_executable(self): @@ -87,52 +88,12 @@ class SpawnTestCase(support.TempdirManager, rv = find_executable(dont_exist_program , path=tmp_dir) self.assertIsNone(rv) - # PATH='': no match, except in the current directory + # test os.defpath: missing PATH environment variable with test_support.EnvironmentVarGuard() as env: - env['PATH'] = '' - with unittest.mock.patch('distutils.spawn.os.confstr', - return_value=tmp_dir, create=True), \ - unittest.mock.patch('distutils.spawn.os.defpath', - tmp_dir): - rv = find_executable(program) - self.assertIsNone(rv) - - # look in current directory - with test_support.change_cwd(tmp_dir): - rv = find_executable(program) - self.assertEqual(rv, program) - - # PATH=':': explicitly looks in the current directory - with test_support.EnvironmentVarGuard() as env: - env['PATH'] = os.pathsep - with unittest.mock.patch('distutils.spawn.os.confstr', - return_value='', create=True), \ - unittest.mock.patch('distutils.spawn.os.defpath', ''): - rv = find_executable(program) - self.assertIsNone(rv) - - # look in current directory - with test_support.change_cwd(tmp_dir): - rv = find_executable(program) - self.assertEqual(rv, program) - - # missing PATH: test os.confstr("CS_PATH") and os.defpath - with test_support.EnvironmentVarGuard() as env: - env.pop('PATH', None) - - # without confstr - with unittest.mock.patch('distutils.spawn.os.confstr', - side_effect=ValueError, - create=True), \ - unittest.mock.patch('distutils.spawn.os.defpath', - tmp_dir): - rv = find_executable(program) - self.assertEqual(rv, filename) + from distutils import spawn + with test_support.swap_attr(spawn.os, 'defpath', tmp_dir): + env.pop('PATH') - # with confstr - with unittest.mock.patch('distutils.spawn.os.confstr', - return_value=tmp_dir, create=True), \ - unittest.mock.patch('distutils.spawn.os.defpath', ''): rv = find_executable(program) self.assertEqual(rv, filename) diff --git a/Lib/distutils/tests/test_sysconfig.py b/Lib/distutils/tests/test_sysconfig.py index 236755d..9e2aeb8 100644 --- a/Lib/distutils/tests/test_sysconfig.py +++ b/Lib/distutils/tests/test_sysconfig.py @@ -1,18 +1,19 @@ """Tests for distutils.sysconfig.""" -import contextlib import os +import test +import unittest import shutil import subprocess import sys import textwrap -import unittest from distutils import sysconfig from distutils.ccompiler import get_default_compiler from distutils.tests import support -from test.support import TESTFN, run_unittest, check_warnings, swap_item +from test.test_support import TESTFN, swap_item -class SysconfigTestCase(support.EnvironGuard, unittest.TestCase): +class SysconfigTestCase(support.EnvironGuard, + unittest.TestCase): def setUp(self): super(SysconfigTestCase, self).setUp() self.makefile = None @@ -24,56 +25,31 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase): super(SysconfigTestCase, self).tearDown() def cleanup_testfn(self): - if os.path.isfile(TESTFN): - os.remove(TESTFN) - elif os.path.isdir(TESTFN): - shutil.rmtree(TESTFN) - - def test_get_config_h_filename(self): - config_h = sysconfig.get_config_h_filename() - self.assertTrue(os.path.isfile(config_h), config_h) + path = test.test_support.TESTFN + if os.path.isfile(path): + os.remove(path) + elif os.path.isdir(path): + shutil.rmtree(path) def test_get_python_lib(self): + lib_dir = sysconfig.get_python_lib() # XXX doesn't work on Linux when Python was never installed before #self.assertTrue(os.path.isdir(lib_dir), lib_dir) # test for pythonxx.lib? self.assertNotEqual(sysconfig.get_python_lib(), sysconfig.get_python_lib(prefix=TESTFN)) - - def test_get_config_vars(self): - cvars = sysconfig.get_config_vars() - self.assertIsInstance(cvars, dict) - self.assertTrue(cvars) - - def test_srcdir(self): - # See Issues #15322, #15364. - srcdir = sysconfig.get_config_var('srcdir') - - self.assertTrue(os.path.isabs(srcdir), srcdir) - self.assertTrue(os.path.isdir(srcdir), srcdir) - - if sysconfig.python_build: - # The python executable has not been installed so srcdir - # should be a full source checkout. - Python_h = os.path.join(srcdir, 'Include', 'Python.h') - self.assertTrue(os.path.exists(Python_h), Python_h) - self.assertTrue(sysconfig._is_python_source_dir(srcdir)) - elif os.name == 'posix': - self.assertEqual( - os.path.dirname(sysconfig.get_makefile_filename()), - srcdir) - - def test_srcdir_independent_of_cwd(self): - # srcdir should be independent of the current working directory - # See Issues #15322, #15364. - srcdir = sysconfig.get_config_var('srcdir') - cwd = os.getcwd() - try: - os.chdir('..') - srcdir2 = sysconfig.get_config_var('srcdir') - finally: - os.chdir(cwd) - self.assertEqual(srcdir, srcdir2) + _sysconfig = __import__('sysconfig') + res = sysconfig.get_python_lib(True, True) + self.assertEqual(_sysconfig.get_path('platstdlib'), res) + + def test_get_python_inc(self): + inc_dir = sysconfig.get_python_inc() + # This is not much of a test. We make sure Python.h exists + # in the directory returned by get_python_inc() but we don't know + # it is the correct file. + self.assertTrue(os.path.isdir(inc_dir), inc_dir) + python_h = os.path.join(inc_dir, "Python.h") + self.assertTrue(os.path.isfile(python_h), python_h) def customize_compiler(self): # make sure AR gets caught @@ -91,17 +67,21 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase): 'CFLAGS': '--sc-cflags', 'CCSHARED': '--sc-ccshared', 'LDSHARED': 'sc_ldshared', - 'SHLIB_SUFFIX': 'sc_shutil_suffix', - - # On macOS, disable _osx_support.customize_compiler() - 'CUSTOMIZED_OSX_COMPILER': 'True', + 'SO': 'sc_shutil_suffix', } comp = compiler() - with contextlib.ExitStack() as cm: + old_vars = dict(sysconfig._config_vars) + try: + # On macOS, disable _osx_support.customize_compiler() + sysconfig._config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True' + for key, value in sysconfig_vars.items(): - cm.enter_context(swap_item(sysconfig._config_vars, key, value)) + sysconfig._config_vars[key] = value sysconfig.customize_compiler(comp) + finally: + sysconfig._config_vars.clear() + sysconfig._config_vars.update(old_vars) return comp @@ -168,7 +148,7 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase): self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix') def test_parse_makefile_base(self): - self.makefile = TESTFN + self.makefile = test.test_support.TESTFN fd = open(self.makefile, 'w') try: fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=LIB'" '\n') @@ -180,7 +160,7 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase): 'OTHER': 'foo'}) def test_parse_makefile_literal_dollar(self): - self.makefile = TESTFN + self.makefile = test.test_support.TESTFN fd = open(self.makefile, 'w') try: fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=\$$LIB'" '\n') @@ -194,13 +174,10 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase): def test_sysconfig_module(self): import sysconfig as global_sysconfig - self.assertEqual(global_sysconfig.get_config_var('CFLAGS'), - sysconfig.get_config_var('CFLAGS')) - self.assertEqual(global_sysconfig.get_config_var('LDFLAGS'), - sysconfig.get_config_var('LDFLAGS')) + self.assertEqual(global_sysconfig.get_config_var('CFLAGS'), sysconfig.get_config_var('CFLAGS')) + self.assertEqual(global_sysconfig.get_config_var('LDFLAGS'), sysconfig.get_config_var('LDFLAGS')) - @unittest.skipIf(sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'), - 'compiler flags customized') + @unittest.skipIf(sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'),'compiler flags customized') def test_sysconfig_compiler_vars(self): # On OS X, binary installers support extension module building on # various levels of the operating system with differing Xcode @@ -219,30 +196,8 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase): import sysconfig as global_sysconfig if sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'): self.skipTest('compiler flags customized') - self.assertEqual(global_sysconfig.get_config_var('LDSHARED'), - sysconfig.get_config_var('LDSHARED')) - self.assertEqual(global_sysconfig.get_config_var('CC'), - sysconfig.get_config_var('CC')) - - @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, - 'EXT_SUFFIX required for this test') - def test_SO_deprecation(self): - self.assertWarns(DeprecationWarning, - sysconfig.get_config_var, 'SO') - - @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, - 'EXT_SUFFIX required for this test') - def test_SO_value(self): - with check_warnings(('', DeprecationWarning)): - self.assertEqual(sysconfig.get_config_var('SO'), - sysconfig.get_config_var('EXT_SUFFIX')) - - @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, - 'EXT_SUFFIX required for this test') - def test_SO_in_vars(self): - vars = sysconfig.get_config_vars() - self.assertIsNotNone(vars['SO']) - self.assertEqual(vars['SO'], vars['EXT_SUFFIX']) + self.assertEqual(global_sysconfig.get_config_var('LDSHARED'), sysconfig.get_config_var('LDSHARED')) + self.assertEqual(global_sysconfig.get_config_var('CC'), sysconfig.get_config_var('CC')) def test_customize_compiler_before_get_config_vars(self): # Issue #21923: test that a Distribution compiler @@ -271,4 +226,4 @@ def test_suite(): if __name__ == '__main__': - run_unittest(test_suite()) + test.test_support.run_unittest(test_suite()) diff --git a/Lib/distutils/tests/test_text_file.py b/Lib/distutils/tests/test_text_file.py index 7e76240..ce19cd4 100644 --- a/Lib/distutils/tests/test_text_file.py +++ b/Lib/distutils/tests/test_text_file.py @@ -3,7 +3,7 @@ import os import unittest from distutils.text_file import TextFile from distutils.tests import support -from test.support import run_unittest +from test.test_support import run_unittest TEST_DATA = """# test file diff --git a/Lib/distutils/tests/test_unixccompiler.py b/Lib/distutils/tests/test_unixccompiler.py index eef702c..3b893c1 100644 --- a/Lib/distutils/tests/test_unixccompiler.py +++ b/Lib/distutils/tests/test_unixccompiler.py @@ -1,7 +1,8 @@ """Tests for distutils.unixccompiler.""" +import os import sys import unittest -from test.support import EnvironmentVarGuard, run_unittest +from test.test_support import EnvironmentVarGuard, run_unittest from distutils import sysconfig from distutils.unixccompiler import UnixCCompiler @@ -51,6 +52,14 @@ class UnixCCompilerTestCase(unittest.TestCase): sysconfig.get_config_var = old_gcv + # irix646 + sys.platform = 'irix646' + self.assertEqual(self.cc.rpath_foo(), ['-rpath', '/foo']) + + # osf1V5 + sys.platform = 'osf1V5' + self.assertEqual(self.cc.rpath_foo(), ['-rpath', '/foo']) + # GCC GNULD sys.platform = 'bar' def gcv(v): @@ -59,7 +68,7 @@ class UnixCCompilerTestCase(unittest.TestCase): elif v == 'GNULD': return 'yes' sysconfig.get_config_var = gcv - self.assertEqual(self.cc.rpath_foo(), '-Wl,--enable-new-dtags,-R/foo') + self.assertEqual(self.cc.rpath_foo(), '-Wl,-R/foo') # GCC non-GNULD sys.platform = 'bar' @@ -80,7 +89,8 @@ class UnixCCompilerTestCase(unittest.TestCase): elif v == 'GNULD': return 'yes' sysconfig.get_config_var = gcv - self.assertEqual(self.cc.rpath_foo(), '-Wl,--enable-new-dtags,-R/foo') + self.assertEqual(self.cc.rpath_foo(), '-Wl,-R/foo') + # non-GCC GNULD sys.platform = 'bar' @@ -102,6 +112,13 @@ class UnixCCompilerTestCase(unittest.TestCase): sysconfig.get_config_var = gcv self.assertEqual(self.cc.rpath_foo(), '-R/foo') + # AIX C/C++ linker + sys.platform = 'aix' + def gcv(v): + return 'xxx' + sysconfig.get_config_var = gcv + self.assertEqual(self.cc.rpath_foo(), '-R/foo') + @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for OS X') def test_osx_cc_overrides_ldshared(self): # Issue #18080: diff --git a/Lib/distutils/tests/test_upload.py b/Lib/distutils/tests/test_upload.py index c17d8e7..d225754 100644 --- a/Lib/distutils/tests/test_upload.py +++ b/Lib/distutils/tests/test_upload.py @@ -1,18 +1,15 @@ +# -*- encoding: utf8 -*- """Tests for distutils.command.upload.""" import os import unittest -import unittest.mock as mock -from urllib.request import HTTPError - -from test.support import run_unittest +from test.test_support import run_unittest from distutils.command import upload as upload_mod from distutils.command.upload import upload from distutils.core import Distribution from distutils.errors import DistutilsError -from distutils.log import ERROR, INFO -from distutils.tests.test_config import PYPIRC, BasePyPIRCCommandTestCase +from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase PYPIRC_LONG_PASSWORD = """\ [distutils] @@ -54,19 +51,11 @@ class FakeOpen(object): self.msg = msg or 'OK' self.code = code or 200 - def getheader(self, name, default=None): - return { - 'content-type': 'text/plain; charset=utf-8', - }.get(name.lower(), default) - - def read(self): - return b'xyzzy' - def getcode(self): return self.code -class uploadTestCase(BasePyPIRCCommandTestCase): +class uploadTestCase(PyPIRCCommandTestCase): def setUp(self): super(uploadTestCase, self).setUp() @@ -122,26 +111,22 @@ class uploadTestCase(BasePyPIRCCommandTestCase): self.write_file(self.rc, PYPIRC_LONG_PASSWORD) # lets run it - pkg_dir, dist = self.create_dist(dist_files=dist_files) + pkg_dir, dist = self.create_dist(dist_files=dist_files, author=u'dédé') cmd = upload(dist) - cmd.show_response = 1 cmd.ensure_finalized() cmd.run() # what did we send ? + self.assertIn('dédé', self.last_open.req.data) headers = dict(self.last_open.req.headers) - self.assertEqual(headers['Content-length'], '2162') - content_type = headers['Content-type'] - self.assertTrue(content_type.startswith('multipart/form-data')) + self.assertEqual(headers['Content-length'], '2159') + self.assertTrue(headers['Content-type'].startswith('multipart/form-data')) self.assertEqual(self.last_open.req.get_method(), 'POST') - expected_url = 'https://upload.pypi.org/legacy/' - self.assertEqual(self.last_open.req.get_full_url(), expected_url) - self.assertTrue(b'xxx' in self.last_open.req.data) - self.assertIn(b'protocol_version', self.last_open.req.data) - - # The PyPI response body was echoed - results = self.get_logs(INFO) - self.assertEqual(results[-1], 75 * '-' + '\nxyzzy\n' + 75 * '-') + self.assertEqual(self.last_open.req.get_full_url(), + 'https://upload.pypi.org/legacy/') + self.assertIn('xxx', self.last_open.req.data) + auth = self.last_open.req.headers['Authorization'] + self.assertNotIn('\n', auth) # bpo-32304: archives whose last byte was b'\r' were corrupted due to # normalization intended for Mac OS 9. @@ -161,45 +146,19 @@ class uploadTestCase(BasePyPIRCCommandTestCase): description='long description\r' ) cmd = upload(dist) - cmd.show_response = 1 cmd.ensure_finalized() cmd.run() headers = dict(self.last_open.req.headers) - self.assertEqual(headers['Content-length'], '2172') + self.assertEqual(headers['Content-length'], '2170') self.assertIn(b'long description\r', self.last_open.req.data) + self.assertNotIn(b'long description\r\n', self.last_open.req.data) def test_upload_fails(self): self.next_msg = "Not Found" self.next_code = 404 self.assertRaises(DistutilsError, self.test_upload) - def test_wrong_exception_order(self): - tmp = self.mkdtemp() - path = os.path.join(tmp, 'xxx') - self.write_file(path) - dist_files = [('xxx', '2.6', path)] # command, pyversion, filename - self.write_file(self.rc, PYPIRC_LONG_PASSWORD) - - pkg_dir, dist = self.create_dist(dist_files=dist_files) - tests = [ - (OSError('oserror'), 'oserror', OSError), - (HTTPError('url', 400, 'httperror', {}, None), - 'Upload failed (400): httperror', DistutilsError), - ] - for exception, expected, raised_exception in tests: - with self.subTest(exception=type(exception).__name__): - with mock.patch('distutils.command.upload.urlopen', - new=mock.Mock(side_effect=exception)): - with self.assertRaises(raised_exception): - cmd = upload(dist) - cmd.ensure_finalized() - cmd.run() - results = self.get_logs(ERROR) - self.assertIn(expected, results[-1]) - self.clear_logs() - - def test_suite(): return unittest.makeSuite(uploadTestCase) diff --git a/Lib/distutils/tests/test_util.py b/Lib/distutils/tests/test_util.py index bf0d433..e081709 100644 --- a/Lib/distutils/tests/test_util.py +++ b/Lib/distutils/tests/test_util.py @@ -2,236 +2,32 @@ import os import sys import unittest -from copy import copy -from test.support import run_unittest -from unittest import mock +from test.test_support import run_unittest, swap_attr -from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError -from distutils.util import (get_platform, convert_path, change_root, - check_environ, split_quoted, strtobool, - rfc822_escape, byte_compile, - grok_environment_error) -from distutils import util # used to patch _environ_checked -from distutils.sysconfig import get_config_vars -from distutils import sysconfig +from distutils.errors import DistutilsByteCompileError from distutils.tests import support -import _osx_support - -class UtilTestCase(support.EnvironGuard, unittest.TestCase): - - def setUp(self): - super(UtilTestCase, self).setUp() - # saving the environment - self.name = os.name - self.platform = sys.platform - self.version = sys.version - self.sep = os.sep - self.join = os.path.join - self.isabs = os.path.isabs - self.splitdrive = os.path.splitdrive - self._config_vars = copy(sysconfig._config_vars) - - # patching os.uname - if hasattr(os, 'uname'): - self.uname = os.uname - self._uname = os.uname() - else: - self.uname = None - self._uname = None - - os.uname = self._get_uname - - def tearDown(self): - # getting back the environment - os.name = self.name - sys.platform = self.platform - sys.version = self.version - os.sep = self.sep - os.path.join = self.join - os.path.isabs = self.isabs - os.path.splitdrive = self.splitdrive - if self.uname is not None: - os.uname = self.uname - else: - del os.uname - sysconfig._config_vars = copy(self._config_vars) - super(UtilTestCase, self).tearDown() - - def _set_uname(self, uname): - self._uname = uname - - def _get_uname(self): - return self._uname - - def test_get_platform(self): - - # windows XP, 32bits - os.name = 'nt' - sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' - '[MSC v.1310 32 bit (Intel)]') - sys.platform = 'win32' - self.assertEqual(get_platform(), 'win32') - - # windows XP, amd64 - os.name = 'nt' - sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' - '[MSC v.1310 32 bit (Amd64)]') - sys.platform = 'win32' - self.assertEqual(get_platform(), 'win-amd64') +from distutils import util # used to patch _environ_checked +from distutils.util import (byte_compile, grok_environment_error, + check_environ, get_platform) - # macbook - os.name = 'posix' - sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) ' - '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]') - sys.platform = 'darwin' - self._set_uname(('Darwin', 'macziade', '8.11.1', - ('Darwin Kernel Version 8.11.1: ' - 'Wed Oct 10 18:23:28 PDT 2007; ' - 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386')) - _osx_support._remove_original_values(get_config_vars()) - get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3' - get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' - '-fwrapv -O3 -Wall -Wstrict-prototypes') +class UtilTestCase(support.EnvironGuard, unittest.TestCase): - cursize = sys.maxsize - sys.maxsize = (2 ** 31)-1 + def test_dont_write_bytecode(self): + # makes sure byte_compile raise a DistutilsError + # if sys.dont_write_bytecode is True + old_dont_write_bytecode = sys.dont_write_bytecode + sys.dont_write_bytecode = True try: - self.assertEqual(get_platform(), 'macosx-10.3-i386') + self.assertRaises(DistutilsByteCompileError, byte_compile, []) finally: - sys.maxsize = cursize - - # macbook with fat binaries (fat, universal or fat64) - _osx_support._remove_original_values(get_config_vars()) - get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4' - get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot ' - '/Developer/SDKs/MacOSX10.4u.sdk ' - '-fno-strict-aliasing -fno-common ' - '-dynamic -DNDEBUG -g -O3') - - self.assertEqual(get_platform(), 'macosx-10.4-fat') - - _osx_support._remove_original_values(get_config_vars()) - os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.1' - self.assertEqual(get_platform(), 'macosx-10.4-fat') - - - _osx_support._remove_original_values(get_config_vars()) - get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot ' - '/Developer/SDKs/MacOSX10.4u.sdk ' - '-fno-strict-aliasing -fno-common ' - '-dynamic -DNDEBUG -g -O3') - - self.assertEqual(get_platform(), 'macosx-10.4-intel') - - _osx_support._remove_original_values(get_config_vars()) - get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot ' - '/Developer/SDKs/MacOSX10.4u.sdk ' - '-fno-strict-aliasing -fno-common ' - '-dynamic -DNDEBUG -g -O3') - self.assertEqual(get_platform(), 'macosx-10.4-fat3') - - _osx_support._remove_original_values(get_config_vars()) - get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot ' - '/Developer/SDKs/MacOSX10.4u.sdk ' - '-fno-strict-aliasing -fno-common ' - '-dynamic -DNDEBUG -g -O3') - self.assertEqual(get_platform(), 'macosx-10.4-universal') - - _osx_support._remove_original_values(get_config_vars()) - get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot ' - '/Developer/SDKs/MacOSX10.4u.sdk ' - '-fno-strict-aliasing -fno-common ' - '-dynamic -DNDEBUG -g -O3') - - self.assertEqual(get_platform(), 'macosx-10.4-fat64') - - for arch in ('ppc', 'i386', 'x86_64', 'ppc64'): - _osx_support._remove_original_values(get_config_vars()) - get_config_vars()['CFLAGS'] = ('-arch %s -isysroot ' - '/Developer/SDKs/MacOSX10.4u.sdk ' - '-fno-strict-aliasing -fno-common ' - '-dynamic -DNDEBUG -g -O3'%(arch,)) - - self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,)) - - - # linux debian sarge - os.name = 'posix' - sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) ' - '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]') - sys.platform = 'linux2' - self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7', - '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686')) - - self.assertEqual(get_platform(), 'linux-i686') - - # XXX more platforms to tests here - - def test_convert_path(self): - # linux/mac - os.sep = '/' - def _join(path): - return '/'.join(path) - os.path.join = _join - - self.assertEqual(convert_path('/home/to/my/stuff'), - '/home/to/my/stuff') - - # win - os.sep = '\\' - def _join(*path): - return '\\'.join(path) - os.path.join = _join - - self.assertRaises(ValueError, convert_path, '/home/to/my/stuff') - self.assertRaises(ValueError, convert_path, 'home/to/my/stuff/') - - self.assertEqual(convert_path('home/to/my/stuff'), - 'home\\to\\my\\stuff') - self.assertEqual(convert_path('.'), - os.curdir) - - def test_change_root(self): - # linux/mac - os.name = 'posix' - def _isabs(path): - return path[0] == '/' - os.path.isabs = _isabs - def _join(*path): - return '/'.join(path) - os.path.join = _join - - self.assertEqual(change_root('/root', '/old/its/here'), - '/root/old/its/here') - self.assertEqual(change_root('/root', 'its/here'), - '/root/its/here') - - # windows - os.name = 'nt' - def _isabs(path): - return path.startswith('c:\\') - os.path.isabs = _isabs - def _splitdrive(path): - if path.startswith('c:'): - return ('', path.replace('c:', '')) - return ('', path) - os.path.splitdrive = _splitdrive - def _join(*path): - return '\\'.join(path) - os.path.join = _join - - self.assertEqual(change_root('c:\\root', 'c:\\old\\its\\here'), - 'c:\\root\\old\\its\\here') - self.assertEqual(change_root('c:\\root', 'its\\here'), - 'c:\\root\\its\\here') - - # BugsBunny os (it's a great os) - os.name = 'BugsBunny' - self.assertRaises(DistutilsPlatformError, - change_root, 'c:\\root', 'its\\here') + sys.dont_write_bytecode = old_dont_write_bytecode - # XXX platforms to be covered: mac + def test_grok_environment_error(self): + # test obsolete function to ensure backward compat (#4931) + exc = IOError("Unable to find batch file") + msg = grok_environment_error(exc) + self.assertEqual(msg, "error: Unable to find batch file") def test_check_environ(self): util._environ_checked = 0 @@ -250,9 +46,11 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase): import pwd # only set pw_dir field, other fields are not used - result = pwd.struct_passwd((None, None, None, None, None, - '/home/distutils', None)) - with mock.patch.object(pwd, 'getpwuid', return_value=result): + def mock_getpwuid(uid): + return pwd.struct_passwd((None, None, None, None, None, + '/home/distutils', None)) + + with swap_attr(pwd, 'getpwuid', mock_getpwuid): check_environ() self.assertEqual(os.environ['HOME'], '/home/distutils') @@ -260,47 +58,12 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase): os.environ.pop('HOME', None) # bpo-10496: Catch pwd.getpwuid() error - with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError): + def getpwuid_err(uid): + raise KeyError + with swap_attr(pwd, 'getpwuid', getpwuid_err): check_environ() self.assertNotIn('HOME', os.environ) - def test_split_quoted(self): - self.assertEqual(split_quoted('""one"" "two" \'three\' \\four'), - ['one', 'two', 'three', 'four']) - - def test_strtobool(self): - yes = ('y', 'Y', 'yes', 'True', 't', 'true', 'True', 'On', 'on', '1') - no = ('n', 'no', 'f', 'false', 'off', '0', 'Off', 'No', 'N') - - for y in yes: - self.assertTrue(strtobool(y)) - - for n in no: - self.assertFalse(strtobool(n)) - - def test_rfc822_escape(self): - header = 'I am a\npoor\nlonesome\nheader\n' - res = rfc822_escape(header) - wanted = ('I am a%(8s)spoor%(8s)slonesome%(8s)s' - 'header%(8s)s') % {'8s': '\n'+8*' '} - self.assertEqual(res, wanted) - - def test_dont_write_bytecode(self): - # makes sure byte_compile raise a DistutilsError - # if sys.dont_write_bytecode is True - old_dont_write_bytecode = sys.dont_write_bytecode - sys.dont_write_bytecode = True - try: - self.assertRaises(DistutilsByteCompileError, byte_compile, []) - finally: - sys.dont_write_bytecode = old_dont_write_bytecode - - def test_grok_environment_error(self): - # test obsolete function to ensure backward compat (#4931) - exc = IOError("Unable to find batch file") - msg = grok_environment_error(exc) - self.assertEqual(msg, "error: Unable to find batch file") - def test_suite(): return unittest.makeSuite(UtilTestCase) diff --git a/Lib/distutils/tests/test_version.py b/Lib/distutils/tests/test_version.py index 8671cd2..2189956 100644 --- a/Lib/distutils/tests/test_version.py +++ b/Lib/distutils/tests/test_version.py @@ -2,7 +2,7 @@ import unittest from distutils.version import LooseVersion from distutils.version import StrictVersion -from test.support import run_unittest +from test.test_support import run_unittest class VersionTestCase(unittest.TestCase): @@ -34,7 +34,7 @@ class VersionTestCase(unittest.TestCase): for v1, v2, wanted in versions: try: - res = StrictVersion(v1)._cmp(StrictVersion(v2)) + res = StrictVersion(v1).__cmp__(StrictVersion(v2)) except ValueError: if wanted is ValueError: continue @@ -45,14 +45,6 @@ class VersionTestCase(unittest.TestCase): self.assertEqual(res, wanted, 'cmp(%s, %s) should be %s, got %s' % (v1, v2, wanted, res)) - res = StrictVersion(v1)._cmp(v2) - self.assertEqual(res, wanted, - 'cmp(%s, %s) should be %s, got %s' % - (v1, v2, wanted, res)) - res = StrictVersion(v1)._cmp(object()) - self.assertIs(res, NotImplemented, - 'cmp(%s, %s) should be NotImplemented, got %s' % - (v1, v2, res)) def test_cmp(self): @@ -67,18 +59,10 @@ class VersionTestCase(unittest.TestCase): for v1, v2, wanted in versions: - res = LooseVersion(v1)._cmp(LooseVersion(v2)) - self.assertEqual(res, wanted, - 'cmp(%s, %s) should be %s, got %s' % - (v1, v2, wanted, res)) - res = LooseVersion(v1)._cmp(v2) + res = LooseVersion(v1).__cmp__(LooseVersion(v2)) self.assertEqual(res, wanted, 'cmp(%s, %s) should be %s, got %s' % (v1, v2, wanted, res)) - res = LooseVersion(v1)._cmp(object()) - self.assertIs(res, NotImplemented, - 'cmp(%s, %s) should be NotImplemented, got %s' % - (v1, v2, res)) def test_suite(): return unittest.makeSuite(VersionTestCase) diff --git a/Lib/distutils/tests/test_versionpredicate.py b/Lib/distutils/tests/test_versionpredicate.py index 28ae09d..1d6c8d5 100644 --- a/Lib/distutils/tests/test_versionpredicate.py +++ b/Lib/distutils/tests/test_versionpredicate.py @@ -4,7 +4,7 @@ import distutils.versionpredicate import doctest -from test.support import run_unittest +from test.test_support import run_unittest def test_suite(): return doctest.DocTestSuite(distutils.versionpredicate) |