[BUG]: Relative paths in the with section on the YAML are relative to the CWD instead of the YAML file #191
3 changed files with 63 additions and 2 deletions
1
docs/changes/newsfragments/191.bugfix
Normal file
1
docs/changes/newsfragments/191.bugfix
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Fix a bug in which relative paths in the YAML ``with`` directive would be computed relative to the current working directory of the process instead of the location of the YAML file by `Fede Raimondo`_.
|
||||||
|
|
@ -6,7 +6,6 @@
|
||||||
|
|
||||||
import importlib
|
import importlib
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, Union
|
from typing import Dict, Union
|
||||||
|
|
@ -56,7 +55,8 @@ def parse_yaml(filepath: Union[str, Path]) -> Dict:
|
||||||
for t_module in to_load:
|
for t_module in to_load:
|
||||||
if t_module.endswith(".py"):
|
if t_module.endswith(".py"):
|
||||||
logger.debug(f"Importing file: {t_module}")
|
logger.debug(f"Importing file: {t_module}")
|
||||||
file_path = Path(os.getcwd()) / t_module
|
# This resolves both absolute and relative paths
|
||||||
|
file_path = filepath.parent / t_module
|
||||||
if not file_path.exists():
|
if not file_path.exists():
|
||||||
raise_error(
|
raise_error(
|
||||||
f"File in 'with' section does not exist: {file_path}"
|
f"File in 'with' section does not exist: {file_path}"
|
||||||
|
|
|
||||||
|
|
@ -75,3 +75,63 @@ def test_parse_yaml_failure_with_multi_module_autoload(tmp_path: Path) -> None:
|
||||||
# Check test file
|
# Check test file
|
||||||
with pytest.raises(ImportError, match="wrong_config"):
|
with pytest.raises(ImportError, match="wrong_config"):
|
||||||
parse_yaml(fname)
|
parse_yaml(fname)
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_yaml_relative_path(tmp_path: Path) -> None:
|
||||||
|
"""Test YAML parsing with relative paths in with.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
tmp_path : pathlib.Path
|
||||||
|
The path to the test directory.
|
||||||
|
|
||||||
|
"""
|
||||||
|
t_tmp_path = tmp_path / "test_relative_with"
|
||||||
|
|
||||||
|
# Write .py to include
|
||||||
|
py_path = t_tmp_path / "external"
|
||||||
|
py_path.mkdir(exist_ok=True, parents=True)
|
||||||
|
py_fname = py_path / "first.py"
|
||||||
|
py_fname.write_text("import numpy as np\n")
|
||||||
|
|
||||||
|
# Write yaml that includes a relative path
|
||||||
|
yaml_path = t_tmp_path / "yamls"
|
||||||
|
yaml_path.mkdir(exist_ok=True, parents=True)
|
||||||
|
yaml_fname = yaml_path / "test_parse_yaml_relative_path.yaml"
|
||||||
|
|
||||||
|
yaml_fname.write_text(
|
||||||
|
"foo: bar\nwith:\n - ../external/first.py\n - scipy\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check test file
|
||||||
|
parse_yaml(yaml_fname)
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_yaml_absolute_path(tmp_path: Path) -> None:
|
||||||
|
"""Test YAML parsing with absolute paths in with.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
tmp_path : pathlib.Path
|
||||||
|
The path to the test directory.
|
||||||
|
|
||||||
|
"""
|
||||||
|
t_tmp_path = tmp_path / "test_relative_with"
|
||||||
|
|
||||||
|
# Write .py to include
|
||||||
|
py_path = t_tmp_path / "external"
|
||||||
|
py_path.mkdir(exist_ok=True, parents=True)
|
||||||
|
py_fname = py_path / "first.py"
|
||||||
|
py_fname.write_text("import numpy as np\n")
|
||||||
|
|
||||||
|
# Write yaml that includes a relative path
|
||||||
|
yaml_path = t_tmp_path / "yamls"
|
||||||
|
yaml_path.mkdir(exist_ok=True, parents=True)
|
||||||
|
yaml_fname = yaml_path / "test_parse_yaml_relative_path.yaml"
|
||||||
|
|
||||||
|
yaml_fname.write_text(
|
||||||
|
f"foo: bar\nwith:\n - {py_fname.absolute()}\n - scipy\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check test file
|
||||||
|
parse_yaml(yaml_fname)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue