Download .venv/Lib/site-packages/pkg_resources/tests/test_working_set.py from DrDavis/PythonProject1: direct link, hf CLI and curl.
- Browser
- Download file 8.54 kB
-
https://huggingface.co/DrDavis/PythonProject1/resolve/main/.venv/Lib/site-packages/pkg_resources/tests/test_working_set.py
- Command line
-
hf download hf://DrDavis/PythonProject1/.venv/Lib/site-packages/pkg_resources/tests/test_working_set.py
-
curl -L -o test_working_set.py https://huggingface.co/DrDavis/PythonProject1/resolve/main/.venv/Lib/site-packages/pkg_resources/tests/test_working_set.py
8.54 kB
| import functools | |
| import inspect | |
| import re | |
| import textwrap | |
| import pytest | |
| import pkg_resources | |
| from .test_resources import Metadata | |
| def strip_comments(s): | |
| return '\n'.join( | |
| line | |
| for line in s.split('\n') | |
| if line.strip() and not line.strip().startswith('#') | |
| ) | |
| def parse_distributions(s): | |
| """ | |
| Parse a series of distribution specs of the form: | |
| {project_name}-{version} | |
| [optional, indented requirements specification] | |
| Example: | |
| foo-0.2 | |
| bar-1.0 | |
| foo>=3.0 | |
| [feature] | |
| baz | |
| yield 2 distributions: | |
| - project_name=foo, version=0.2 | |
| - project_name=bar, version=1.0, | |
| requires=['foo>=3.0', 'baz; extra=="feature"'] | |
| """ | |
| s = s.strip() | |
| for spec in re.split(r'\n(?=[^\s])', s): | |
| if not spec: | |
| continue | |
| fields = spec.split('\n', 1) | |
| assert 1 <= len(fields) <= 2 | |
| name, version = fields.pop(0).rsplit('-', 1) | |
| if fields: | |
| requires = textwrap.dedent(fields.pop(0)) | |
| metadata = Metadata(('requires.txt', requires)) | |
| else: | |
| metadata = None | |
| dist = pkg_resources.Distribution( | |
| project_name=name, version=version, metadata=metadata | |
| ) | |
| yield dist | |
| class FakeInstaller: | |
| def __init__(self, installable_dists) -> None: | |
| self._installable_dists = installable_dists | |
| def __call__(self, req): | |
| return next( | |
| iter(filter(lambda dist: dist in req, self._installable_dists)), None | |
| ) | |
| def parametrize_test_working_set_resolve(*test_list): | |
| idlist = [] | |
| argvalues = [] | |
| for test in test_list: | |
| ( | |
| name, | |
| installed_dists, | |
| installable_dists, | |
| requirements, | |
| expected1, | |
| expected2, | |
| ) = ( | |
| strip_comments(s.lstrip()) | |
| for s in textwrap.dedent(test).lstrip().split('\n\n', 5) | |
| ) | |
| installed_dists = list(parse_distributions(installed_dists)) | |
| installable_dists = list(parse_distributions(installable_dists)) | |
| requirements = list(pkg_resources.parse_requirements(requirements)) | |
| for id_, replace_conflicting, expected in ( | |
| (name, False, expected1), | |
| (name + '_replace_conflicting', True, expected2), | |
| ): | |
| idlist.append(id_) | |
| expected = strip_comments(expected.strip()) | |
| if re.match(r'\w+$', expected): | |
| expected = getattr(pkg_resources, expected) | |
| assert issubclass(expected, Exception) | |
| else: | |
| expected = list(parse_distributions(expected)) | |
| argvalues.append( | |
| pytest.param( | |
| installed_dists, | |
| installable_dists, | |
| requirements, | |
| replace_conflicting, | |
| expected, | |
| ) | |
| ) | |
| return pytest.mark.parametrize( | |
| 'installed_dists,installable_dists,' | |
| 'requirements,replace_conflicting,' | |
| 'resolved_dists_or_exception', | |
| argvalues, | |
| ids=idlist, | |
| ) | |
| def test_working_set_resolve( | |
| installed_dists, | |
| installable_dists, | |
| requirements, | |
| replace_conflicting, | |
| resolved_dists_or_exception, | |
| ): | |
| ws = pkg_resources.WorkingSet([]) | |
| list(map(ws.add, installed_dists)) | |
| resolve_call = functools.partial( | |
| ws.resolve, | |
| requirements, | |
| installer=FakeInstaller(installable_dists), | |
| replace_conflicting=replace_conflicting, | |
| ) | |
| if inspect.isclass(resolved_dists_or_exception): | |
| with pytest.raises(resolved_dists_or_exception): | |
| resolve_call() | |
| else: | |
| assert sorted(resolve_call()) == sorted(resolved_dists_or_exception) | |