Skip to content
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

fix(dependency_getter): stricten URL guessing #570

Merged
merged 1 commit into from
Mar 7, 2024
Merged
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
6 changes: 3 additions & 3 deletions deptry/dependency_getter/requirements_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Match
from urllib.parse import urlparse

from deptry.dependency import Dependency
from deptry.dependency_getter.base import DependenciesExtract, DependencyGetter
Expand Down Expand Up @@ -114,8 +114,8 @@ def _check_if_dependency_is_conditional(line: str) -> bool:
return ";" in line

@staticmethod
def _line_is_url(line: str) -> Match[str] | None:
return re.search(r"^(http|https|git\+https)", line)
def _line_is_url(line: str) -> bool:
return urlparse(line).scheme != ""

@staticmethod
def _extract_name_from_url(line: str) -> str | None:
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/dependency_getter/test_requirements_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from pathlib import Path

import pytest

from deptry.dependency_getter.requirements_txt import RequirementsTxtDependencyGetter
from tests.utils import run_within_dir

Expand All @@ -26,6 +28,7 @@ def test_parse_requirements_txt(tmp_path: Path) -> None:
docopt == 0.6.1
requests [security] >= 2.8.1, == 2.8.* ; python_version < "2.7"
fox-python
httpx==0.25.2
"""
with run_within_dir(tmp_path):
with Path("requirements.txt").open("w") as f:
Expand All @@ -38,7 +41,7 @@ def test_parse_requirements_txt(tmp_path: Path) -> None:
dependencies_extract = getter.get()
dependencies = dependencies_extract.dependencies

assert len(dependencies) == 18
assert len(dependencies) == 19
assert len(dependencies_extract.dev_dependencies) == 0

assert dependencies[1].name == "colorama"
Expand Down Expand Up @@ -185,3 +188,25 @@ def test_dev_multiple_with_arguments(tmp_path: Path) -> None:

assert dev_dependencies[0].name == "click"
assert dev_dependencies[1].name == "bar"


@pytest.mark.parametrize(
("line", "expected"),
[
("foo", False),
("http", False),
("https", False),
("httpx", False),
("git+http", False),
("git+https", False),
("http://", True),
("https://", True),
("git+http://", True),
("git+https://", True),
("file://", True),
("file:///", True),
("httpx://", True),
],
)
def test__line_is_url(line: str, expected: bool) -> None:
assert RequirementsTxtDependencyGetter._line_is_url(line) is expected
Loading