Skip to content
Open
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: 4 additions & 2 deletions pretext/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ def requirements_version(dirpath: Optional[Path] = None) -> Optional[str]:
return None
try:
with open(pp / "requirements.txt", "r") as f:
REGEX = r"\s*pretext(book)?(\[.*\])?\s*==\s*(?P<version>[\d\.]+)\s*"
for line in f.readlines():
if ("pretext ==" in line) or ("pretextbook ==" in line):
return line.split("==")[1].strip()
match = re.match(REGEX, line)
if match:
return match.group("version")
except Exception as e:
log.debug("Could not read `requirements.txt`:")
log.debug(e)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,32 @@ def test_is_unmodified() -> None:
b"foo\n<!-- Managed automatically by PreTeXt authoring tools -->\nbar"
)
assert utils.is_unmodified("foo", magic_comment)


def test_requirements_version(tmp_path: Path) -> None:
# Create a minimal project.ptx so project_path() can find the project root
(tmp_path / "project.ptx").write_text("")

cases = [
("pretext == 2.36.0", "2.36.0"),
("pretextbook == 1.2.3", "1.2.3"),
("pretext[prefigure] == 2.36.0", "2.36.0"),
(" pretext == 3.0.0 ", "3.0.0"),
("pretext[prefigure,extra] == 0.9.1", "0.9.1"),
]

for line, expected in cases:
req_file = tmp_path / "requirements.txt"
req_file.write_text(line + "\n")
assert utils.requirements_version(tmp_path) == expected, f"Failed for: {line!r}"

# Lines that should NOT match
non_matching = [
"numpy == 1.0.0",
"pretext ==",
"pretext",
]
for line in non_matching:
req_file = tmp_path / "requirements.txt"
req_file.write_text(line + "\n")
assert utils.requirements_version(tmp_path) is None, f"Should not match: {line!r}"
Loading