|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | +from click import ClickException |
| 5 | +from click.testing import CliRunner |
| 6 | + |
| 7 | +import astrbot.cli.utils.plugin as plugin_utils |
| 8 | +from astrbot.cli.commands.cmd_plug import plug |
| 9 | + |
| 10 | + |
| 11 | +def _write_plugin(path: Path, name: str = "astrbot_plugin_local_demo") -> None: |
| 12 | + path.mkdir(parents=True) |
| 13 | + (path / "metadata.yaml").write_text( |
| 14 | + "\n".join( |
| 15 | + [ |
| 16 | + f"name: {name}", |
| 17 | + "desc: Local plugin", |
| 18 | + "version: 1.0.0", |
| 19 | + "author: AstrBot", |
| 20 | + "repo: https://example.com/local-plugin", |
| 21 | + ], |
| 22 | + ), |
| 23 | + encoding="utf-8", |
| 24 | + ) |
| 25 | + (path / "main.py").write_text("PLUGIN_LOADED = True\n", encoding="utf-8") |
| 26 | + |
| 27 | + |
| 28 | +def _write_ignored_plugin_files(path: Path) -> None: |
| 29 | + for ignored_dir in [".git", ".venv", "__pycache__", ".idea", ".vscode", ".zed"]: |
| 30 | + ignored_path = path / ignored_dir |
| 31 | + ignored_path.mkdir() |
| 32 | + (ignored_path / "ignored.txt").write_text("ignored\n", encoding="utf-8") |
| 33 | + (path / "__pycache__" / "main.pyc").write_bytes(b"ignored") |
| 34 | + |
| 35 | + |
| 36 | +def _write_astrbot_root(path: Path) -> None: |
| 37 | + (path / ".astrbot").touch() |
| 38 | + (path / "data" / "plugins").mkdir(parents=True) |
| 39 | + |
| 40 | + |
| 41 | +def test_plugin_install_editable_symlinks_local_plugin( |
| 42 | + monkeypatch: pytest.MonkeyPatch, |
| 43 | + tmp_path: Path, |
| 44 | +) -> None: |
| 45 | + root = tmp_path / "root" |
| 46 | + source = tmp_path / "source-plugin" |
| 47 | + root.mkdir() |
| 48 | + _write_astrbot_root(root) |
| 49 | + _write_plugin(source) |
| 50 | + monkeypatch.chdir(root) |
| 51 | + |
| 52 | + result = CliRunner().invoke( |
| 53 | + plug, |
| 54 | + ["install", "-e", str(source)], |
| 55 | + catch_exceptions=False, |
| 56 | + ) |
| 57 | + |
| 58 | + target = root / "data" / "plugins" / "astrbot_plugin_local_demo" |
| 59 | + target = root / "data" / "plugins" / "astrbot_plugin_local_demo" |
| 60 | + assert result.exit_code == 0 |
| 61 | + assert target.is_symlink() |
| 62 | + assert (target / "metadata.yaml").exists() |
| 63 | + assert (target / "main.py").read_text(encoding="utf-8") == "PLUGIN_LOADED = True\n" |
| 64 | + |
| 65 | + |
| 66 | +def test_plugin_install_accepts_local_path_without_editable_flag( |
| 67 | + monkeypatch: pytest.MonkeyPatch, |
| 68 | + tmp_path: Path, |
| 69 | +) -> None: |
| 70 | + root = tmp_path / "root" |
| 71 | + source = tmp_path / "source-plugin" |
| 72 | + root.mkdir() |
| 73 | + _write_astrbot_root(root) |
| 74 | + _write_plugin(source) |
| 75 | + _write_ignored_plugin_files(source) |
| 76 | + monkeypatch.chdir(root) |
| 77 | + |
| 78 | + result = CliRunner().invoke(plug, ["install", str(source)]) |
| 79 | + |
| 80 | + target = root / "data" / "plugins" / "astrbot_plugin_local_demo" |
| 81 | + assert result.exit_code == 0 |
| 82 | + assert not target.is_symlink() |
| 83 | + assert (target / "metadata.yaml").exists() |
| 84 | + assert not (target / ".git").exists() |
| 85 | + assert not (target / ".venv").exists() |
| 86 | + assert not (target / "__pycache__").exists() |
| 87 | + assert not (target / ".idea").exists() |
| 88 | + assert not (target / ".vscode").exists() |
| 89 | + assert not (target / ".zed").exists() |
| 90 | + |
| 91 | + |
| 92 | +def test_plugin_install_editable_rejects_existing_plugin( |
| 93 | + monkeypatch: pytest.MonkeyPatch, |
| 94 | + tmp_path: Path, |
| 95 | +) -> None: |
| 96 | + root = tmp_path / "root" |
| 97 | + source = tmp_path / "source-plugin" |
| 98 | + root.mkdir() |
| 99 | + _write_astrbot_root(root) |
| 100 | + _write_plugin(source) |
| 101 | + _write_plugin(root / "data" / "plugins" / "astrbot_plugin_local_demo") |
| 102 | + monkeypatch.chdir(root) |
| 103 | + |
| 104 | + result = CliRunner().invoke(plug, ["install", "-e", str(source)]) |
| 105 | + |
| 106 | + assert result.exit_code != 0 |
| 107 | + assert "already exists" in result.output |
| 108 | + |
| 109 | + |
| 110 | +def test_plugin_install_rejects_plugin_name_with_path_separator( |
| 111 | + monkeypatch: pytest.MonkeyPatch, |
| 112 | + tmp_path: Path, |
| 113 | +) -> None: |
| 114 | + root = tmp_path / "root" |
| 115 | + source = tmp_path / "source-plugin" |
| 116 | + root.mkdir() |
| 117 | + _write_astrbot_root(root) |
| 118 | + _write_plugin(source, name="../bad_plugin") |
| 119 | + monkeypatch.chdir(root) |
| 120 | + |
| 121 | + result = CliRunner().invoke(plug, ["install", str(source)]) |
| 122 | + |
| 123 | + assert result.exit_code != 0 |
| 124 | + assert "invalid name" in result.output |
| 125 | + assert not (root / "data" / "bad_plugin").exists() |
| 126 | + |
| 127 | + |
| 128 | +def test_plugin_install_copy_does_not_delete_existing_target_on_race( |
| 129 | + monkeypatch: pytest.MonkeyPatch, |
| 130 | + tmp_path: Path, |
| 131 | +) -> None: |
| 132 | + root = tmp_path / "root" |
| 133 | + source = tmp_path / "source-plugin" |
| 134 | + root.mkdir() |
| 135 | + _write_astrbot_root(root) |
| 136 | + _write_plugin(source) |
| 137 | + monkeypatch.chdir(root) |
| 138 | + |
| 139 | + target = root / "data" / "plugins" / "astrbot_plugin_local_demo" |
| 140 | + target.mkdir() |
| 141 | + marker = target / "keep.txt" |
| 142 | + marker.write_text("keep\n", encoding="utf-8") |
| 143 | + |
| 144 | + result = CliRunner().invoke(plug, ["install", str(source)]) |
| 145 | + |
| 146 | + assert result.exit_code != 0 |
| 147 | + assert "already exists" in result.output |
| 148 | + assert marker.read_text(encoding="utf-8") == "keep\n" |
| 149 | + |
| 150 | + |
| 151 | +def test_plugin_install_copy_does_not_delete_concurrently_created_target( |
| 152 | + monkeypatch: pytest.MonkeyPatch, |
| 153 | + tmp_path: Path, |
| 154 | +) -> None: |
| 155 | + source = tmp_path / "source-plugin" |
| 156 | + plugins_dir = tmp_path / "plugins" |
| 157 | + _write_plugin(source) |
| 158 | + |
| 159 | + target = plugins_dir / "astrbot_plugin_local_demo" |
| 160 | + |
| 161 | + def create_target_then_fail( |
| 162 | + _source_path: Path, |
| 163 | + _plugins_dir: Path, |
| 164 | + _target_path: Path, |
| 165 | + ) -> None: |
| 166 | + target.mkdir(parents=True) |
| 167 | + (target / "keep.txt").write_text("keep\n", encoding="utf-8") |
| 168 | + raise FileExistsError |
| 169 | + |
| 170 | + monkeypatch.setattr(plugin_utils, "_copy_local_plugin", create_target_then_fail) |
| 171 | + |
| 172 | + with pytest.raises(ClickException, match="already exists"): |
| 173 | + plugin_utils.install_local_plugin(source, plugins_dir) |
| 174 | + |
| 175 | + assert (target / "keep.txt").read_text(encoding="utf-8") == "keep\n" |
| 176 | + |
| 177 | + |
| 178 | +def test_plugin_install_requires_name_or_editable_path( |
| 179 | + monkeypatch: pytest.MonkeyPatch, |
| 180 | + tmp_path: Path, |
| 181 | +) -> None: |
| 182 | + root = tmp_path / "root" |
| 183 | + root.mkdir() |
| 184 | + _write_astrbot_root(root) |
| 185 | + monkeypatch.chdir(root) |
| 186 | + |
| 187 | + result = CliRunner().invoke(plug, ["install"]) |
| 188 | + |
| 189 | + assert result.exit_code != 0 |
| 190 | + assert "Missing plugin name or local plugin path" in result.output |
0 commit comments