Skip to content

Commit 7dd80b3

Browse files
authored
Merge pull request #59 from hyperbrowserai/fix-overwrite
fix file overwrite
2 parents 49a8ce0 + 13685c1 commit 7dd80b3

6 files changed

Lines changed: 756 additions & 13 deletions

File tree

hyperbrowser/client/managers/async_manager/sandboxes/sandbox_files.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
SandboxFileCopyParams,
1919
SandboxFileDeleteParams,
2020
SandboxFileInfo,
21+
SandboxFileMoveParams,
2122
SandboxFileReadResult,
2223
SandboxFileSystemEvent,
2324
SandboxFileWriteEntry,
@@ -461,14 +462,24 @@ async def mkdir(
461462
) -> bool:
462463
return await self.make_dir(path, parents=parents, mode=mode)
463464

464-
async def rename(self, old_path: str, new_path: str) -> SandboxFileInfo:
465+
async def rename(
466+
self,
467+
old_path: str,
468+
new_path: str,
469+
*,
470+
overwrite: Optional[bool] = None,
471+
) -> SandboxFileInfo:
472+
payload = SandboxFileMoveParams(
473+
source=old_path,
474+
destination=new_path,
475+
overwrite=overwrite,
476+
).model_dump(exclude_none=True)
477+
payload["from"] = payload.pop("source")
478+
payload["to"] = payload.pop("destination")
465479
payload = await self._transport.request_json(
466480
"/sandbox/files/move",
467481
method="POST",
468-
json_body={
469-
"from": old_path,
470-
"to": new_path,
471-
},
482+
json_body=payload,
472483
headers={"content-type": "application/json"},
473484
)
474485
return _normalize_file_info(payload["entry"])
@@ -480,7 +491,7 @@ async def move(
480491
destination: str,
481492
overwrite: Optional[bool] = None,
482493
) -> SandboxFileInfo:
483-
return await self.rename(source, destination)
494+
return await self.rename(source, destination, overwrite=overwrite)
484495

485496
async def remove(self, path: str, *, recursive: Optional[bool] = None) -> None:
486497
await self._transport.request_json(

hyperbrowser/client/managers/sync_manager/sandboxes/sandbox_files.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
SandboxFileCopyParams,
1818
SandboxFileDeleteParams,
1919
SandboxFileInfo,
20+
SandboxFileMoveParams,
2021
SandboxFileReadResult,
2122
SandboxFileSystemEvent,
2223
SandboxFileWriteEntry,
@@ -442,14 +443,24 @@ def mkdir(
442443
) -> bool:
443444
return self.make_dir(path, parents=parents, mode=mode)
444445

445-
def rename(self, old_path: str, new_path: str) -> SandboxFileInfo:
446+
def rename(
447+
self,
448+
old_path: str,
449+
new_path: str,
450+
*,
451+
overwrite: Optional[bool] = None,
452+
) -> SandboxFileInfo:
453+
payload = SandboxFileMoveParams(
454+
source=old_path,
455+
destination=new_path,
456+
overwrite=overwrite,
457+
).model_dump(exclude_none=True)
458+
payload["from"] = payload.pop("source")
459+
payload["to"] = payload.pop("destination")
446460
payload = self._transport.request_json(
447461
"/sandbox/files/move",
448462
method="POST",
449-
json_body={
450-
"from": old_path,
451-
"to": new_path,
452-
},
463+
json_body=payload,
453464
headers={"content-type": "application/json"},
454465
)
455466
return _normalize_file_info(payload["entry"])
@@ -461,7 +472,7 @@ def move(
461472
destination: str,
462473
overwrite: Optional[bool] = None,
463474
) -> SandboxFileInfo:
464-
return self.rename(source, destination)
475+
return self.rename(source, destination, overwrite=overwrite)
465476

466477
def remove(self, path: str, *, recursive: Optional[bool] = None) -> None:
467478
self._transport.request_json(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "hyperbrowser"
3-
version = "0.85.0"
3+
version = "0.86.0"
44
description = "Python SDK for hyperbrowser"
55
authors = ["Nikhil Shahi <nshahi1998@gmail.com>"]
66
license = "MIT"

tests/sandbox/e2e/test_async_files.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,35 @@ async def test_async_sandbox_files_e2e():
327327
assert await sandbox.files.read_text(existing_target) == "existing target"
328328
assert (await sandbox.files.get_info(destination_link)).symlink_target is None
329329

330+
move_source = f"{base_dir}/move-overwrite/source.txt"
331+
move_existing_target = f"{base_dir}/move-overwrite/existing-target.txt"
332+
move_destination_link = f"{base_dir}/move-overwrite/destination-link.txt"
333+
await sandbox.files.write_text(move_source, "move source payload")
334+
await sandbox.files.write_text(move_existing_target, "move existing target")
335+
result = await sandbox.exec(
336+
_bash_exec(
337+
f'mkdir -p "{base_dir}/move-overwrite" && ln -sfn "{move_existing_target}" "{move_destination_link}"'
338+
)
339+
)
340+
assert result.exit_code == 0
341+
await sandbox.files.move(
342+
source=move_source,
343+
destination=move_destination_link,
344+
overwrite=True,
345+
)
346+
assert (
347+
await sandbox.files.read_text(move_destination_link)
348+
== "move source payload"
349+
)
350+
assert (
351+
await sandbox.files.read_text(move_existing_target)
352+
== "move existing target"
353+
)
354+
assert (
355+
await sandbox.files.get_info(move_destination_link)
356+
).symlink_target is None
357+
assert await sandbox.files.exists(move_source) is False
358+
330359
chmod_path = f"{base_dir}/chmod/file.txt"
331360
await sandbox.files.write_text(chmod_path, "chmod me")
332361
await sandbox.files.chmod(path=chmod_path, mode="0640")

tests/sandbox/e2e/test_files.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,27 @@ def test_sandbox_files_e2e():
294294
assert sandbox.files.read_text(existing_target) == "existing target"
295295
assert sandbox.files.get_info(destination_link).symlink_target is None
296296

297+
move_source = f"{base_dir}/move-overwrite/source.txt"
298+
move_existing_target = f"{base_dir}/move-overwrite/existing-target.txt"
299+
move_destination_link = f"{base_dir}/move-overwrite/destination-link.txt"
300+
sandbox.files.write_text(move_source, "move source payload")
301+
sandbox.files.write_text(move_existing_target, "move existing target")
302+
result = sandbox.exec(
303+
_bash_exec(
304+
f'mkdir -p "{base_dir}/move-overwrite" && ln -sfn "{move_existing_target}" "{move_destination_link}"'
305+
)
306+
)
307+
assert result.exit_code == 0
308+
sandbox.files.move(
309+
source=move_source,
310+
destination=move_destination_link,
311+
overwrite=True,
312+
)
313+
assert sandbox.files.read_text(move_destination_link) == "move source payload"
314+
assert sandbox.files.read_text(move_existing_target) == "move existing target"
315+
assert sandbox.files.get_info(move_destination_link).symlink_target is None
316+
assert sandbox.files.exists(move_source) is False
317+
297318
chmod_path = f"{base_dir}/chmod/file.txt"
298319
sandbox.files.write_text(chmod_path, "chmod me")
299320
sandbox.files.chmod(path=chmod_path, mode="0640")

0 commit comments

Comments
 (0)