Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def _parse_file(self, file: Path) -> PackageStore | None:
logger.debug("Unknown encoding for file: %s", file)
return None

dependencies = set(line.strip() for line in lines if not line.startswith("#"))
dependencies = set(
line.split("#")[0].strip() for line in lines if not line.startswith("#")
)

return PackageStore(
type=self.file_type,
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def pkg_with_reqs_txt(tmp_path_factory):
return base_dir


@pytest.fixture(scope="module")
def pkg_with_reqs_txt_and_comments(tmp_path_factory):
base_dir = tmp_path_factory.mktemp("foo")
req_file = base_dir / "requirements.txt"
reqs = "# comment\nrequests==2.31.0\nblack==23.7.*\nmypy~=1.4 # comment\npylint>1\n"
req_file.write_text(reqs)
return base_dir


@pytest.fixture(scope="module")
def pkg_with_reqs_txt_utf_16(tmp_path_factory):
base_dir = tmp_path_factory.mktemp("foo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ def test_open_error(self, pkg_with_reqs_txt, mocker):
parser = RequirementsTxtParser(pkg_with_reqs_txt)
found = parser.parse()
assert len(found) == 0

def test_trailing_comments(self, pkg_with_reqs_txt_and_comments):
parser = RequirementsTxtParser(pkg_with_reqs_txt_and_comments)
found = parser.parse()
assert len(found) == 1
store = found[0]
assert store.type.value == "requirements.txt"
assert store.file == pkg_with_reqs_txt_and_comments / parser.file_type.value
assert store.py_versions == []
assert len(store.dependencies) == 4