Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3)

Delta Between Two Patch Sets: Lib/distutils/tests/test_upload.py

Issue 21722: teach distutils "upload" to exit with code != 0 when error occurs
Left Patch Set: Created 5 years, 11 months ago
Right Patch Set: Created 5 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/distutils/command/upload.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # -*- encoding: utf8 -*-
1 """Tests for distutils.command.upload.""" 2 """Tests for distutils.command.upload."""
2 import os 3 import os
3 import unittest 4 import unittest
4 from test.support import run_unittest 5 from test.test_support import run_unittest
5 6
6 from distutils.command import upload as upload_mod 7 from distutils.command import upload as upload_mod
7 from distutils.command.upload import upload 8 from distutils.command.upload import upload
8 from distutils.core import Distribution 9 from distutils.core import Distribution
9 from distutils.errors import DistutilsError 10 from distutils.errors import DistutilsError
10 from distutils.log import INFO
11 11
12 from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase 12 from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
13 13
14 PYPIRC_LONG_PASSWORD = """\ 14 PYPIRC_LONG_PASSWORD = """\
15 [distutils] 15 [distutils]
16 16
17 index-servers = 17 index-servers =
18 server1 18 server1
19 server2 19 server2
20 20
(...skipping 22 matching lines...) Expand all
43 class FakeOpen(object): 43 class FakeOpen(object):
44 44
45 def __init__(self, url, msg=None, code=None): 45 def __init__(self, url, msg=None, code=None):
46 self.url = url 46 self.url = url
47 if not isinstance(url, str): 47 if not isinstance(url, str):
48 self.req = url 48 self.req = url
49 else: 49 else:
50 self.req = None 50 self.req = None
51 self.msg = msg or 'OK' 51 self.msg = msg or 'OK'
52 self.code = code or 200 52 self.code = code or 200
53
54 def getheader(self, name, default=None):
55 return {
56 'content-type': 'text/plain; charset=utf-8',
57 }.get(name.lower(), default)
58
59 def read(self):
60 return b'xyzzy'
61 53
62 def getcode(self): 54 def getcode(self):
63 return self.code 55 return self.code
64 56
65 57
66 class uploadTestCase(PyPIRCCommandTestCase): 58 class uploadTestCase(PyPIRCCommandTestCase):
67 59
68 def setUp(self): 60 def setUp(self):
69 super(uploadTestCase, self).setUp() 61 super(uploadTestCase, self).setUp()
70 self.old_open = upload_mod.urlopen 62 self.old_open = upload_mod.urlopen
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 104
113 def test_upload(self): 105 def test_upload(self):
114 tmp = self.mkdtemp() 106 tmp = self.mkdtemp()
115 path = os.path.join(tmp, 'xxx') 107 path = os.path.join(tmp, 'xxx')
116 self.write_file(path) 108 self.write_file(path)
117 command, pyversion, filename = 'xxx', '2.6', path 109 command, pyversion, filename = 'xxx', '2.6', path
118 dist_files = [(command, pyversion, filename)] 110 dist_files = [(command, pyversion, filename)]
119 self.write_file(self.rc, PYPIRC_LONG_PASSWORD) 111 self.write_file(self.rc, PYPIRC_LONG_PASSWORD)
120 112
121 # lets run it 113 # lets run it
122 pkg_dir, dist = self.create_dist(dist_files=dist_files) 114 pkg_dir, dist = self.create_dist(dist_files=dist_files, author=u'dédé')
123 cmd = upload(dist) 115 cmd = upload(dist)
124 cmd.show_response = 1
125 cmd.ensure_finalized() 116 cmd.ensure_finalized()
126 cmd.run() 117 cmd.run()
127 118
128 # what did we send ? 119 # what did we send ?
120 self.assertIn('dédé', self.last_open.req.data)
129 headers = dict(self.last_open.req.headers) 121 headers = dict(self.last_open.req.headers)
130 self.assertEqual(headers['Content-length'], '2087') 122 self.assertEqual(headers['Content-length'], '2085')
131 content_type = headers['Content-type'] 123 self.assertTrue(headers['Content-type'].startswith('multipart/form-data' ))
132 self.assertTrue(content_type.startswith('multipart/form-data'))
133 self.assertEqual(self.last_open.req.get_method(), 'POST') 124 self.assertEqual(self.last_open.req.get_method(), 'POST')
134 expected_url = 'https://pypi.python.org/pypi' 125 self.assertEqual(self.last_open.req.get_full_url(),
135 self.assertEqual(self.last_open.req.get_full_url(), expected_url) 126 'https://pypi.python.org/pypi')
136 self.assertTrue(b'xxx' in self.last_open.req.data) 127 self.assertIn('xxx', self.last_open.req.data)
137 128 auth = self.last_open.req.headers['Authorization']
138 # The PyPI response body was echoed 129 self.assertNotIn('\n', auth)
139 results = self.get_logs(INFO)
140 self.assertIn('xyzzy\n', results[-1])
141 130
142 def test_upload_fails(self): 131 def test_upload_fails(self):
143 try: 132 try:
144 self.next_msg = "Not Found" 133 self.next_msg = "Not Found"
145 self.next_code = 404 134 self.next_code = 404
146 self.assertRaises(DistutilsError, self.test_upload) 135 self.assertRaises(DistutilsError, self.test_upload)
147 finally: 136 finally:
148 self.next_msg = None 137 self.next_msg = None
AntoinePitrou 2014/06/18 23:16:41 You don't need to reset them, since setUp() is run
mdengler 2014/06/18 23:47:07 True! I just thought things would be more obvious
149 self.next_code = None 138 self.next_code = None
150 139
151 def test_suite(): 140 def test_suite():
152 return unittest.makeSuite(uploadTestCase) 141 return unittest.makeSuite(uploadTestCase)
153 142
154 if __name__ == "__main__": 143 if __name__ == "__main__":
155 run_unittest(test_suite()) 144 run_unittest(test_suite())
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+