diff --git a/pip_check_reqs/find_extra_reqs.py b/pip_check_reqs/find_extra_reqs.py index 84d63b5..b24670b 100644 --- a/pip_check_reqs/find_extra_reqs.py +++ b/pip_check_reqs/find_extra_reqs.py @@ -27,26 +27,21 @@ def find_extra_reqs(options, requirements_filename): ) for package in search_packages_info(all_pkgs): - if isinstance(package, dict): # pragma: no cover - package_name = package['name'] - package_location = package['location'] - package_files = package.get('files', []) or [] - else: # pragma: no cover - package_name = package.name - package_location = package.location - package_files = [] - for item in (package.files or []): - here = pathlib.Path('.').resolve() - item_location_rel = (pathlib.Path(package_location) / item) - item_location = item_location_rel.resolve() - try: - relative_item_location = item_location.relative_to(here) - except ValueError: - # Ideally we would use Pathlib.is_relative_to rather than - # checking for a ValueError, but that is only available in - # Python 3.9+. - relative_item_location = item_location - package_files.append(str(relative_item_location)) + package_name = package.name + package_location = package.location + package_files = [] + for item in (package.files or []): + here = pathlib.Path('.').resolve() + item_location_rel = (pathlib.Path(package_location) / item) + item_location = item_location_rel.resolve() + try: + relative_item_location = item_location.relative_to(here) + except ValueError: + # Ideally we would use Pathlib.is_relative_to rather than + # checking for a ValueError, but that is only available in + # Python 3.9+. + relative_item_location = item_location + package_files.append(str(relative_item_location)) log.debug('installed package: %s (at %s)', package_name, package_location) diff --git a/pip_check_reqs/find_missing_reqs.py b/pip_check_reqs/find_missing_reqs.py index 773959c..9322304 100644 --- a/pip_check_reqs/find_missing_reqs.py +++ b/pip_check_reqs/find_missing_reqs.py @@ -31,26 +31,21 @@ def find_missing_reqs(options, requirements_filename): ) for package in search_packages_info(all_pkgs): - if isinstance(package, dict): # pragma: no cover - package_name = package['name'] - package_location = package['location'] - package_files = package.get('files', []) or [] - else: # pragma: no cover - package_name = package.name - package_location = package.location - package_files = [] - for item in (package.files or []): - here = pathlib.Path('.').resolve() - item_location_rel = (pathlib.Path(package_location) / item) - item_location = item_location_rel.resolve() - try: - relative_item_location = item_location.relative_to(here) - except ValueError: - # Ideally we would use Pathlib.is_relative_to rather than - # checking for a ValueError, but that is only available in - # Python 3.9+. - relative_item_location = item_location - package_files.append(str(relative_item_location)) + package_name = package.name + package_location = package.location + package_files = [] + for item in (package.files or []): + here = pathlib.Path('.').resolve() + item_location_rel = (pathlib.Path(package_location) / item) + item_location = item_location_rel.resolve() + try: + relative_item_location = item_location.relative_to(here) + except ValueError: + # Ideally we would use Pathlib.is_relative_to rather than + # checking for a ValueError, but that is only available in + # Python 3.9+. + relative_item_location = item_location + package_files.append(str(relative_item_location)) log.debug('installed package: %s (at %s)', package_name, package_location) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/package_info_mock.py b/tests/package_info_mock.py new file mode 100644 index 0000000..7d6d9c0 --- /dev/null +++ b/tests/package_info_mock.py @@ -0,0 +1,9 @@ +from typing import List, Optional, NamedTuple + + +# A stand-in for pip._internal.commands.show._PackageInfo, as returned by +# search_packages_info from the same module +class _PackageInfo(NamedTuple): + name: str + location: str + files: Optional[List[str]] diff --git a/tests/test_find_extra_reqs.py b/tests/test_find_extra_reqs.py index 95795e0..cf45310 100644 --- a/tests/test_find_extra_reqs.py +++ b/tests/test_find_extra_reqs.py @@ -12,6 +12,8 @@ from pip_check_reqs import find_extra_reqs, common +from .package_info_mock import _PackageInfo + @pytest.fixture def fake_opts(): @@ -68,11 +70,10 @@ class FakePathDistribution: pretend.call_recorder(lambda **kwargs: installed_distributions), ) packages_info = [ - dict(name='spam', - location='site-spam', - files=['spam/__init__.py', 'spam/shrub.py']), - dict(name='shrub', location='site-spam', files=['shrub.py']), - dict(name='pass', location='site-spam', files=['pass.py']), + _PackageInfo(name='spam', location='site-spam', + files=['spam/__init__.py', 'spam/shrub.py']), + _PackageInfo(name='shrub', location='site-spam', files=['shrub.py']), + _PackageInfo(name='pass', location='site-spam', files=['pass.py']), ] monkeypatch.setattr(find_extra_reqs, 'search_packages_info', diff --git a/tests/test_find_missing_reqs.py b/tests/test_find_missing_reqs.py index c8f88e2..834b7e2 100644 --- a/tests/test_find_missing_reqs.py +++ b/tests/test_find_missing_reqs.py @@ -1,6 +1,7 @@ from __future__ import absolute_import from dataclasses import dataclass import importlib +import site from typing import Dict, Optional import logging @@ -12,6 +13,8 @@ from pip_check_reqs import find_missing_reqs, common +from .package_info_mock import _PackageInfo + @pytest.fixture def fake_opts(): @@ -41,11 +44,12 @@ def parse_args(self): def test_find_missing_reqs(monkeypatch, tmp_path: Path): + site_path = site.getsitepackages()[0] imported_modules = dict(spam=common.FoundModule('spam', - 'site-spam/spam.py', + f'{site_path}/spam.py', [('ham.py', 1)]), shrub=common.FoundModule('shrub', - 'site-spam/shrub.py', + f'{site_path}/shrub.py', [('ham.py', 3)]), ignore=common.FoundModule('ignore', 'ignore.py', [('ham.py', 2)])) @@ -67,11 +71,11 @@ class FakePathDistribution: pretend.call_recorder(lambda **kwargs: installed_distributions), ) packages_info = [ - dict(name='spam', - location='site-spam', - files=['spam/__init__.py', 'spam/shrub.py']), - dict(name='shrub', location='site-spam', files=['shrub.py']), - dict(name='pass', location='site-spam', files=['pass.py']), + _PackageInfo(name='spam', + location=site_path, + files=['spam/__init__.py', 'spam/shrub.py']), + _PackageInfo(name='shrub', location=site_path, files=['shrub.py']), + _PackageInfo(name='pass', location=site_path, files=['pass.py']), ] monkeypatch.setattr(find_missing_reqs, 'search_packages_info',