-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: validate WebUI dist before reuse #8785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,7 +10,7 @@ | |||||||||
| import pytest | ||||||||||
|
|
||||||||||
| from astrbot.core.utils.io import should_use_bundled_dashboard_dist | ||||||||||
| from main import check_dashboard_files, check_env | ||||||||||
| from main import VERSION, check_dashboard_files, check_env | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class _version_info: | ||||||||||
|
|
@@ -158,6 +158,8 @@ async def test_check_dashboard_files_exists_and_version_match(monkeypatch): | |||||||||
| """Tests that dashboard is not downloaded when it exists and version matches.""" | ||||||||||
| # Mock os.path.exists to return True | ||||||||||
| monkeypatch.setattr(os.path, "exists", lambda x: True) | ||||||||||
| monkeypatch.setattr(os.path, "isfile", lambda x: True) | ||||||||||
| monkeypatch.setattr(os.path, "isdir", lambda x: True) | ||||||||||
|
Comment on lines
+161
to
+162
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Avoid globally returning True for os.path.isfile/isdir in these tests to better reflect real filesystem state In
This will keep the tests aligned with the actual filesystem checks and reduce false positives as the implementation evolves. Suggested implementation: orig_isfile = os.path.isfile
def _test_isfile(path):
path_str = os.fspath(path)
if path_str.endswith(os.path.join("data", "dist", "index.html")):
return True
if path_str.endswith(os.path.join("data", "dist", "assets", "version")):
return True
return orig_isfile(path)
monkeypatch.setattr(os.path, "isfile", _test_isfile) orig_isdir = os.path.isdir
def _test_isdir(path):
path_str = os.fspath(path)
if path_str.endswith(os.path.join("data", "dist", "assets")):
return True
return orig_isdir(path)
monkeypatch.setattr(os.path, "isdir", _test_isdir)
Comment on lines
+161
to
+162
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Globally mocking
Suggested change
|
||||||||||
|
|
||||||||||
| # Mock get_dashboard_version to return the current version | ||||||||||
| with mock.patch("main.get_dashboard_version") as mock_get_version: | ||||||||||
|
|
@@ -176,6 +178,8 @@ async def test_check_dashboard_files_exists_and_version_match(monkeypatch): | |||||||||
| async def test_check_dashboard_files_exists_but_version_mismatch(monkeypatch): | ||||||||||
| """Tests that a warning is logged when dashboard version mismatches.""" | ||||||||||
| monkeypatch.setattr(os.path, "exists", lambda x: True) | ||||||||||
| monkeypatch.setattr(os.path, "isfile", lambda x: True) | ||||||||||
| monkeypatch.setattr(os.path, "isdir", lambda x: True) | ||||||||||
|
Comment on lines
+181
to
+182
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Globally mocking
Suggested change
|
||||||||||
|
|
||||||||||
| with mock.patch( | ||||||||||
| "main.get_dashboard_version", mock.AsyncMock(return_value="v0.0.1") | ||||||||||
|
|
@@ -225,6 +229,9 @@ async def test_check_dashboard_files_uses_bundled_dist_when_data_dist_is_stale( | |||||||||
| data_dist = data_dir / "dist" | ||||||||||
| bundled_dist = tmp_path / "bundled-dist" | ||||||||||
| data_dist.mkdir(parents=True) | ||||||||||
| (data_dist / "index.html").write_text("", encoding="utf-8") | ||||||||||
| (data_dist / "assets").mkdir() | ||||||||||
| (data_dist / "assets" / "version").write_text("v0.0.1", encoding="utf-8") | ||||||||||
| bundled_dist.mkdir() | ||||||||||
|
|
||||||||||
| with mock.patch("main.get_astrbot_data_path", return_value=str(data_dir)): | ||||||||||
|
|
@@ -245,6 +252,25 @@ async def test_check_dashboard_files_uses_bundled_dist_when_data_dist_is_stale( | |||||||||
| mock_download.assert_not_called() | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @pytest.mark.asyncio | ||||||||||
| async def test_check_dashboard_files_redownloads_incomplete_data_dist(tmp_path): | ||||||||||
| """Tests that a partial data/dist does not skip dashboard download.""" | ||||||||||
| data_dir = tmp_path / "data" | ||||||||||
| data_dist = data_dir / "dist" | ||||||||||
| data_dist.mkdir(parents=True) | ||||||||||
|
|
||||||||||
| with mock.patch("main.get_astrbot_data_path", return_value=str(data_dir)): | ||||||||||
| with mock.patch("main.get_dashboard_version") as mock_get_version: | ||||||||||
| with mock.patch( | ||||||||||
| "main.download_dashboard", mock.AsyncMock() | ||||||||||
| ) as mock_download: | ||||||||||
| result = await check_dashboard_files() | ||||||||||
|
|
||||||||||
| assert result == str(data_dist) | ||||||||||
| mock_get_version.assert_not_called() | ||||||||||
| mock_download.assert_awaited_once_with(version=f"v{VERSION}", latest=False) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @pytest.mark.asyncio | ||||||||||
| async def test_check_dashboard_files_with_webui_dir_arg(monkeypatch): | ||||||||||
| """Tests that providing a valid webui_dir skips all checks.""" | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the WebUI directory is incomplete or corrupted, simply logging a warning and proceeding to download might lead to extraction conflicts (e.g., if
data/distis actually a file instead of a directory, or if there are leftover orphaned files from a previous failed installation). Cleaning up the incomplete path before downloading ensures a clean and reliable extraction state.