diff --git a/docreader/tests/test_tempfile_context.py b/docreader/tests/test_tempfile_context.py new file mode 100644 index 00000000..45bf3775 --- /dev/null +++ b/docreader/tests/test_tempfile_context.py @@ -0,0 +1,89 @@ +import os +import tempfile +import unittest +from unittest.mock import patch + +from docreader.utils.tempfile import TempFileContext + + +class TempFileContextTest(unittest.TestCase): + def test_removes_file_when_write_fails_inside_enter(self): + created = [] + real_ntf = tempfile.NamedTemporaryFile + + def failing_ntf(*args, **kwargs): + f = real_ntf(*args, **kwargs) + created.append(f.name) + + class Wrapper: + def __getattr__(self, name): + return getattr(f, name) + + def write(self, data): + raise OSError(28, "No space left on device") + + return Wrapper() + + with patch("docreader.utils.tempfile.tempfile.NamedTemporaryFile", failing_ntf): + with self.assertRaises(OSError): + with TempFileContext(b"payload", ".doc"): + pass + + self.assertEqual(len(created), 1) + self.assertFalse(os.path.exists(created[0])) + + def test_removes_file_when_close_reraises_after_flush_fails(self): + # CPython NamedTemporaryFile.close() flushes again; after ENOSPC, + # close() typically re-raises and must not skip unlink or replace + # the original write/flush error. + created = [] + real_ntf = tempfile.NamedTemporaryFile + + def failing_ntf(*args, **kwargs): + f = real_ntf(*args, **kwargs) + created.append(f.name) + + class Wrapper: + def __init__(self): + self.name = f.name + + def write(self, data): + f.write(data) + + def flush(self): + raise OSError(28, "No space left on device") + + def close(self): + try: + raise OSError(5, "Input/output error") + finally: + f.close() + + return Wrapper() + + with patch("docreader.utils.tempfile.tempfile.NamedTemporaryFile", failing_ntf): + with self.assertRaises(OSError) as ctx: + with TempFileContext(b"payload", ".doc"): + pass + self.assertEqual(ctx.exception.errno, 28) + + self.assertEqual(len(created), 1) + self.assertFalse(os.path.exists(created[0])) + + def test_normal_roundtrip_and_cleanup(self): + payload = bytes(range(256)) * 4 + with TempFileContext(payload, ".doc") as path: + self.assertTrue(path.endswith(".doc")) + with open(path, "rb") as f: + self.assertEqual(f.read(), payload) + self.assertFalse(os.path.exists(path)) + + def test_exception_inside_body_still_cleans_up(self): + with self.assertRaises(ValueError): + with TempFileContext(b"x", ".doc") as path: + raise ValueError("body") + self.assertFalse(os.path.exists(path)) + + +if __name__ == "__main__": + unittest.main() diff --git a/docreader/utils/tempfile.py b/docreader/utils/tempfile.py index ab61619b..7e502288 100644 --- a/docreader/utils/tempfile.py +++ b/docreader/utils/tempfile.py @@ -16,13 +16,37 @@ class TempFileContext: self.suffix = suffix self.file = None + def _cleanup(self): + """Close and unlink the temp file. close() may re-raise a prior flush + error (ENOSPC), so unlink must still run and cleanup errors must not + replace the original exception. + """ + temp_file = getattr(self, "temp_file", None) + if not temp_file: + return + try: + temp_file.close() + except OSError: + pass + try: + os.remove(temp_file.name) + except OSError: + pass + def __enter__(self): """ Create file when entering context """ self.temp_file = tempfile.NamedTemporaryFile(suffix=self.suffix, delete=False) - self.temp_file.write(self.file_content) - self.temp_file.flush() + # If write/flush fails (e.g. ENOSPC), __exit__ is never called because + # the exception escapes __enter__, so clean up here or the file leaks + # permanently (delete=False means GC will not remove it either). + try: + self.temp_file.write(self.file_content) + self.temp_file.flush() + except BaseException: + self._cleanup() + raise logger.info( f"Saved {self.suffix} content to temporary file: {self.temp_file.name}" ) @@ -32,11 +56,10 @@ class TempFileContext: """ Delete file when exiting context """ - if self.temp_file: - self.temp_file.close() - if os.path.exists(self.temp_file.name): - os.remove(self.temp_file.name) - logger.info(f"File {self.temp_file.name} has been deleted.") + if getattr(self, "temp_file", None): + name = self.temp_file.name + self._cleanup() + logger.info(f"File {name} has been deleted.") # Return False to propagate exception (if any exception occurred) return False