Skip to content

Commit 8671c2e

Browse files
Copilotoscarlevin
andauthored
Fix regex-based version extraction in requirements.txt parsing (#1122)
* Initial plan * Fix review comments: remove duplicate import, fix regex syntax, add tests Co-authored-by: oscarlevin <6504596+oscarlevin@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: oscarlevin <6504596+oscarlevin@users.noreply.github.com> Co-authored-by: Oscar Levin <oscar.levin@gmail.com>
1 parent 5e47f3e commit 8671c2e

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

pretext/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import logging
1717
import logging.handlers
1818
import psutil
19-
import re
2019
import typing as t
2120
from . import types as pt # PreTeXt types
2221
from lxml import etree as ET # noqa: N812
@@ -134,7 +133,7 @@ def requirements_version(dirpath: Optional[Path] = None) -> Optional[str]:
134133
return None
135134
try:
136135
with open(pp / "requirements.txt", "r") as f:
137-
REGEX = r"\s*pretext(book)?(\[.*\])?\s*==\s*(?P<version>[\d\.]+)\s*"gm
136+
REGEX = r"\s*pretext(book)?(\[.*\])?\s*==\s*(?P<version>[\d\.]+)\s*"
138137
for line in f.readlines():
139138
match = re.match(REGEX, line)
140139
if match:

tests/test_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,32 @@ def test_is_unmodified() -> None:
4242
b"foo\n<!-- Managed automatically by PreTeXt authoring tools -->\nbar"
4343
)
4444
assert utils.is_unmodified("foo", magic_comment)
45+
46+
47+
def test_requirements_version(tmp_path: Path) -> None:
48+
# Create a minimal project.ptx so project_path() can find the project root
49+
(tmp_path / "project.ptx").write_text("")
50+
51+
cases = [
52+
("pretext == 2.36.0", "2.36.0"),
53+
("pretextbook == 1.2.3", "1.2.3"),
54+
("pretext[prefigure] == 2.36.0", "2.36.0"),
55+
(" pretext == 3.0.0 ", "3.0.0"),
56+
("pretext[prefigure,extra] == 0.9.1", "0.9.1"),
57+
]
58+
59+
for line, expected in cases:
60+
req_file = tmp_path / "requirements.txt"
61+
req_file.write_text(line + "\n")
62+
assert utils.requirements_version(tmp_path) == expected, f"Failed for: {line!r}"
63+
64+
# Lines that should NOT match
65+
non_matching = [
66+
"numpy == 1.0.0",
67+
"pretext ==",
68+
"pretext",
69+
]
70+
for line in non_matching:
71+
req_file = tmp_path / "requirements.txt"
72+
req_file.write_text(line + "\n")
73+
assert utils.requirements_version(tmp_path) is None, f"Should not match: {line!r}"

0 commit comments

Comments
 (0)