From d97df06ce7be7e9b406d18bc9d7744c6343e87f3 Mon Sep 17 00:00:00 2001 From: benoit74 Date: Fri, 20 Dec 2024 09:11:20 +0000 Subject: [PATCH] Ignore silently if file is already gone in delete callback --- src/zimscraperlib/filesystem.py | 6 ++---- tests/filesystem/test_filesystem.py | 6 ++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/zimscraperlib/filesystem.py b/src/zimscraperlib/filesystem.py index 7d22065..7ce6f90 100644 --- a/src/zimscraperlib/filesystem.py +++ b/src/zimscraperlib/filesystem.py @@ -2,7 +2,6 @@ Shortcuts to retrieve mime type using magic""" -import os import pathlib from contextlib import contextmanager from tempfile import TemporaryDirectory @@ -40,10 +39,9 @@ def get_content_mimetype(content: bytes | str) -> str: return MIME_OVERRIDES.get(detected_mime, detected_mime) -def delete_callback(fpath: str | pathlib.Path): +def delete_callback(fpath: pathlib.Path): """helper deleting passed filepath""" - - os.unlink(fpath) + fpath.unlink(missing_ok=True) @contextmanager diff --git a/tests/filesystem/test_filesystem.py b/tests/filesystem/test_filesystem.py index 6518c59..a25fcb1 100644 --- a/tests/filesystem/test_filesystem.py +++ b/tests/filesystem/test_filesystem.py @@ -57,6 +57,12 @@ def test_delete_callback(tmp_path: pathlib.Path): assert not fpath.exists() + # file already gone should not be a problem + delete_callback(fpath) + + # wrong path should not be a problem + delete_callback(pathlib.Path("/foo.txt")) + def test_path_from_tmp_dir(): tempdir = TemporaryDirectory()