1 """Tests for distutils.command.upload."""
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
= """\
23 class FakeOpen(object):
25 def __init__(self
, url
):
27 if not isinstance(url
, str):
37 class uploadTestCase(PyPIRCCommandTestCase
):
40 super(uploadTestCase
, self
).setUp()
41 self
.old_open
= upload_mod
.urlopen
42 upload_mod
.urlopen
= self
._urlopen
46 upload_mod
.urlopen
= self
.old_open
47 super(uploadTestCase
, self
).tearDown()
49 def _urlopen(self
, url
):
50 self
.last_open
= FakeOpen(url
)
53 def test_finalize_options(self
):
56 self
.write_file(self
.rc
, PYPIRC
)
59 cmd
.finalize_options()
60 for attr
, waited
in (('username', 'me'), ('password', 'secret'),
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
)
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
79 cmd
.finalize_options()
80 self
.assertEquals(cmd
.password
, 'xxx')
82 def test_upload(self
):
84 path
= os
.path
.join(tmp
, 'xxx')
86 command
, pyversion
, filename
= 'xxx', '2.6', path
87 dist_files
= [(command
, pyversion
, filename
)]
88 self
.write_file(self
.rc
, PYPIRC
)
91 pkg_dir
, dist
= self
.create_dist(dist_files
=dist_files
)
93 cmd
.ensure_finalized()
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
)
106 return unittest
.makeSuite(uploadTestCase
)
108 if __name__
== "__main__":
109 unittest
.main(defaultTest
="test_suite")