-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_utilities.py
More file actions
85 lines (69 loc) · 2.93 KB
/
test_utilities.py
File metadata and controls
85 lines (69 loc) · 2.93 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
74
75
76
77
78
79
80
81
82
83
import pytest
import tempfile
import os.path
import pathlib
import stat
from pytest_mock import MockerFixture
import simvue.utilities as sv_util
@pytest.mark.utilities
@pytest.mark.parametrize(
"is_file,hash", [
(True, "c7be1ed902fb8dd4d48997c6452f5d7e509fbcdbe2808b16bcf4edce4c07d14e"),
(False, "50ea7be4eeb62777c05b06e21e9e5f6e8ce39442e7efbd17da8d57d5b18f5035")
]
)
def test_calculate_hash(is_file: bool, hash: str) -> None:
if is_file:
with tempfile.TemporaryDirectory() as tempd:
with open(out_file := os.path.join(tempd, "temp.txt"), "w") as out_f:
out_f.write("This is a test")
assert sv_util.calculate_sha256(filename=out_file, is_file=is_file) == hash
else:
assert sv_util.calculate_sha256(filename="temp.txt", is_file=is_file) == hash
@pytest.mark.config
@pytest.mark.parametrize(
"user_area", (True, False),
ids=("permitted_dir", "out_of_user_area")
)
def test_find_first_file_search(user_area: bool, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture) -> None:
# Deactivate the server checks for this test
monkeypatch.setenv("SIMVUE_NO_SERVER_CHECK", "True")
monkeypatch.delenv("SIMVUE_TOKEN", False)
monkeypatch.delenv("SIMVUE_URL", False)
with tempfile.TemporaryDirectory() as temp_d:
_path = pathlib.Path(temp_d)
_path_sub = _path.joinpath("level_0")
_path_sub.mkdir()
for i in range(1, 5):
_path_sub = _path_sub.joinpath(f"level_{i}")
_path_sub.mkdir()
mocker.patch("pathlib.Path.cwd", lambda *_: _path_sub)
if user_area:
_path.joinpath("level_0").joinpath("simvue.toml").touch()
_path.chmod(stat.S_IXUSR)
_result = sv_util.find_first_instance_of_file("simvue.toml", check_user_space=False)
else:
_path.chmod(stat.S_IXUSR)
_result = sv_util.find_first_instance_of_file("simvue.toml", check_user_space=False) is None
_path.chmod(stat.S_IRWXU)
assert _result
@pytest.mark.config
def test_find_first_file_at_root(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture) -> None:
# Deactivate the server checks for this test
monkeypatch.setenv("SIMVUE_NO_SERVER_CHECK", "True")
monkeypatch.delenv("SIMVUE_TOKEN", False)
monkeypatch.delenv("SIMVUE_URL", False)
@property
def _returns_self(self):
return self
with tempfile.TemporaryDirectory() as temp_d:
_path = pathlib.Path(temp_d)
_path_sub = _path.joinpath("level_0")
_path_sub.mkdir()
_path.joinpath("level_0").joinpath("simvue.toml").touch()
for i in range(1, 5):
_path_sub = _path_sub.joinpath(f"level_{i}")
_path_sub.mkdir()
mocker.patch("pathlib.Path.parent", _returns_self)
mocker.patch("pathlib.Path.cwd", lambda *_: _path_sub)
assert not sv_util.find_first_instance_of_file("simvue.toml", check_user_space=False)