From def15dafdaad285f5e5dd392a1e5512730335591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Sat, 20 Aug 2011 06:27:18 +0200 Subject: [PATCH 1/9] Refactor the copying of xxmodule.c in distutils tests (#12141). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I need to copy this file in another test too, so I moved the support code to distutils.tests.support and improved it: - don’t skip when run from the Lib/distutils/tests directory - use proper skip machinery instead of custom print/return/test suite fiddling. --- Lib/distutils/tests/support.py | 42 +++++++++++++++++++++++++++ Lib/distutils/tests/test_build_ext.py | 28 +++--------------- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/Lib/distutils/tests/support.py b/Lib/distutils/tests/support.py index e258d2e58dd..0e33827fe82 100644 --- a/Lib/distutils/tests/support.py +++ b/Lib/distutils/tests/support.py @@ -2,12 +2,15 @@ import os import shutil import tempfile +import unittest +import sysconfig from copy import deepcopy from distutils import log from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL from distutils.core import Distribution + class LoggingSilencer(object): def setUp(self): @@ -41,6 +44,7 @@ class LoggingSilencer(object): def clear_logs(self): self.logs = [] + class TempdirManager(object): """Mix-in class that handles temporary directories for test cases. @@ -97,6 +101,7 @@ class TempdirManager(object): return pkg_dir, dist + class DummyCommand: """Class to store options for retrieval via set_undefined_options().""" @@ -107,6 +112,7 @@ class DummyCommand: def ensure_finalized(self): pass + class EnvironGuard(object): def setUp(self): @@ -123,3 +129,39 @@ class EnvironGuard(object): del os.environ[key] super(EnvironGuard, self).tearDown() + + +def copy_xxmodule_c(directory): + """Helper for tests that need the xxmodule.c source file. + + Example use: + + def test_compile(self): + copy_xxmodule_c(self.tmpdir) + self.assertIn('xxmodule.c', os.listdir(self.tmpdir) + + If the source file can be found, it will be copied to *directory*. If not, + the test will be skipped. Errors during copy are not caught. + """ + filename = _get_xxmodule_path() + if filename is None: + raise unittest.SkipTest('cannot find xxmodule.c (test must run in ' + 'the python build dir)') + shutil.copy(filename, directory) + + +def _get_xxmodule_path(): + srcdir = sysconfig.get_config_var('srcdir') + candidates = [ + # use installed copy if available + os.path.join(os.path.dirname(__file__), 'xxmodule.c'), + # otherwise try using copy from build directory + os.path.join(srcdir, 'Modules', 'xxmodule.c'), + # srcdir mysteriously can be $srcdir/Lib/distutils/tests when + # this file is run from its parent directory, so walk up the + # tree to find the real srcdir + os.path.join(srcdir, '..', '..', '..', 'Modules', 'xxmodule.c'), + ] + for path in candidates: + if os.path.exists(path): + return path diff --git a/Lib/distutils/tests/test_build_ext.py b/Lib/distutils/tests/test_build_ext.py index 0ce7f0f81c8..de53afb1c3b 100644 --- a/Lib/distutils/tests/test_build_ext.py +++ b/Lib/distutils/tests/test_build_ext.py @@ -1,14 +1,13 @@ import sys import os -import shutil from io import StringIO import textwrap from distutils.core import Distribution from distutils.command.build_ext import build_ext from distutils import sysconfig -from distutils.tests.support import TempdirManager -from distutils.tests.support import LoggingSilencer +from distutils.tests.support import (TempdirManager, LoggingSilencer, + copy_xxmodule_c) from distutils.extension import Extension from distutils.errors import ( CompileError, DistutilsPlatformError, DistutilsSetupError, @@ -16,20 +15,11 @@ from distutils.errors import ( import unittest from test import support -from test.support import run_unittest # http://bugs.python.org/issue4373 # Don't load the xx module more than once. ALREADY_TESTED = False -def _get_source_filename(): - # use installed copy if available - tests_f = os.path.join(os.path.dirname(__file__), 'xxmodule.c') - if os.path.exists(tests_f): - return tests_f - # otherwise try using copy from build directory - srcdir = sysconfig.get_config_var('srcdir') - return os.path.join(srcdir, 'Modules', 'xxmodule.c') class BuildExtTestCase(TempdirManager, LoggingSilencer, @@ -41,9 +31,6 @@ class BuildExtTestCase(TempdirManager, self.tmp_dir = self.mkdtemp() self.sys_path = sys.path, sys.path[:] sys.path.append(self.tmp_dir) - filename = _get_source_filename() - if os.path.exists(filename): - shutil.copy(filename, self.tmp_dir) if sys.version > "2.6": import site self.old_user_base = site.USER_BASE @@ -72,9 +59,8 @@ class BuildExtTestCase(TempdirManager, def test_build_ext(self): global ALREADY_TESTED + copy_xxmodule_c(self.tmp_dir) xx_c = os.path.join(self.tmp_dir, 'xxmodule.c') - if not os.path.exists(xx_c): - return xx_ext = Extension('xx', [xx_c]) dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]}) dist.package_dir = self.tmp_dir @@ -518,13 +504,7 @@ class BuildExtTestCase(TempdirManager, def test_suite(): - src = _get_source_filename() - if not os.path.exists(src): - if support.verbose: - print('test_build_ext: Cannot find source code (test' - ' must run in python build dir)') - return unittest.TestSuite() - else: return unittest.makeSuite(BuildExtTestCase) + return unittest.makeSuite(BuildExtTestCase) if __name__ == '__main__': support.run_unittest(test_suite()) From 9358bfdaffe513e2604764bf1b2f72a59f495555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Sat, 20 Aug 2011 07:00:41 +0200 Subject: [PATCH 2/9] Rework test_record a bit to make the test more exact --- Lib/distutils/tests/test_install.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Lib/distutils/tests/test_install.py b/Lib/distutils/tests/test_install.py index ed69b0cbb06..3e47d819fe1 100644 --- a/Lib/distutils/tests/test_install.py +++ b/Lib/distutils/tests/test_install.py @@ -1,7 +1,6 @@ """Tests for distutils.command.install.""" import os -import os.path import sys import unittest import site @@ -167,33 +166,36 @@ class InstallTestCase(support.TempdirManager, self.assertRaises(DistutilsOptionError, cmd.finalize_options) def test_record(self): - install_dir = self.mkdtemp() - pkgdir, dist = self.create_dist() + project_dir, dist = self.create_dist(scripts=['hello']) + self.addCleanup(os.chdir, os.getcwd()) + os.chdir(project_dir) + self.write_file('hello', "print('o hai')") - dist = Distribution() cmd = install(dist) dist.command_obj['install'] = cmd cmd.root = install_dir - cmd.record = os.path.join(pkgdir, 'RECORD') + cmd.record = os.path.join(project_dir, 'RECORD') cmd.ensure_finalized() - cmd.run() - # let's check the RECORD file was created with one - # line (the egg info file) f = open(cmd.record) try: - self.assertEqual(len(f.readlines()), 1) + content = f.read() finally: f.close() + found = [os.path.basename(line) for line in content.splitlines()] + expected = ['hello', + 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]] + self.assertEqual(found, expected) + def test_debug_mode(self): # this covers the code called when DEBUG is set old_logs_len = len(self.logs) install_module.DEBUG = True try: - with captured_stdout() as stdout: + with captured_stdout(): self.test_record() finally: install_module.DEBUG = False From 17725410850094241d459bce50ac1e8f416cfa63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Sat, 20 Aug 2011 07:08:51 +0200 Subject: [PATCH 3/9] Add a test for extension modules in the distutils record file. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I made a note a month ago that install --record wrote incorrect entries for extension modules (I think the problem was that the first character of the file was stripped), so I’m now adding a test to try to reproduce that in the current versions. --- Lib/distutils/tests/test_install.py | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Lib/distutils/tests/test_install.py b/Lib/distutils/tests/test_install.py index 3e47d819fe1..2133fa79162 100644 --- a/Lib/distutils/tests/test_install.py +++ b/Lib/distutils/tests/test_install.py @@ -7,11 +7,14 @@ import site from test.support import captured_stdout, run_unittest +from distutils import sysconfig from distutils.command.install import install from distutils.command import install as install_module +from distutils.command.build_ext import build_ext from distutils.command.install import INSTALL_SCHEMES from distutils.core import Distribution from distutils.errors import DistutilsOptionError +from distutils.extension import Extension from distutils.tests import support @@ -190,6 +193,36 @@ class InstallTestCase(support.TempdirManager, 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]] self.assertEqual(found, expected) + def test_record_extensions(self): + install_dir = self.mkdtemp() + project_dir, dist = self.create_dist(ext_modules=[ + Extension('xx', ['xxmodule.c'])]) + self.addCleanup(os.chdir, os.getcwd()) + os.chdir(project_dir) + support.copy_xxmodule_c(project_dir) + + buildcmd = build_ext(dist) + buildcmd.ensure_finalized() + buildcmd.run() + + cmd = install(dist) + dist.command_obj['install'] = cmd + cmd.root = install_dir + cmd.record = os.path.join(project_dir, 'RECORD') + cmd.ensure_finalized() + cmd.run() + + f = open(cmd.record) + try: + content = f.read() + finally: + f.close() + + found = [os.path.basename(line) for line in content.splitlines()] + expected = ['xx%s' % sysconfig.get_config_var('SO'), + 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]] + self.assertEqual(found, expected) + def test_debug_mode(self): # this covers the code called when DEBUG is set old_logs_len = len(self.logs) From 60b0d31e35238275ef9f72ca1be71e4efae88fc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Sat, 20 Aug 2011 07:27:47 +0200 Subject: [PATCH 4/9] Refactor the copying of xxmodule.c in packaging tests (#12141). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I need to copy this file in another test too, so I moved the support code to distutils.tests.support and improved it to use proper skip machinery instead of custom print/return/test suite fiddling. Contrary to my similar change in distutils tests, I did not add support for finding xxmodule.c when running a test from the tests directory, because in that case my compiler didn’t find Python.h, so I figured it’s better to skip than to fail. --- Lib/packaging/tests/support.py | 35 ++++++++++++++++++- Lib/packaging/tests/test_command_build_ext.py | 29 ++------------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Lib/packaging/tests/support.py b/Lib/packaging/tests/support.py index c6e3f728793..a9535ab5845 100644 --- a/Lib/packaging/tests/support.py +++ b/Lib/packaging/tests/support.py @@ -32,6 +32,7 @@ import shutil import logging import weakref import tempfile +import sysconfig from packaging.dist import Distribution from packaging.tests import unittest @@ -39,7 +40,7 @@ from test.support import requires_zlib, unlink __all__ = ['LoggingCatcher', 'TempdirManager', 'EnvironRestorer', 'DummyCommand', 'unittest', 'create_distribution', - 'skip_unless_symlink', 'requires_zlib'] + 'skip_unless_symlink', 'requires_zlib', 'copy_xxmodule_c'] logger = logging.getLogger('packaging') @@ -271,6 +272,38 @@ def fake_dec(*args, **kw): return _wrap +def copy_xxmodule_c(directory): + """Helper for tests that need the xxmodule.c source file. + + Example use: + + def test_compile(self): + copy_xxmodule_c(self.tmpdir) + self.assertIn('xxmodule.c', os.listdir(self.tmpdir) + + If the source file can be found, it will be copied to *directory*. If not, + the test will be skipped. Errors during copy are not caught. + """ + filename = _get_xxmodule_path() + if filename is None: + raise unittest.SkipTest('cannot find xxmodule.c (test must run in ' + 'the python build dir)') + shutil.copy(filename, directory) + + +def _get_xxmodule_path(): + srcdir = sysconfig.get_config_var('srcdir') + candidates = [ + # use installed copy if available + os.path.join(os.path.dirname(__file__), 'xxmodule.c'), + # otherwise try using copy from build directory + os.path.join(srcdir, 'Modules', 'xxmodule.c'), + ] + for path in candidates: + if os.path.exists(path): + return path + + try: from test.support import skip_unless_symlink except ImportError: diff --git a/Lib/packaging/tests/test_command_build_ext.py b/Lib/packaging/tests/test_command_build_ext.py index e144fd0738b..9521adcedfc 100644 --- a/Lib/packaging/tests/test_command_build_ext.py +++ b/Lib/packaging/tests/test_command_build_ext.py @@ -1,7 +1,6 @@ import os import sys import site -import shutil import sysconfig import textwrap from io import StringIO @@ -12,17 +11,7 @@ from packaging.command.build_ext import build_ext from packaging.compiler.extension import Extension from test.script_helper import assert_python_ok -from packaging.tests import support, unittest, verbose, unload - - -def _get_source_filename(): - # use installed copy if available - tests_f = os.path.join(os.path.dirname(__file__), 'xxmodule.c') - if os.path.exists(tests_f): - return tests_f - # otherwise try using copy from build directory - srcdir = sysconfig.get_config_var('srcdir') - return os.path.join(srcdir, 'Modules', 'xxmodule.c') +from packaging.tests import support, unittest, verbose class BuildExtTestCase(support.TempdirManager, @@ -33,9 +22,6 @@ class BuildExtTestCase(support.TempdirManager, # Note that we're making changes to sys.path super(BuildExtTestCase, self).setUp() self.tmp_dir = self.mkdtemp() - filename = _get_source_filename() - if os.path.exists(filename): - shutil.copy(filename, self.tmp_dir) self.old_user_base = site.USER_BASE site.USER_BASE = self.mkdtemp() build_ext.USER_BASE = site.USER_BASE @@ -68,10 +54,8 @@ class BuildExtTestCase(support.TempdirManager, cmd.library_dirs = value.split(os.pathsep) def test_build_ext(self): + support.copy_xxmodule_c(self.tmp_dir) xx_c = os.path.join(self.tmp_dir, 'xxmodule.c') - if not os.path.exists(xx_c): - # skipping if we cannot find it - return xx_ext = Extension('xx', [xx_c]) dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]}) dist.package_dir = self.tmp_dir @@ -455,14 +439,7 @@ class BuildExtTestCase(support.TempdirManager, def test_suite(): - src = _get_source_filename() - if not os.path.exists(src): - if verbose: - print('test_command_build_ext: Cannot find source code (test' - ' must run in python build dir)') - return unittest.TestSuite() - else: - return unittest.makeSuite(BuildExtTestCase) + return unittest.makeSuite(BuildExtTestCase) if __name__ == '__main__': unittest.main(defaultTest='test_suite') From 746e72d59c958bba39d8e755f4cda38010790478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Sat, 20 Aug 2011 07:34:43 +0200 Subject: [PATCH 5/9] Rework test_old_record a bit to make the test more exact (i.e. to check the files found are what we expect) --- .../tests/test_command_install_dist.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Lib/packaging/tests/test_command_install_dist.py b/Lib/packaging/tests/test_command_install_dist.py index 7821a3afde7..c06a3c38bc4 100644 --- a/Lib/packaging/tests/test_command_install_dist.py +++ b/Lib/packaging/tests/test_command_install_dist.py @@ -2,22 +2,19 @@ import os import sys - from sysconfig import (get_scheme_names, get_config_vars, _SCHEMES, get_config_var, get_path) -_CONFIG_VARS = get_config_vars() - -from packaging.tests import captured_stdout - from packaging.command.install_dist import install_dist -from packaging.command import install_dist as install_module from packaging.dist import Distribution from packaging.errors import PackagingOptionError from packaging.tests import unittest, support +_CONFIG_VARS = get_config_vars() + + class InstallTestCase(support.TempdirManager, support.LoggingCatcher, unittest.TestCase): @@ -178,21 +175,24 @@ class InstallTestCase(support.TempdirManager, def test_old_record(self): # test pre-PEP 376 --record option (outside dist-info dir) install_dir = self.mkdtemp() - pkgdir, dist = self.create_dist() + project_dir, dist = self.create_dist(scripts=['hello']) + #self.addCleanup(os.chdir, os.getcwd()) + os.chdir(project_dir) + self.write_file('hello', "print('o hai')") - dist = Distribution() cmd = install_dist(dist) dist.command_obj['install_dist'] = cmd cmd.root = install_dir - cmd.record = os.path.join(pkgdir, 'filelist') + cmd.record = os.path.join(project_dir, 'filelist') cmd.ensure_finalized() cmd.run() - # let's check the record file was created with four - # lines, one for each .dist-info entry: METADATA, - # INSTALLER, REQUSTED, RECORD with open(cmd.record) as f: - self.assertEqual(len(f.readlines()), 4) + content = f.read() + + found = [os.path.basename(line) for line in content.splitlines()] + expected = ['hello', 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD'] + self.assertEqual(found, expected) # XXX test that fancy_getopt is okay with options named # record and no-record but unrelated From 540edc6e660258f98cc91d39db4085c2140a8f8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Sat, 20 Aug 2011 07:42:56 +0200 Subject: [PATCH 6/9] Add a test for extension modules in the old-style record file --- .../tests/test_command_install_dist.py | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Lib/packaging/tests/test_command_install_dist.py b/Lib/packaging/tests/test_command_install_dist.py index c06a3c38bc4..ea7c3d35628 100644 --- a/Lib/packaging/tests/test_command_install_dist.py +++ b/Lib/packaging/tests/test_command_install_dist.py @@ -6,6 +6,7 @@ from sysconfig import (get_scheme_names, get_config_vars, _SCHEMES, get_config_var, get_path) from packaging.command.install_dist import install_dist +from packaging.compiler.extension import Extension from packaging.dist import Distribution from packaging.errors import PackagingOptionError @@ -176,7 +177,6 @@ class InstallTestCase(support.TempdirManager, # test pre-PEP 376 --record option (outside dist-info dir) install_dir = self.mkdtemp() project_dir, dist = self.create_dist(scripts=['hello']) - #self.addCleanup(os.chdir, os.getcwd()) os.chdir(project_dir) self.write_file('hello', "print('o hai')") @@ -197,6 +197,29 @@ class InstallTestCase(support.TempdirManager, # XXX test that fancy_getopt is okay with options named # record and no-record but unrelated + def test_old_record_extensions(self): + # test pre-PEP 376 --record option with ext modules + install_dir = self.mkdtemp() + project_dir, dist = self.create_dist(ext_modules=[ + Extension('xx', ['xxmodule.c'])]) + os.chdir(project_dir) + support.copy_xxmodule_c(project_dir) + + cmd = install_dist(dist) + dist.command_obj['install_dist'] = cmd + cmd.root = install_dir + cmd.record = os.path.join(project_dir, 'filelist') + cmd.ensure_finalized() + cmd.run() + + with open(cmd.record) as f: + content = f.read() + + found = [os.path.basename(line) for line in content.splitlines()] + expected = ['xx%s' % get_config_var('SO'), + 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD'] + self.assertEqual(found, expected) + def test_suite(): return unittest.makeSuite(InstallTestCase) From ba9b2689be0746c36171311dadcb64464ce6baae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Sat, 20 Aug 2011 09:19:25 +0200 Subject: [PATCH 7/9] Minor cleanup - Rename an attribute and create it in initialize_options instead of finalize_options to match the other install_* classes - Remove unnecessary method call in tests --- Lib/packaging/command/install_distinfo.py | 14 +++++++------- .../tests/test_command_install_distinfo.py | 5 ----- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/Lib/packaging/command/install_distinfo.py b/Lib/packaging/command/install_distinfo.py index 3390a1f0a7c..0e1577e7dc2 100644 --- a/Lib/packaging/command/install_distinfo.py +++ b/Lib/packaging/command/install_distinfo.py @@ -41,6 +41,7 @@ class install_distinfo(Command): self.requested = None self.no_record = None self.no_resources = None + self.outfiles = [] def finalize_options(self): self.set_undefined_options('install_dist', @@ -67,7 +68,6 @@ class install_distinfo(Command): to_filename(safe_version(metadata['Version']))) self.distinfo_dir = os.path.join(self.distinfo_dir, basename) - self.outputs = [] def run(self): # FIXME dry-run should be used at a finer level, so that people get @@ -87,19 +87,19 @@ class install_distinfo(Command): metadata_path = os.path.join(self.distinfo_dir, 'METADATA') logger.info('creating %s', metadata_path) self.distribution.metadata.write(metadata_path) - self.outputs.append(metadata_path) + self.outfiles.append(metadata_path) installer_path = os.path.join(self.distinfo_dir, 'INSTALLER') logger.info('creating %s', installer_path) with open(installer_path, 'w') as f: f.write(self.installer) - self.outputs.append(installer_path) + self.outfiles.append(installer_path) if self.requested: requested_path = os.path.join(self.distinfo_dir, 'REQUESTED') logger.info('creating %s', requested_path) open(requested_path, 'wb').close() - self.outputs.append(requested_path) + self.outfiles.append(requested_path) if not self.no_resources: @@ -115,7 +115,7 @@ class install_distinfo(Command): for tuple in install_data.get_resources_out(): writer.writerow(tuple) - self.outputs.append(resources_path) + self.outfiles.append(resources_path) if not self.no_record: record_path = os.path.join(self.distinfo_dir, 'RECORD') @@ -141,10 +141,10 @@ class install_distinfo(Command): # add the RECORD file itself writer.writerow((record_path, '', '')) - self.outputs.append(record_path) + self.outfiles.append(record_path) def get_outputs(self): - return self.outputs + return self.outfiles # The following functions are taken from setuptools' pkg_resources module. diff --git a/Lib/packaging/tests/test_command_install_distinfo.py b/Lib/packaging/tests/test_command_install_distinfo.py index ade191c57a0..9853700892b 100644 --- a/Lib/packaging/tests/test_command_install_distinfo.py +++ b/Lib/packaging/tests/test_command_install_distinfo.py @@ -45,7 +45,6 @@ class InstallDistinfoTestCase(support.TempdirManager, cmd = install_distinfo(dist) dist.command_obj['install_distinfo'] = cmd - cmd.initialize_options() cmd.distinfo_dir = install_dir cmd.ensure_finalized() cmd.run() @@ -73,7 +72,6 @@ class InstallDistinfoTestCase(support.TempdirManager, cmd = install_distinfo(dist) dist.command_obj['install_distinfo'] = cmd - cmd.initialize_options() cmd.distinfo_dir = install_dir cmd.installer = 'bacon-python' cmd.ensure_finalized() @@ -94,7 +92,6 @@ class InstallDistinfoTestCase(support.TempdirManager, cmd = install_distinfo(dist) dist.command_obj['install_distinfo'] = cmd - cmd.initialize_options() cmd.distinfo_dir = install_dir cmd.requested = False cmd.ensure_finalized() @@ -115,7 +112,6 @@ class InstallDistinfoTestCase(support.TempdirManager, cmd = install_distinfo(dist) dist.command_obj['install_distinfo'] = cmd - cmd.initialize_options() cmd.distinfo_dir = install_dir cmd.no_record = True cmd.ensure_finalized() @@ -153,7 +149,6 @@ class InstallDistinfoTestCase(support.TempdirManager, cmd = install_distinfo(dist) dist.command_obj['install_distinfo'] = cmd - cmd.initialize_options() cmd.distinfo_dir = install_dir cmd.ensure_finalized() cmd.run() From 0a733627f968e7a886fea64cb3c5bddc32eb55ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Sat, 20 Aug 2011 09:31:25 +0200 Subject: [PATCH 8/9] Add a simple test for the packaging RECORD file. The existing test_record is not easily extendable to add script files or extension modules: it collects all files from fake_dists and generates a RECORD file at runtime. I felt more comfortable adding a new test written from scratch more self-contained (just one project with well-defined files) and more stupid (the checksums and sizes are computed once and hard-coded). --- .../tests/test_command_install_distinfo.py | 54 ++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/Lib/packaging/tests/test_command_install_distinfo.py b/Lib/packaging/tests/test_command_install_distinfo.py index 9853700892b..994cdd8f5c5 100644 --- a/Lib/packaging/tests/test_command_install_distinfo.py +++ b/Lib/packaging/tests/test_command_install_distinfo.py @@ -3,10 +3,11 @@ import os import csv import hashlib -import sys +import sysconfig from packaging.command.install_distinfo import install_distinfo from packaging.command.cmd import Command +from packaging.compiler.extension import Extension from packaging.metadata import Metadata from packaging.tests import unittest, support @@ -121,6 +122,57 @@ class InstallDistinfoTestCase(support.TempdirManager, self.checkLists(os.listdir(dist_info), ['METADATA', 'REQUESTED', 'INSTALLER']) + def test_record_basic(self): + install_dir = self.mkdtemp() + modules_dest = os.path.join(install_dir, 'lib') + scripts_dest = os.path.join(install_dir, 'bin') + project_dir, dist = self.create_dist( + name='Spamlib', version='0.1', + py_modules=['spam'], scripts=['spamd'], + ext_modules=[Extension('_speedspam', ['_speedspam.c'])]) + + # using a real install_dist command is too painful, so we use a mock + # class that's only a holder for options to be used by install_distinfo + # and we create placeholder files manually instead of using build_*. + # the install_* commands will still be consulted by install_distinfo. + os.chdir(project_dir) + self.write_file('spam', '# Python module') + self.write_file('spamd', '# Python script') + extmod = '_speedspam%s' % sysconfig.get_config_var('SO') + self.write_file(extmod, '') + + install = DummyInstallCmd(dist) + install.outputs = ['spam', 'spamd', extmod] + install.install_lib = modules_dest + install.install_scripts = scripts_dest + dist.command_obj['install_dist'] = install + + cmd = install_distinfo(dist) + cmd.ensure_finalized() + dist.command_obj['install_distinfo'] = cmd + cmd.run() + + record = os.path.join(modules_dest, 'Spamlib-0.1.dist-info', 'RECORD') + with open(record, encoding='utf-8') as fp: + content = fp.read() + + found = [] + for line in content.splitlines(): + filename, checksum, size = line.split(',') + filename = os.path.basename(filename) + found.append((filename, checksum, size)) + + expected = [ + ('spam', '6ab2f288ef2545868effe68757448b45', '15'), + ('spamd','d13e6156ce78919a981e424b2fdcd974', '15'), + (extmod, 'd41d8cd98f00b204e9800998ecf8427e', '0'), + ('METADATA', '846de67e49c3b92c81fb1ebd7bc07046', '172'), + ('INSTALLER', '44e3fde05f3f537ed85831969acf396d', '9'), + ('REQUESTED', 'd41d8cd98f00b204e9800998ecf8427e', '0'), + ('RECORD', '', ''), + ] + self.assertEqual(found, expected) + def test_record(self): pkg_dir, dist = self.create_dist(name='foo', version='1.0') From 19ffe600e9956703e34a3e4ab1b7399ddf63609d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Sat, 20 Aug 2011 19:52:07 +0200 Subject: [PATCH 9/9] Fix sdist test on Windows (#12678). Patch by Jeremy Kloth. --- Lib/distutils/tests/test_sdist.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/distutils/tests/test_sdist.py b/Lib/distutils/tests/test_sdist.py index 440af9886cc..f34f786c927 100644 --- a/Lib/distutils/tests/test_sdist.py +++ b/Lib/distutils/tests/test_sdist.py @@ -365,6 +365,7 @@ class SDistTestCase(PyPIRCCommandTestCase): def test_manual_manifest(self): # check that a MANIFEST without a marker is left alone dist, cmd = self.get_cmd() + cmd.formats = ['gztar'] cmd.ensure_finalized() self.write_file((self.tmp_dir, cmd.manifest), 'README.manual') self.write_file((self.tmp_dir, 'README.manual'),