Add missing issue number in Misc/NEWS entry.
[python.git] / Lib / distutils / tests / test_upload.py
blob0d95a0917eb231af9a8f4c7e1674f2952cb81701
1 """Tests for distutils.command.upload."""
2 import sys
3 import os
4 import unittest
6 from distutils.command import upload as upload_mod
7 from distutils.command.upload import upload
8 from distutils.core import Distribution
10 from distutils.tests import support
11 from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
13 PYPIRC_NOPASSWORD = """\
14 [distutils]
16 index-servers =
17 server1
19 [server1]
20 username:me
21 """
23 class FakeOpen(object):
25 def __init__(self, url):
26 self.url = url
27 if not isinstance(url, str):
28 self.req = url
29 else:
30 self.req = None
31 self.msg = 'OK'
33 def getcode(self):
34 return 200
37 class uploadTestCase(PyPIRCCommandTestCase):
39 def setUp(self):
40 super(uploadTestCase, self).setUp()
41 self.old_open = upload_mod.urlopen
42 upload_mod.urlopen = self._urlopen
43 self.last_open = None
45 def tearDown(self):
46 upload_mod.urlopen = self.old_open
47 super(uploadTestCase, self).tearDown()
49 def _urlopen(self, url):
50 self.last_open = FakeOpen(url)
51 return self.last_open
53 def test_finalize_options(self):
55 # new format
56 self.write_file(self.rc, PYPIRC)
57 dist = Distribution()
58 cmd = upload(dist)
59 cmd.finalize_options()
60 for attr, waited in (('username', 'me'), ('password', 'secret'),
61 ('realm', 'pypi'),
62 ('repository', 'http://pypi.python.org/pypi')):
63 self.assertEquals(getattr(cmd, attr), waited)
65 def test_saved_password(self):
66 # file with no password
67 self.write_file(self.rc, PYPIRC_NOPASSWORD)
69 # make sure it passes
70 dist = Distribution()
71 cmd = upload(dist)
72 cmd.finalize_options()
73 self.assertEquals(cmd.password, None)
75 # make sure we get it as well, if another command
76 # initialized it at the dist level
77 dist.password = 'xxx'
78 cmd = upload(dist)
79 cmd.finalize_options()
80 self.assertEquals(cmd.password, 'xxx')
82 def test_upload(self):
83 tmp = self.mkdtemp()
84 path = os.path.join(tmp, 'xxx')
85 self.write_file(path)
86 command, pyversion, filename = 'xxx', '2.6', path
87 dist_files = [(command, pyversion, filename)]
88 self.write_file(self.rc, PYPIRC)
90 # lets run it
91 pkg_dir, dist = self.create_dist(dist_files=dist_files)
92 cmd = upload(dist)
93 cmd.ensure_finalized()
94 cmd.run()
96 # what did we send ?
97 headers = dict(self.last_open.req.headers)
98 self.assertEquals(headers['Content-length'], '2086')
99 self.assertTrue(headers['Content-type'].startswith('multipart/form-data'))
100 self.assertEquals(self.last_open.req.get_method(), 'POST')
101 self.assertEquals(self.last_open.req.get_full_url(),
102 'http://pypi.python.org/pypi')
103 self.assertTrue('xxx' in self.last_open.req.data)
105 def test_suite():
106 return unittest.makeSuite(uploadTestCase)
108 if __name__ == "__main__":
109 unittest.main(defaultTest="test_suite")