[BUG]: Relative paths in the with section on the YAML are relative to the CWD instead of the YAML file #191

Merged
fraimondo merged 3 commits from fix/191 into main 2023-03-30 14:51:08 +00:00
3 changed files with 63 additions and 2 deletions

View 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`_.

View file

@ -6,7 +6,6 @@
import importlib
import importlib.util
import os
import sys
from pathlib import Path
from typing import Dict, Union
@ -56,7 +55,8 @@ def parse_yaml(filepath: Union[str, Path]) -> Dict:
for t_module in to_load:
if t_module.endswith(".py"):
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():
raise_error(
f"File in 'with' section does not exist: {file_path}"

View file

@ -75,3 +75,63 @@ def test_parse_yaml_failure_with_multi_module_autoload(tmp_path: Path) -> None:
# Check test file
with pytest.raises(ImportError, match="wrong_config"):
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)