Skip to content

Commit

Permalink
requirements parser handles trailing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Feb 28, 2024
1 parent 7075794 commit d3d3037
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
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

0 comments on commit d3d3037

Please sign in to comment.