Spaces:
Running
Running
File size: 21,971 Bytes
5a76ab0 5d06dbc 5a76ab0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 | from pathlib import Path
from typing import Any, Callable
import pytest
from langchain.tools import ToolRuntime
from langchain_core.tools import BaseTool, StructuredTool
from multi_agent_sdlc.tools.coder.filesystem import (
coder_create_directory,
coder_delete_directory,
coder_delete_file,
coder_move_path,
coder_write_file,
)
from multi_agent_sdlc.workflow.state import DevState
def get_tool_function(
tool: BaseTool,
) -> Callable[..., Any]:
if not isinstance(tool, StructuredTool):
raise TypeError(f"Expected StructuredTool, got {type(tool).__name__}")
if tool.func is None:
raise TypeError(f"Tool {tool.name} does not have a synchronous function")
return tool.func
def test_write_file_creates_file(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
function = get_tool_function(coder_write_file)
result = function(
path="main.py",
content="print('hello')\n",
runtime=tool_runtime,
)
file_path = project_directory / "main.py"
assert file_path.is_file()
assert file_path.read_text(encoding="utf-8") == "print('hello')\n"
assert result == "Written production file: main.py"
def test_write_file_creates_parent_directories(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
function = get_tool_function(coder_write_file)
function(
path="src/services/main.py",
content="value = 1\n",
runtime=tool_runtime,
)
file_path = project_directory / "src/services/main.py"
assert file_path.is_file()
assert file_path.read_text(encoding="utf-8") == "value = 1\n"
def test_write_file_overwrites_existing_file(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
file_path = project_directory / "main.py"
file_path.write_text("old", encoding="utf-8")
function = get_tool_function(coder_write_file)
function(
path="main.py",
content="new",
runtime=tool_runtime,
)
assert file_path.read_text(encoding="utf-8") == "new"
def test_write_file_rejects_directory(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
directory = project_directory / "main.py"
directory.mkdir()
function = get_tool_function(coder_write_file)
with pytest.raises(IsADirectoryError):
function(
path="main.py",
content="new",
runtime=tool_runtime,
)
assert directory.is_dir()
def test_write_file_rejects_test_path(
tool_runtime: ToolRuntime[DevState],
) -> None:
function = get_tool_function(coder_write_file)
with pytest.raises(PermissionError):
function(
path="tests/test_main.py",
content="",
runtime=tool_runtime,
)
def test_write_file_rejects_path_escape(
tool_runtime: ToolRuntime[DevState],
) -> None:
function = get_tool_function(coder_write_file)
with pytest.raises(PermissionError):
function(
path="../outside.py",
content="",
runtime=tool_runtime,
)
def test_create_directory_creates_directory(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
function = get_tool_function(coder_create_directory)
result = function(
path="src/services",
runtime=tool_runtime,
)
assert (project_directory / "src/services").is_dir()
assert result == "Created production directory: src/services"
def test_create_directory_creates_parent_directories(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
function = get_tool_function(coder_create_directory)
function(
path="src/services/internal",
runtime=tool_runtime,
)
assert (project_directory / "src/services/internal").is_dir()
def test_create_directory_allows_existing_directory(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
directory = project_directory / "src"
directory.mkdir()
function = get_tool_function(coder_create_directory)
result = function(
path="src",
runtime=tool_runtime,
)
assert directory.is_dir()
assert result == "Created production directory: src"
def test_create_directory_rejects_existing_file(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
file_path = project_directory / "src"
file_path.write_text("content", encoding="utf-8")
function = get_tool_function(coder_create_directory)
with pytest.raises(FileExistsError):
function(
path="src",
runtime=tool_runtime,
)
assert file_path.is_file()
def test_create_directory_rejects_test_path(
tool_runtime: ToolRuntime[DevState],
) -> None:
function = get_tool_function(coder_create_directory)
with pytest.raises(PermissionError):
function(
path="tests/unit",
runtime=tool_runtime,
)
def test_move_path_renames_file(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
source = project_directory / "old.py"
source.write_text("value = 1", encoding="utf-8")
function = get_tool_function(coder_move_path)
result = function(
source_path="old.py",
destination_path="new.py",
runtime=tool_runtime,
)
destination = project_directory / "new.py"
assert not source.exists()
assert destination.is_file()
assert destination.read_text(encoding="utf-8") == "value = 1"
assert result == "Moved production path: old.py -> new.py"
def test_move_path_moves_file_between_directories(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
source_directory = project_directory / "src"
destination_directory = project_directory / "app"
source_directory.mkdir()
destination_directory.mkdir()
source = source_directory / "main.py"
source.write_text("value = 1", encoding="utf-8")
function = get_tool_function(coder_move_path)
function(
source_path="src/main.py",
destination_path="app/main.py",
runtime=tool_runtime,
)
destination = destination_directory / "main.py"
assert not source.exists()
assert destination.is_file()
assert destination.read_text(encoding="utf-8") == "value = 1"
def test_move_path_renames_directory(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
source = project_directory / "old_package"
source.mkdir()
(source / "module.py").write_text("value = 1", encoding="utf-8")
function = get_tool_function(coder_move_path)
function(
source_path="old_package",
destination_path="new_package",
runtime=tool_runtime,
)
destination = project_directory / "new_package"
assert not source.exists()
assert destination.is_dir()
assert (destination / "module.py").is_file()
def test_move_path_rejects_missing_source(
tool_runtime: ToolRuntime[DevState],
) -> None:
function = get_tool_function(coder_move_path)
with pytest.raises(FileNotFoundError):
function(
source_path="missing.py",
destination_path="new.py",
runtime=tool_runtime,
)
def test_move_path_rejects_existing_destination(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
source = project_directory / "old.py"
destination = project_directory / "new.py"
source.write_text("old", encoding="utf-8")
destination.write_text("new", encoding="utf-8")
function = get_tool_function(coder_move_path)
with pytest.raises(FileExistsError):
function(
source_path="old.py",
destination_path="new.py",
runtime=tool_runtime,
)
assert source.exists()
assert destination.read_text(encoding="utf-8") == "new"
def test_move_path_rejects_missing_destination_parent(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
source = project_directory / "main.py"
source.write_text("", encoding="utf-8")
function = get_tool_function(coder_move_path)
with pytest.raises(FileNotFoundError):
function(
source_path="main.py",
destination_path="app/main.py",
runtime=tool_runtime,
)
assert source.exists()
def test_move_path_rejects_directory_inside_itself(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
directory = project_directory / "app"
directory.mkdir()
function = get_tool_function(coder_move_path)
with pytest.raises(ValueError):
function(
source_path="app",
destination_path="app/internal",
runtime=tool_runtime,
)
assert directory.is_dir()
def test_move_path_rejects_test_source(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
tests_directory = project_directory / "tests"
tests_directory.mkdir()
test_file = tests_directory / "test_app.py"
test_file.write_text("", encoding="utf-8")
function = get_tool_function(coder_move_path)
with pytest.raises(PermissionError):
function(
source_path="tests/test_app.py",
destination_path="test_app.py",
runtime=tool_runtime,
)
assert test_file.exists()
def test_move_path_rejects_test_destination(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
source = project_directory / "main.py"
source.write_text("", encoding="utf-8")
function = get_tool_function(coder_move_path)
with pytest.raises(PermissionError):
function(
source_path="main.py",
destination_path="tests/main.py",
runtime=tool_runtime,
)
assert source.exists()
def test_delete_file_deletes_existing_file(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
file_path = project_directory / "obsolete.py"
file_path.write_text("value = 1", encoding="utf-8")
function = get_tool_function(coder_delete_file)
result = function(
path="obsolete.py",
runtime=tool_runtime,
)
assert not file_path.exists()
assert result == "Deleted production file: obsolete.py"
def test_delete_file_deletes_nested_file(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
directory = project_directory / "src" / "legacy"
directory.mkdir(parents=True)
file_path = directory / "obsolete.py"
file_path.write_text("value = 1", encoding="utf-8")
function = get_tool_function(coder_delete_file)
function(
path="src/legacy/obsolete.py",
runtime=tool_runtime,
)
assert not file_path.exists()
assert directory.is_dir()
def test_delete_file_does_not_delete_sibling(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
directory = project_directory / "src"
directory.mkdir()
target = directory / "obsolete.py"
sibling = directory / "keep.py"
target.write_text("delete", encoding="utf-8")
sibling.write_text("keep", encoding="utf-8")
function = get_tool_function(coder_delete_file)
function(
path="src/obsolete.py",
runtime=tool_runtime,
)
assert not target.exists()
assert sibling.exists()
assert sibling.read_text(encoding="utf-8") == "keep"
assert directory.is_dir()
def test_delete_file_rejects_missing_file(
tool_runtime: ToolRuntime[DevState],
) -> None:
function = get_tool_function(coder_delete_file)
with pytest.raises(FileNotFoundError):
function(
path="missing.py",
runtime=tool_runtime,
)
def test_delete_file_rejects_directory(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
directory = project_directory / "src"
directory.mkdir()
marker = directory / "marker.py"
marker.write_text("keep", encoding="utf-8")
function = get_tool_function(coder_delete_file)
with pytest.raises(IsADirectoryError):
function(
path="src",
runtime=tool_runtime,
)
assert directory.is_dir()
assert marker.exists()
def test_delete_file_rejects_test_file(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
tests_directory = project_directory / "tests"
tests_directory.mkdir()
test_file = tests_directory / "test_app.py"
test_file.write_text("keep", encoding="utf-8")
function = get_tool_function(coder_delete_file)
with pytest.raises(PermissionError):
function(
path="tests/test_app.py",
runtime=tool_runtime,
)
assert test_file.exists()
assert test_file.read_text(encoding="utf-8") == "keep"
@pytest.mark.parametrize(
"path",
[
"../outside.py",
"../../outside.py",
"/tmp/outside.py",
],
)
def test_delete_file_rejects_path_escape(
tool_runtime: ToolRuntime[DevState],
path: str,
) -> None:
function = get_tool_function(coder_delete_file)
with pytest.raises(PermissionError):
function(
path=path,
runtime=tool_runtime,
)
@pytest.mark.parametrize(
"path",
[
"terminal-calculator/file.py",
"sandbox/terminal-calculator/file.py",
],
)
def test_delete_file_rejects_project_prefix(
tool_runtime: ToolRuntime[DevState],
path: str,
) -> None:
function = get_tool_function(coder_delete_file)
with pytest.raises(PermissionError):
function(
path=path,
runtime=tool_runtime,
)
def test_delete_file_rejects_external_symlink(
tmp_path: Path,
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
outside_file = tmp_path / "outside.py"
outside_file.write_text("keep", encoding="utf-8")
alias = project_directory / "external.py"
alias.symlink_to(outside_file)
function = get_tool_function(coder_delete_file)
with pytest.raises(PermissionError):
function(
path="external.py",
runtime=tool_runtime,
)
assert outside_file.exists()
assert outside_file.read_text(encoding="utf-8") == "keep"
assert alias.is_symlink()
def test_delete_file_allows_internal_symlink_target(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
target = project_directory / "real.py"
target.write_text("value = 1", encoding="utf-8")
alias = project_directory / "alias.py"
alias.symlink_to(target)
function = get_tool_function(coder_delete_file)
function(
path="alias.py",
runtime=tool_runtime,
)
assert not target.exists()
assert alias.is_symlink()
def test_delete_directory_deletes_empty_directory(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
directory = project_directory / "obsolete"
directory.mkdir()
function = get_tool_function(coder_delete_directory)
result = function(
path="obsolete",
runtime=tool_runtime,
)
assert not directory.exists()
assert result == "Deleted production directory: obsolete"
def test_delete_directory_deletes_nonempty_directory(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
directory = project_directory / "obsolete"
directory.mkdir()
file_path = directory / "module.py"
file_path.write_text("value = 1", encoding="utf-8")
function = get_tool_function(coder_delete_directory)
function(
path="obsolete",
runtime=tool_runtime,
)
assert not directory.exists()
assert not file_path.exists()
def test_delete_directory_deletes_nested_tree(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
directory = project_directory / "obsolete"
nested_directory = directory / "package" / "internal"
nested_directory.mkdir(parents=True)
root_file = directory / "README.md"
nested_file = nested_directory / "module.py"
root_file.write_text("obsolete", encoding="utf-8")
nested_file.write_text("value = 1", encoding="utf-8")
function = get_tool_function(coder_delete_directory)
function(
path="obsolete",
runtime=tool_runtime,
)
assert not directory.exists()
assert not root_file.exists()
assert not nested_directory.exists()
assert not nested_file.exists()
def test_delete_directory_does_not_delete_sibling(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
target = project_directory / "obsolete"
sibling = project_directory / "keep"
target.mkdir()
sibling.mkdir()
target_file = target / "delete.py"
sibling_file = sibling / "keep.py"
target_file.write_text("delete", encoding="utf-8")
sibling_file.write_text("keep", encoding="utf-8")
function = get_tool_function(coder_delete_directory)
function(
path="obsolete",
runtime=tool_runtime,
)
assert not target.exists()
assert sibling.is_dir()
assert sibling_file.exists()
assert sibling_file.read_text(encoding="utf-8") == "keep"
def test_delete_directory_rejects_missing_directory(
tool_runtime: ToolRuntime[DevState],
) -> None:
function = get_tool_function(coder_delete_directory)
with pytest.raises(FileNotFoundError):
function(
path="missing",
runtime=tool_runtime,
)
def test_delete_directory_rejects_file(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
file_path = project_directory / "main.py"
file_path.write_text("keep", encoding="utf-8")
function = get_tool_function(coder_delete_directory)
with pytest.raises(NotADirectoryError):
function(
path="main.py",
runtime=tool_runtime,
)
assert file_path.exists()
assert file_path.read_text(encoding="utf-8") == "keep"
@pytest.mark.parametrize(
"path",
[
".",
"./",
],
)
def test_delete_directory_rejects_project_root(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
path: str,
) -> None:
marker = project_directory / "marker.txt"
marker.write_text("keep", encoding="utf-8")
function = get_tool_function(coder_delete_directory)
with pytest.raises(PermissionError):
function(
path=path,
runtime=tool_runtime,
)
assert project_directory.is_dir()
assert marker.exists()
assert marker.read_text(encoding="utf-8") == "keep"
@pytest.mark.parametrize(
"path",
[
"tests",
"tests/unit",
"tests/integration",
],
)
def test_delete_directory_rejects_test_directory(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
path: str,
) -> None:
target = project_directory / path
target.mkdir(parents=True)
marker = target / "test_marker.py"
marker.write_text("keep", encoding="utf-8")
function = get_tool_function(coder_delete_directory)
with pytest.raises(PermissionError):
function(
path=path,
runtime=tool_runtime,
)
assert target.is_dir()
assert marker.exists()
assert marker.read_text(encoding="utf-8") == "keep"
@pytest.mark.parametrize(
"path",
[
"../outside",
"../../outside",
"/tmp/outside",
],
)
def test_delete_directory_rejects_path_escape(
tool_runtime: ToolRuntime[DevState],
path: str,
) -> None:
function = get_tool_function(coder_delete_directory)
with pytest.raises(PermissionError):
function(
path=path,
runtime=tool_runtime,
)
@pytest.mark.parametrize(
"path",
[
"terminal-calculator/obsolete",
"sandbox/terminal-calculator/obsolete",
],
)
def test_delete_directory_rejects_project_prefix(
tool_runtime: ToolRuntime[DevState],
path: str,
) -> None:
function = get_tool_function(coder_delete_directory)
with pytest.raises(PermissionError):
function(
path=path,
runtime=tool_runtime,
)
def test_delete_directory_rejects_external_symlink(
tmp_path: Path,
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
outside_directory = tmp_path / "outside"
outside_directory.mkdir()
marker = outside_directory / "marker.txt"
marker.write_text("keep", encoding="utf-8")
alias = project_directory / "external"
alias.symlink_to(
outside_directory,
target_is_directory=True,
)
function = get_tool_function(coder_delete_directory)
with pytest.raises(PermissionError):
function(
path="external",
runtime=tool_runtime,
)
assert outside_directory.is_dir()
assert marker.exists()
assert marker.read_text(encoding="utf-8") == "keep"
assert alias.is_symlink()
def test_delete_directory_allows_internal_symlink_target(
project_directory: Path,
tool_runtime: ToolRuntime[DevState],
) -> None:
target = project_directory / "real_directory"
target.mkdir()
target_file = target / "module.py"
target_file.write_text("value = 1", encoding="utf-8")
alias = project_directory / "alias"
alias.symlink_to(
target,
target_is_directory=True,
)
function = get_tool_function(coder_delete_directory)
function(
path="alias",
runtime=tool_runtime,
)
assert not target.exists()
assert not target_file.exists()
assert alias.is_symlink()
|