| 1 |
import sys |
| 2 |
import copy |
| 3 |
import json |
| 4 |
import shutil |
| 5 |
import pathlib |
| 6 |
import textwrap |
| 7 |
import functools |
| 8 |
import contextlib |
| 9 |
|
| 10 |
from test.support import import_helper |
| 11 |
from test.support import os_helper |
| 12 |
from test.support import requires_zlib |
| 13 |
|
| 14 |
from . import _path |
| 15 |
from ._path import FilesSpec |
| 16 |
|
| 17 |
|
| 18 |
try: |
| 19 |
from importlib import resources # type: ignore |
| 20 |
|
| 21 |
getattr(resources, 'files') |
| 22 |
getattr(resources, 'as_file') |
| 23 |
except (ImportError, AttributeError): |
| 24 |
import importlib_resources as resources # type: ignore |
| 25 |
|
| 26 |
|
| 27 |
@contextlib.contextmanager |
| 28 |
def tmp_path(): |
| 29 |
""" |
| 30 |
Like os_helper.temp_dir, but yields a pathlib.Path. |
| 31 |
""" |
| 32 |
with os_helper.temp_dir() as path: |
| 33 |
yield pathlib.Path(path) |
| 34 |
|
| 35 |
|
| 36 |
@contextlib.contextmanager |
| 37 |
def install_finder(finder): |
| 38 |
sys.meta_path.append(finder) |
| 39 |
try: |
| 40 |
yield |
| 41 |
finally: |
| 42 |
sys.meta_path.remove(finder) |
| 43 |
|
| 44 |
|
| 45 |
class Fixtures: |
| 46 |
def setUp(self): |
| 47 |
self.fixtures = contextlib.ExitStack() |
| 48 |
self.addCleanup(self.fixtures.close) |
| 49 |
|
| 50 |
|
| 51 |
class SiteDir(Fixtures): |
| 52 |
def setUp(self): |
| 53 |
super().setUp() |
| 54 |
self.site_dir = self.fixtures.enter_context(tmp_path()) |
| 55 |
|
| 56 |
|
| 57 |
class OnSysPath(Fixtures): |
| 58 |
@staticmethod |
| 59 |
@contextlib.contextmanager |
| 60 |
def add_sys_path(dir): |
| 61 |
sys.path[:0] = [str(dir)] |
| 62 |
try: |
| 63 |
yield |
| 64 |
finally: |
| 65 |
sys.path.remove(str(dir)) |
| 66 |
|
| 67 |
def setUp(self): |
| 68 |
super().setUp() |
| 69 |
self.fixtures.enter_context(self.add_sys_path(self.site_dir)) |
| 70 |
self.fixtures.enter_context(import_helper.isolated_modules()) |
| 71 |
|
| 72 |
|
| 73 |
class SiteBuilder(SiteDir): |
| 74 |
def setUp(self): |
| 75 |
super().setUp() |
| 76 |
for cls in self.__class__.mro(): |
| 77 |
with contextlib.suppress(AttributeError): |
| 78 |
build_files(cls.files, prefix=self.site_dir) |
| 79 |
|
| 80 |
|
| 81 |
class DistInfoPkg(OnSysPath, SiteBuilder): |
| 82 |
files: FilesSpec = { |
| 83 |
"distinfo_pkg-1.0.0.dist-info": { |
| 84 |
"METADATA": """ |
| 85 |
Name: distinfo-pkg |
| 86 |
Author: Steven Ma |
| 87 |
Version: 1.0.0 |
| 88 |
Requires-Dist: wheel >= 1.0 |
| 89 |
Requires-Dist: pytest; extra == 'test' |
| 90 |
Keywords: sample package |
| 91 |
|
| 92 |
Once upon a time |
| 93 |
There was a distinfo pkg |
| 94 |
""", |
| 95 |
"RECORD": "mod.py,sha256=abc,20\n", |
| 96 |
"entry_points.txt": """ |
| 97 |
[entries] |
| 98 |
main = mod:main |
| 99 |
ns:sub = mod:main |
| 100 |
""", |
| 101 |
}, |
| 102 |
"mod.py": """ |
| 103 |
def main(): |
| 104 |
print("hello world") |
| 105 |
""", |
| 106 |
} |
| 107 |
|
| 108 |
def make_uppercase(self): |
| 109 |
""" |
| 110 |
Rewrite metadata with everything uppercase. |
| 111 |
""" |
| 112 |
shutil.rmtree(self.site_dir / "distinfo_pkg-1.0.0.dist-info") |
| 113 |
files = copy.deepcopy(DistInfoPkg.files) |
| 114 |
info = files["distinfo_pkg-1.0.0.dist-info"] |
| 115 |
info["METADATA"] = info["METADATA"].upper() |
| 116 |
build_files(files, self.site_dir) |
| 117 |
|
| 118 |
|
| 119 |
class DistInfoPkgEditable(DistInfoPkg): |
| 120 |
""" |
| 121 |
Package with a PEP 660 direct_url.json. |
| 122 |
""" |
| 123 |
|
| 124 |
some_hash = '524127ce937f7cb65665130c695abd18ca386f60bb29687efb976faa1596fdcc' |
| 125 |
files: FilesSpec = { |
| 126 |
'distinfo_pkg-1.0.0.dist-info': { |
| 127 |
'direct_url.json': json.dumps({ |
| 128 |
"archive_info": { |
| 129 |
"hash": f"sha256={some_hash}", |
| 130 |
"hashes": {"sha256": f"{some_hash}"}, |
| 131 |
}, |
| 132 |
"url": "file:///path/to/distinfo_pkg-1.0.0.editable-py3-none-any.whl", |
| 133 |
}) |
| 134 |
}, |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
class DistInfoPkgWithDot(OnSysPath, SiteBuilder): |
| 139 |
files: FilesSpec = { |
| 140 |
"pkg_dot-1.0.0.dist-info": { |
| 141 |
"METADATA": """ |
| 142 |
Name: pkg.dot |
| 143 |
Version: 1.0.0 |
| 144 |
""", |
| 145 |
}, |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
class DistInfoPkgWithDotLegacy(OnSysPath, SiteBuilder): |
| 150 |
files: FilesSpec = { |
| 151 |
"pkg.dot-1.0.0.dist-info": { |
| 152 |
"METADATA": """ |
| 153 |
Name: pkg.dot |
| 154 |
Version: 1.0.0 |
| 155 |
""", |
| 156 |
}, |
| 157 |
"pkg.lot.egg-info": { |
| 158 |
"METADATA": """ |
| 159 |
Name: pkg.lot |
| 160 |
Version: 1.0.0 |
| 161 |
""", |
| 162 |
}, |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
class DistInfoPkgOffPath(SiteBuilder): |
| 167 |
files = DistInfoPkg.files |
| 168 |
|
| 169 |
|
| 170 |
class EggInfoPkg(OnSysPath, SiteBuilder): |
| 171 |
files: FilesSpec = { |
| 172 |
"egginfo_pkg.egg-info": { |
| 173 |
"PKG-INFO": """ |
| 174 |
Name: egginfo-pkg |
| 175 |
Author: Steven Ma |
| 176 |
License: Unknown |
| 177 |
Version: 1.0.0 |
| 178 |
Classifier: Intended Audience :: Developers |
| 179 |
Classifier: Topic :: Software Development :: Libraries |
| 180 |
Keywords: sample package |
| 181 |
Description: Once upon a time |
| 182 |
There was an egginfo package |
| 183 |
""", |
| 184 |
"SOURCES.txt": """ |
| 185 |
mod.py |
| 186 |
egginfo_pkg.egg-info/top_level.txt |
| 187 |
""", |
| 188 |
"entry_points.txt": """ |
| 189 |
[entries] |
| 190 |
main = mod:main |
| 191 |
""", |
| 192 |
"requires.txt": """ |
| 193 |
wheel >= 1.0; python_version >= "2.7" |
| 194 |
[test] |
| 195 |
pytest |
| 196 |
""", |
| 197 |
"top_level.txt": "mod\n", |
| 198 |
}, |
| 199 |
"mod.py": """ |
| 200 |
def main(): |
| 201 |
print("hello world") |
| 202 |
""", |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
class EggInfoPkgPipInstalledNoToplevel(OnSysPath, SiteBuilder): |
| 207 |
files: FilesSpec = { |
| 208 |
"egg_with_module_pkg.egg-info": { |
| 209 |
"PKG-INFO": "Name: egg_with_module-pkg", |
| 210 |
# SOURCES.txt is made from the source archive, and contains files |
| 211 |
# (setup.py) that are not present after installation. |
| 212 |
"SOURCES.txt": """ |
| 213 |
egg_with_module.py |
| 214 |
setup.py |
| 215 |
egg_with_module_pkg.egg-info/PKG-INFO |
| 216 |
egg_with_module_pkg.egg-info/SOURCES.txt |
| 217 |
egg_with_module_pkg.egg-info/top_level.txt |
| 218 |
""", |
| 219 |
# installed-files.txt is written by pip, and is a strictly more |
| 220 |
# accurate source than SOURCES.txt as to the installed contents of |
| 221 |
# the package. |
| 222 |
"installed-files.txt": """ |
| 223 |
../egg_with_module.py |
| 224 |
PKG-INFO |
| 225 |
SOURCES.txt |
| 226 |
top_level.txt |
| 227 |
""", |
| 228 |
# missing top_level.txt (to trigger fallback to installed-files.txt) |
| 229 |
}, |
| 230 |
"egg_with_module.py": """ |
| 231 |
def main(): |
| 232 |
print("hello world") |
| 233 |
""", |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
class EggInfoPkgPipInstalledExternalDataFiles(OnSysPath, SiteBuilder): |
| 238 |
files: FilesSpec = { |
| 239 |
"egg_with_module_pkg.egg-info": { |
| 240 |
"PKG-INFO": "Name: egg_with_module-pkg", |
| 241 |
# SOURCES.txt is made from the source archive, and contains files |
| 242 |
# (setup.py) that are not present after installation. |
| 243 |
"SOURCES.txt": """ |
| 244 |
egg_with_module.py |
| 245 |
setup.py |
| 246 |
egg_with_module.json |
| 247 |
egg_with_module_pkg.egg-info/PKG-INFO |
| 248 |
egg_with_module_pkg.egg-info/SOURCES.txt |
| 249 |
egg_with_module_pkg.egg-info/top_level.txt |
| 250 |
""", |
| 251 |
# installed-files.txt is written by pip, and is a strictly more |
| 252 |
# accurate source than SOURCES.txt as to the installed contents of |
| 253 |
# the package. |
| 254 |
"installed-files.txt": """ |
| 255 |
../../../etc/jupyter/jupyter_notebook_config.d/relative.json |
| 256 |
/etc/jupyter/jupyter_notebook_config.d/absolute.json |
| 257 |
../egg_with_module.py |
| 258 |
PKG-INFO |
| 259 |
SOURCES.txt |
| 260 |
top_level.txt |
| 261 |
""", |
| 262 |
# missing top_level.txt (to trigger fallback to installed-files.txt) |
| 263 |
}, |
| 264 |
"egg_with_module.py": """ |
| 265 |
def main(): |
| 266 |
print("hello world") |
| 267 |
""", |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
class EggInfoPkgPipInstalledNoModules(OnSysPath, SiteBuilder): |
| 272 |
files: FilesSpec = { |
| 273 |
"egg_with_no_modules_pkg.egg-info": { |
| 274 |
"PKG-INFO": "Name: egg_with_no_modules-pkg", |
| 275 |
# SOURCES.txt is made from the source archive, and contains files |
| 276 |
# (setup.py) that are not present after installation. |
| 277 |
"SOURCES.txt": """ |
| 278 |
setup.py |
| 279 |
egg_with_no_modules_pkg.egg-info/PKG-INFO |
| 280 |
egg_with_no_modules_pkg.egg-info/SOURCES.txt |
| 281 |
egg_with_no_modules_pkg.egg-info/top_level.txt |
| 282 |
""", |
| 283 |
# installed-files.txt is written by pip, and is a strictly more |
| 284 |
# accurate source than SOURCES.txt as to the installed contents of |
| 285 |
# the package. |
| 286 |
"installed-files.txt": """ |
| 287 |
PKG-INFO |
| 288 |
SOURCES.txt |
| 289 |
top_level.txt |
| 290 |
""", |
| 291 |
# top_level.txt correctly reflects that no modules are installed |
| 292 |
"top_level.txt": b"\n", |
| 293 |
}, |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
class EggInfoPkgSourcesFallback(OnSysPath, SiteBuilder): |
| 298 |
files: FilesSpec = { |
| 299 |
"sources_fallback_pkg.egg-info": { |
| 300 |
"PKG-INFO": "Name: sources_fallback-pkg", |
| 301 |
# SOURCES.txt is made from the source archive, and contains files |
| 302 |
# (setup.py) that are not present after installation. |
| 303 |
"SOURCES.txt": """ |
| 304 |
sources_fallback.py |
| 305 |
setup.py |
| 306 |
sources_fallback_pkg.egg-info/PKG-INFO |
| 307 |
sources_fallback_pkg.egg-info/SOURCES.txt |
| 308 |
""", |
| 309 |
# missing installed-files.txt (i.e. not installed by pip) and |
| 310 |
# missing top_level.txt (to trigger fallback to SOURCES.txt) |
| 311 |
}, |
| 312 |
"sources_fallback.py": """ |
| 313 |
def main(): |
| 314 |
print("hello world") |
| 315 |
""", |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
class EggInfoFile(OnSysPath, SiteBuilder): |
| 320 |
files: FilesSpec = { |
| 321 |
"egginfo_file.egg-info": """ |
| 322 |
Metadata-Version: 1.0 |
| 323 |
Name: egginfo_file |
| 324 |
Version: 0.1 |
| 325 |
Summary: An example package |
| 326 |
Home-page: www.example.com |
| 327 |
Author: Eric Haffa-Vee |
| 328 |
Author-email: [email protected] |
| 329 |
License: UNKNOWN |
| 330 |
Description: UNKNOWN |
| 331 |
Platform: UNKNOWN |
| 332 |
""", |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
# dedent all text strings before writing |
| 337 |
orig = _path.create.registry[str] |
| 338 |
_path.create.register(str, lambda content, path: orig(DALS(content), path)) |
| 339 |
|
| 340 |
|
| 341 |
build_files = _path.build |
| 342 |
|
| 343 |
|
| 344 |
def build_record(file_defs): |
| 345 |
return ''.join(f'{name},,\n' for name in record_names(file_defs)) |
| 346 |
|
| 347 |
|
| 348 |
def record_names(file_defs): |
| 349 |
recording = _path.Recording() |
| 350 |
_path.build(file_defs, recording) |
| 351 |
return recording.record |
| 352 |
|
| 353 |
|
| 354 |
class FileBuilder: |
| 355 |
def unicode_filename(self): |
| 356 |
return os_helper.FS_NONASCII or self.skip( |
| 357 |
"File system does not support non-ascii." |
| 358 |
) |
| 359 |
|
| 360 |
|
| 361 |
def DALS(str): |
| 362 |
"Dedent and left-strip" |
| 363 |
return textwrap.dedent(str).lstrip() |
| 364 |
|
| 365 |
|
| 366 |
@requires_zlib() |
| 367 |
class ZipFixtures: |
| 368 |
root = 'test.test_importlib.metadata.data' |
| 369 |
|
| 370 |
def _fixture_on_path(self, filename): |
| 371 |
pkg_file = resources.files(self.root).joinpath(filename) |
| 372 |
file = self.resources.enter_context(resources.as_file(pkg_file)) |
| 373 |
assert file.name.startswith('example'), file.name |
| 374 |
sys.path.insert(0, str(file)) |
| 375 |
self.resources.callback(sys.path.pop, 0) |
| 376 |
|
| 377 |
def setUp(self): |
| 378 |
# Add self.zip_name to the front of sys.path. |
| 379 |
self.resources = contextlib.ExitStack() |
| 380 |
self.addCleanup(self.resources.close) |
| 381 |
|
| 382 |
|
| 383 |
def parameterize(*args_set): |
| 384 |
"""Run test method with a series of parameters.""" |
| 385 |
|
| 386 |
def wrapper(func): |
| 387 |
@functools.wraps(func) |
| 388 |
def _inner(self): |
| 389 |
for args in args_set: |
| 390 |
with self.subTest(**args): |
| 391 |
func(self, **args) |
| 392 |
|
| 393 |
return _inner |
| 394 |
|
| 395 |
return wrapper |