|
2 | 2 |
|
3 | 3 | import builtins
|
4 | 4 | import datetime
|
| 5 | +import inspect |
5 | 6 | import os
|
6 | 7 | import plistlib
|
7 | 8 | import stat
|
@@ -425,3 +426,60 @@ def test_normalize_path_backslash_sep(self, unnormalized, expected):
|
425 | 426 | """Ensure path seps are cleaned on backslash path sep systems."""
|
426 | 427 | result = pkg_resources.normalize_path(unnormalized)
|
427 | 428 | assert result.endswith(expected)
|
| 429 | + |
| 430 | + |
| 431 | +class TestWorkdirRequire: |
| 432 | + def fake_site_packages(self, tmp_path, monkeypatch, dist_files): |
| 433 | + site_packages = tmp_path / "site-packages" |
| 434 | + site_packages.mkdir() |
| 435 | + for file, content in self.FILES.items(): |
| 436 | + path = site_packages / file |
| 437 | + path.parent.mkdir(exist_ok=True, parents=True) |
| 438 | + path.write_text(inspect.cleandoc(content), encoding="utf-8") |
| 439 | + |
| 440 | + monkeypatch.setattr(sys, "path", [site_packages]) |
| 441 | + return os.fspath(site_packages) |
| 442 | + |
| 443 | + FILES = { |
| 444 | + "pkg1_mod-1.2.3.dist-info/METADATA": """ |
| 445 | + Metadata-Version: 2.4 |
| 446 | + Name: pkg1.mod |
| 447 | + Version: 1.2.3 |
| 448 | + """, |
| 449 | + "pkg2.mod-0.42.dist-info/METADATA": """ |
| 450 | + Metadata-Version: 2.1 |
| 451 | + Name: pkg2.mod |
| 452 | + Version: 0.42 |
| 453 | + """, |
| 454 | + "pkg3_mod.egg-info/PKG-INFO": """ |
| 455 | + Name: pkg3.mod |
| 456 | + Version: 1.2.3.4 |
| 457 | + """, |
| 458 | + "pkg4.mod.egg-info/PKG-INFO": """ |
| 459 | + Name: pkg4.mod |
| 460 | + Version: 0.42.1 |
| 461 | + """, |
| 462 | + } |
| 463 | + |
| 464 | + @pytest.mark.parametrize( |
| 465 | + ("version", "requirement"), |
| 466 | + [ |
| 467 | + ("1.2.3", "pkg1.mod>=1"), |
| 468 | + ("0.42", "pkg2.mod>=0.4"), |
| 469 | + ("1.2.3.4", "pkg3.mod<=2"), |
| 470 | + ("0.42.1", "pkg4.mod>0.2,<1"), |
| 471 | + ], |
| 472 | + ) |
| 473 | + def test_require_non_normalised_name( |
| 474 | + self, tmp_path, monkeypatch, version, requirement |
| 475 | + ): |
| 476 | + # https://github.com/pypa/setuptools/issues/4853 |
| 477 | + site_packages = self.fake_site_packages(tmp_path, monkeypatch, self.FILES) |
| 478 | + ws = pkg_resources.WorkingSet([site_packages]) |
| 479 | + |
| 480 | + for req in [requirement, requirement.replace(".", "-")]: |
| 481 | + [dist] = ws.require(req) |
| 482 | + assert dist.version == version |
| 483 | + assert os.path.samefile( |
| 484 | + os.path.commonpath([dist.location, site_packages]), site_packages |
| 485 | + ) |
0 commit comments