diff --git a/pretext/utils.py b/pretext/utils.py index 86b2b6e0..27772d87 100644 --- a/pretext/utils.py +++ b/pretext/utils.py @@ -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[\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) diff --git a/pyproject.toml b/pyproject.toml index 1c2ca4af..5c8e106e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ # ---------------- [tool.poetry] name = "pretext" -version = "2.36.1" +version = "2.37.2" description = "A package to author, build, and deploy PreTeXt projects." readme = "README.md" homepage = "https://pretextbook.org" diff --git a/tests/test_utils.py b/tests/test_utils.py index 998feda8..489812d5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -42,3 +42,34 @@ def test_is_unmodified() -> None: b"foo\n\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}"