Skip to content

Commit 909feea

Browse files
committed
test: expand updater archive root coverage
1 parent e27094f commit 909feea

3 files changed

Lines changed: 24 additions & 20 deletions

File tree

astrbot/core/star/updator.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ def unzip_file(self, zip_path: str, target_dir: str) -> None:
7878
update_dir = self._normalize_archive_root_dir(z.namelist()[0])
7979
z.extractall(target_dir)
8080

81-
target_root_path = self._normalize_archive_path(target_dir)
82-
update_root_path = self._normalize_archive_path(target_root_path, update_dir)
81+
update_root_path = os.path.normpath(os.path.join(target_dir, update_dir))
8382
files = os.listdir(update_root_path)
8483
for f in files:
85-
update_item_path = self._normalize_archive_path(update_root_path, f)
86-
target_item_path = self._normalize_archive_path(target_root_path, f)
84+
update_item_path = os.path.normpath(os.path.join(update_root_path, f))
85+
target_item_path = os.path.normpath(os.path.join(target_dir, f))
8786
if os.path.isdir(update_item_path):
8887
if os.path.exists(target_item_path):
8988
shutil.rmtree(target_item_path, onerror=on_error)

astrbot/core/zip_updator.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,9 @@ def parse_github_url(self, url: str):
231231
return author, repo, branch
232232
raise ValueError("无效的 GitHub URL")
233233

234-
@staticmethod
235-
def _normalize_archive_path(*parts: str) -> str:
236-
return os.path.normpath(os.path.join(*parts))
237-
238234
@staticmethod
239235
def _normalize_archive_root_dir(path: str) -> str:
240-
normalized = RepoZipUpdator._normalize_archive_path(path)
236+
normalized = os.path.normpath(path)
241237
return "" if normalized == "." else normalized
242238

243239
def unzip_file(self, zip_path: str, target_dir: str) -> None:
@@ -249,12 +245,11 @@ def unzip_file(self, zip_path: str, target_dir: str) -> None:
249245
z.extractall(target_dir)
250246
logger.debug(f"解压文件完成: {zip_path}")
251247

252-
target_root_path = self._normalize_archive_path(target_dir)
253-
update_root_path = self._normalize_archive_path(target_root_path, update_dir)
248+
update_root_path = os.path.normpath(os.path.join(target_dir, update_dir))
254249
files = os.listdir(update_root_path)
255250
for f in files:
256-
update_item_path = self._normalize_archive_path(update_root_path, f)
257-
target_item_path = self._normalize_archive_path(target_root_path, f)
251+
update_item_path = os.path.normpath(os.path.join(update_root_path, f))
252+
target_item_path = os.path.normpath(os.path.join(target_dir, f))
258253
if os.path.isdir(update_item_path):
259254
if os.path.exists(target_item_path):
260255
shutil.rmtree(target_item_path, onerror=on_error)

tests/test_updator_socks.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ntpath
2+
import posixpath
23
from dataclasses import dataclass, field
34
from pathlib import Path
45
from types import SimpleNamespace
@@ -126,6 +127,10 @@ def extractall(self, target_dir: str) -> None: # noqa: ARG002
126127
return None
127128

128129

130+
def _build_fake_archive_entries(archive_root: str) -> list[str]:
131+
return [archive_root, posixpath.join(archive_root, ".dockerignore")]
132+
133+
129134
def _build_fake_httpx_module(state: _FakeAsyncClientState) -> SimpleNamespace:
130135
class _FakeAsyncClient:
131136
def __init__(self, **kwargs):
@@ -420,13 +425,22 @@ async def test_download_file_logs_url_and_target_path_on_failure(
420425
assert any(str(target_path) in message for message in log_messages)
421426

422427

428+
@pytest.mark.parametrize(
429+
"archive_root",
430+
[
431+
"AstrBotDevs-AstrBot-39386ee/",
432+
"AstrBotDevs-AstrBot-39386ee",
433+
"owner-repo-branch/subdir/",
434+
".",
435+
],
436+
)
423437
def test_repo_unzip_file_normalizes_windows_extended_length_paths(
424438
monkeypatch: pytest.MonkeyPatch,
439+
archive_root: str,
425440
) -> None:
426441
import astrbot.core.zip_updator as zip_updator_module
427442

428443
target_dir = r"\\?\\C:\\Users\\admin\\AppData\\Local\\AstrBot\\backend\\app"
429-
archive_root = "AstrBotDevs-AstrBot-39386ee/"
430444
expected_root = ntpath.normpath(ntpath.join(target_dir, archive_root))
431445
expected_file = ntpath.normpath(
432446
ntpath.join(target_dir, archive_root, ".dockerignore")
@@ -447,9 +461,7 @@ def fake_listdir(path: str) -> list[str]:
447461
monkeypatch.setattr(
448462
zip_updator_module.zipfile,
449463
"ZipFile",
450-
lambda path, mode: _FakeZipArchive(
451-
[archive_root, f"{archive_root}.dockerignore"]
452-
),
464+
lambda path, mode: _FakeZipArchive(_build_fake_archive_entries(archive_root)),
453465
)
454466
monkeypatch.setattr(zip_updator_module.logger, "debug", lambda message: None)
455467
monkeypatch.setattr(zip_updator_module.logger, "warning", lambda message: None)
@@ -504,9 +516,7 @@ def fake_listdir(path: str) -> list[str]:
504516
monkeypatch.setattr(
505517
plugin_updator_module.zipfile,
506518
"ZipFile",
507-
lambda path, mode: _FakeZipArchive(
508-
[archive_root, f"{archive_root}.dockerignore"]
509-
),
519+
lambda path, mode: _FakeZipArchive(_build_fake_archive_entries(archive_root)),
510520
)
511521
monkeypatch.setattr(plugin_updator_module.logger, "info", lambda message: None)
512522
monkeypatch.setattr(plugin_updator_module.logger, "warning", lambda message: None)

0 commit comments

Comments
 (0)