Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1 | """Tests for distutils.command.register.""" |
| 2 | import sys |
| 3 | import os |
| 4 | import unittest |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 5 | import getpass |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 6 | import urllib |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 7 | import warnings |
| 8 | |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 9 | from test.support import check_warnings, run_unittest |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 10 | |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 11 | from distutils.command import register as register_module |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 12 | from distutils.command.register import register |
| 13 | from distutils.core import Distribution |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 14 | from distutils.errors import DistutilsSetupError |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 15 | |
| 16 | from distutils.tests import support |
| 17 | from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase |
| 18 | |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 19 | PYPIRC_NOPASSWORD = """\ |
| 20 | [distutils] |
| 21 | |
| 22 | index-servers = |
| 23 | server1 |
| 24 | |
| 25 | [server1] |
| 26 | username:me |
| 27 | """ |
| 28 | |
| 29 | WANTED_PYPIRC = """\ |
| 30 | [distutils] |
| 31 | index-servers = |
| 32 | pypi |
| 33 | |
| 34 | [pypi] |
| 35 | username:tarek |
| 36 | password:password |
| 37 | """ |
| 38 | |
| 39 | class Inputs(object): |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 40 | """Fakes user inputs.""" |
| 41 | def __init__(self, *answers): |
| 42 | self.answers = answers |
| 43 | self.index = 0 |
| 44 | |
| 45 | def __call__(self, prompt=''): |
| 46 | try: |
| 47 | return self.answers[self.index] |
| 48 | finally: |
| 49 | self.index += 1 |
| 50 | |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 51 | class FakeOpener(object): |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 52 | """Fakes a PyPI server""" |
| 53 | def __init__(self): |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 54 | self.reqs = [] |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 55 | |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 56 | def __call__(self, *args): |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 57 | return self |
| 58 | |
| 59 | def open(self, req): |
| 60 | self.reqs.append(req) |
| 61 | return self |
| 62 | |
| 63 | def read(self): |
| 64 | return 'xxx' |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 65 | |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 66 | class RegisterTestCase(PyPIRCCommandTestCase): |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 67 | |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 68 | def setUp(self): |
Hirokazu Yamamoto | 2703fd9 | 2009-05-29 09:14:04 +0000 | [diff] [blame] | 69 | super(RegisterTestCase, self).setUp() |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 70 | # patching the password prompt |
| 71 | self._old_getpass = getpass.getpass |
| 72 | def _getpass(prompt): |
| 73 | return 'password' |
| 74 | getpass.getpass = _getpass |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 75 | self.old_opener = urllib.request.build_opener |
| 76 | self.conn = urllib.request.build_opener = FakeOpener() |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 77 | |
| 78 | def tearDown(self): |
| 79 | getpass.getpass = self._old_getpass |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 80 | urllib.request.build_opener = self.old_opener |
Hirokazu Yamamoto | 2703fd9 | 2009-05-29 09:14:04 +0000 | [diff] [blame] | 81 | super(RegisterTestCase, self).tearDown() |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 82 | |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 83 | def _get_cmd(self, metadata=None): |
| 84 | if metadata is None: |
| 85 | metadata = {'url': 'xxx', 'author': 'xxx', |
| 86 | 'author_email': 'xxx', |
| 87 | 'name': 'xxx', 'version': 'xxx'} |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 88 | pkg_info, dist = self.create_dist(**metadata) |
| 89 | return register(dist) |
| 90 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 91 | def test_create_pypirc(self): |
| 92 | # this test makes sure a .pypirc file |
| 93 | # is created when requested. |
| 94 | |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 95 | # let's create a register instance |
| 96 | cmd = self._get_cmd() |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 97 | |
| 98 | # we shouldn't have a .pypirc file yet |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 99 | self.assertTrue(not os.path.exists(self.rc)) |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 100 | |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 101 | # patching input and getpass.getpass |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 102 | # so register gets happy |
| 103 | # |
| 104 | # Here's what we are faking : |
| 105 | # use your existing login (choice 1.) |
| 106 | # Username : 'tarek' |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 107 | # Password : 'password' |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 108 | # Save your login (y/N)? : 'y' |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 109 | inputs = Inputs('1', 'tarek', 'y') |
Benjamin Peterson | 467a7bd | 2008-12-27 17:00:44 +0000 | [diff] [blame] | 110 | register_module.input = inputs.__call__ |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 111 | # let's run the command |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 112 | try: |
| 113 | cmd.run() |
| 114 | finally: |
| 115 | del register_module.input |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 116 | |
| 117 | # we should have a brand new .pypirc file |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 118 | self.assertTrue(os.path.exists(self.rc)) |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 119 | |
| 120 | # with the content similar to WANTED_PYPIRC |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 121 | f = open(self.rc) |
| 122 | try: |
| 123 | content = f.read() |
| 124 | self.assertEquals(content, WANTED_PYPIRC) |
| 125 | finally: |
| 126 | f.close() |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 127 | |
| 128 | # now let's make sure the .pypirc file generated |
| 129 | # really works : we shouldn't be asked anything |
| 130 | # if we run the command again |
| 131 | def _no_way(prompt=''): |
| 132 | raise AssertionError(prompt) |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 133 | register_module.input = _no_way |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 134 | |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 135 | cmd.show_response = 1 |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 136 | cmd.run() |
| 137 | |
| 138 | # let's see what the server received : we should |
| 139 | # have 2 similar requests |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 140 | self.assertTrue(self.conn.reqs, 2) |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 141 | req1 = dict(self.conn.reqs[0].headers) |
| 142 | req2 = dict(self.conn.reqs[1].headers) |
| 143 | |
| 144 | self.assertEquals(req1['Content-length'], '1374') |
| 145 | self.assertEquals(req2['Content-length'], '1374') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 146 | self.assertTrue((b'xxx') in self.conn.reqs[1].data) |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 147 | |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 148 | def test_password_not_in_file(self): |
| 149 | |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 150 | self.write_file(self.rc, PYPIRC_NOPASSWORD) |
| 151 | cmd = self._get_cmd() |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 152 | cmd._set_config() |
| 153 | cmd.finalize_options() |
| 154 | cmd.send_metadata() |
| 155 | |
| 156 | # dist.password should be set |
| 157 | # therefore used afterwards by other commands |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 158 | self.assertEquals(cmd.distribution.password, 'password') |
| 159 | |
| 160 | def test_registering(self): |
| 161 | # this test runs choice 2 |
| 162 | cmd = self._get_cmd() |
| 163 | inputs = Inputs('2', 'tarek', 'tarek@ziade.org') |
| 164 | register_module.input = inputs.__call__ |
| 165 | try: |
| 166 | # let's run the command |
| 167 | cmd.run() |
| 168 | finally: |
| 169 | del register_module.input |
| 170 | |
| 171 | # we should have send a request |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 172 | self.assertTrue(self.conn.reqs, 1) |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 173 | req = self.conn.reqs[0] |
| 174 | headers = dict(req.headers) |
| 175 | self.assertEquals(headers['Content-length'], '608') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 176 | self.assertTrue((b'tarek') in req.data) |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 177 | |
| 178 | def test_password_reset(self): |
| 179 | # this test runs choice 3 |
| 180 | cmd = self._get_cmd() |
| 181 | inputs = Inputs('3', 'tarek@ziade.org') |
| 182 | register_module.input = inputs.__call__ |
| 183 | try: |
| 184 | # let's run the command |
| 185 | cmd.run() |
| 186 | finally: |
| 187 | del register_module.input |
| 188 | |
| 189 | # we should have send a request |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 190 | self.assertTrue(self.conn.reqs, 1) |
Tarek Ziadé | baf5180 | 2009-03-31 21:37:16 +0000 | [diff] [blame] | 191 | req = self.conn.reqs[0] |
| 192 | headers = dict(req.headers) |
| 193 | self.assertEquals(headers['Content-length'], '290') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 194 | self.assertTrue((b'tarek') in req.data) |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 195 | |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 196 | def test_strict(self): |
| 197 | # testing the script option |
| 198 | # when on, the register command stops if |
| 199 | # the metadata is incomplete or if |
| 200 | # long_description is not reSt compliant |
| 201 | |
| 202 | # empty metadata |
| 203 | cmd = self._get_cmd({}) |
| 204 | cmd.ensure_finalized() |
| 205 | cmd.strict = 1 |
| 206 | self.assertRaises(DistutilsSetupError, cmd.run) |
| 207 | |
| 208 | # we don't test the reSt feature if docutils |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 209 | # is not installed |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 210 | try: |
| 211 | import docutils |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 212 | except ImportError: |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 213 | return |
| 214 | |
| 215 | # metadata are OK but long_description is broken |
| 216 | metadata = {'url': 'xxx', 'author': 'xxx', |
| 217 | 'author_email': 'xxx', |
| 218 | 'name': 'xxx', 'version': 'xxx', |
| 219 | 'long_description': 'title\n==\n\ntext'} |
| 220 | |
| 221 | cmd = self._get_cmd(metadata) |
| 222 | cmd.ensure_finalized() |
| 223 | cmd.strict = 1 |
| 224 | self.assertRaises(DistutilsSetupError, cmd.run) |
| 225 | |
| 226 | # now something that works |
| 227 | metadata['long_description'] = 'title\n=====\n\ntext' |
| 228 | cmd = self._get_cmd(metadata) |
| 229 | cmd.ensure_finalized() |
| 230 | cmd.strict = 1 |
Tarek Ziadé | 4bcceef | 2010-10-03 14:18:09 +0000 | [diff] [blame] | 231 | inputs = Inputs('1', 'tarek', 'y') |
| 232 | register_module.input = inputs.__call__ |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 233 | # let's run the command |
| 234 | try: |
| 235 | cmd.run() |
| 236 | finally: |
Tarek Ziadé | 4bcceef | 2010-10-03 14:18:09 +0000 | [diff] [blame] | 237 | del register_module.input |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 238 | |
| 239 | # strict is not by default |
| 240 | cmd = self._get_cmd() |
| 241 | cmd.ensure_finalized() |
Tarek Ziadé | 4bcceef | 2010-10-03 14:18:09 +0000 | [diff] [blame] | 242 | inputs = Inputs('1', 'tarek', 'y') |
| 243 | register_module.input = inputs.__call__ |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 244 | # let's run the command |
| 245 | try: |
| 246 | cmd.run() |
| 247 | finally: |
Tarek Ziadé | 4bcceef | 2010-10-03 14:18:09 +0000 | [diff] [blame] | 248 | del register_module.input |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 249 | |
| 250 | def test_check_metadata_deprecated(self): |
| 251 | # makes sure make_metadata is deprecated |
| 252 | cmd = self._get_cmd() |
| 253 | with check_warnings() as w: |
| 254 | warnings.simplefilter("always") |
| 255 | cmd.check_metadata() |
| 256 | self.assertEquals(len(w.warnings), 1) |
| 257 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 258 | def test_suite(): |
Tarek Ziadé | 5af55c6 | 2009-05-16 16:52:13 +0000 | [diff] [blame] | 259 | return unittest.makeSuite(RegisterTestCase) |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 260 | |
| 261 | if __name__ == "__main__": |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 262 | run_unittest(test_suite()) |