Skip to content

Commit

Permalink
fix(dependency_getter): stricten URL guessing (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner authored Mar 7, 2024
1 parent bd78e3e commit 63ca221
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
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

0 comments on commit 63ca221

Please sign in to comment.