-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_util.py
95 lines (76 loc) · 2.91 KB
/
test_util.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import platform
from pathlib import Path
import pytest
from hipercow.util import (
check_python_version,
find_file_descend,
subprocess_run,
transient_envvars,
transient_working_directory,
)
def test_find_descend(tmp_path):
(tmp_path / "a" / "b" / "c" / "d").mkdir(parents=True)
(tmp_path / "a" / "b" / ".foo").mkdir(parents=True)
assert (
find_file_descend(".foo", tmp_path / "a/b/c/d")
== (tmp_path / "a" / "b").resolve()
)
assert find_file_descend(".foo", tmp_path / "a") is None
def test_transient_working_directory(tmp_path):
here = Path.cwd()
with transient_working_directory(None):
assert Path.cwd() == here
with transient_working_directory(tmp_path):
assert Path.cwd() == tmp_path
def test_run_process_and_capture_output(tmp_path):
path = tmp_path / "output"
res = subprocess_run(["echo", "hello"], filename=path)
assert res.returncode == 0
assert path.exists()
with open(path) as f:
assert f.read().strip() == "hello"
def test_can_cope_with_missing_path(tmp_path, capsys):
cmd = ["nosuchexe", "arg"]
res = subprocess_run(cmd)
assert res.args == cmd
assert res.returncode == -1
out = capsys.readouterr()
assert len(out.out) > 0
tmp = tmp_path / "log"
res = subprocess_run(cmd, filename=tmp)
assert capsys.readouterr().out == ""
assert res.args == cmd
assert res.returncode == -1
with pytest.raises(FileNotFoundError):
res = subprocess_run(cmd, check=True)
def test_can_set_envvars():
with transient_envvars({"hc_a": "1"}):
assert os.environ.get("hc_a") == "1"
assert os.environ.get("hc_a") is None
def test_can_unset_envvars_if_none():
with transient_envvars({"hc_a": "1", "hc_b": None, "hc_c": "3"}):
assert os.environ.get("hc_a") == "1"
assert "hc_b" not in os.environ
assert os.environ.get("hc_c") == "3"
with transient_envvars({"hc_b": "2", "hc_c": None}):
assert os.environ.get("hc_a") == "1"
assert os.environ.get("hc_b") == "2"
assert os.environ.get("hc_c") is None
assert "hc_c" not in os.environ
assert os.environ.get("hc_a") == "1"
assert "hc_b" not in os.environ
assert os.environ.get("hc_c") == "3"
def test_can_check_python_version():
ours3 = platform.python_version()
ours2 = ".".join(ours3.split(".")[:2])
assert check_python_version(None) == ours2
assert check_python_version(ours3) == ours2
assert check_python_version(ours2) == ours2
assert check_python_version("3.10") == "3.10"
assert check_python_version("3.11") == "3.11"
with pytest.raises(Exception, match="does not parse as a valid version"):
check_python_version("3.11a")
with pytest.raises(Exception, match="is not supported"):
check_python_version("3.9")
assert check_python_version("3.9", ["3.9", "3.10"]) == "3.9"