Skip to content

Commit 78e15d1

Browse files
committed
Find nearest Simvue config in nested directories
1 parent 988edf3 commit 78e15d1

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

simvue/utilities.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ def find_first_instance_of_file(
4747
file_names = [file_names]
4848

4949
for file_name in file_names:
50-
_user_file = pathlib.Path.cwd().joinpath(file_name)
51-
if _user_file.exists():
52-
return _user_file
50+
_current_path = pathlib.Path.cwd()
51+
52+
while os.access(_current_path, os.R_OK):
53+
_user_file = _current_path.joinpath(file_name)
54+
if _user_file.exists():
55+
return _user_file
56+
_current_path = _current_path.parent
5357

5458
# If the user is running on different mounted volume or outside
5559
# of their user space then the above will not return the file

tests/functional/test_utilities.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import pytest
22
import tempfile
33
import os.path
4+
import pathlib
5+
import stat
6+
7+
from pytest_mock import MockerFixture
48

59
import simvue.utilities as sv_util
610

@@ -19,3 +23,37 @@ 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(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+
39+
with tempfile.TemporaryDirectory() as temp_d:
40+
_path = pathlib.Path(temp_d)
41+
_path_sub = _path.joinpath("level_0")
42+
_path_sub.mkdir()
43+
44+
for i in range(1, 5):
45+
_path_sub = _path_sub.joinpath(f"level_{i}")
46+
_path_sub.mkdir()
47+
mocker.patch("pathlib.Path.cwd", lambda *_: _path_sub)
48+
49+
if user_area:
50+
_path.joinpath("level_0").joinpath("simvue.toml").touch()
51+
_path.chmod(stat.S_IXUSR)
52+
_result = sv_util.find_first_instance_of_file("simvue.toml", check_user_space=False)
53+
else:
54+
_path.chmod(stat.S_IXUSR)
55+
_result = sv_util.find_first_instance_of_file("simvue.toml", check_user_space=False) is None
56+
57+
_path.chmod(stat.S_IRWXU)
58+
assert _result
59+

0 commit comments

Comments
 (0)