Allow relative paths for custom file by StephenNneji · Pull Request #198 · RascalSoftware/python-RAT · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions ratapi/models.py
10 changes: 8 additions & 2 deletions ratapi/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,12 +956,16 @@ def make_data_dict(item):
elif field == "custom_files":

def make_custom_file_dict(item):
return {
file_dict = {
"name": item.name,
"filename": item.filename,
"language": item.language,
"path": try_relative_to(item.path, filepath.parent),
}
if item.name != item.function_name:
file_dict["function_name"] = item.function_name

return file_dict

json_dict["custom_files"] = [make_custom_file_dict(file) for file in attr]

Expand Down Expand Up @@ -1062,7 +1066,9 @@ def try_relative_to(path: Path, relative_to: Path) -> str:
"""
path = Path(path)
relative_to = Path(relative_to)
if path.is_relative_to(relative_to):
if not path.is_absolute():
return str(path)
elif path.is_relative_to(relative_to):
return str(path.relative_to(relative_to))
else:
warnings.warn(
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8537,3 +8537,11 @@ def absorption():
"""The project from the absorption example."""
project, _ = ratapi.examples.absorption()
return project


@pytest.fixture
def absorption_different_function():
"""The project from the absorption example with a function name different from filename."""
project, _ = ratapi.examples.absorption()
project.custom_files[0].function_name = "test_func"
return project
11 changes: 5 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Test the pydantic models."""

import pathlib
import re
from collections.abc import Callable

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


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


def test_data_eq() -> None:
Expand Down
20 changes: 11 additions & 9 deletions tests/test_project.py