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