diff --git a/koyeb/sandbox/filesystem.py b/koyeb/sandbox/filesystem.py index 0bb132f3..2e33cf60 100644 --- a/koyeb/sandbox/filesystem.py +++ b/koyeb/sandbox/filesystem.py @@ -2537,6 +2537,7 @@ def __init__( self.mode = mode self.encoding = encoding self._closed = False + self._written: Optional[Union[str, bytes]] = None def read(self) -> Union[str, bytes]: """Read file content synchronously""" @@ -2569,7 +2570,17 @@ def write(self, content: Union[str, bytes]) -> None: except SandboxFileNotFoundError: pass + accumulate = "w" in self.mode and "a" not in self.mode + if accumulate: + # write_file replaces the whole file, so accumulate across writes. + if self._written is not None: + if isinstance(self._written, bytes) != isinstance(content, bytes): + raise TypeError("Cannot mix bytes and str content in write mode") + content = self._written + content + self.filesystem.write_file(self.path, content, encoding=self.encoding) + if accumulate: + self._written = content def close(self) -> None: """Close the file""" @@ -2597,6 +2608,7 @@ def __init__( self.mode = mode self.encoding = encoding self._closed = False + self._written: Optional[Union[str, bytes]] = None async def read(self) -> Union[str, bytes]: """Read file content asynchronously""" @@ -2631,7 +2643,17 @@ async def write(self, content: Union[str, bytes]) -> None: except SandboxFileNotFoundError: pass + accumulate = "w" in self.mode and "a" not in self.mode + if accumulate: + # write_file replaces the whole file, so accumulate across writes. + if self._written is not None: + if isinstance(self._written, bytes) != isinstance(content, bytes): + raise TypeError("Cannot mix bytes and str content in write mode") + content = self._written + content + await self.filesystem.write_file(self.path, content, encoding=self.encoding) + if accumulate: + self._written = content def close(self) -> None: """Close the file""" diff --git a/koyeb/sandbox/test_filesystem.py b/koyeb/sandbox/test_filesystem.py index 1514674c..2bcaed04 100644 --- a/koyeb/sandbox/test_filesystem.py +++ b/koyeb/sandbox/test_filesystem.py @@ -341,6 +341,25 @@ def test_read_write_modes(self): handle.write("data") self.assertEqual(client.calls[0][0], "write_file") + def test_write_mode_accumulates_multiple_writes(self): + client = _FakeSyncClient() + with _fs(client) as fs: + with fs.open("/b", "w") as handle: + handle.write("first\n") + handle.write("second\n") + self.assertEqual(client.calls[-1], ("write_file", "/b", "first\nsecond\n")) + + def test_write_mode_retry_after_failed_write_does_not_duplicate(self): + client = _FakeSyncClient() + with _fs(client) as fs: + with fs.open("/b", "w") as handle: + client.replies["write_file"] = RuntimeError("boom") + with self.assertRaises(Exception): + handle.write("first") + del client.replies["write_file"] + handle.write("first") + self.assertEqual(client.calls[-1], ("write_file", "/b", "first")) + def test_wrong_mode_raises(self): with _fs(_FakeSyncClient()) as fs: with self.assertRaises(ValueError): @@ -357,6 +376,28 @@ def test_closed_handle_raises(self): handle.read() +class TestAsyncSandboxFileIO(unittest.IsolatedAsyncioTestCase): + async def test_write_mode_accumulates_multiple_writes(self): + client = _FakeAsyncClient() + with _async_fs(client) as fs: + async with fs.open("/b", "w") as handle: + await handle.write("first\n") + await handle.write("second\n") + self.assertEqual(client.calls[-1], ("write_file", "/b", "first\nsecond\n")) + + + async def test_write_mode_retry_after_failed_write_does_not_duplicate(self): + client = _FakeAsyncClient() + with _async_fs(client) as fs: + async with fs.open("/b", "w") as handle: + client.replies["write_file"] = RuntimeError("boom") + with self.assertRaises(Exception): + await handle.write("first") + del client.replies["write_file"] + await handle.write("first") + self.assertEqual(client.calls[-1], ("write_file", "/b", "first")) + + class TestAsyncReplyInterpretation(unittest.TestCase): """Async twin classifies through the same taxonomy."""