ยปCore Development>Code coverage>Lib/distutils/tests/test_upload.py

Python code coverage for Lib/distutils/tests/test_upload.py

#countcontent
1n/a"""Tests for distutils.command.upload."""
2n/aimport os
3n/aimport unittest
4n/aimport unittest.mock as mock
5n/afrom urllib.request import HTTPError
6n/a
7n/afrom test.support import run_unittest
8n/a
9n/afrom distutils.command import upload as upload_mod
10n/afrom distutils.command.upload import upload
11n/afrom distutils.core import Distribution
12n/afrom distutils.errors import DistutilsError
13n/afrom distutils.log import ERROR, INFO
14n/a
15n/afrom distutils.tests.test_config import PYPIRC, BasePyPIRCCommandTestCase
16n/a
17n/aPYPIRC_LONG_PASSWORD = """\
18n/a[distutils]
19n/a
20n/aindex-servers =
21n/a server1
22n/a server2
23n/a
24n/a[server1]
25n/ausername:me
26n/apassword:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
27n/a
28n/a[server2]
29n/ausername:meagain
30n/apassword: secret
31n/arealm:acme
32n/arepository:http://another.pypi/
33n/a"""
34n/a
35n/a
36n/aPYPIRC_NOPASSWORD = """\
37n/a[distutils]
38n/a
39n/aindex-servers =
40n/a server1
41n/a
42n/a[server1]
43n/ausername:me
44n/a"""
45n/a
46n/aclass FakeOpen(object):
47n/a
48n/a def __init__(self, url, msg=None, code=None):
49n/a self.url = url
50n/a if not isinstance(url, str):
51n/a self.req = url
52n/a else:
53n/a self.req = None
54n/a self.msg = msg or 'OK'
55n/a self.code = code or 200
56n/a
57n/a def getheader(self, name, default=None):
58n/a return {
59n/a 'content-type': 'text/plain; charset=utf-8',
60n/a }.get(name.lower(), default)
61n/a
62n/a def read(self):
63n/a return b'xyzzy'
64n/a
65n/a def getcode(self):
66n/a return self.code
67n/a
68n/a
69n/aclass uploadTestCase(BasePyPIRCCommandTestCase):
70n/a
71n/a def setUp(self):
72n/a super(uploadTestCase, self).setUp()
73n/a self.old_open = upload_mod.urlopen
74n/a upload_mod.urlopen = self._urlopen
75n/a self.last_open = None
76n/a self.next_msg = None
77n/a self.next_code = None
78n/a
79n/a def tearDown(self):
80n/a upload_mod.urlopen = self.old_open
81n/a super(uploadTestCase, self).tearDown()
82n/a
83n/a def _urlopen(self, url):
84n/a self.last_open = FakeOpen(url, msg=self.next_msg, code=self.next_code)
85n/a return self.last_open
86n/a
87n/a def test_finalize_options(self):
88n/a
89n/a # new format
90n/a self.write_file(self.rc, PYPIRC)
91n/a dist = Distribution()
92n/a cmd = upload(dist)
93n/a cmd.finalize_options()
94n/a for attr, waited in (('username', 'me'), ('password', 'secret'),
95n/a ('realm', 'pypi'),
96n/a ('repository', 'https://upload.pypi.org/legacy/')):
97n/a self.assertEqual(getattr(cmd, attr), waited)
98n/a
99n/a def test_saved_password(self):
100n/a # file with no password
101n/a self.write_file(self.rc, PYPIRC_NOPASSWORD)
102n/a
103n/a # make sure it passes
104n/a dist = Distribution()
105n/a cmd = upload(dist)
106n/a cmd.finalize_options()
107n/a self.assertEqual(cmd.password, None)
108n/a
109n/a # make sure we get it as well, if another command
110n/a # initialized it at the dist level
111n/a dist.password = 'xxx'
112n/a cmd = upload(dist)
113n/a cmd.finalize_options()
114n/a self.assertEqual(cmd.password, 'xxx')
115n/a
116n/a def test_upload(self):
117n/a tmp = self.mkdtemp()
118n/a path = os.path.join(tmp, 'xxx')
119n/a self.write_file(path)
120n/a command, pyversion, filename = 'xxx', '2.6', path
121n/a dist_files = [(command, pyversion, filename)]
122n/a self.write_file(self.rc, PYPIRC_LONG_PASSWORD)
123n/a
124n/a # lets run it
125n/a pkg_dir, dist = self.create_dist(dist_files=dist_files)
126n/a cmd = upload(dist)
127n/a cmd.show_response = 1
128n/a cmd.ensure_finalized()
129n/a cmd.run()
130n/a
131n/a # what did we send ?
132n/a headers = dict(self.last_open.req.headers)
133n/a self.assertEqual(headers['Content-length'], '2162')
134n/a content_type = headers['Content-type']
135n/a self.assertTrue(content_type.startswith('multipart/form-data'))
136n/a self.assertEqual(self.last_open.req.get_method(), 'POST')
137n/a expected_url = 'https://upload.pypi.org/legacy/'
138n/a self.assertEqual(self.last_open.req.get_full_url(), expected_url)
139n/a self.assertTrue(b'xxx' in self.last_open.req.data)
140n/a self.assertIn(b'protocol_version', self.last_open.req.data)
141n/a
142n/a # The PyPI response body was echoed
143n/a results = self.get_logs(INFO)
144n/a self.assertEqual(results[-1], 75 * '-' + '\nxyzzy\n' + 75 * '-')
145n/a
146n/a def test_upload_fails(self):
147n/a self.next_msg = "Not Found"
148n/a self.next_code = 404
149n/a self.assertRaises(DistutilsError, self.test_upload)
150n/a
151n/a def test_wrong_exception_order(self):
152n/a tmp = self.mkdtemp()
153n/a path = os.path.join(tmp, 'xxx')
154n/a self.write_file(path)
155n/a dist_files = [('xxx', '2.6', path)] # command, pyversion, filename
156n/a self.write_file(self.rc, PYPIRC_LONG_PASSWORD)
157n/a
158n/a pkg_dir, dist = self.create_dist(dist_files=dist_files)
159n/a tests = [
160n/a (OSError('oserror'), 'oserror', OSError),
161n/a (HTTPError('url', 400, 'httperror', {}, None),
162n/a 'Upload failed (400): httperror', DistutilsError),
163n/a ]
164n/a for exception, expected, raised_exception in tests:
165n/a with self.subTest(exception=type(exception).__name__):
166n/a with mock.patch('distutils.command.upload.urlopen',
167n/a new=mock.Mock(side_effect=exception)):
168n/a with self.assertRaises(raised_exception):
169n/a cmd = upload(dist)
170n/a cmd.ensure_finalized()
171n/a cmd.run()
172n/a results = self.get_logs(ERROR)
173n/a self.assertIn(expected, results[-1])
174n/a self.clear_logs()
175n/a
176n/a
177n/adef test_suite():
178n/a return unittest.makeSuite(uploadTestCase)
179n/a
180n/aif __name__ == "__main__":
181n/a run_unittest(test_suite())