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

Side by Side Diff: Lib/distutils/tests/test_upload.py

Issue 21722: teach distutils "upload" to exit with code != 0 when error occurs
Patch Set: Created 5 years, 12 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:
View unified diff | Download patch
« no previous file with comments | « Lib/distutils/command/upload.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Tests for distutils.command.upload.""" 1 """Tests for distutils.command.upload."""
2 import os 2 import os
3 import unittest 3 import unittest
4 from test.support import run_unittest 4 from test.support import run_unittest
5 5
6 from distutils.command import upload as upload_mod 6 from distutils.command import upload as upload_mod
7 from distutils.command.upload import upload 7 from distutils.command.upload import upload
8 from distutils.core import Distribution 8 from distutils.core import Distribution
9 from distutils.errors import DistutilsError
9 from distutils.log import INFO 10 from distutils.log import INFO
10 11
11 from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase 12 from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
12 13
13 PYPIRC_LONG_PASSWORD = """\ 14 PYPIRC_LONG_PASSWORD = """\
14 [distutils] 15 [distutils]
15 16
16 index-servers = 17 index-servers =
17 server1 18 server1
18 server2 19 server2
(...skipping 15 matching lines...) Expand all
34 35
35 index-servers = 36 index-servers =
36 server1 37 server1
37 38
38 [server1] 39 [server1]
39 username:me 40 username:me
40 """ 41 """
41 42
42 class FakeOpen(object): 43 class FakeOpen(object):
43 44
44 def __init__(self, url): 45 def __init__(self, url, msg=None, code=None):
45 self.url = url 46 self.url = url
46 if not isinstance(url, str): 47 if not isinstance(url, str):
47 self.req = url 48 self.req = url
48 else: 49 else:
49 self.req = None 50 self.req = None
50 self.msg = 'OK' 51 self.msg = msg or 'OK'
52 self.code = code or 200
51 53
52 def getheader(self, name, default=None): 54 def getheader(self, name, default=None):
53 return { 55 return {
54 'content-type': 'text/plain; charset=utf-8', 56 'content-type': 'text/plain; charset=utf-8',
55 }.get(name.lower(), default) 57 }.get(name.lower(), default)
56 58
57 def read(self): 59 def read(self):
58 return b'xyzzy' 60 return b'xyzzy'
59 61
60 def getcode(self): 62 def getcode(self):
61 return 200 63 return self.code
62 64
63 65
64 class uploadTestCase(PyPIRCCommandTestCase): 66 class uploadTestCase(PyPIRCCommandTestCase):
65 67
66 def setUp(self): 68 def setUp(self):
67 super(uploadTestCase, self).setUp() 69 super(uploadTestCase, self).setUp()
68 self.old_open = upload_mod.urlopen 70 self.old_open = upload_mod.urlopen
69 upload_mod.urlopen = self._urlopen 71 upload_mod.urlopen = self._urlopen
70 self.last_open = None 72 self.last_open = None
73 self.next_msg = None
74 self.next_code = None
71 75
72 def tearDown(self): 76 def tearDown(self):
73 upload_mod.urlopen = self.old_open 77 upload_mod.urlopen = self.old_open
74 super(uploadTestCase, self).tearDown() 78 super(uploadTestCase, self).tearDown()
75 79
76 def _urlopen(self, url): 80 def _urlopen(self, url):
77 self.last_open = FakeOpen(url) 81 self.last_open = FakeOpen(url, msg=self.next_msg, code=self.next_code)
78 return self.last_open 82 return self.last_open
79 83
80 def test_finalize_options(self): 84 def test_finalize_options(self):
81 85
82 # new format 86 # new format
83 self.write_file(self.rc, PYPIRC) 87 self.write_file(self.rc, PYPIRC)
84 dist = Distribution() 88 dist = Distribution()
85 cmd = upload(dist) 89 cmd = upload(dist)
86 cmd.finalize_options() 90 cmd.finalize_options()
87 for attr, waited in (('username', 'me'), ('password', 'secret'), 91 for attr, waited in (('username', 'me'), ('password', 'secret'),
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 self.assertTrue(content_type.startswith('multipart/form-data')) 132 self.assertTrue(content_type.startswith('multipart/form-data'))
129 self.assertEqual(self.last_open.req.get_method(), 'POST') 133 self.assertEqual(self.last_open.req.get_method(), 'POST')
130 expected_url = 'https://pypi.python.org/pypi' 134 expected_url = 'https://pypi.python.org/pypi'
131 self.assertEqual(self.last_open.req.get_full_url(), expected_url) 135 self.assertEqual(self.last_open.req.get_full_url(), expected_url)
132 self.assertTrue(b'xxx' in self.last_open.req.data) 136 self.assertTrue(b'xxx' in self.last_open.req.data)
133 137
134 # The PyPI response body was echoed 138 # The PyPI response body was echoed
135 results = self.get_logs(INFO) 139 results = self.get_logs(INFO)
136 self.assertIn('xyzzy\n', results[-1]) 140 self.assertIn('xyzzy\n', results[-1])
137 141
142 def test_upload_fails(self):
143 try:
144 self.next_msg = "Not Found"
145 self.next_code = 404
146 self.assertRaises(DistutilsError, self.test_upload)
147 finally:
148 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 150
139 def test_suite(): 151 def test_suite():
140 return unittest.makeSuite(uploadTestCase) 152 return unittest.makeSuite(uploadTestCase)
141 153
142 if __name__ == "__main__": 154 if __name__ == "__main__":
143 run_unittest(test_suite()) 155 run_unittest(test_suite())
OLDNEW
« no previous file with comments | « Lib/distutils/command/upload.py ('k') | no next file » | no next file with comments »

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