Skip to content

Commit 39223ed

Browse files
committed
Allow relative paths
1 parent 048c4e2 commit 39223ed

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

ratapi/models.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,7 @@ class CustomFile(RATModel):
313313
filename: str = ""
314314
function_name: str = ""
315315
language: Languages = Languages.Python
316-
path: pathlib.Path = pathlib.Path(".").resolve()
317-
318-
@field_validator("path")
319-
@classmethod
320-
def resolve_relative_paths(cls, path: pathlib.Path) -> pathlib.Path:
321-
"""Return the absolute path of the given path."""
322-
return path.resolve()
316+
path: pathlib.Path = pathlib.Path(".")
323317

324318
def model_post_init(self, __context: Any) -> None:
325319
"""Autogenerate the function name from the filename if not set.

ratapi/project.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,9 @@ def try_relative_to(path: Path, relative_to: Path) -> str:
10621062
"""
10631063
path = Path(path)
10641064
relative_to = Path(relative_to)
1065-
if path.is_relative_to(relative_to):
1065+
if not path.is_absolute():
1066+
return str(path)
1067+
elif path.is_relative_to(relative_to):
10661068
return str(path.relative_to(relative_to))
10671069
else:
10681070
warnings.warn(

tests/test_models.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Test the pydantic models."""
22

3-
import pathlib
43
import re
54
from collections.abc import Callable
65

@@ -101,11 +100,11 @@ def test_initialise_with_extra_fields(self, model: Callable, model_params: dict)
101100
model(new_field=1, **model_params)
102101

103102

104-
def test_custom_file_path_is_absolute() -> None:
105-
"""If we use provide a relative path to the custom file model, it should be converted to an absolute path."""
106-
relative_path = pathlib.Path("./relative_path")
107-
custom_file = ratapi.models.CustomFile(path=relative_path)
108-
assert custom_file.path.is_absolute()
103+
# def test_custom_file_path_is_absolute() -> None:
104+
# """If we use provide a relative path to the custom file model, it should be converted to an absolute path."""
105+
# relative_path = pathlib.Path("./relative_path")
106+
# custom_file = ratapi.models.CustomFile(path=relative_path)
107+
# assert custom_file.path.is_absolute()
109108

110109

111110
def test_data_eq() -> None:

tests/test_project.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,24 +1637,25 @@ def test_save_load(project, request):
16371637
def test_relative_paths():
16381638
"""Test that ``try_relative_to`` correctly creates relative paths to subfolders."""
16391639

1640-
with tempfile.TemporaryDirectory() as tmp:
1641-
data_path = Path(tmp, "data/myfile.dat")
1640+
cur_path = Path(".").resolve()
1641+
data_path = cur_path / "data/myfile.dat"
1642+
assert Path(ratapi.project.try_relative_to(data_path, cur_path)) == Path("data/myfile.dat")
16421643

1643-
assert Path(ratapi.project.try_relative_to(data_path, tmp)) == Path("data/myfile.dat")
1644+
# relative path will be left relative.
1645+
data_path = "data/myfile.dat"
1646+
assert Path(ratapi.project.try_relative_to(data_path, cur_path)) == Path("data/myfile.dat")
16441647

16451648

16461649
def test_relative_paths_warning():
16471650
"""Test that we get a warning for trying to walk up paths."""
16481651

1649-
data_path = "/tmp/project/data/mydata.dat"
1650-
relative_path = "/tmp/project/project_path/myproj.dat"
1652+
cur_path = Path(".").resolve()
1653+
data_path = cur_path / "tmp/project/data/mydata.dat"
1654+
relative_path = cur_path / "tmp/project/project_path/myproj.dat"
16511655

16521656
with pytest.warns(
16531657
match="Could not write custom file path as relative to the project directory, "
16541658
"which means that it may not work on other devices. If you would like to share your project, "
16551659
"make sure your custom files are in a subfolder of the project save location.",
16561660
):
1657-
assert (
1658-
Path(ratapi.project.try_relative_to(data_path, relative_path))
1659-
== Path("/tmp/project/data/mydata.dat").resolve()
1660-
)
1661+
assert Path(ratapi.project.try_relative_to(data_path, relative_path)) == data_path

0 commit comments

Comments
 (0)