use absolute paths

This commit is contained in:
2026-04-23 19:56:11 +02:00
committed by ForgeCode
parent fb5d0f7c0c
commit e5b63ac6fb
3 changed files with 29 additions and 3 deletions
+11 -1
View File
@@ -1,9 +1,19 @@
from __future__ import annotations from __future__ import annotations
import os
from pathlib import Path
from chromy.utilities import ingest_file from chromy.utilities import ingest_file
def handle_import(collection: str, file: str) -> int: 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}'.") print(f"Added {records_added} records to collection '{collection}'.")
return 0 return 0
+9 -1
View File
@@ -2,6 +2,7 @@ from __future__ import annotations
import unittest import unittest
from collections.abc import Sequence from collections.abc import Sequence
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
from click.testing import Result from click.testing import Result
@@ -11,6 +12,10 @@ from chromy.cli import app
class CliTests(unittest.TestCase): class CliTests(unittest.TestCase):
@staticmethod
def _fixture_path(path: str) -> str:
return str(Path(path).resolve())
def test_list_collections(self) -> None: def test_list_collections(self) -> None:
with patch( with patch(
"chromy.handlers.list_collections.list_collections", "chromy.handlers.list_collections.list_collections",
@@ -60,7 +65,10 @@ class CliTests(unittest.TestCase):
) as ingest_file: ) as ingest_file:
result = _invoke(["import", "notes", "romeo_and_juliet.txt"]) 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.exit_code, 0)
self.assertEqual(result.stdout, "Added 3 records to collection 'notes'.\n") self.assertEqual(result.stdout, "Added 3 records to collection 'notes'.\n")
+9 -1
View File
@@ -4,6 +4,7 @@ import io
import unittest import unittest
from collections.abc import Callable from collections.abc import Callable
from contextlib import redirect_stdout from contextlib import redirect_stdout
from pathlib import Path
from typing import TypeVar from typing import TypeVar
from unittest.mock import patch from unittest.mock import patch
@@ -21,6 +22,10 @@ CommandT = TypeVar("CommandT")
class HandlerTests(unittest.TestCase): 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: def test_list_collections_prints_empty_message(self) -> None:
with patch( with patch(
"chromy.handlers.list_collections.list_collections", return_value=[] "chromy.handlers.list_collections.list_collections", return_value=[]
@@ -94,7 +99,10 @@ class HandlerTests(unittest.TestCase):
"romeo_and_juliet.txt", "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(exit_code, 0)
self.assertEqual(output, "Added 3 records to collection 'notes'.\n") self.assertEqual(output, "Added 3 records to collection 'notes'.\n")