diff --git a/Lib/distutils/command/upload.py b/Lib/distutils/command/upload.py --- a/Lib/distutils/command/upload.py +++ b/Lib/distutils/command/upload.py @@ -165,15 +165,15 @@ class upload(PyPIRCCommand): # We can't use urllib since we need to send the Basic # auth right with the first request # TODO(jhylton): Can we fix urllib? - schema, netloc, url, params, query, fragments = \ + scheme, netloc, url, params, query, fragments = \ urllib.parse.urlparse(self.repository) assert not params and not query and not fragments - if schema == 'http': + if scheme == 'http': http = httpclient.HTTPConnection(netloc) - elif schema == 'https': + elif scheme == 'https': http = httpclient.HTTPSConnection(netloc) else: - raise AssertionError("unsupported schema "+schema) + raise AssertionError("unsupported scheme %r " % scheme) data = '' loglevel = log.INFO diff --git a/Lib/distutils/config.py b/Lib/distutils/config.py --- a/Lib/distutils/config.py +++ b/Lib/distutils/config.py @@ -60,7 +60,7 @@ class PyPIRCCommand(Command): if os.path.exists(rc): self.announce('Using PyPI login from %s' % rc) repository = self.repository or self.DEFAULT_REPOSITORY - realm = self.realm or self.DEFAULT_REALM + #realm = self.realm or self.DEFAULT_REALM config = ConfigParser() config.read(rc) diff --git a/Lib/distutils/tests/test_upload.py b/Lib/distutils/tests/test_upload.py --- a/Lib/distutils/tests/test_upload.py +++ b/Lib/distutils/tests/test_upload.py @@ -2,13 +2,32 @@ import os import unittest import http.client as httpclient +from textwrap import dedent from test.support import run_unittest +from test.script_helper import assert_python_ok from distutils.command.upload import upload from distutils.core import Distribution from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase +try: + import zlib +except ImportError: + zlib = None + + +PYPIRC_CUSTOM_SERVER = """ +[distutils] +index-servers = + acme + +[acme] +username = me +password = 42 +repository = http://acme.invalid/ +""" + PYPIRC_LONG_PASSWORD = """\ [distutils] @@ -27,7 +46,6 @@ realm:acme repository:http://another.pypi/ """ - PYPIRC_NOPASSWORD = """\ [distutils] @@ -37,11 +55,14 @@ index-servers = [server1] username:me """ + + class Response(object): def __init__(self, status=200, reason='OK'): self.status = status self.reason = reason + class FakeConnection(object): def __init__(self): @@ -68,16 +89,17 @@ class FakeConnection(object): def getresponse(self): return Response() -class uploadTestCase(PyPIRCCommandTestCase): + +class UploadTestCase(PyPIRCCommandTestCase): def setUp(self): - super(uploadTestCase, self).setUp() + super(UploadTestCase, self).setUp() self.old_class = httpclient.HTTPConnection self.conn = httpclient.HTTPConnection = FakeConnection() def tearDown(self): httpclient.HTTPConnection = self.old_class - super(uploadTestCase, self).tearDown() + super(UploadTestCase, self).tearDown() def test_finalize_options(self): @@ -86,10 +108,10 @@ class uploadTestCase(PyPIRCCommandTestCa dist = Distribution() cmd = upload(dist) cmd.finalize_options() - for attr, waited in (('username', 'me'), ('password', 'secret'), - ('realm', 'pypi'), - ('repository', 'http://pypi.python.org/pypi')): - self.assertEqual(getattr(cmd, attr), waited) + for attr, expected in (('username', 'me'), ('password', 'secret'), + ('realm', 'pypi'), + ('repository', 'http://pypi.python.org/pypi')): + self.assertEqual(getattr(cmd, attr), expected) def test_saved_password(self): # file with no password @@ -116,23 +138,60 @@ class uploadTestCase(PyPIRCCommandTestCa dist_files = [(command, pyversion, filename)] self.write_file(self.rc, PYPIRC_LONG_PASSWORD) - # lets run it - pkg_dir, dist = self.create_dist(dist_files=dist_files) + # let's run it + dist = self.create_dist(dist_files=dist_files, author='dédé')[1] cmd = upload(dist) cmd.ensure_finalized() cmd.run() - # what did we send ? + # what did we send? headers = dict(self.conn.headers) - self.assertEqual(headers['Content-length'], '2087') - self.assertTrue(headers['Content-type'].startswith('multipart/form-data')) - self.assertFalse('\n' in headers['Authorization']) + self.assertEqual(headers['Content-length'], '2086') + self.assertTrue(headers['Content-type'].startswith( + 'multipart/form-data')) + self.assertNotIn('\n', headers['Authorization']) + self.assertEqual(self.conn.requests, [('POST', '/pypi')]) + self.assertIn('dédé'.encode('utf-8'), self.conn.body) + self.assertIn(b'xxx', self.conn.body) - self.assertEqual(self.conn.requests, [('POST', '/pypi')]) - self.assertTrue((b'xxx') in self.conn.body) + def _test_upload_r(self, pypirc, expected): + self.write_file(self.rc, pypirc) + project_dir = self.mkdtemp() + os.chdir(project_dir) + self.write_file('setup.py', dedent("""\ + from distutils.core import setup + + setup(name='goats', + version='3.0', + author='merwok', + url='http://example.org/goats', + author_email='merwok@example.org') + """)) + # shave a few yaks required by sdist + self.write_file('MANIFEST.in', '') + self.write_file('README', '') + + # the network connection will fail, but we'll get enough + # info to be sure the correct repo was used + # (setup.py will exit with 0, *sigh*) + out = assert_python_ok('setup.py', 'sdist', '--formats=zip', + 'upload', '-r', 'server2')[1] + lastlog = out.split(b'\n')[-2] + self.assertEqual(lastlog, expected) + + @unittest.skipUnless(zlib, 'needs zlib') + def test_upload_r_1(self): + expected = b'Submitting dist/goats-3.0.zip to http://acme.unvalid/' + self._test_upload_r(PYPIRC_CUSTOM_SERVER, expected) + + @unittest.skipUnless(zlib, 'needs zlib') + def test_upload_r_2(self): + expected = b'Submitting dist/goats-3.0.zip to http://another.pypi/' + self._test_upload_r(PYPIRC_LONG_PASSWORD, expected) + def test_suite(): - return unittest.makeSuite(uploadTestCase) + return unittest.makeSuite(UploadTestCase) if __name__ == "__main__": run_unittest(test_suite())