diff --git a/glotaran/project/test/test_project.py b/glotaran/project/test/test_project.py index 808e98654..ecee06bb8 100644 --- a/glotaran/project/test/test_project.py +++ b/glotaran/project/test/test_project.py @@ -1,5 +1,6 @@ from __future__ import annotations +import sys from importlib.metadata import distribution from pathlib import Path from shutil import rmtree @@ -7,6 +8,7 @@ from typing import Literal import pytest +from _pytest.monkeypatch import MonkeyPatch from _pytest.recwarn import WarningsRecorder from IPython.core.formatters import format_display_data @@ -24,6 +26,7 @@ from glotaran.testing.simulated_data.sequential_spectral_decay import ( PARAMETERS as example_parameter, ) +from glotaran.utils.io import chdir_context @pytest.fixture(scope="module") @@ -345,6 +348,27 @@ def test_generators_allow_overwrite(project_folder: Path, project_file: Path): assert len(list(filter(lambda p: p.label.startswith("rates"), parameters.all()))) == 3 +def test_relative_paths(tmp_path: Path, monkeypatch: MonkeyPatch): + """Test project using relative paths where cwd and script folder differ.""" + script_folder = tmp_path / "project" + script_folder.mkdir(parents=True, exist_ok=True) + with chdir_context(tmp_path), monkeypatch.context() as m: + # Pretend the currently running script is located at ``script_folder / "script.py"`` + m.setattr( + sys.modules[globals()["__name__"]], + "__file__", + (script_folder / "script.py").as_posix(), + ) + data_import_path = "import_data.nc" + save_dataset(example_dataset, data_import_path) + assert (tmp_path / data_import_path).is_file() is True + + project = Project.open("project.gta") + assert (script_folder / "project.gta").is_file() is True + project.import_data(f"../{data_import_path}", name="dataset_1") + assert (script_folder / "data/dataset_1.nc").is_file() is True + + def test_missing_file_errors(tmp_path: Path, project_folder: Path): """Error when accessing non existing files.""" with pytest.raises(FileNotFoundError) as exc_info: