Make a number of small changes to ease the backport to distutils2

This commit is contained in:
Éric Araujo 2011-09-18 20:11:48 +02:00
parent 41479450ec
commit 229011d949
19 changed files with 104 additions and 125 deletions

View file

@ -16,7 +16,7 @@ distribution and needed by extension modules.
import os import os
from packaging.command.cmd import Command from packaging.command.cmd import Command
from packaging.errors import PackagingSetupError from packaging.errors import PackagingSetupError
from packaging.compiler import customize_compiler from packaging.compiler import customize_compiler, new_compiler
from packaging import logger from packaging import logger
@ -93,7 +93,6 @@ class build_clib(Command):
return return
# Yech -- this is cut 'n pasted from build_ext.py! # Yech -- this is cut 'n pasted from build_ext.py!
from packaging.compiler import new_compiler
self.compiler = new_compiler(compiler=self.compiler, self.compiler = new_compiler(compiler=self.compiler,
dry_run=self.dry_run, dry_run=self.dry_run,
force=self.force) force=self.force)

View file

@ -87,6 +87,7 @@ class upload_docs(Command):
content_type, body = encode_multipart(fields, files) content_type, body = encode_multipart(fields, files)
credentials = self.username + ':' + self.password credentials = self.username + ':' + self.password
# FIXME should use explicit encoding
auth = b"Basic " + base64.encodebytes(credentials.encode()).strip() auth = b"Basic " + base64.encodebytes(credentials.encode()).strip()
logger.info("Submitting documentation to %s", self.repository) logger.info("Submitting documentation to %s", self.repository)

View file

@ -127,7 +127,7 @@ class UnixCCompiler(CCompiler):
executables['ranlib'] = ["ranlib"] executables['ranlib'] = ["ranlib"]
# Needed for the filename generation methods provided by the base # Needed for the filename generation methods provided by the base
# class, CCompiler. NB. whoever instantiates/uses a particular # class, CCompiler. XXX whoever instantiates/uses a particular
# UnixCCompiler instance should set 'shared_lib_ext' -- we set a # UnixCCompiler instance should set 'shared_lib_ext' -- we set a
# reasonable common default here, but it's not necessarily used on all # reasonable common default here, but it's not necessarily used on all
# Unices! # Unices!

View file

@ -1,12 +1,13 @@
"""PEP 376 implementation.""" """PEP 376 implementation."""
import io
import os import os
import re import re
import csv import csv
import sys import sys
import zipimport import zipimport
from io import StringIO
from hashlib import md5 from hashlib import md5
from packaging import logger from packaging import logger
from packaging.errors import PackagingError from packaging.errors import PackagingError
from packaging.version import suggest_normalized_version, VersionPredicate from packaging.version import suggest_normalized_version, VersionPredicate
@ -173,7 +174,7 @@ class Distribution:
def get_resource_path(self, relative_path): def get_resource_path(self, relative_path):
with self.get_distinfo_file('RESOURCES') as resources_file: with self.get_distinfo_file('RESOURCES') as resources_file:
resources_reader = csv.reader(resources_file, delimiter=',', resources_reader = csv.reader(resources_file, delimiter=',',
lineterminator='\n') lineterminator='\n')
for relative, destination in resources_reader: for relative, destination in resources_reader:
if relative == relative_path: if relative == relative_path:
return destination return destination
@ -334,7 +335,7 @@ class EggInfoDistribution:
else: else:
# FIXME handle the case where zipfile is not available # FIXME handle the case where zipfile is not available
zipf = zipimport.zipimporter(path) zipf = zipimport.zipimporter(path)
fileobj = io.StringIO( fileobj = StringIO(
zipf.get_data('EGG-INFO/PKG-INFO').decode('utf8')) zipf.get_data('EGG-INFO/PKG-INFO').decode('utf8'))
self.metadata = Metadata(fileobj=fileobj) self.metadata = Metadata(fileobj=fileobj)
try: try:

View file

@ -6,17 +6,15 @@ packaging.tests package. Each test module has a name starting with
to return an initialized unittest.TestSuite instance. to return an initialized unittest.TestSuite instance.
Utility code is included in packaging.tests.support. Utility code is included in packaging.tests.support.
"""
# Put this text back for the backport Always import unittest from this module: it will be unittest from the
#Always import unittest from this module, it will be the right version standard library for packaging tests and unittest2 for distutils2 tests.
#(standard library unittest for 3.2 and higher, third-party unittest2 """
#elease for older versions).
import os import os
import sys import sys
import unittest import unittest
from test.support import TESTFN from io import StringIO
# XXX move helpers to support, add tests for them, remove things that # XXX move helpers to support, add tests for them, remove things that
# duplicate test.support (or keep them for the backport; needs thinking) # duplicate test.support (or keep them for the backport; needs thinking)
@ -115,9 +113,8 @@ def reap_children():
def captured_stdout(func, *args, **kw): def captured_stdout(func, *args, **kw):
import io
orig_stdout = getattr(sys, 'stdout') orig_stdout = getattr(sys, 'stdout')
setattr(sys, 'stdout', io.StringIO()) setattr(sys, 'stdout', StringIO())
try: try:
res = func(*args, **kw) res = func(*args, **kw)
sys.stdout.seek(0) sys.stdout.seek(0)

View file

@ -14,6 +14,8 @@ def test_main():
start_dir = os.path.dirname(__file__) start_dir = os.path.dirname(__file__)
top_dir = os.path.dirname(os.path.dirname(start_dir)) top_dir = os.path.dirname(os.path.dirname(start_dir))
test_loader = unittest.TestLoader() test_loader = unittest.TestLoader()
# XXX find out how to use unittest.main, to get command-line options
# (failfast, catch, etc.)
run_unittest(test_loader.discover(start_dir, top_level_dir=top_dir)) run_unittest(test_loader.discover(start_dir, top_level_dir=top_dir))
finally: finally:
reap_children() reap_children()

View file

@ -40,6 +40,7 @@ from xmlrpc.server import SimpleXMLRPCServer
from packaging.tests import unittest from packaging.tests import unittest
PYPI_DEFAULT_STATIC_PATH = os.path.join( PYPI_DEFAULT_STATIC_PATH = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'pypiserver') os.path.dirname(os.path.abspath(__file__)), 'pypiserver')
@ -219,7 +220,7 @@ class PyPIRequestHandler(SimpleHTTPRequestHandler):
relative_path += "index.html" relative_path += "index.html"
if relative_path.endswith('.tar.gz'): if relative_path.endswith('.tar.gz'):
with open(fs_path + relative_path, 'br') as file: with open(fs_path + relative_path, 'rb') as file:
data = file.read() data = file.read()
headers = [('Content-type', 'application/x-gtar')] headers = [('Content-type', 'application/x-gtar')]
else: else:
@ -260,8 +261,8 @@ class PyPIRequestHandler(SimpleHTTPRequestHandler):
self.send_header(header, value) self.send_header(header, value)
self.end_headers() self.end_headers()
if type(data) is str: if isinstance(data, str):
data = data.encode() data = data.encode('utf-8')
self.wfile.write(data) self.wfile.write(data)

View file

@ -4,6 +4,7 @@ import site
import sysconfig import sysconfig
import textwrap import textwrap
from io import StringIO from io import StringIO
from sysconfig import _CONFIG_VARS
from packaging.dist import Distribution from packaging.dist import Distribution
from packaging.errors import (UnknownFileError, CompileError, from packaging.errors import (UnknownFileError, CompileError,
PackagingPlatformError) PackagingPlatformError)
@ -78,7 +79,6 @@ class BuildExtTestCase(support.TempdirManager,
old = sys.platform old = sys.platform
sys.platform = 'sunos' # fooling finalize_options sys.platform = 'sunos' # fooling finalize_options
from sysconfig import _CONFIG_VARS
old_var = _CONFIG_VARS.get('Py_ENABLE_SHARED') old_var = _CONFIG_VARS.get('Py_ENABLE_SHARED')
_CONFIG_VARS['Py_ENABLE_SHARED'] = 1 _CONFIG_VARS['Py_ENABLE_SHARED'] = 1

View file

@ -40,7 +40,7 @@ class InstallLibTestCase(support.TempdirManager,
cmd.finalize_options() cmd.finalize_options()
self.assertEqual(cmd.optimize, 2) self.assertEqual(cmd.optimize, 2)
@unittest.skipIf(no_bytecode, 'byte-compile not supported') @unittest.skipIf(no_bytecode, 'byte-compile disabled')
def test_byte_compile(self): def test_byte_compile(self):
pkg_dir, dist = self.create_dist() pkg_dir, dist = self.create_dist()
cmd = install_lib(dist) cmd = install_lib(dist)

View file

@ -1,6 +1,5 @@
"""Tests for packaging.command.upload.""" """Tests for packaging.command.upload."""
import os import os
import sys
from packaging.command.upload import upload from packaging.command.upload import upload
from packaging.dist import Distribution from packaging.dist import Distribution
@ -103,22 +102,23 @@ class UploadTestCase(support.TempdirManager, support.EnvironRestorer,
command, pyversion, filename = 'xxx', '3.3', path command, pyversion, filename = 'xxx', '3.3', path
dist_files = [(command, pyversion, filename)] dist_files = [(command, pyversion, filename)]
# lets run it # let's run it
pkg_dir, dist = self.create_dist(dist_files=dist_files, author='dédé') dist = self.create_dist(dist_files=dist_files, author='dédé')[1]
cmd = upload(dist) cmd = upload(dist)
cmd.ensure_finalized() cmd.ensure_finalized()
cmd.repository = self.pypi.full_address cmd.repository = self.pypi.full_address
cmd.run() cmd.run()
# what did we send ? # what did we send?
handler, request_data = self.pypi.requests[-1] handler, request_data = self.pypi.requests[-1]
headers = handler.headers headers = handler.headers
#self.assertIn('dédé', str(request_data)) self.assertIn('dédé'.encode('utf-8'), request_data)
self.assertIn(b'xxx', request_data) self.assertIn(b'xxx', request_data)
self.assertEqual(int(headers['content-length']), len(request_data)) self.assertEqual(int(headers['content-length']), len(request_data))
self.assertLess(int(headers['content-length']), 2500) self.assertLess(int(headers['content-length']), 2500)
self.assertTrue(headers['content-type'].startswith('multipart/form-data')) self.assertTrue(headers['content-type'].startswith(
'multipart/form-data'))
self.assertEqual(handler.command, 'POST') self.assertEqual(handler.command, 'POST')
self.assertNotIn('\n', headers['authorization']) self.assertNotIn('\n', headers['authorization'])
@ -132,20 +132,16 @@ class UploadTestCase(support.TempdirManager, support.EnvironRestorer,
self.write_file(os.path.join(docs_path, "index.html"), "yellow") self.write_file(os.path.join(docs_path, "index.html"), "yellow")
self.write_file(self.rc, PYPIRC) self.write_file(self.rc, PYPIRC)
# lets run it # let's run it
pkg_dir, dist = self.create_dist(dist_files=dist_files, author='dédé') dist = self.create_dist(dist_files=dist_files, author='dédé')[1]
cmd = upload(dist) cmd = upload(dist)
cmd.get_finalized_command("build").run() cmd.get_finalized_command("build").run()
cmd.upload_docs = True cmd.upload_docs = True
cmd.ensure_finalized() cmd.ensure_finalized()
cmd.repository = self.pypi.full_address cmd.repository = self.pypi.full_address
prev_dir = os.getcwd() os.chdir(self.tmp_dir)
try: cmd.run()
os.chdir(self.tmp_dir)
cmd.run()
finally:
os.chdir(prev_dir)
handler, request_data = self.pypi.requests[-1] handler, request_data = self.pypi.requests[-1]
action, name, content = request_data.split( action, name, content = request_data.split(

View file

@ -1,6 +1,5 @@
"""Tests for packaging.command.upload_docs.""" """Tests for packaging.command.upload_docs."""
import os import os
import sys
import shutil import shutil
import zipfile import zipfile
try: try:
@ -52,34 +51,27 @@ class UploadDocsTestCase(support.TempdirManager,
def test_default_uploaddir(self): def test_default_uploaddir(self):
sandbox = self.mkdtemp() sandbox = self.mkdtemp()
previous = os.getcwd()
os.chdir(sandbox) os.chdir(sandbox)
try: os.mkdir("build")
os.mkdir("build") self.prepare_sample_dir("build")
self.prepare_sample_dir("build") self.cmd.ensure_finalized()
self.cmd.ensure_finalized() self.assertEqual(self.cmd.upload_dir, os.path.join("build", "docs"))
self.assertEqual(self.cmd.upload_dir, os.path.join("build", "docs"))
finally:
os.chdir(previous)
def test_default_uploaddir_looks_for_doc_also(self): def test_default_uploaddir_looks_for_doc_also(self):
sandbox = self.mkdtemp() sandbox = self.mkdtemp()
previous = os.getcwd()
os.chdir(sandbox) os.chdir(sandbox)
try: os.mkdir("build")
os.mkdir("build") self.prepare_sample_dir("build")
self.prepare_sample_dir("build") os.rename(os.path.join("build", "docs"), os.path.join("build", "doc"))
os.rename(os.path.join("build", "docs"), os.path.join("build", "doc")) self.cmd.ensure_finalized()
self.cmd.ensure_finalized() self.assertEqual(self.cmd.upload_dir, os.path.join("build", "doc"))
self.assertEqual(self.cmd.upload_dir, os.path.join("build", "doc"))
finally:
os.chdir(previous)
def prepare_sample_dir(self, sample_dir=None): def prepare_sample_dir(self, sample_dir=None):
if sample_dir is None: if sample_dir is None:
sample_dir = self.mkdtemp() sample_dir = self.mkdtemp()
os.mkdir(os.path.join(sample_dir, "docs")) os.mkdir(os.path.join(sample_dir, "docs"))
self.write_file(os.path.join(sample_dir, "docs", "index.html"), "Ce mortel ennui") self.write_file(os.path.join(sample_dir, "docs", "index.html"),
"Ce mortel ennui")
self.write_file(os.path.join(sample_dir, "index.html"), "Oh la la") self.write_file(os.path.join(sample_dir, "index.html"), "Oh la la")
return sample_dir return sample_dir
@ -108,9 +100,8 @@ class UploadDocsTestCase(support.TempdirManager,
self.assertTrue(handler.headers['content-type'] self.assertTrue(handler.headers['content-type']
.startswith('multipart/form-data;')) .startswith('multipart/form-data;'))
action, name, version, content =\ action, name, version, content = request_data.split(
request_data.split("----------------GHSKFJDLGDS7543FJKLFHRE75642756743254".encode())[1:5] b'----------------GHSKFJDLGDS7543FJKLFHRE75642756743254')[1:5]
# check that we picked the right chunks # check that we picked the right chunks
self.assertIn(b'name=":action"', action) self.assertIn(b'name=":action"', action)
@ -126,27 +117,25 @@ class UploadDocsTestCase(support.TempdirManager,
@unittest.skipIf(_ssl is None, 'Needs SSL support') @unittest.skipIf(_ssl is None, 'Needs SSL support')
def test_https_connection(self): def test_https_connection(self):
https_called = False self.https_called = False
self.addCleanup(
orig_https = upload_docs_mod.http.client.HTTPSConnection setattr, upload_docs_mod.http.client, 'HTTPSConnection',
upload_docs_mod.http.client.HTTPSConnection)
def https_conn_wrapper(*args): def https_conn_wrapper(*args):
nonlocal https_called self.https_called = True
https_called = True
# the testing server is http # the testing server is http
return upload_docs_mod.http.client.HTTPConnection(*args) return upload_docs_mod.http.client.HTTPConnection(*args)
upload_docs_mod.http.client.HTTPSConnection = https_conn_wrapper upload_docs_mod.http.client.HTTPSConnection = https_conn_wrapper
try:
self.prepare_command()
self.cmd.run()
self.assertFalse(https_called)
self.cmd.repository = self.cmd.repository.replace("http", "https") self.prepare_command()
self.cmd.run() self.cmd.run()
self.assertTrue(https_called) self.assertFalse(self.https_called)
finally:
upload_docs_mod.http.client.HTTPSConnection = orig_https self.cmd.repository = self.cmd.repository.replace("http", "https")
self.cmd.run()
self.assertTrue(self.https_called)
def test_handling_response(self): def test_handling_response(self):
self.pypi.default_response_status = '403 Forbidden' self.pypi.default_response_status = '403 Forbidden'
@ -155,7 +144,8 @@ class UploadDocsTestCase(support.TempdirManager,
self.assertIn('Upload failed (403): Forbidden', self.get_logs()[-1]) self.assertIn('Upload failed (403): Forbidden', self.get_logs()[-1])
self.pypi.default_response_status = '301 Moved Permanently' self.pypi.default_response_status = '301 Moved Permanently'
self.pypi.default_response_headers.append(("Location", "brand_new_location")) self.pypi.default_response_headers.append(
("Location", "brand_new_location"))
self.cmd.run() self.cmd.run()
self.assertIn('brand_new_location', self.get_logs()[-1]) self.assertIn('brand_new_location', self.get_logs()[-1])
@ -185,6 +175,7 @@ class UploadDocsTestCase(support.TempdirManager,
self.assertTrue(record, "should report the response") self.assertTrue(record, "should report the response")
self.assertIn(self.pypi.default_response_data, record) self.assertIn(self.pypi.default_response_data, record)
def test_suite(): def test_suite():
return unittest.makeSuite(UploadDocsTestCase) return unittest.makeSuite(UploadDocsTestCase)

View file

@ -1,8 +1,8 @@
"""Tests for packaging.create.""" """Tests for packaging.create."""
import io
import os import os
import sys import sys
import sysconfig import sysconfig
from io import StringIO
from textwrap import dedent from textwrap import dedent
from packaging.create import MainProgram, ask_yn, ask, main from packaging.create import MainProgram, ask_yn, ask, main
@ -20,8 +20,8 @@ class CreateTestCase(support.TempdirManager,
super(CreateTestCase, self).setUp() super(CreateTestCase, self).setUp()
self._stdin = sys.stdin # TODO use Inputs self._stdin = sys.stdin # TODO use Inputs
self._stdout = sys.stdout self._stdout = sys.stdout
sys.stdin = io.StringIO() sys.stdin = StringIO()
sys.stdout = io.StringIO() sys.stdout = StringIO()
self._cwd = os.getcwd() self._cwd = os.getcwd()
self.wdir = self.mkdtemp() self.wdir = self.mkdtemp()
os.chdir(self.wdir) os.chdir(self.wdir)
@ -135,7 +135,8 @@ class CreateTestCase(support.TempdirManager,
sys.stdin.seek(0) sys.stdin.seek(0)
main() main()
with open(os.path.join(self.wdir, 'setup.cfg'), encoding='utf-8') as fp: path = os.path.join(self.wdir, 'setup.cfg')
with open(path, encoding='utf-8') as fp:
contents = fp.read() contents = fp.read()
self.assertEqual(contents, dedent("""\ self.assertEqual(contents, dedent("""\
@ -210,7 +211,9 @@ ho, baby!
sys.stdin.seek(0) sys.stdin.seek(0)
# FIXME Out of memory error. # FIXME Out of memory error.
main() main()
with open(os.path.join(self.wdir, 'setup.cfg'), encoding='utf-8') as fp:
path = os.path.join(self.wdir, 'setup.cfg')
with open(path, encoding='utf-8') as fp:
contents = fp.read() contents = fp.read()
self.assertEqual(contents, dedent("""\ self.assertEqual(contents, dedent("""\

View file

@ -49,8 +49,8 @@ class FakeDistsMixin:
# distributions # distributions
tmpdir = tempfile.mkdtemp() tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir) self.addCleanup(shutil.rmtree, tmpdir)
self.fake_dists_path = os.path.join(tmpdir, 'fake_dists') self.fake_dists_path = os.path.realpath(
self.fake_dists_path = os.path.realpath(self.fake_dists_path) os.path.join(tmpdir, 'fake_dists'))
fake_dists_src = os.path.abspath( fake_dists_src = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'fake_dists')) os.path.join(os.path.dirname(__file__), 'fake_dists'))
shutil.copytree(fake_dists_src, self.fake_dists_path) shutil.copytree(fake_dists_src, self.fake_dists_path)
@ -58,6 +58,7 @@ class FakeDistsMixin:
# back (to avoid getting a read-only copy of a read-only file). we # back (to avoid getting a read-only copy of a read-only file). we
# could pass a custom copy_function to change the mode of files, but # could pass a custom copy_function to change the mode of files, but
# shutil gives no control over the mode of directories :( # shutil gives no control over the mode of directories :(
# see http://bugs.python.org/issue1666318
for root, dirs, files in os.walk(self.fake_dists_path): for root, dirs, files in os.walk(self.fake_dists_path):
os.chmod(root, 0o755) os.chmod(root, 0o755)
for f in files: for f in files:

View file

@ -1,10 +1,11 @@
"""Tests for packaging.depgraph """ """Tests for packaging.depgraph """
import io
import os import os
import re import re
import sys import sys
import packaging.database from io import StringIO
from packaging import depgraph from packaging import depgraph
from packaging.database import get_distribution, enable_cache, disable_cache
from packaging.tests import unittest, support from packaging.tests import unittest, support
from packaging.tests.support import requires_zlib from packaging.tests.support import requires_zlib
@ -30,13 +31,13 @@ class DepGraphTestCase(support.LoggingCatcher,
path = os.path.abspath(path) path = os.path.abspath(path)
sys.path.insert(0, path) sys.path.insert(0, path)
self.addCleanup(sys.path.remove, path) self.addCleanup(sys.path.remove, path)
self.addCleanup(packaging.database.enable_cache) self.addCleanup(enable_cache)
packaging.database.disable_cache() disable_cache()
def test_generate_graph(self): def test_generate_graph(self):
dists = [] dists = []
for name in self.DISTROS_DIST: for name in self.DISTROS_DIST:
dist = packaging.database.get_distribution(name) dist = get_distribution(name)
self.assertNotEqual(dist, None) self.assertNotEqual(dist, None)
dists.append(dist) dists.append(dist)
@ -61,7 +62,7 @@ class DepGraphTestCase(support.LoggingCatcher,
def test_generate_graph_egg(self): def test_generate_graph_egg(self):
dists = [] dists = []
for name in self.DISTROS_DIST + self.DISTROS_EGG: for name in self.DISTROS_DIST + self.DISTROS_EGG:
dist = packaging.database.get_distribution(name, use_egg_info=True) dist = get_distribution(name, use_egg_info=True)
self.assertNotEqual(dist, None) self.assertNotEqual(dist, None)
dists.append(dist) dists.append(dist)
@ -104,7 +105,7 @@ class DepGraphTestCase(support.LoggingCatcher,
def test_dependent_dists(self): def test_dependent_dists(self):
dists = [] dists = []
for name in self.DISTROS_DIST: for name in self.DISTROS_DIST:
dist = packaging.database.get_distribution(name) dist = get_distribution(name)
self.assertNotEqual(dist, None) self.assertNotEqual(dist, None)
dists.append(dist) dists.append(dist)
@ -123,7 +124,7 @@ class DepGraphTestCase(support.LoggingCatcher,
def test_dependent_dists_egg(self): def test_dependent_dists_egg(self):
dists = [] dists = []
for name in self.DISTROS_DIST + self.DISTROS_EGG: for name in self.DISTROS_DIST + self.DISTROS_EGG:
dist = packaging.database.get_distribution(name, use_egg_info=True) dist = get_distribution(name, use_egg_info=True)
self.assertNotEqual(dist, None) self.assertNotEqual(dist, None)
dists.append(dist) dists.append(dist)
@ -158,12 +159,12 @@ class DepGraphTestCase(support.LoggingCatcher,
dists = [] dists = []
for name in self.DISTROS_DIST + self.DISTROS_EGG: for name in self.DISTROS_DIST + self.DISTROS_EGG:
dist = packaging.database.get_distribution(name, use_egg_info=True) dist = get_distribution(name, use_egg_info=True)
self.assertNotEqual(dist, None) self.assertNotEqual(dist, None)
dists.append(dist) dists.append(dist)
graph = depgraph.generate_graph(dists) graph = depgraph.generate_graph(dists)
buf = io.StringIO() buf = StringIO()
depgraph.graph_to_dot(graph, buf) depgraph.graph_to_dot(graph, buf)
buf.seek(0) buf.seek(0)
matches = [] matches = []
@ -189,12 +190,12 @@ class DepGraphTestCase(support.LoggingCatcher,
dists = [] dists = []
for name in self.DISTROS_DIST + self.DISTROS_EGG: for name in self.DISTROS_DIST + self.DISTROS_EGG:
dist = packaging.database.get_distribution(name, use_egg_info=True) dist = get_distribution(name, use_egg_info=True)
self.assertNotEqual(dist, None) self.assertNotEqual(dist, None)
dists.append(dist) dists.append(dist)
graph = depgraph.generate_graph(dists) graph = depgraph.generate_graph(dists)
buf = io.StringIO() buf = StringIO()
depgraph.graph_to_dot(graph, buf, skip_disconnected=False) depgraph.graph_to_dot(graph, buf, skip_disconnected=False)
buf.seek(0) buf.seek(0)
lines = buf.readlines() lines = buf.readlines()
@ -250,12 +251,12 @@ class DepGraphTestCase(support.LoggingCatcher,
dists = [] dists = []
for name in self.DISTROS_DIST + self.DISTROS_EGG + self.BAD_EGGS: for name in self.DISTROS_DIST + self.DISTROS_EGG + self.BAD_EGGS:
dist = packaging.database.get_distribution(name, use_egg_info=True) dist = get_distribution(name, use_egg_info=True)
self.assertNotEqual(dist, None) self.assertNotEqual(dist, None)
dists.append(dist) dists.append(dist)
graph = depgraph.generate_graph(dists) graph = depgraph.generate_graph(dists)
buf = io.StringIO() buf = StringIO()
depgraph.graph_to_dot(graph, buf) depgraph.graph_to_dot(graph, buf)
buf.seek(0) buf.seek(0)
matches = [] matches = []
@ -273,7 +274,7 @@ class DepGraphTestCase(support.LoggingCatcher,
def test_repr(self): def test_repr(self):
dists = [] dists = []
for name in self.DISTROS_DIST + self.DISTROS_EGG + self.BAD_EGGS: for name in self.DISTROS_DIST + self.DISTROS_EGG + self.BAD_EGGS:
dist = packaging.database.get_distribution(name, use_egg_info=True) dist = get_distribution(name, use_egg_info=True)
self.assertNotEqual(dist, None) self.assertNotEqual(dist, None)
dists.append(dist) dists.append(dist)
@ -282,7 +283,7 @@ class DepGraphTestCase(support.LoggingCatcher,
@requires_zlib @requires_zlib
def test_main(self): def test_main(self):
tempout = io.StringIO() tempout = StringIO()
old = sys.stdout old = sys.stdout
sys.stdout = tempout sys.stdout = tempout
oldargv = sys.argv[:] oldargv = sys.argv[:]

View file

@ -3,13 +3,14 @@ import os
import sys import sys
import logging import logging
import textwrap import textwrap
import packaging.dist import packaging.dist
from packaging.dist import Distribution from packaging.dist import Distribution
from packaging.command import set_command from packaging.command import set_command
from packaging.command.cmd import Command from packaging.command.cmd import Command
from packaging.errors import PackagingModuleError, PackagingOptionError from packaging.errors import PackagingModuleError, PackagingOptionError
from packaging.tests import TESTFN, captured_stdout from packaging.tests import captured_stdout
from packaging.tests import support, unittest from packaging.tests import support, unittest
from packaging.tests.support import create_distribution from packaging.tests.support import create_distribution
from test.support import unload from test.support import unload
@ -48,12 +49,13 @@ class DistributionTestCase(support.TempdirManager,
@unittest.skip('needs to be updated') @unittest.skip('needs to be updated')
def test_debug_mode(self): def test_debug_mode(self):
self.addCleanup(os.unlink, TESTFN) tmpdir = self.mkdtemp()
with open(TESTFN, "w") as f: setupcfg = os.path.join(tmpdir, 'setup.cfg')
with open(setupcfg, "w") as f:
f.write("[global]\n") f.write("[global]\n")
f.write("command_packages = foo.bar, splat") f.write("command_packages = foo.bar, splat")
files = [TESTFN] files = [setupcfg]
sys.argv.append("build") sys.argv.append("build")
__, stdout = captured_stdout(create_distribution, files) __, stdout = captured_stdout(create_distribution, files)
self.assertEqual(stdout, '') self.assertEqual(stdout, '')

View file

@ -1,8 +1,8 @@
"""Tests for the packaging.install module.""" """Tests for the packaging.install module."""
import os import os
import logging import logging
from sysconfig import is_python_build
from tempfile import mkstemp from tempfile import mkstemp
from sysconfig import is_python_build
from packaging import install from packaging import install
from packaging.pypi.xmlrpc import Client from packaging.pypi.xmlrpc import Client

View file

@ -1,13 +1,12 @@
"""Tests for packaging.command.bdist.""" """Tests for packaging.command.bdist."""
import sys
import urllib.request import urllib.request
import urllib.parse import urllib.parse
import urllib.error import urllib.error
try: try:
import threading import threading
from packaging.tests.pypi_server import PyPIServer, PYPI_DEFAULT_STATIC_PATH from packaging.tests.pypi_server import (
PyPIServer, PYPI_DEFAULT_STATIC_PATH)
except ImportError: except ImportError:
threading = None threading = None
PyPIServer = None PyPIServer = None
@ -32,18 +31,19 @@ class PyPIServerTest(unittest.TestCase):
headers = {"X-test-header": "Mister Iceberg"} headers = {"X-test-header": "Mister Iceberg"}
request = urllib.request.Request(server.full_address, data, headers) request = urllib.request.Request(
server.full_address, data, headers)
urllib.request.urlopen(request) urllib.request.urlopen(request)
self.assertEqual(len(server.requests), 1) self.assertEqual(len(server.requests), 1)
handler, request_data = server.requests[-1] handler, request_data = server.requests[-1]
self.assertIn(data, request_data) self.assertIn(data, request_data)
self.assertIn("x-test-header", handler.headers) self.assertIn("x-test-header", handler.headers)
self.assertEqual(handler.headers["x-test-header"], "Mister Iceberg") self.assertEqual(handler.headers["x-test-header"],
"Mister Iceberg")
finally: finally:
server.stop() server.stop()
def test_serve_static_content(self): def test_serve_static_content(self):
# PYPI Mocked server can serve static content from disk. # PYPI Mocked server can serve static content from disk.
@ -74,7 +74,8 @@ class PyPIServerTest(unittest.TestCase):
self.assertTrue(uses_local_files_for(server, "/simple/index.html")) self.assertTrue(uses_local_files_for(server, "/simple/index.html"))
# and another one in another root path # and another one in another root path
self.assertTrue(uses_local_files_for(server, "/external/index.html")) self.assertTrue(uses_local_files_for(server,
"/external/index.html"))
finally: finally:
server.stop() server.stop()

View file

@ -2,11 +2,10 @@
import os import os
import sys import sys
import shutil
from io import StringIO from io import StringIO
from packaging import install from packaging import install
from packaging.tests import unittest, support, TESTFN from packaging.tests import unittest, support
from packaging.run import main from packaging.run import main
from test.script_helper import assert_python_ok from test.script_helper import assert_python_ok
@ -35,28 +34,14 @@ class RunTestCase(support.TempdirManager,
def setUp(self): def setUp(self):
super(RunTestCase, self).setUp() super(RunTestCase, self).setUp()
self.old_stdout = sys.stdout self.old_stdout = sys.stdout
self.cleanup_testfn()
self.old_argv = sys.argv, sys.argv[:] self.old_argv = sys.argv, sys.argv[:]
def tearDown(self): def tearDown(self):
sys.stdout = self.old_stdout sys.stdout = self.old_stdout
self.cleanup_testfn()
sys.argv = self.old_argv[0] sys.argv = self.old_argv[0]
sys.argv[:] = self.old_argv[1] sys.argv[:] = self.old_argv[1]
super(RunTestCase, self).tearDown() super(RunTestCase, self).tearDown()
def cleanup_testfn(self):
path = TESTFN
if os.path.isfile(path):
os.remove(path)
elif os.path.isdir(path):
shutil.rmtree(path)
def write_setup(self, text, path=TESTFN):
with open(path, "w") as fp:
fp.write(text)
return path
# TODO restore the tests removed six months ago and port them to pysetup # TODO restore the tests removed six months ago and port them to pysetup
def test_install(self): def test_install(self):

View file

@ -15,7 +15,7 @@ from packaging.errors import (
from packaging import util from packaging import util
from packaging.dist import Distribution from packaging.dist import Distribution
from packaging.util import ( from packaging.util import (
convert_path, change_root, split_quoted, strtobool, convert_path, change_root, split_quoted, strtobool, run_2to3,
get_compiler_versions, _MAC_OS_X_LD_VERSION, byte_compile, find_packages, get_compiler_versions, _MAC_OS_X_LD_VERSION, byte_compile, find_packages,
spawn, get_pypirc_path, generate_pypirc, read_pypirc, resolve_name, iglob, spawn, get_pypirc_path, generate_pypirc, read_pypirc, resolve_name, iglob,
RICH_GLOB, egginfo_to_distinfo, is_setuptools, is_distutils, is_packaging, RICH_GLOB, egginfo_to_distinfo, is_setuptools, is_distutils, is_packaging,
@ -374,7 +374,7 @@ class UtilTestCase(support.EnvironRestorer,
res = find_packages([root], ['pkg1.pkg2']) res = find_packages([root], ['pkg1.pkg2'])
self.assertEqual(set(res), set(['pkg1', 'pkg5', 'pkg1.pkg3', self.assertEqual(set(res), set(['pkg1', 'pkg5', 'pkg1.pkg3',
'pkg1.pkg3.pkg6'])) 'pkg1.pkg3.pkg6']))
def test_resolve_name(self): def test_resolve_name(self):
self.assertIs(str, resolve_name('builtins.str')) self.assertIs(str, resolve_name('builtins.str'))
@ -416,7 +416,6 @@ class UtilTestCase(support.EnvironRestorer,
file_handle.write(content) file_handle.write(content)
file_handle.flush() file_handle.flush()
file_handle.seek(0) file_handle.seek(0)
from packaging.util import run_2to3
run_2to3([file_name]) run_2to3([file_name])
new_content = "".join(file_handle.read()) new_content = "".join(file_handle.read())
file_handle.close() file_handle.close()
@ -432,7 +431,6 @@ class UtilTestCase(support.EnvironRestorer,
file_handle.write(content) file_handle.write(content)
file_handle.flush() file_handle.flush()
file_handle.seek(0) file_handle.seek(0)
from packaging.util import run_2to3
run_2to3([file_name], doctests_only=True) run_2to3([file_name], doctests_only=True)
new_content = "".join(file_handle.readlines()) new_content = "".join(file_handle.readlines())
file_handle.close() file_handle.close()