From e5b63ac6fbe8d137c90b5e14e187c78a5276021b Mon Sep 17 00:00:00 2001 From: Matteo Rosati Date: Thu, 23 Apr 2026 19:56:11 +0200 Subject: [PATCH] use absolute paths --- chromy/handlers/import_data.py | 12 +++++++++++- tests/test_cli.py | 10 +++++++++- tests/test_handlers.py | 10 +++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/chromy/handlers/import_data.py b/chromy/handlers/import_data.py index 643d574..55aa5d8 100644 --- a/chromy/handlers/import_data.py +++ b/chromy/handlers/import_data.py @@ -1,9 +1,19 @@ from __future__ import annotations +import os +from pathlib import Path from chromy.utilities import ingest_file def handle_import(collection: str, file: str) -> int: - records_added = ingest_file(collection, file) + if not os.path.exists(file): + raise FileNotFoundError() + + # This ensures that the path saved as file_name in the metadatas is the + # full path rather than a simple file name. + file_path = Path(file) + full_file_path = str(file_path.resolve(file_path)) + + records_added = ingest_file(collection, full_file_path) print(f"Added {records_added} records to collection '{collection}'.") return 0 diff --git a/tests/test_cli.py b/tests/test_cli.py index f23c986..f63a534 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2,6 +2,7 @@ from __future__ import annotations import unittest from collections.abc import Sequence +from pathlib import Path from unittest.mock import patch from click.testing import Result @@ -11,6 +12,10 @@ from chromy.cli import app class CliTests(unittest.TestCase): + @staticmethod + def _fixture_path(path: str) -> str: + return str(Path(path).resolve()) + def test_list_collections(self) -> None: with patch( "chromy.handlers.list_collections.list_collections", @@ -60,7 +65,10 @@ class CliTests(unittest.TestCase): ) as ingest_file: result = _invoke(["import", "notes", "romeo_and_juliet.txt"]) - ingest_file.assert_called_once_with("notes", "romeo_and_juliet.txt") + ingest_file.assert_called_once_with( + "notes", + self._fixture_path("romeo_and_juliet.txt"), + ) self.assertEqual(result.exit_code, 0) self.assertEqual(result.stdout, "Added 3 records to collection 'notes'.\n") diff --git a/tests/test_handlers.py b/tests/test_handlers.py index 49fb01f..144947c 100644 --- a/tests/test_handlers.py +++ b/tests/test_handlers.py @@ -4,6 +4,7 @@ import io import unittest from collections.abc import Callable from contextlib import redirect_stdout +from pathlib import Path from typing import TypeVar from unittest.mock import patch @@ -21,6 +22,10 @@ CommandT = TypeVar("CommandT") class HandlerTests(unittest.TestCase): + @staticmethod + def _fixture_path(path: str) -> str: + return str(Path(path).resolve()) + def test_list_collections_prints_empty_message(self) -> None: with patch( "chromy.handlers.list_collections.list_collections", return_value=[] @@ -94,7 +99,10 @@ class HandlerTests(unittest.TestCase): "romeo_and_juliet.txt", ) - ingest_file.assert_called_once_with("notes", "romeo_and_juliet.txt") + ingest_file.assert_called_once_with( + "notes", + self._fixture_path("romeo_and_juliet.txt"), + ) self.assertEqual(exit_code, 0) self.assertEqual(output, "Added 3 records to collection 'notes'.\n")