| 1 | n/a | """Tests for packaging.util.""" |
|---|
| 2 | n/a | import os |
|---|
| 3 | n/a | import sys |
|---|
| 4 | n/a | import time |
|---|
| 5 | n/a | import logging |
|---|
| 6 | n/a | import tempfile |
|---|
| 7 | n/a | import textwrap |
|---|
| 8 | n/a | import warnings |
|---|
| 9 | n/a | import subprocess |
|---|
| 10 | n/a | from io import StringIO |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | from packaging.errors import ( |
|---|
| 13 | n/a | PackagingPlatformError, PackagingFileError, |
|---|
| 14 | n/a | PackagingExecError, InstallationException) |
|---|
| 15 | n/a | from packaging import util |
|---|
| 16 | n/a | from packaging.dist import Distribution |
|---|
| 17 | n/a | from packaging.util import ( |
|---|
| 18 | n/a | convert_path, change_root, split_quoted, strtobool, run_2to3, |
|---|
| 19 | n/a | get_compiler_versions, _MAC_OS_X_LD_VERSION, byte_compile, find_packages, |
|---|
| 20 | n/a | spawn, get_pypirc_path, generate_pypirc, read_pypirc, resolve_name, iglob, |
|---|
| 21 | n/a | RICH_GLOB, egginfo_to_distinfo, is_setuptools, is_distutils, is_packaging, |
|---|
| 22 | n/a | get_install_method, cfg_to_args, generate_setup_py, encode_multipart) |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | from packaging.tests import support, unittest |
|---|
| 25 | n/a | from packaging.tests.test_config import SETUP_CFG |
|---|
| 26 | n/a | from test.script_helper import assert_python_ok, assert_python_failure |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | PYPIRC = """\ |
|---|
| 30 | n/a | [distutils] |
|---|
| 31 | n/a | index-servers = |
|---|
| 32 | n/a | pypi |
|---|
| 33 | n/a | server1 |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | [pypi] |
|---|
| 36 | n/a | username:me |
|---|
| 37 | n/a | password:xxxx |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | [server1] |
|---|
| 40 | n/a | repository:http://example.com |
|---|
| 41 | n/a | username:tarek |
|---|
| 42 | n/a | password:secret |
|---|
| 43 | n/a | """ |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | PYPIRC_OLD = """\ |
|---|
| 46 | n/a | [server-login] |
|---|
| 47 | n/a | username:tarek |
|---|
| 48 | n/a | password:secret |
|---|
| 49 | n/a | """ |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | WANTED = """\ |
|---|
| 52 | n/a | [distutils] |
|---|
| 53 | n/a | index-servers = |
|---|
| 54 | n/a | pypi |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | [pypi] |
|---|
| 57 | n/a | username:tarek |
|---|
| 58 | n/a | password:xxx |
|---|
| 59 | n/a | """ |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | EXPECTED_MULTIPART_OUTPUT = [ |
|---|
| 62 | n/a | b'---x', |
|---|
| 63 | n/a | b'Content-Disposition: form-data; name="username"', |
|---|
| 64 | n/a | b'', |
|---|
| 65 | n/a | b'wok', |
|---|
| 66 | n/a | b'---x', |
|---|
| 67 | n/a | b'Content-Disposition: form-data; name="password"', |
|---|
| 68 | n/a | b'', |
|---|
| 69 | n/a | b'secret', |
|---|
| 70 | n/a | b'---x', |
|---|
| 71 | n/a | b'Content-Disposition: form-data; name="picture"; filename="wok.png"', |
|---|
| 72 | n/a | b'', |
|---|
| 73 | n/a | b'PNG89', |
|---|
| 74 | n/a | b'---x--', |
|---|
| 75 | n/a | b'', |
|---|
| 76 | n/a | ] |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | class FakePopen: |
|---|
| 80 | n/a | test_class = None |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | def __init__(self, args, bufsize=0, executable=None, |
|---|
| 83 | n/a | stdin=None, stdout=None, stderr=None, |
|---|
| 84 | n/a | preexec_fn=None, close_fds=False, |
|---|
| 85 | n/a | shell=False, cwd=None, env=None, universal_newlines=False, |
|---|
| 86 | n/a | startupinfo=None, creationflags=0, |
|---|
| 87 | n/a | restore_signals=True, start_new_session=False, |
|---|
| 88 | n/a | pass_fds=()): |
|---|
| 89 | n/a | if isinstance(args, str): |
|---|
| 90 | n/a | args = args.split() |
|---|
| 91 | n/a | self.cmd = args[0] |
|---|
| 92 | n/a | exes = self.test_class._exes |
|---|
| 93 | n/a | if self.cmd not in exes: |
|---|
| 94 | n/a | # we don't want to call the system, returning an empty |
|---|
| 95 | n/a | # output so it doesn't match |
|---|
| 96 | n/a | self.stdout = StringIO() |
|---|
| 97 | n/a | self.stderr = StringIO() |
|---|
| 98 | n/a | else: |
|---|
| 99 | n/a | self.stdout = StringIO(exes[self.cmd]) |
|---|
| 100 | n/a | self.stderr = StringIO() |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | def communicate(self, input=None, timeout=None): |
|---|
| 103 | n/a | return self.stdout.read(), self.stderr.read() |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | def wait(self, timeout=None): |
|---|
| 106 | n/a | return 0 |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | class UtilTestCase(support.EnvironRestorer, |
|---|
| 110 | n/a | support.TempdirManager, |
|---|
| 111 | n/a | support.LoggingCatcher, |
|---|
| 112 | n/a | unittest.TestCase): |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | restore_environ = ['HOME', 'PLAT'] |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | def setUp(self): |
|---|
| 117 | n/a | super(UtilTestCase, self).setUp() |
|---|
| 118 | n/a | self.addCleanup(os.chdir, os.getcwd()) |
|---|
| 119 | n/a | tempdir = self.mkdtemp() |
|---|
| 120 | n/a | self.rc = os.path.join(tempdir, '.pypirc') |
|---|
| 121 | n/a | os.environ['HOME'] = tempdir |
|---|
| 122 | n/a | os.chdir(tempdir) |
|---|
| 123 | n/a | # saving the environment |
|---|
| 124 | n/a | self.name = os.name |
|---|
| 125 | n/a | self.platform = sys.platform |
|---|
| 126 | n/a | self.version = sys.version |
|---|
| 127 | n/a | self.sep = os.sep |
|---|
| 128 | n/a | self.join = os.path.join |
|---|
| 129 | n/a | self.isabs = os.path.isabs |
|---|
| 130 | n/a | self.splitdrive = os.path.splitdrive |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | # patching os.uname |
|---|
| 133 | n/a | if hasattr(os, 'uname'): |
|---|
| 134 | n/a | self.uname = os.uname |
|---|
| 135 | n/a | self._uname = os.uname() |
|---|
| 136 | n/a | else: |
|---|
| 137 | n/a | self.uname = None |
|---|
| 138 | n/a | self._uname = None |
|---|
| 139 | n/a | os.uname = self._get_uname |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | def _get_uname(self): |
|---|
| 142 | n/a | return self._uname |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | def tearDown(self): |
|---|
| 145 | n/a | # getting back the environment |
|---|
| 146 | n/a | os.name = self.name |
|---|
| 147 | n/a | sys.platform = self.platform |
|---|
| 148 | n/a | sys.version = self.version |
|---|
| 149 | n/a | os.sep = self.sep |
|---|
| 150 | n/a | os.path.join = self.join |
|---|
| 151 | n/a | os.path.isabs = self.isabs |
|---|
| 152 | n/a | os.path.splitdrive = self.splitdrive |
|---|
| 153 | n/a | if self.uname is not None: |
|---|
| 154 | n/a | os.uname = self.uname |
|---|
| 155 | n/a | else: |
|---|
| 156 | n/a | del os.uname |
|---|
| 157 | n/a | super(UtilTestCase, self).tearDown() |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | def mock_popen(self): |
|---|
| 160 | n/a | self.old_find_executable = util.find_executable |
|---|
| 161 | n/a | util.find_executable = self._find_executable |
|---|
| 162 | n/a | self._exes = {} |
|---|
| 163 | n/a | self.old_popen = subprocess.Popen |
|---|
| 164 | n/a | self.old_stdout = sys.stdout |
|---|
| 165 | n/a | self.old_stderr = sys.stderr |
|---|
| 166 | n/a | FakePopen.test_class = self |
|---|
| 167 | n/a | subprocess.Popen = FakePopen |
|---|
| 168 | n/a | self.addCleanup(self.unmock_popen) |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | def unmock_popen(self): |
|---|
| 171 | n/a | util.find_executable = self.old_find_executable |
|---|
| 172 | n/a | subprocess.Popen = self.old_popen |
|---|
| 173 | n/a | sys.stdout = self.old_stdout |
|---|
| 174 | n/a | sys.stderr = self.old_stderr |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | def test_set_platform(self): |
|---|
| 177 | n/a | self.addCleanup(util.set_platform, util.get_platform()) |
|---|
| 178 | n/a | util.set_platform("fake") |
|---|
| 179 | n/a | self.assertEqual("fake", util.get_platform()) |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | def test_convert_path(self): |
|---|
| 182 | n/a | # linux/mac |
|---|
| 183 | n/a | os.sep = '/' |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | def _join(path): |
|---|
| 186 | n/a | return '/'.join(path) |
|---|
| 187 | n/a | os.path.join = _join |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | self.assertEqual(convert_path('/home/to/my/stuff'), |
|---|
| 190 | n/a | '/home/to/my/stuff') |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | # win |
|---|
| 193 | n/a | os.sep = '\\' |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | def _join(*path): |
|---|
| 196 | n/a | return '\\'.join(path) |
|---|
| 197 | n/a | os.path.join = _join |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | self.assertRaises(ValueError, convert_path, '/home/to/my/stuff') |
|---|
| 200 | n/a | self.assertRaises(ValueError, convert_path, 'home/to/my/stuff/') |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | self.assertEqual(convert_path('home/to/my/stuff'), |
|---|
| 203 | n/a | 'home\\to\\my\\stuff') |
|---|
| 204 | n/a | self.assertEqual(convert_path('.'), |
|---|
| 205 | n/a | os.curdir) |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | def test_change_root(self): |
|---|
| 208 | n/a | # linux/mac |
|---|
| 209 | n/a | os.name = 'posix' |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | def _isabs(path): |
|---|
| 212 | n/a | return path[0] == '/' |
|---|
| 213 | n/a | os.path.isabs = _isabs |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | def _join(*path): |
|---|
| 216 | n/a | return '/'.join(path) |
|---|
| 217 | n/a | os.path.join = _join |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | self.assertEqual(change_root('/root', '/old/its/here'), |
|---|
| 220 | n/a | '/root/old/its/here') |
|---|
| 221 | n/a | self.assertEqual(change_root('/root', 'its/here'), |
|---|
| 222 | n/a | '/root/its/here') |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | # windows |
|---|
| 225 | n/a | os.name = 'nt' |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | def _isabs(path): |
|---|
| 228 | n/a | return path.startswith('c:\\') |
|---|
| 229 | n/a | os.path.isabs = _isabs |
|---|
| 230 | n/a | |
|---|
| 231 | n/a | def _splitdrive(path): |
|---|
| 232 | n/a | if path.startswith('c:'): |
|---|
| 233 | n/a | return '', path.replace('c:', '') |
|---|
| 234 | n/a | return '', path |
|---|
| 235 | n/a | os.path.splitdrive = _splitdrive |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | def _join(*path): |
|---|
| 238 | n/a | return '\\'.join(path) |
|---|
| 239 | n/a | os.path.join = _join |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | self.assertEqual(change_root('c:\\root', 'c:\\old\\its\\here'), |
|---|
| 242 | n/a | 'c:\\root\\old\\its\\here') |
|---|
| 243 | n/a | self.assertEqual(change_root('c:\\root', 'its\\here'), |
|---|
| 244 | n/a | 'c:\\root\\its\\here') |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | # BugsBunny os (it's a great os) |
|---|
| 247 | n/a | os.name = 'BugsBunny' |
|---|
| 248 | n/a | self.assertRaises(PackagingPlatformError, |
|---|
| 249 | n/a | change_root, 'c:\\root', 'its\\here') |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | # XXX platforms to be covered: os2, mac |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | def test_split_quoted(self): |
|---|
| 254 | n/a | self.assertEqual(split_quoted('""one"" "two" \'three\' \\four'), |
|---|
| 255 | n/a | ['one', 'two', 'three', 'four']) |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | def test_strtobool(self): |
|---|
| 258 | n/a | yes = ('y', 'Y', 'yes', 'True', 't', 'true', 'True', 'On', 'on', '1') |
|---|
| 259 | n/a | no = ('n', 'no', 'f', 'false', 'off', '0', 'Off', 'No', 'N') |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | for y in yes: |
|---|
| 262 | n/a | self.assertTrue(strtobool(y)) |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | for n in no: |
|---|
| 265 | n/a | self.assertFalse(strtobool(n)) |
|---|
| 266 | n/a | |
|---|
| 267 | n/a | def test_find_exe_version(self): |
|---|
| 268 | n/a | # the ld version scheme under MAC OS is: |
|---|
| 269 | n/a | # ^@(#)PROGRAM:ld PROJECT:ld64-VERSION |
|---|
| 270 | n/a | # |
|---|
| 271 | n/a | # where VERSION is a 2-digit number for major |
|---|
| 272 | n/a | # revisions. For instance under Leopard, it's |
|---|
| 273 | n/a | # currently 77 |
|---|
| 274 | n/a | # |
|---|
| 275 | n/a | # Dots are used when branching is done. |
|---|
| 276 | n/a | # |
|---|
| 277 | n/a | # The SnowLeopard ld64 is currently 95.2.12 |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | for output, version in (('@(#)PROGRAM:ld PROJECT:ld64-77', '77'), |
|---|
| 280 | n/a | ('@(#)PROGRAM:ld PROJECT:ld64-95.2.12', |
|---|
| 281 | n/a | '95.2.12')): |
|---|
| 282 | n/a | result = _MAC_OS_X_LD_VERSION.search(output) |
|---|
| 283 | n/a | self.assertEqual(result.group(1), version) |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | def _find_executable(self, name): |
|---|
| 286 | n/a | if name in self._exes: |
|---|
| 287 | n/a | return name |
|---|
| 288 | n/a | return None |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | def test_get_compiler_versions(self): |
|---|
| 291 | n/a | self.mock_popen() |
|---|
| 292 | n/a | # get_versions calls distutils.spawn.find_executable on |
|---|
| 293 | n/a | # 'gcc', 'ld' and 'dllwrap' |
|---|
| 294 | n/a | self.assertEqual(get_compiler_versions(), (None, None, None)) |
|---|
| 295 | n/a | |
|---|
| 296 | n/a | # Let's fake we have 'gcc' and it returns '3.4.5' |
|---|
| 297 | n/a | self._exes['gcc'] = 'gcc (GCC) 3.4.5 (mingw special)\nFSF' |
|---|
| 298 | n/a | res = get_compiler_versions() |
|---|
| 299 | n/a | self.assertEqual(str(res[0]), '3.4.5') |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | # and let's see what happens when the version |
|---|
| 302 | n/a | # doesn't match the regular expression |
|---|
| 303 | n/a | # (\d+\.\d+(\.\d+)*) |
|---|
| 304 | n/a | self._exes['gcc'] = 'very strange output' |
|---|
| 305 | n/a | res = get_compiler_versions() |
|---|
| 306 | n/a | self.assertEqual(res[0], None) |
|---|
| 307 | n/a | |
|---|
| 308 | n/a | # same thing for ld |
|---|
| 309 | n/a | if sys.platform != 'darwin': |
|---|
| 310 | n/a | self._exes['ld'] = 'GNU ld version 2.17.50 20060824' |
|---|
| 311 | n/a | res = get_compiler_versions() |
|---|
| 312 | n/a | self.assertEqual(str(res[1]), '2.17.50') |
|---|
| 313 | n/a | self._exes['ld'] = '@(#)PROGRAM:ld PROJECT:ld64-77' |
|---|
| 314 | n/a | res = get_compiler_versions() |
|---|
| 315 | n/a | self.assertEqual(res[1], None) |
|---|
| 316 | n/a | else: |
|---|
| 317 | n/a | self._exes['ld'] = 'GNU ld version 2.17.50 20060824' |
|---|
| 318 | n/a | res = get_compiler_versions() |
|---|
| 319 | n/a | self.assertEqual(res[1], None) |
|---|
| 320 | n/a | self._exes['ld'] = '@(#)PROGRAM:ld PROJECT:ld64-77' |
|---|
| 321 | n/a | res = get_compiler_versions() |
|---|
| 322 | n/a | self.assertEqual(str(res[1]), '77') |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | # and dllwrap |
|---|
| 325 | n/a | self._exes['dllwrap'] = 'GNU dllwrap 2.17.50 20060824\nFSF' |
|---|
| 326 | n/a | res = get_compiler_versions() |
|---|
| 327 | n/a | self.assertEqual(str(res[2]), '2.17.50') |
|---|
| 328 | n/a | self._exes['dllwrap'] = 'Cheese Wrap' |
|---|
| 329 | n/a | res = get_compiler_versions() |
|---|
| 330 | n/a | self.assertEqual(res[2], None) |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | def test_byte_compile_under_B(self): |
|---|
| 333 | n/a | # make sure byte compilation works under -B (dont_write_bytecode) |
|---|
| 334 | n/a | self.addCleanup(setattr, sys, 'dont_write_bytecode', |
|---|
| 335 | n/a | sys.dont_write_bytecode) |
|---|
| 336 | n/a | sys.dont_write_bytecode = True |
|---|
| 337 | n/a | byte_compile([]) |
|---|
| 338 | n/a | |
|---|
| 339 | n/a | def test_newer(self): |
|---|
| 340 | n/a | self.assertRaises(PackagingFileError, util.newer, 'xxx', 'xxx') |
|---|
| 341 | n/a | self.newer_f1 = self.mktempfile() |
|---|
| 342 | n/a | time.sleep(1) |
|---|
| 343 | n/a | self.newer_f2 = self.mktempfile() |
|---|
| 344 | n/a | self.assertTrue(util.newer(self.newer_f2.name, self.newer_f1.name)) |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | def test_find_packages(self): |
|---|
| 347 | n/a | # let's create a structure we want to scan: |
|---|
| 348 | n/a | # |
|---|
| 349 | n/a | # pkg1 |
|---|
| 350 | n/a | # __init__ |
|---|
| 351 | n/a | # pkg2 |
|---|
| 352 | n/a | # __init__ |
|---|
| 353 | n/a | # pkg3 |
|---|
| 354 | n/a | # __init__ |
|---|
| 355 | n/a | # pkg6 |
|---|
| 356 | n/a | # __init__ |
|---|
| 357 | n/a | # pkg4 <--- not a pkg |
|---|
| 358 | n/a | # pkg8 |
|---|
| 359 | n/a | # __init__ |
|---|
| 360 | n/a | # pkg5 |
|---|
| 361 | n/a | # __init__ |
|---|
| 362 | n/a | # |
|---|
| 363 | n/a | root = self.mkdtemp() |
|---|
| 364 | n/a | pkg1 = os.path.join(root, 'pkg1') |
|---|
| 365 | n/a | os.makedirs(os.path.join(pkg1, 'pkg2')) |
|---|
| 366 | n/a | os.makedirs(os.path.join(pkg1, 'pkg3', 'pkg6')) |
|---|
| 367 | n/a | os.makedirs(os.path.join(pkg1, 'pkg4', 'pkg8')) |
|---|
| 368 | n/a | os.makedirs(os.path.join(root, 'pkg5')) |
|---|
| 369 | n/a | self.write_file((pkg1, '__init__.py')) |
|---|
| 370 | n/a | self.write_file((pkg1, 'pkg2', '__init__.py')) |
|---|
| 371 | n/a | self.write_file((pkg1, 'pkg3', '__init__.py')) |
|---|
| 372 | n/a | self.write_file((pkg1, 'pkg3', 'pkg6', '__init__.py')) |
|---|
| 373 | n/a | self.write_file((pkg1, 'pkg4', 'pkg8', '__init__.py')) |
|---|
| 374 | n/a | self.write_file((root, 'pkg5', '__init__.py')) |
|---|
| 375 | n/a | |
|---|
| 376 | n/a | res = find_packages([root], ['pkg1.pkg2']) |
|---|
| 377 | n/a | self.assertEqual(sorted(res), |
|---|
| 378 | n/a | ['pkg1', 'pkg1.pkg3', 'pkg1.pkg3.pkg6', 'pkg5']) |
|---|
| 379 | n/a | |
|---|
| 380 | n/a | def test_resolve_name(self): |
|---|
| 381 | n/a | # test raw module name |
|---|
| 382 | n/a | tmpdir = self.mkdtemp() |
|---|
| 383 | n/a | sys.path.append(tmpdir) |
|---|
| 384 | n/a | self.addCleanup(sys.path.remove, tmpdir) |
|---|
| 385 | n/a | self.write_file((tmpdir, 'hello.py'), '') |
|---|
| 386 | n/a | |
|---|
| 387 | n/a | os.makedirs(os.path.join(tmpdir, 'a', 'b')) |
|---|
| 388 | n/a | self.write_file((tmpdir, 'a', '__init__.py'), '') |
|---|
| 389 | n/a | self.write_file((tmpdir, 'a', 'b', '__init__.py'), '') |
|---|
| 390 | n/a | self.write_file((tmpdir, 'a', 'b', 'c.py'), 'class Foo: pass') |
|---|
| 391 | n/a | self.write_file((tmpdir, 'a', 'b', 'd.py'), textwrap.dedent("""\ |
|---|
| 392 | n/a | class FooBar: |
|---|
| 393 | n/a | class Bar: |
|---|
| 394 | n/a | def baz(self): |
|---|
| 395 | n/a | pass |
|---|
| 396 | n/a | """)) |
|---|
| 397 | n/a | |
|---|
| 398 | n/a | # check Python, C and built-in module |
|---|
| 399 | n/a | self.assertEqual(resolve_name('hello').__name__, 'hello') |
|---|
| 400 | n/a | self.assertEqual(resolve_name('_csv').__name__, '_csv') |
|---|
| 401 | n/a | self.assertEqual(resolve_name('sys').__name__, 'sys') |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | # test module.attr |
|---|
| 404 | n/a | self.assertIs(resolve_name('builtins.str'), str) |
|---|
| 405 | n/a | self.assertIsNone(resolve_name('hello.__doc__')) |
|---|
| 406 | n/a | self.assertEqual(resolve_name('a.b.c.Foo').__name__, 'Foo') |
|---|
| 407 | n/a | self.assertEqual(resolve_name('a.b.d.FooBar.Bar.baz').__name__, 'baz') |
|---|
| 408 | n/a | |
|---|
| 409 | n/a | # error if module not found |
|---|
| 410 | n/a | self.assertRaises(ImportError, resolve_name, 'nonexistent') |
|---|
| 411 | n/a | self.assertRaises(ImportError, resolve_name, 'non.existent') |
|---|
| 412 | n/a | self.assertRaises(ImportError, resolve_name, 'a.no') |
|---|
| 413 | n/a | self.assertRaises(ImportError, resolve_name, 'a.b.no') |
|---|
| 414 | n/a | self.assertRaises(ImportError, resolve_name, 'a.b.no.no') |
|---|
| 415 | n/a | self.assertRaises(ImportError, resolve_name, 'inva-lid') |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | # looking up built-in names is not supported |
|---|
| 418 | n/a | self.assertRaises(ImportError, resolve_name, 'str') |
|---|
| 419 | n/a | |
|---|
| 420 | n/a | # error if module found but not attr |
|---|
| 421 | n/a | self.assertRaises(ImportError, resolve_name, 'a.b.Spam') |
|---|
| 422 | n/a | self.assertRaises(ImportError, resolve_name, 'a.b.c.Spam') |
|---|
| 423 | n/a | |
|---|
| 424 | n/a | @support.skip_2to3_optimize |
|---|
| 425 | n/a | def test_run_2to3_on_code(self): |
|---|
| 426 | n/a | content = "print 'test'" |
|---|
| 427 | n/a | converted_content = "print('test')" |
|---|
| 428 | n/a | file_handle = self.mktempfile() |
|---|
| 429 | n/a | file_name = file_handle.name |
|---|
| 430 | n/a | file_handle.write(content) |
|---|
| 431 | n/a | file_handle.flush() |
|---|
| 432 | n/a | file_handle.seek(0) |
|---|
| 433 | n/a | run_2to3([file_name]) |
|---|
| 434 | n/a | new_content = "".join(file_handle.read()) |
|---|
| 435 | n/a | file_handle.close() |
|---|
| 436 | n/a | self.assertEqual(new_content, converted_content) |
|---|
| 437 | n/a | |
|---|
| 438 | n/a | @support.skip_2to3_optimize |
|---|
| 439 | n/a | def test_run_2to3_on_doctests(self): |
|---|
| 440 | n/a | # to check if text files containing doctests only get converted. |
|---|
| 441 | n/a | content = ">>> print 'test'\ntest\n" |
|---|
| 442 | n/a | converted_content = ">>> print('test')\ntest\n\n" |
|---|
| 443 | n/a | file_handle = self.mktempfile() |
|---|
| 444 | n/a | file_name = file_handle.name |
|---|
| 445 | n/a | file_handle.write(content) |
|---|
| 446 | n/a | file_handle.flush() |
|---|
| 447 | n/a | file_handle.seek(0) |
|---|
| 448 | n/a | run_2to3([file_name], doctests_only=True) |
|---|
| 449 | n/a | new_content = "".join(file_handle.readlines()) |
|---|
| 450 | n/a | file_handle.close() |
|---|
| 451 | n/a | self.assertEqual(new_content, converted_content) |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | @unittest.skipUnless(os.name in ('nt', 'posix'), |
|---|
| 454 | n/a | 'runs only under posix or nt') |
|---|
| 455 | n/a | def test_spawn(self): |
|---|
| 456 | n/a | tmpdir = self.mkdtemp() |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | # creating something executable |
|---|
| 459 | n/a | # through the shell that returns 1 |
|---|
| 460 | n/a | if os.name == 'posix': |
|---|
| 461 | n/a | exe = os.path.join(tmpdir, 'foo.sh') |
|---|
| 462 | n/a | self.write_file(exe, '#!/bin/sh\nexit 1') |
|---|
| 463 | n/a | os.chmod(exe, 0o777) |
|---|
| 464 | n/a | else: |
|---|
| 465 | n/a | exe = os.path.join(tmpdir, 'foo.bat') |
|---|
| 466 | n/a | self.write_file(exe, 'exit 1') |
|---|
| 467 | n/a | |
|---|
| 468 | n/a | os.chmod(exe, 0o777) |
|---|
| 469 | n/a | self.assertRaises(PackagingExecError, spawn, [exe]) |
|---|
| 470 | n/a | |
|---|
| 471 | n/a | # now something that works |
|---|
| 472 | n/a | if os.name == 'posix': |
|---|
| 473 | n/a | exe = os.path.join(tmpdir, 'foo.sh') |
|---|
| 474 | n/a | self.write_file(exe, '#!/bin/sh\nexit 0') |
|---|
| 475 | n/a | os.chmod(exe, 0o777) |
|---|
| 476 | n/a | else: |
|---|
| 477 | n/a | exe = os.path.join(tmpdir, 'foo.bat') |
|---|
| 478 | n/a | self.write_file(exe, 'exit 0') |
|---|
| 479 | n/a | |
|---|
| 480 | n/a | os.chmod(exe, 0o777) |
|---|
| 481 | n/a | spawn([exe]) # should work without any error |
|---|
| 482 | n/a | |
|---|
| 483 | n/a | def test_server_registration(self): |
|---|
| 484 | n/a | # This test makes sure we know how to: |
|---|
| 485 | n/a | # 1. handle several sections in .pypirc |
|---|
| 486 | n/a | # 2. handle the old format |
|---|
| 487 | n/a | |
|---|
| 488 | n/a | # new format |
|---|
| 489 | n/a | self.write_file(self.rc, PYPIRC) |
|---|
| 490 | n/a | config = read_pypirc() |
|---|
| 491 | n/a | |
|---|
| 492 | n/a | config = sorted(config.items()) |
|---|
| 493 | n/a | expected = [('password', 'xxxx'), ('realm', 'pypi'), |
|---|
| 494 | n/a | ('repository', 'http://pypi.python.org/pypi'), |
|---|
| 495 | n/a | ('server', 'pypi'), ('username', 'me')] |
|---|
| 496 | n/a | self.assertEqual(config, expected) |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | # old format |
|---|
| 499 | n/a | self.write_file(self.rc, PYPIRC_OLD) |
|---|
| 500 | n/a | config = read_pypirc() |
|---|
| 501 | n/a | config = sorted(config.items()) |
|---|
| 502 | n/a | expected = [('password', 'secret'), ('realm', 'pypi'), |
|---|
| 503 | n/a | ('repository', 'http://pypi.python.org/pypi'), |
|---|
| 504 | n/a | ('server', 'server-login'), ('username', 'tarek')] |
|---|
| 505 | n/a | self.assertEqual(config, expected) |
|---|
| 506 | n/a | |
|---|
| 507 | n/a | def test_server_empty_registration(self): |
|---|
| 508 | n/a | rc = get_pypirc_path() |
|---|
| 509 | n/a | self.assertFalse(os.path.exists(rc)) |
|---|
| 510 | n/a | generate_pypirc('tarek', 'xxx') |
|---|
| 511 | n/a | self.assertTrue(os.path.exists(rc)) |
|---|
| 512 | n/a | with open(rc) as f: |
|---|
| 513 | n/a | content = f.read() |
|---|
| 514 | n/a | self.assertEqual(content, WANTED) |
|---|
| 515 | n/a | |
|---|
| 516 | n/a | def test_cfg_to_args(self): |
|---|
| 517 | n/a | opts = {'description-file': 'README', 'extra-files': '', |
|---|
| 518 | n/a | 'setup-hooks': 'packaging.tests.test_config.version_hook'} |
|---|
| 519 | n/a | self.write_file('setup.cfg', SETUP_CFG % opts, encoding='utf-8') |
|---|
| 520 | n/a | self.write_file('README', 'loooong description') |
|---|
| 521 | n/a | |
|---|
| 522 | n/a | with warnings.catch_warnings(): |
|---|
| 523 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
|---|
| 524 | n/a | args = cfg_to_args() |
|---|
| 525 | n/a | # use Distribution to get the contents of the setup.cfg file |
|---|
| 526 | n/a | dist = Distribution() |
|---|
| 527 | n/a | dist.parse_config_files() |
|---|
| 528 | n/a | metadata = dist.metadata |
|---|
| 529 | n/a | |
|---|
| 530 | n/a | self.assertEqual(args['name'], metadata['Name']) |
|---|
| 531 | n/a | # + .dev1 because the test SETUP_CFG also tests a hook function in |
|---|
| 532 | n/a | # test_config.py for appending to the version string |
|---|
| 533 | n/a | self.assertEqual(args['version'] + '.dev1', metadata['Version']) |
|---|
| 534 | n/a | self.assertEqual(args['author'], metadata['Author']) |
|---|
| 535 | n/a | self.assertEqual(args['author_email'], metadata['Author-Email']) |
|---|
| 536 | n/a | self.assertEqual(args['maintainer'], metadata['Maintainer']) |
|---|
| 537 | n/a | self.assertEqual(args['maintainer_email'], |
|---|
| 538 | n/a | metadata['Maintainer-Email']) |
|---|
| 539 | n/a | self.assertEqual(args['description'], metadata['Summary']) |
|---|
| 540 | n/a | self.assertEqual(args['long_description'], metadata['Description']) |
|---|
| 541 | n/a | self.assertEqual(args['classifiers'], metadata['Classifier']) |
|---|
| 542 | n/a | self.assertEqual(args['requires'], metadata['Requires-Dist']) |
|---|
| 543 | n/a | self.assertEqual(args['provides'], metadata['Provides-Dist']) |
|---|
| 544 | n/a | |
|---|
| 545 | n/a | self.assertEqual(args['package_dir'].get(''), dist.package_dir) |
|---|
| 546 | n/a | self.assertEqual(args['packages'], dist.packages) |
|---|
| 547 | n/a | self.assertEqual(args['scripts'], dist.scripts) |
|---|
| 548 | n/a | self.assertEqual(args['py_modules'], dist.py_modules) |
|---|
| 549 | n/a | |
|---|
| 550 | n/a | def test_generate_setup_py(self): |
|---|
| 551 | n/a | os.chdir(self.mkdtemp()) |
|---|
| 552 | n/a | self.write_file('setup.cfg', textwrap.dedent("""\ |
|---|
| 553 | n/a | [metadata] |
|---|
| 554 | n/a | name = SPAM |
|---|
| 555 | n/a | classifier = Programming Language :: Python |
|---|
| 556 | n/a | """)) |
|---|
| 557 | n/a | generate_setup_py() |
|---|
| 558 | n/a | self.assertTrue(os.path.exists('setup.py'), 'setup.py not created') |
|---|
| 559 | n/a | rc, out, err = assert_python_ok('setup.py', '--name') |
|---|
| 560 | n/a | self.assertEqual(out, b'SPAM\n') |
|---|
| 561 | n/a | self.assertEqual(err, b'') |
|---|
| 562 | n/a | |
|---|
| 563 | n/a | # a generated setup.py should complain if no setup.cfg is present |
|---|
| 564 | n/a | os.unlink('setup.cfg') |
|---|
| 565 | n/a | rc, out, err = assert_python_failure('setup.py', '--name') |
|---|
| 566 | n/a | self.assertIn(b'setup.cfg', err) |
|---|
| 567 | n/a | |
|---|
| 568 | n/a | def test_encode_multipart(self): |
|---|
| 569 | n/a | fields = [('username', 'wok'), ('password', 'secret')] |
|---|
| 570 | n/a | files = [('picture', 'wok.png', b'PNG89')] |
|---|
| 571 | n/a | content_type, body = encode_multipart(fields, files, b'-x') |
|---|
| 572 | n/a | self.assertEqual(b'multipart/form-data; boundary=-x', content_type) |
|---|
| 573 | n/a | self.assertEqual(EXPECTED_MULTIPART_OUTPUT, body.split(b'\r\n')) |
|---|
| 574 | n/a | |
|---|
| 575 | n/a | |
|---|
| 576 | n/a | class GlobTestCaseBase(support.TempdirManager, |
|---|
| 577 | n/a | support.LoggingCatcher, |
|---|
| 578 | n/a | unittest.TestCase): |
|---|
| 579 | n/a | |
|---|
| 580 | n/a | def build_files_tree(self, files): |
|---|
| 581 | n/a | tempdir = self.mkdtemp() |
|---|
| 582 | n/a | for filepath in files: |
|---|
| 583 | n/a | is_dir = filepath.endswith('/') |
|---|
| 584 | n/a | filepath = os.path.join(tempdir, *filepath.split('/')) |
|---|
| 585 | n/a | if is_dir: |
|---|
| 586 | n/a | dirname = filepath |
|---|
| 587 | n/a | else: |
|---|
| 588 | n/a | dirname = os.path.dirname(filepath) |
|---|
| 589 | n/a | if dirname and not os.path.exists(dirname): |
|---|
| 590 | n/a | os.makedirs(dirname) |
|---|
| 591 | n/a | if not is_dir: |
|---|
| 592 | n/a | self.write_file(filepath, 'babar') |
|---|
| 593 | n/a | return tempdir |
|---|
| 594 | n/a | |
|---|
| 595 | n/a | @staticmethod |
|---|
| 596 | n/a | def os_dependent_path(path): |
|---|
| 597 | n/a | path = path.rstrip('/').split('/') |
|---|
| 598 | n/a | return os.path.join(*path) |
|---|
| 599 | n/a | |
|---|
| 600 | n/a | def clean_tree(self, spec): |
|---|
| 601 | n/a | files = [] |
|---|
| 602 | n/a | for path, includes in spec.items(): |
|---|
| 603 | n/a | if includes: |
|---|
| 604 | n/a | files.append(self.os_dependent_path(path)) |
|---|
| 605 | n/a | return files |
|---|
| 606 | n/a | |
|---|
| 607 | n/a | |
|---|
| 608 | n/a | class GlobTestCase(GlobTestCaseBase): |
|---|
| 609 | n/a | |
|---|
| 610 | n/a | def assertGlobMatch(self, glob, spec): |
|---|
| 611 | n/a | tempdir = self.build_files_tree(spec) |
|---|
| 612 | n/a | expected = self.clean_tree(spec) |
|---|
| 613 | n/a | os.chdir(tempdir) |
|---|
| 614 | n/a | result = list(iglob(glob)) |
|---|
| 615 | n/a | self.assertCountEqual(expected, result) |
|---|
| 616 | n/a | |
|---|
| 617 | n/a | def test_regex_rich_glob(self): |
|---|
| 618 | n/a | matches = RICH_GLOB.findall( |
|---|
| 619 | n/a | r"babar aime les {fraises} est les {huitres}") |
|---|
| 620 | n/a | self.assertEqual(["fraises", "huitres"], matches) |
|---|
| 621 | n/a | |
|---|
| 622 | n/a | def test_simple_glob(self): |
|---|
| 623 | n/a | glob = '*.tp?' |
|---|
| 624 | n/a | spec = {'coucou.tpl': True, |
|---|
| 625 | n/a | 'coucou.tpj': True, |
|---|
| 626 | n/a | 'Donotwant': False} |
|---|
| 627 | n/a | self.assertGlobMatch(glob, spec) |
|---|
| 628 | n/a | |
|---|
| 629 | n/a | def test_simple_glob_in_dir(self): |
|---|
| 630 | n/a | glob = os.path.join('babar', '*.tp?') |
|---|
| 631 | n/a | spec = {'babar/coucou.tpl': True, |
|---|
| 632 | n/a | 'babar/coucou.tpj': True, |
|---|
| 633 | n/a | 'babar/toto.bin': False, |
|---|
| 634 | n/a | 'Donotwant': False} |
|---|
| 635 | n/a | self.assertGlobMatch(glob, spec) |
|---|
| 636 | n/a | |
|---|
| 637 | n/a | def test_recursive_glob_head(self): |
|---|
| 638 | n/a | glob = os.path.join('**', 'tip', '*.t?l') |
|---|
| 639 | n/a | spec = {'babar/zaza/zuzu/tip/coucou.tpl': True, |
|---|
| 640 | n/a | 'babar/z/tip/coucou.tpl': True, |
|---|
| 641 | n/a | 'babar/tip/coucou.tpl': True, |
|---|
| 642 | n/a | 'babar/zeop/tip/babar/babar.tpl': False, |
|---|
| 643 | n/a | 'babar/z/tip/coucou.bin': False, |
|---|
| 644 | n/a | 'babar/toto.bin': False, |
|---|
| 645 | n/a | 'zozo/zuzu/tip/babar.tpl': True, |
|---|
| 646 | n/a | 'zozo/tip/babar.tpl': True, |
|---|
| 647 | n/a | 'Donotwant': False} |
|---|
| 648 | n/a | self.assertGlobMatch(glob, spec) |
|---|
| 649 | n/a | |
|---|
| 650 | n/a | def test_recursive_glob_tail(self): |
|---|
| 651 | n/a | glob = os.path.join('babar', '**') |
|---|
| 652 | n/a | spec = {'babar/zaza/': True, |
|---|
| 653 | n/a | 'babar/zaza/zuzu/': True, |
|---|
| 654 | n/a | 'babar/zaza/zuzu/babar.xml': True, |
|---|
| 655 | n/a | 'babar/zaza/zuzu/toto.xml': True, |
|---|
| 656 | n/a | 'babar/zaza/zuzu/toto.csv': True, |
|---|
| 657 | n/a | 'babar/zaza/coucou.tpl': True, |
|---|
| 658 | n/a | 'babar/bubu.tpl': True, |
|---|
| 659 | n/a | 'zozo/zuzu/tip/babar.tpl': False, |
|---|
| 660 | n/a | 'zozo/tip/babar.tpl': False, |
|---|
| 661 | n/a | 'Donotwant': False} |
|---|
| 662 | n/a | self.assertGlobMatch(glob, spec) |
|---|
| 663 | n/a | |
|---|
| 664 | n/a | def test_recursive_glob_middle(self): |
|---|
| 665 | n/a | glob = os.path.join('babar', '**', 'tip', '*.t?l') |
|---|
| 666 | n/a | spec = {'babar/zaza/zuzu/tip/coucou.tpl': True, |
|---|
| 667 | n/a | 'babar/z/tip/coucou.tpl': True, |
|---|
| 668 | n/a | 'babar/tip/coucou.tpl': True, |
|---|
| 669 | n/a | 'babar/zeop/tip/babar/babar.tpl': False, |
|---|
| 670 | n/a | 'babar/z/tip/coucou.bin': False, |
|---|
| 671 | n/a | 'babar/toto.bin': False, |
|---|
| 672 | n/a | 'zozo/zuzu/tip/babar.tpl': False, |
|---|
| 673 | n/a | 'zozo/tip/babar.tpl': False, |
|---|
| 674 | n/a | 'Donotwant': False} |
|---|
| 675 | n/a | self.assertGlobMatch(glob, spec) |
|---|
| 676 | n/a | |
|---|
| 677 | n/a | def test_glob_set_tail(self): |
|---|
| 678 | n/a | glob = os.path.join('bin', '*.{bin,sh,exe}') |
|---|
| 679 | n/a | spec = {'bin/babar.bin': True, |
|---|
| 680 | n/a | 'bin/zephir.sh': True, |
|---|
| 681 | n/a | 'bin/celestine.exe': True, |
|---|
| 682 | n/a | 'bin/cornelius.bat': False, |
|---|
| 683 | n/a | 'bin/cornelius.xml': False, |
|---|
| 684 | n/a | 'toto/yurg': False, |
|---|
| 685 | n/a | 'Donotwant': False} |
|---|
| 686 | n/a | self.assertGlobMatch(glob, spec) |
|---|
| 687 | n/a | |
|---|
| 688 | n/a | def test_glob_set_middle(self): |
|---|
| 689 | n/a | glob = os.path.join('xml', '{babar,toto}.xml') |
|---|
| 690 | n/a | spec = {'xml/babar.xml': True, |
|---|
| 691 | n/a | 'xml/toto.xml': True, |
|---|
| 692 | n/a | 'xml/babar.xslt': False, |
|---|
| 693 | n/a | 'xml/cornelius.sgml': False, |
|---|
| 694 | n/a | 'xml/zephir.xml': False, |
|---|
| 695 | n/a | 'toto/yurg.xml': False, |
|---|
| 696 | n/a | 'Donotwant': False} |
|---|
| 697 | n/a | self.assertGlobMatch(glob, spec) |
|---|
| 698 | n/a | |
|---|
| 699 | n/a | def test_glob_set_head(self): |
|---|
| 700 | n/a | glob = os.path.join('{xml,xslt}', 'babar.*') |
|---|
| 701 | n/a | spec = {'xml/babar.xml': True, |
|---|
| 702 | n/a | 'xml/toto.xml': False, |
|---|
| 703 | n/a | 'xslt/babar.xslt': True, |
|---|
| 704 | n/a | 'xslt/toto.xslt': False, |
|---|
| 705 | n/a | 'toto/yurg.xml': False, |
|---|
| 706 | n/a | 'Donotwant': False} |
|---|
| 707 | n/a | self.assertGlobMatch(glob, spec) |
|---|
| 708 | n/a | |
|---|
| 709 | n/a | def test_glob_all(self): |
|---|
| 710 | n/a | dirs = '{%s,%s}' % (os.path.join('xml', '*'), |
|---|
| 711 | n/a | os.path.join('xslt', '**')) |
|---|
| 712 | n/a | glob = os.path.join(dirs, 'babar.xml') |
|---|
| 713 | n/a | spec = {'xml/a/babar.xml': True, |
|---|
| 714 | n/a | 'xml/b/babar.xml': True, |
|---|
| 715 | n/a | 'xml/a/c/babar.xml': False, |
|---|
| 716 | n/a | 'xslt/a/babar.xml': True, |
|---|
| 717 | n/a | 'xslt/b/babar.xml': True, |
|---|
| 718 | n/a | 'xslt/a/c/babar.xml': True, |
|---|
| 719 | n/a | 'toto/yurg.xml': False, |
|---|
| 720 | n/a | 'Donotwant': False} |
|---|
| 721 | n/a | self.assertGlobMatch(glob, spec) |
|---|
| 722 | n/a | |
|---|
| 723 | n/a | def test_invalid_glob_pattern(self): |
|---|
| 724 | n/a | invalids = [ |
|---|
| 725 | n/a | 'ppooa**', |
|---|
| 726 | n/a | 'azzaeaz4**/', |
|---|
| 727 | n/a | '/**ddsfs', |
|---|
| 728 | n/a | '**##1e"&e', |
|---|
| 729 | n/a | 'DSFb**c009', |
|---|
| 730 | n/a | '{', |
|---|
| 731 | n/a | '{aaQSDFa', |
|---|
| 732 | n/a | '}', |
|---|
| 733 | n/a | 'aQSDFSaa}', |
|---|
| 734 | n/a | '{**a,', |
|---|
| 735 | n/a | ',**a}', |
|---|
| 736 | n/a | '{a**,', |
|---|
| 737 | n/a | ',b**}', |
|---|
| 738 | n/a | '{a**a,babar}', |
|---|
| 739 | n/a | '{bob,b**z}', |
|---|
| 740 | n/a | ] |
|---|
| 741 | n/a | for pattern in invalids: |
|---|
| 742 | n/a | self.assertRaises(ValueError, iglob, pattern) |
|---|
| 743 | n/a | |
|---|
| 744 | n/a | |
|---|
| 745 | n/a | class EggInfoToDistInfoTestCase(support.TempdirManager, |
|---|
| 746 | n/a | support.LoggingCatcher, |
|---|
| 747 | n/a | unittest.TestCase): |
|---|
| 748 | n/a | |
|---|
| 749 | n/a | def get_metadata_file_paths(self, distinfo_path): |
|---|
| 750 | n/a | req_metadata_files = ['METADATA', 'RECORD', 'INSTALLER'] |
|---|
| 751 | n/a | metadata_file_paths = [] |
|---|
| 752 | n/a | for metadata_file in req_metadata_files: |
|---|
| 753 | n/a | path = os.path.join(distinfo_path, metadata_file) |
|---|
| 754 | n/a | metadata_file_paths.append(path) |
|---|
| 755 | n/a | return metadata_file_paths |
|---|
| 756 | n/a | |
|---|
| 757 | n/a | def test_egginfo_to_distinfo_setuptools(self): |
|---|
| 758 | n/a | distinfo = 'hello-0.1.1-py3.3.dist-info' |
|---|
| 759 | n/a | egginfo = 'hello-0.1.1-py3.3.egg-info' |
|---|
| 760 | n/a | dirs = [egginfo] |
|---|
| 761 | n/a | files = ['hello.py', 'hello.pyc'] |
|---|
| 762 | n/a | extra_metadata = ['dependency_links.txt', 'entry_points.txt', |
|---|
| 763 | n/a | 'not-zip-safe', 'PKG-INFO', 'top_level.txt', |
|---|
| 764 | n/a | 'SOURCES.txt'] |
|---|
| 765 | n/a | for f in extra_metadata: |
|---|
| 766 | n/a | files.append(os.path.join(egginfo, f)) |
|---|
| 767 | n/a | |
|---|
| 768 | n/a | tempdir, record_file = self.build_dist_tree(files, dirs) |
|---|
| 769 | n/a | distinfo_path = os.path.join(tempdir, distinfo) |
|---|
| 770 | n/a | egginfo_path = os.path.join(tempdir, egginfo) |
|---|
| 771 | n/a | metadata_file_paths = self.get_metadata_file_paths(distinfo_path) |
|---|
| 772 | n/a | |
|---|
| 773 | n/a | egginfo_to_distinfo(record_file) |
|---|
| 774 | n/a | # test that directories and files get created |
|---|
| 775 | n/a | self.assertTrue(os.path.isdir(distinfo_path)) |
|---|
| 776 | n/a | self.assertTrue(os.path.isdir(egginfo_path)) |
|---|
| 777 | n/a | |
|---|
| 778 | n/a | for mfile in metadata_file_paths: |
|---|
| 779 | n/a | self.assertTrue(os.path.isfile(mfile)) |
|---|
| 780 | n/a | |
|---|
| 781 | n/a | def test_egginfo_to_distinfo_distutils(self): |
|---|
| 782 | n/a | distinfo = 'hello-0.1.1-py3.3.dist-info' |
|---|
| 783 | n/a | egginfo = 'hello-0.1.1-py3.3.egg-info' |
|---|
| 784 | n/a | # egginfo is a file in distutils which contains the metadata |
|---|
| 785 | n/a | files = ['hello.py', 'hello.pyc', egginfo] |
|---|
| 786 | n/a | |
|---|
| 787 | n/a | tempdir, record_file = self.build_dist_tree(files, dirs=[]) |
|---|
| 788 | n/a | distinfo_path = os.path.join(tempdir, distinfo) |
|---|
| 789 | n/a | egginfo_path = os.path.join(tempdir, egginfo) |
|---|
| 790 | n/a | metadata_file_paths = self.get_metadata_file_paths(distinfo_path) |
|---|
| 791 | n/a | |
|---|
| 792 | n/a | egginfo_to_distinfo(record_file) |
|---|
| 793 | n/a | # test that directories and files get created |
|---|
| 794 | n/a | self.assertTrue(os.path.isdir(distinfo_path)) |
|---|
| 795 | n/a | self.assertTrue(os.path.isfile(egginfo_path)) |
|---|
| 796 | n/a | |
|---|
| 797 | n/a | for mfile in metadata_file_paths: |
|---|
| 798 | n/a | self.assertTrue(os.path.isfile(mfile)) |
|---|
| 799 | n/a | |
|---|
| 800 | n/a | def build_dist_tree(self, files, dirs): |
|---|
| 801 | n/a | tempdir = self.mkdtemp() |
|---|
| 802 | n/a | record_file_path = os.path.join(tempdir, 'RECORD') |
|---|
| 803 | n/a | file_paths, dir_paths = ([], []) |
|---|
| 804 | n/a | for d in dirs: |
|---|
| 805 | n/a | path = os.path.join(tempdir, d) |
|---|
| 806 | n/a | os.makedirs(path) |
|---|
| 807 | n/a | dir_paths.append(path) |
|---|
| 808 | n/a | for f in files: |
|---|
| 809 | n/a | path = os.path.join(tempdir, f) |
|---|
| 810 | n/a | with open(path, 'w') as _f: |
|---|
| 811 | n/a | _f.write(f) |
|---|
| 812 | n/a | file_paths.append(path) |
|---|
| 813 | n/a | |
|---|
| 814 | n/a | with open(record_file_path, 'w') as record_file: |
|---|
| 815 | n/a | for fpath in file_paths: |
|---|
| 816 | n/a | record_file.write(fpath + '\n') |
|---|
| 817 | n/a | for dpath in dir_paths: |
|---|
| 818 | n/a | record_file.write(dpath + '\n') |
|---|
| 819 | n/a | |
|---|
| 820 | n/a | return (tempdir, record_file_path) |
|---|
| 821 | n/a | |
|---|
| 822 | n/a | |
|---|
| 823 | n/a | class PackagingLibChecks(support.TempdirManager, |
|---|
| 824 | n/a | support.LoggingCatcher, |
|---|
| 825 | n/a | unittest.TestCase): |
|---|
| 826 | n/a | |
|---|
| 827 | n/a | def setUp(self): |
|---|
| 828 | n/a | super(PackagingLibChecks, self).setUp() |
|---|
| 829 | n/a | self._empty_dir = self.mkdtemp() |
|---|
| 830 | n/a | |
|---|
| 831 | n/a | def test_empty_package_is_not_based_on_anything(self): |
|---|
| 832 | n/a | self.assertFalse(is_setuptools(self._empty_dir)) |
|---|
| 833 | n/a | self.assertFalse(is_distutils(self._empty_dir)) |
|---|
| 834 | n/a | self.assertFalse(is_packaging(self._empty_dir)) |
|---|
| 835 | n/a | |
|---|
| 836 | n/a | def test_setup_py_importing_setuptools_is_setuptools_based(self): |
|---|
| 837 | n/a | self.assertTrue(is_setuptools(self._setuptools_setup_py_pkg())) |
|---|
| 838 | n/a | |
|---|
| 839 | n/a | def test_egg_info_dir_and_setup_py_is_setuptools_based(self): |
|---|
| 840 | n/a | self.assertTrue(is_setuptools(self._setuptools_egg_info_pkg())) |
|---|
| 841 | n/a | |
|---|
| 842 | n/a | def test_egg_info_and_non_setuptools_setup_py_is_setuptools_based(self): |
|---|
| 843 | n/a | self.assertTrue(is_setuptools(self._egg_info_with_no_setuptools())) |
|---|
| 844 | n/a | |
|---|
| 845 | n/a | def test_setup_py_not_importing_setuptools_is_not_setuptools_based(self): |
|---|
| 846 | n/a | self.assertFalse(is_setuptools(self._random_setup_py_pkg())) |
|---|
| 847 | n/a | |
|---|
| 848 | n/a | def test_setup_py_importing_distutils_is_distutils_based(self): |
|---|
| 849 | n/a | self.assertTrue(is_distutils(self._distutils_setup_py_pkg())) |
|---|
| 850 | n/a | |
|---|
| 851 | n/a | def test_pkg_info_file_and_setup_py_is_distutils_based(self): |
|---|
| 852 | n/a | self.assertTrue(is_distutils(self._distutils_pkg_info())) |
|---|
| 853 | n/a | |
|---|
| 854 | n/a | def test_pkg_info_and_non_distutils_setup_py_is_distutils_based(self): |
|---|
| 855 | n/a | self.assertTrue(is_distutils(self._pkg_info_with_no_distutils())) |
|---|
| 856 | n/a | |
|---|
| 857 | n/a | def test_setup_py_not_importing_distutils_is_not_distutils_based(self): |
|---|
| 858 | n/a | self.assertFalse(is_distutils(self._random_setup_py_pkg())) |
|---|
| 859 | n/a | |
|---|
| 860 | n/a | def test_setup_cfg_with_no_metadata_section_is_not_packaging_based(self): |
|---|
| 861 | n/a | self.assertFalse(is_packaging(self._setup_cfg_with_no_metadata_pkg())) |
|---|
| 862 | n/a | |
|---|
| 863 | n/a | def test_setup_cfg_with_valid_metadata_section_is_packaging_based(self): |
|---|
| 864 | n/a | self.assertTrue(is_packaging(self._valid_setup_cfg_pkg())) |
|---|
| 865 | n/a | |
|---|
| 866 | n/a | def test_setup_cfg_and_invalid_setup_cfg_is_not_packaging_based(self): |
|---|
| 867 | n/a | self.assertFalse(is_packaging(self._invalid_setup_cfg_pkg())) |
|---|
| 868 | n/a | |
|---|
| 869 | n/a | def test_get_install_method_with_setuptools_pkg(self): |
|---|
| 870 | n/a | path = self._setuptools_setup_py_pkg() |
|---|
| 871 | n/a | self.assertEqual("setuptools", get_install_method(path)) |
|---|
| 872 | n/a | |
|---|
| 873 | n/a | def test_get_install_method_with_distutils_pkg(self): |
|---|
| 874 | n/a | path = self._distutils_pkg_info() |
|---|
| 875 | n/a | self.assertEqual("distutils", get_install_method(path)) |
|---|
| 876 | n/a | |
|---|
| 877 | n/a | def test_get_install_method_with_packaging_pkg(self): |
|---|
| 878 | n/a | path = self._valid_setup_cfg_pkg() |
|---|
| 879 | n/a | self.assertEqual("packaging", get_install_method(path)) |
|---|
| 880 | n/a | |
|---|
| 881 | n/a | def test_get_install_method_with_unknown_pkg(self): |
|---|
| 882 | n/a | path = self._invalid_setup_cfg_pkg() |
|---|
| 883 | n/a | self.assertRaises(InstallationException, get_install_method, path) |
|---|
| 884 | n/a | |
|---|
| 885 | n/a | def test_is_setuptools_logs_setup_py_text_found(self): |
|---|
| 886 | n/a | is_setuptools(self._setuptools_setup_py_pkg()) |
|---|
| 887 | n/a | expected = ['setup.py file found.', |
|---|
| 888 | n/a | 'No egg-info directory found.', |
|---|
| 889 | n/a | 'Found setuptools text in setup.py.'] |
|---|
| 890 | n/a | self.assertEqual(expected, self.get_logs(logging.DEBUG)) |
|---|
| 891 | n/a | |
|---|
| 892 | n/a | def test_is_setuptools_logs_setup_py_text_not_found(self): |
|---|
| 893 | n/a | directory = self._random_setup_py_pkg() |
|---|
| 894 | n/a | is_setuptools(directory) |
|---|
| 895 | n/a | expected = ['setup.py file found.', 'No egg-info directory found.', |
|---|
| 896 | n/a | 'No setuptools text found in setup.py.'] |
|---|
| 897 | n/a | self.assertEqual(expected, self.get_logs(logging.DEBUG)) |
|---|
| 898 | n/a | |
|---|
| 899 | n/a | def test_is_setuptools_logs_egg_info_dir_found(self): |
|---|
| 900 | n/a | is_setuptools(self._setuptools_egg_info_pkg()) |
|---|
| 901 | n/a | expected = ['setup.py file found.', 'Found egg-info directory.'] |
|---|
| 902 | n/a | self.assertEqual(expected, self.get_logs(logging.DEBUG)) |
|---|
| 903 | n/a | |
|---|
| 904 | n/a | def test_is_distutils_logs_setup_py_text_found(self): |
|---|
| 905 | n/a | is_distutils(self._distutils_setup_py_pkg()) |
|---|
| 906 | n/a | expected = ['setup.py file found.', |
|---|
| 907 | n/a | 'No PKG-INFO file found.', |
|---|
| 908 | n/a | 'Found distutils text in setup.py.'] |
|---|
| 909 | n/a | self.assertEqual(expected, self.get_logs(logging.DEBUG)) |
|---|
| 910 | n/a | |
|---|
| 911 | n/a | def test_is_distutils_logs_setup_py_text_not_found(self): |
|---|
| 912 | n/a | directory = self._random_setup_py_pkg() |
|---|
| 913 | n/a | is_distutils(directory) |
|---|
| 914 | n/a | expected = ['setup.py file found.', 'No PKG-INFO file found.', |
|---|
| 915 | n/a | 'No distutils text found in setup.py.'] |
|---|
| 916 | n/a | self.assertEqual(expected, self.get_logs(logging.DEBUG)) |
|---|
| 917 | n/a | |
|---|
| 918 | n/a | def test_is_distutils_logs_pkg_info_file_found(self): |
|---|
| 919 | n/a | is_distutils(self._distutils_pkg_info()) |
|---|
| 920 | n/a | expected = ['setup.py file found.', 'PKG-INFO file found.'] |
|---|
| 921 | n/a | self.assertEqual(expected, self.get_logs(logging.DEBUG)) |
|---|
| 922 | n/a | |
|---|
| 923 | n/a | def test_is_packaging_logs_setup_cfg_found(self): |
|---|
| 924 | n/a | is_packaging(self._valid_setup_cfg_pkg()) |
|---|
| 925 | n/a | expected = ['setup.cfg file found.'] |
|---|
| 926 | n/a | self.assertEqual(expected, self.get_logs(logging.DEBUG)) |
|---|
| 927 | n/a | |
|---|
| 928 | n/a | def test_is_packaging_logs_setup_cfg_not_found(self): |
|---|
| 929 | n/a | is_packaging(self._empty_dir) |
|---|
| 930 | n/a | expected = ['No setup.cfg file found.'] |
|---|
| 931 | n/a | self.assertEqual(expected, self.get_logs(logging.DEBUG)) |
|---|
| 932 | n/a | |
|---|
| 933 | n/a | def _write_setuptools_setup_py(self, directory): |
|---|
| 934 | n/a | self.write_file((directory, 'setup.py'), |
|---|
| 935 | n/a | "from setuptools import setup") |
|---|
| 936 | n/a | |
|---|
| 937 | n/a | def _write_distutils_setup_py(self, directory): |
|---|
| 938 | n/a | self.write_file([directory, 'setup.py'], |
|---|
| 939 | n/a | "from distutils.core import setup") |
|---|
| 940 | n/a | |
|---|
| 941 | n/a | def _write_packaging_setup_cfg(self, directory): |
|---|
| 942 | n/a | self.write_file([directory, 'setup.cfg'], |
|---|
| 943 | n/a | ("[metadata]\n" |
|---|
| 944 | n/a | "name = mypackage\n" |
|---|
| 945 | n/a | "version = 0.1.0\n")) |
|---|
| 946 | n/a | |
|---|
| 947 | n/a | def _setuptools_setup_py_pkg(self): |
|---|
| 948 | n/a | tmp = self.mkdtemp() |
|---|
| 949 | n/a | self._write_setuptools_setup_py(tmp) |
|---|
| 950 | n/a | return tmp |
|---|
| 951 | n/a | |
|---|
| 952 | n/a | def _distutils_setup_py_pkg(self): |
|---|
| 953 | n/a | tmp = self.mkdtemp() |
|---|
| 954 | n/a | self._write_distutils_setup_py(tmp) |
|---|
| 955 | n/a | return tmp |
|---|
| 956 | n/a | |
|---|
| 957 | n/a | def _valid_setup_cfg_pkg(self): |
|---|
| 958 | n/a | tmp = self.mkdtemp() |
|---|
| 959 | n/a | self._write_packaging_setup_cfg(tmp) |
|---|
| 960 | n/a | return tmp |
|---|
| 961 | n/a | |
|---|
| 962 | n/a | def _setuptools_egg_info_pkg(self): |
|---|
| 963 | n/a | tmp = self.mkdtemp() |
|---|
| 964 | n/a | self._write_setuptools_setup_py(tmp) |
|---|
| 965 | n/a | tempfile.mkdtemp(suffix='.egg-info', dir=tmp) |
|---|
| 966 | n/a | return tmp |
|---|
| 967 | n/a | |
|---|
| 968 | n/a | def _distutils_pkg_info(self): |
|---|
| 969 | n/a | tmp = self._distutils_setup_py_pkg() |
|---|
| 970 | n/a | self.write_file([tmp, 'PKG-INFO'], '', encoding='UTF-8') |
|---|
| 971 | n/a | return tmp |
|---|
| 972 | n/a | |
|---|
| 973 | n/a | def _setup_cfg_with_no_metadata_pkg(self): |
|---|
| 974 | n/a | tmp = self.mkdtemp() |
|---|
| 975 | n/a | self.write_file([tmp, 'setup.cfg'], |
|---|
| 976 | n/a | ("[othersection]\n" |
|---|
| 977 | n/a | "foo = bar\n")) |
|---|
| 978 | n/a | return tmp |
|---|
| 979 | n/a | |
|---|
| 980 | n/a | def _invalid_setup_cfg_pkg(self): |
|---|
| 981 | n/a | tmp = self.mkdtemp() |
|---|
| 982 | n/a | self.write_file([tmp, 'setup.cfg'], |
|---|
| 983 | n/a | ("[metadata]\n" |
|---|
| 984 | n/a | "name = john\n" |
|---|
| 985 | n/a | "last_name = doe\n")) |
|---|
| 986 | n/a | return tmp |
|---|
| 987 | n/a | |
|---|
| 988 | n/a | def _egg_info_with_no_setuptools(self): |
|---|
| 989 | n/a | tmp = self._random_setup_py_pkg() |
|---|
| 990 | n/a | tempfile.mkdtemp(suffix='.egg-info', dir=tmp) |
|---|
| 991 | n/a | return tmp |
|---|
| 992 | n/a | |
|---|
| 993 | n/a | def _pkg_info_with_no_distutils(self): |
|---|
| 994 | n/a | tmp = self._random_setup_py_pkg() |
|---|
| 995 | n/a | self.write_file([tmp, 'PKG-INFO'], '', encoding='UTF-8') |
|---|
| 996 | n/a | return tmp |
|---|
| 997 | n/a | |
|---|
| 998 | n/a | def _random_setup_py_pkg(self): |
|---|
| 999 | n/a | tmp = self.mkdtemp() |
|---|
| 1000 | n/a | self.write_file((tmp, 'setup.py'), "from mypackage import setup") |
|---|
| 1001 | n/a | return tmp |
|---|
| 1002 | n/a | |
|---|
| 1003 | n/a | |
|---|
| 1004 | n/a | def test_suite(): |
|---|
| 1005 | n/a | suite = unittest.makeSuite(UtilTestCase) |
|---|
| 1006 | n/a | suite.addTest(unittest.makeSuite(GlobTestCase)) |
|---|
| 1007 | n/a | suite.addTest(unittest.makeSuite(EggInfoToDistInfoTestCase)) |
|---|
| 1008 | n/a | suite.addTest(unittest.makeSuite(PackagingLibChecks)) |
|---|
| 1009 | n/a | return suite |
|---|
| 1010 | n/a | |
|---|
| 1011 | n/a | |
|---|
| 1012 | n/a | if __name__ == "__main__": |
|---|
| 1013 | n/a | unittest.main(defaultTest="test_suite") |
|---|