|
16 | 16 |
|
17 | 17 | import asyncio |
18 | 18 | import builtins |
| 19 | +import contextlib |
| 20 | +import errno |
19 | 21 | import io |
| 22 | +import os |
20 | 23 | import pathlib |
21 | 24 | import struct |
22 | 25 | import threading |
@@ -586,6 +589,155 @@ def strict(skill_id, error): |
586 | 589 | list_skills_in_dir(_skills_dir_with_one_invalid(tmp_path), on_error=strict) |
587 | 590 |
|
588 | 591 |
|
| 592 | +def _skills_dir_with_blocked_child(tmp_path): |
| 593 | + """Builds one valid skill, one plain file, and one blocked directory.""" |
| 594 | + skills_dir = tmp_path / "skills" |
| 595 | + skills_dir.mkdir() |
| 596 | + (skills_dir / "good").mkdir() |
| 597 | + (skills_dir / "good" / "SKILL.md").write_text( |
| 598 | + "---\nname: good\ndescription: desc\n---\nbody" |
| 599 | + ) |
| 600 | + blocked = skills_dir / "blocked-skill" |
| 601 | + blocked.mkdir() |
| 602 | + (blocked / "SKILL.md").write_text( |
| 603 | + "---\nname: blocked-skill\ndescription: desc\n---\nbody" |
| 604 | + ) |
| 605 | + (skills_dir / "notes.txt").write_text("not a skill") |
| 606 | + return skills_dir, blocked |
| 607 | + |
| 608 | + |
| 609 | +@contextlib.contextmanager |
| 610 | +def _deny_child_metadata(blocked): |
| 611 | + """Raise PermissionError from os.stat for one child. |
| 612 | +
|
| 613 | + The failure is injected at the stat boundary. Path.is_dir() is forced to |
| 614 | + return False for that child, which is what Python 3.14 and 3.15 do when |
| 615 | + stat fails. Mocking is_dir() to raise would hide that change. |
| 616 | + """ |
| 617 | + real_stat = os.stat |
| 618 | + real_is_dir = pathlib.Path.is_dir |
| 619 | + denied = { |
| 620 | + os.fspath(blocked), |
| 621 | + os.fspath(blocked.resolve()), |
| 622 | + } |
| 623 | + |
| 624 | + def fake_stat(path, *args, **kwargs): |
| 625 | + if os.fspath(path) in denied: |
| 626 | + raise PermissionError( |
| 627 | + errno.EACCES, "Permission denied", os.fspath(blocked) |
| 628 | + ) |
| 629 | + return real_stat(path, *args, **kwargs) |
| 630 | + |
| 631 | + def fake_is_dir(self, *args, **kwargs): |
| 632 | + if os.fspath(self) in denied: |
| 633 | + return False |
| 634 | + return real_is_dir(self, *args, **kwargs) |
| 635 | + |
| 636 | + with ( |
| 637 | + mock.patch("os.stat", side_effect=fake_stat), |
| 638 | + mock.patch.object(pathlib.Path, "is_dir", fake_is_dir), |
| 639 | + ): |
| 640 | + yield |
| 641 | + |
| 642 | + |
| 643 | +@mock.patch("logging.warning") |
| 644 | +def test_list_skills_in_dir_reports_inaccessible_directory_to_on_error( |
| 645 | + mock_logging_warning, tmp_path |
| 646 | +): |
| 647 | + """on_error receives a child whose directory metadata cannot be read.""" |
| 648 | + skills_dir, blocked = _skills_dir_with_blocked_child(tmp_path) |
| 649 | + failures = [] |
| 650 | + |
| 651 | + with _deny_child_metadata(blocked): |
| 652 | + skills = list_skills_in_dir( |
| 653 | + skills_dir, |
| 654 | + on_error=lambda skill_id, error: failures.append((skill_id, error)), |
| 655 | + ) |
| 656 | + |
| 657 | + assert list(skills) == ["good"] |
| 658 | + assert [skill_id for skill_id, _ in failures] == ["blocked-skill"] |
| 659 | + assert isinstance(failures[0][1], PermissionError) |
| 660 | + mock_logging_warning.assert_not_called() |
| 661 | + |
| 662 | + |
| 663 | +@mock.patch("logging.warning") |
| 664 | +def test_list_skills_in_dir_logs_inaccessible_directory_without_on_error( |
| 665 | + mock_logging_warning, tmp_path |
| 666 | +): |
| 667 | + """Without on_error, an inaccessible child is logged and skipped.""" |
| 668 | + skills_dir, blocked = _skills_dir_with_blocked_child(tmp_path) |
| 669 | + |
| 670 | + with _deny_child_metadata(blocked): |
| 671 | + skills = list_skills_in_dir(skills_dir) |
| 672 | + |
| 673 | + assert list(skills) == ["good"] |
| 674 | + mock_logging_warning.assert_called_once() |
| 675 | + args, _ = mock_logging_warning.call_args |
| 676 | + assert "Skipping invalid skill" in args[0] |
| 677 | + assert args[1] == "blocked-skill" |
| 678 | + |
| 679 | + |
| 680 | +def test_list_skills_in_dir_on_error_can_abort_for_inaccessible_directory( |
| 681 | + tmp_path, |
| 682 | +): |
| 683 | + """Raising from on_error aborts when directory metadata cannot be read.""" |
| 684 | + skills_dir, blocked = _skills_dir_with_blocked_child(tmp_path) |
| 685 | + |
| 686 | + def strict(skill_id, error): |
| 687 | + del skill_id # Unused: the error already names the skill. |
| 688 | + raise error |
| 689 | + |
| 690 | + with _deny_child_metadata(blocked): |
| 691 | + with pytest.raises(PermissionError, match="Permission denied"): |
| 692 | + list_skills_in_dir(skills_dir, on_error=strict) |
| 693 | + |
| 694 | + |
| 695 | +def test_list_skills_in_dir_reports_unsearchable_directory(tmp_path): |
| 696 | + """Children of a non searchable skills directory are reported to on_error.""" |
| 697 | + if not hasattr(os, "geteuid") or os.geteuid() == 0: |
| 698 | + pytest.skip("directory search permission is not enforced for this user") |
| 699 | + |
| 700 | + skills_dir, _ = _skills_dir_with_blocked_child(tmp_path) |
| 701 | + failures = [] |
| 702 | + # Readable, but not searchable: listing works and stat of each child fails. |
| 703 | + skills_dir.chmod(0o644) |
| 704 | + try: |
| 705 | + skills = list_skills_in_dir( |
| 706 | + skills_dir, |
| 707 | + on_error=lambda skill_id, error: failures.append((skill_id, error)), |
| 708 | + ) |
| 709 | + finally: |
| 710 | + skills_dir.chmod(0o755) |
| 711 | + |
| 712 | + assert skills == {} |
| 713 | + assert sorted(skill_id for skill_id, _ in failures) == [ |
| 714 | + "blocked-skill", |
| 715 | + "good", |
| 716 | + "notes.txt", |
| 717 | + ] |
| 718 | + assert all(isinstance(error, PermissionError) for _, error in failures) |
| 719 | + |
| 720 | + |
| 721 | +def test_list_skills_in_dir_skips_broken_symlink(tmp_path): |
| 722 | + """A dangling symlink is skipped and is not reported as a skill failure.""" |
| 723 | + skills_dir = tmp_path / "skills" |
| 724 | + skills_dir.mkdir() |
| 725 | + (skills_dir / "good").mkdir() |
| 726 | + (skills_dir / "good" / "SKILL.md").write_text( |
| 727 | + "---\nname: good\ndescription: desc\n---\nbody" |
| 728 | + ) |
| 729 | + (skills_dir / "missing").symlink_to(skills_dir / "nowhere") |
| 730 | + failures = [] |
| 731 | + |
| 732 | + skills = list_skills_in_dir( |
| 733 | + skills_dir, |
| 734 | + on_error=lambda skill_id, error: failures.append((skill_id, error)), |
| 735 | + ) |
| 736 | + |
| 737 | + assert list(skills) == ["good"] |
| 738 | + assert failures == [] |
| 739 | + |
| 740 | + |
589 | 741 | def test__load_skill_from_zip_bytes(): |
590 | 742 | """Tests loading a skill directly from in-memory zip file bytes.""" |
591 | 743 |
|
|
0 commit comments