Skip to content

Commit 54cfdea

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 4380eb8 commit 54cfdea

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
@@ -78,6 +78,8 @@ def from_path(path: Path | list[Path] | None) -> '_InternalCF | None':
7878
paths = path
7979
if isinstance(paths, Path):
8080
paths = [Path(p) for p in str(paths).split(os.pathsep)]
81+
if not all([p.is_absolute() for p in paths]):
82+
raise WestNotFound(f"config file path(s) must be absolute: '{paths_to_str(paths)}'")
8183
paths = [p for p in paths if p.exists()]
8284
return _InternalCF(paths) if paths else None
8385

tests/test_config.py

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

592592

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

0 commit comments

Comments
 (0)