Skip to content

Commit 2540895

Browse files
authored
Fix config search to include home directory (#621)
1 parent 1bcbb49 commit 2540895

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

servicex/configuration.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,18 @@ def _add_from_path(cls, path: Optional[Path] = None, walk_up_tree: bool = False)
143143

144144
dir = dir.parent
145145

146+
# If nothing was found walking up the directory tree, look in the user's
147+
# home directory as a final fallback. This mirrors the behaviour
148+
# documented for the search path.
149+
if config is None and not path:
150+
home = Path.home()
151+
# Look first for `.servicex` and then for `servicex.yaml` just as we
152+
# did in the directory walk above.
153+
for cfg_name in [name, alt_name] if alt_name else [name]:
154+
f = home / cfg_name
155+
if f.exists():
156+
with open(f) as config_file:
157+
config = yaml.safe_load(config_file)
158+
break
159+
146160
return config

tests/test_config.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2727
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
import os
29+
from pathlib import Path
2930
from unittest.mock import patch
3031
import pytest
3132

@@ -66,3 +67,28 @@ def test_default_cache_path(tempdir):
6667
c = Configuration.read(config_path="tests/example_config_no_cache_path.yaml")
6768
assert c.cache_path == "mytemp/servicex_p_higgs"
6869
del os.environ["USER"]
70+
71+
72+
def test_read_from_home(monkeypatch, tmp_path):
73+
"""Ensure configuration can be located in the user's home directory."""
74+
75+
# Create a fake home directory with a servicex.yaml file
76+
home = tmp_path / "home"
77+
home.mkdir()
78+
cfg = home / "servicex.yaml"
79+
cfg.write_text(
80+
"""
81+
api_endpoints:
82+
- endpoint: http://localhost:5000
83+
name: localhost
84+
"""
85+
)
86+
87+
# Patch Path.home to point to our fake home and move cwd elsewhere
88+
monkeypatch.setattr(Path, "home", lambda: home)
89+
work = tmp_path / "work"
90+
work.mkdir()
91+
monkeypatch.chdir(work)
92+
93+
c = Configuration.read()
94+
assert c.api_endpoints[0].endpoint == "http://localhost:5000"

0 commit comments

Comments
 (0)