Skip to content

Commit 33dee41

Browse files
ensure that config file paths are absolute
WEST_CONFIG_* environment variables require absolute paths for their specified config files to ensure independency from the current working directory.
1 parent f449afc commit 33dee41

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/west/configuration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def from_paths(paths: list[Path] | None) -> '_InternalCF | None':
7474
paths = paths or []
7575
if not paths:
7676
return None
77+
if not all(p.is_absolute() for p in paths):
78+
raise WestNotFound(f"config file path(s) must be absolute: '{paths_to_str(paths)}'")
7779
paths = [p for p in paths if p.exists()]
7880
return _InternalCF(paths) if paths else None
7981

tests/test_config.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,29 @@ def test_config_precedence():
589589
assert cfg(f=LOCAL)['pytest']['precedence'] == 'local'
590590

591591

592+
TEST_CASES_CONFIG_RELATIVE = [
593+
# (flag, env_var)
594+
('--local', 'WEST_CONFIG_LOCAL'),
595+
('--system', 'WEST_CONFIG_SYSTEM'),
596+
('--global', 'WEST_CONFIG_GLOBAL'),
597+
]
598+
599+
600+
@pytest.mark.parametrize("test_case", TEST_CASES_CONFIG_RELATIVE)
601+
def test_config_relative_paths(test_case, config_tmpdir):
602+
flag, env_var = test_case
603+
604+
# one relative config file path is specified
605+
os.environ[env_var] = 'config1'
606+
_, stderr = cmd_raises(f'config {flag} some.key', WestNotFound)
607+
assert f"config file path(s) must be absolute: '{os.environ[env_var]}'" in stderr
608+
609+
# one of multiple config file paths is relative
610+
os.environ[env_var] = f'config1{os.pathsep}config2'
611+
_, stderr = cmd_raises(f'config {flag} some.key', WestNotFound)
612+
assert f"config file path(s) must be absolute: '{os.environ[env_var]}'" in stderr
613+
614+
592615
def test_config_multiple(config_tmpdir):
593616
# Verify that local settings take precedence over global ones,
594617
# but that both values are still available, and that setting

0 commit comments

Comments
 (0)