11import pytest
22import tempfile
33import os .path
4+ import pathlib
5+ import stat
6+
7+ from pytest_mock import MockerFixture
48
59import simvue .utilities as sv_util
610
@@ -19,3 +23,63 @@ def test_calculate_hash(is_file: bool, hash: str) -> None:
1923 assert sv_util .calculate_sha256 (filename = out_file , is_file = is_file ) == hash
2024 else :
2125 assert sv_util .calculate_sha256 (filename = "temp.txt" , is_file = is_file ) == hash
26+
27+ @pytest .mark .config
28+ @pytest .mark .parametrize (
29+ "user_area" , (True , False ),
30+ ids = ("permitted_dir" , "out_of_user_area" )
31+ )
32+ def test_find_first_file_search (user_area : bool , monkeypatch : pytest .MonkeyPatch , mocker : MockerFixture ) -> None :
33+ # Deactivate the server checks for this test
34+ monkeypatch .setenv ("SIMVUE_NO_SERVER_CHECK" , "True" )
35+ monkeypatch .delenv ("SIMVUE_TOKEN" , False )
36+ monkeypatch .delenv ("SIMVUE_URL" , False )
37+
38+ with tempfile .TemporaryDirectory () as temp_d :
39+ _path = pathlib .Path (temp_d )
40+ _path_sub = _path .joinpath ("level_0" )
41+ _path_sub .mkdir ()
42+
43+ for i in range (1 , 5 ):
44+ _path_sub = _path_sub .joinpath (f"level_{ i } " )
45+ _path_sub .mkdir ()
46+ mocker .patch ("pathlib.Path.cwd" , lambda * _ : _path_sub )
47+
48+ if user_area :
49+ _path .joinpath ("level_0" ).joinpath ("simvue.toml" ).touch ()
50+ _path .chmod (stat .S_IXUSR )
51+ _result = sv_util .find_first_instance_of_file ("simvue.toml" , check_user_space = False )
52+ else :
53+ _path .chmod (stat .S_IXUSR )
54+ _result = sv_util .find_first_instance_of_file ("simvue.toml" , check_user_space = False ) is None
55+
56+ _path .chmod (stat .S_IRWXU )
57+ assert _result
58+
59+ @pytest .mark .config
60+ def test_find_first_file_at_root (monkeypatch : pytest .MonkeyPatch , mocker : MockerFixture ) -> None :
61+ # Deactivate the server checks for this test
62+ monkeypatch .setenv ("SIMVUE_NO_SERVER_CHECK" , "True" )
63+ monkeypatch .delenv ("SIMVUE_TOKEN" , False )
64+ monkeypatch .delenv ("SIMVUE_URL" , False )
65+
66+ @property
67+ def _returns_self (self ):
68+ return self
69+
70+
71+ with tempfile .TemporaryDirectory () as temp_d :
72+ _path = pathlib .Path (temp_d )
73+ _path_sub = _path .joinpath ("level_0" )
74+ _path_sub .mkdir ()
75+ _path .joinpath ("level_0" ).joinpath ("simvue.toml" ).touch ()
76+
77+ for i in range (1 , 5 ):
78+ _path_sub = _path_sub .joinpath (f"level_{ i } " )
79+ _path_sub .mkdir ()
80+ mocker .patch ("pathlib.Path.parent" , _returns_self )
81+ mocker .patch ("pathlib.Path.cwd" , lambda * _ : _path_sub )
82+
83+ assert not sv_util .find_first_instance_of_file ("simvue.toml" , check_user_space = False )
84+
85+
0 commit comments