Skip to content

Remove redundant code paths #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions pip_check_reqs/find_extra_reqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 15 additions & 20 deletions pip_check_reqs/find_missing_reqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Empty file added tests/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions tests/package_info_mock.py
Original file line number Diff line number Diff line change
@@ -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]]
11 changes: 6 additions & 5 deletions tests/test_find_extra_reqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from pip_check_reqs import find_extra_reqs, common

from .package_info_mock import _PackageInfo


@pytest.fixture
def fake_opts():
Expand Down Expand Up @@ -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',
Expand Down
18 changes: 11 additions & 7 deletions tests/test_find_missing_reqs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import
from dataclasses import dataclass
import importlib
import site
from typing import Dict, Optional

import logging
Expand All @@ -12,6 +13,8 @@

from pip_check_reqs import find_missing_reqs, common

from .package_info_mock import _PackageInfo


@pytest.fixture
def fake_opts():
Expand Down Expand Up @@ -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)]))
Expand All @@ -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',
Expand Down