66from distutils .command import upload as upload_mod
77from distutils .command .upload import upload
88from distutils .core import Distribution
9+ from distutils .errors import DistutilsError
910from distutils .log import INFO
1011
1112from distutils .tests .test_config import PYPIRC , PyPIRCCommandTestCase
4142
4243class FakeOpen (object ):
4344
44- def __init__ (self , url ):
45+ def __init__ (self , url , msg = None , code = None ):
4546 self .url = url
4647 if not isinstance (url , str ):
4748 self .req = url
4849 else :
4950 self .req = None
50- self .msg = 'OK'
51+ self .msg = msg or 'OK'
52+ self .code = code or 200
5153
5254 def getheader (self , name , default = None ):
5355 return {
@@ -58,7 +60,7 @@ def read(self):
5860 return b'xyzzy'
5961
6062 def getcode (self ):
61- return 200
63+ return self . code
6264
6365
6466class uploadTestCase (PyPIRCCommandTestCase ):
@@ -68,13 +70,15 @@ def setUp(self):
6870 self .old_open = upload_mod .urlopen
6971 upload_mod .urlopen = self ._urlopen
7072 self .last_open = None
73+ self .next_msg = None
74+ self .next_code = None
7175
7276 def tearDown (self ):
7377 upload_mod .urlopen = self .old_open
7478 super (uploadTestCase , self ).tearDown ()
7579
7680 def _urlopen (self , url ):
77- self .last_open = FakeOpen (url )
81+ self .last_open = FakeOpen (url , msg = self . next_msg , code = self . next_code )
7882 return self .last_open
7983
8084 def test_finalize_options (self ):
@@ -135,6 +139,10 @@ def test_upload(self):
135139 results = self .get_logs (INFO )
136140 self .assertIn ('xyzzy\n ' , results [- 1 ])
137141
142+ def test_upload_fails (self ):
143+ self .next_msg = "Not Found"
144+ self .next_code = 404
145+ self .assertRaises (DistutilsError , self .test_upload )
138146
139147def test_suite ():
140148 return unittest .makeSuite (uploadTestCase )
0 commit comments