-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest_utils.py
More file actions
73 lines (60 loc) · 2.34 KB
/
test_utils.py
File metadata and controls
73 lines (60 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
from pathlib import Path
from pretext import utils
def test_working_directory(tmp_path: Path) -> None:
os.chdir(tmp_path)
subdir = Path("foobar")
subdir.mkdir()
assert Path().resolve() == tmp_path.resolve()
with utils.working_directory(subdir):
assert Path().resolve().parent == tmp_path.resolve()
# TODO check path returns afterward
def test_project_path(tmp_path: Path) -> None:
os.chdir(tmp_path)
Path("project.ptx").write_text("")
assert Path("project.ptx").exists()
assert utils.project_path_found().resolve() == tmp_path.resolve()
subdir = Path("foobar")
print(subdir.resolve())
subdir.mkdir()
os.chdir(subdir)
assert utils.project_path_found().resolve() == Path().resolve().parent
def test_parse_git_remote() -> None:
valids = [
"git@github.com:PreTeXtBook/pretext-cli.git",
"https://github.com/PreTeXtBook/pretext-cli.git",
"https://github.com/PreTeXtBook/pretext-cli",
"https://github.com/PreTeXtBook/pretext-cli/",
]
for string in valids:
assert utils.parse_git_remote(string)[0] == "PreTeXtBook"
assert utils.parse_git_remote(string)[1] == "pretext-cli"
def test_is_unmodified() -> None:
magic_comment = (
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}"