fix: 修复验证test的 Windows 兼容性问题#8562
Conversation
There was a problem hiding this comment.
Code Review
This pull request focuses on improving cross-platform compatibility, particularly for Windows environments, by normalizing file paths to POSIX format (using forward slashes) and handling Windows line endings (\r\n) in test assertions. It also mocks the platform in pip-related tests. Feedback is provided regarding the use of chr(92) in skill_manager.py, suggesting the use of a standard escaped backslash string literal "\\" instead to improve readability.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
Hey - I've found 4 issues, and left some high level feedback:
- In
_build_skill_read_command_example, consider usingPath(path).as_posix()(orpath.replace("\\", "/")withoutchr(92)) instead of a magicchr(92)literal to improve readability and reduce the chance of subtle escaping bugs. - The repeated
.replace("\r\n", "\n")normalization in multiple tests could be factored into a small helper function (e.g.,normalize_newlines(text)) to make the intent clearer and avoid duplication. - For the tests that depend on a particular platform, using
monkeypatch.setattr(sys, "platform", "linux")(with a direct module reference) instead of the string form may be slightly clearer and avoids issues if the import path changes.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `_build_skill_read_command_example`, consider using `Path(path).as_posix()` (or `path.replace("\\", "/")` without `chr(92)`) instead of a magic `chr(92)` literal to improve readability and reduce the chance of subtle escaping bugs.
- The repeated `.replace("\r\n", "\n")` normalization in multiple tests could be factored into a small helper function (e.g., `normalize_newlines(text)`) to make the intent clearer and avoid duplication.
- For the tests that depend on a particular platform, using `monkeypatch.setattr(sys, "platform", "linux")` (with a direct module reference) instead of the string form may be slightly clearer and avoids issues if the import path changes.
## Individual Comments
### Comment 1
<location path="astrbot/core/skills/skill_manager.py" line_range="199" />
<code_context>
if _is_windows_prompt_path(path):
command = "type"
- path_arg = f'"{os.path.normpath(path)}"'
+ path_arg = f'"{path.replace(chr(92), "/")}"'
else:
command = "cat"
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Consider preserving Windows path normalization while converting separators.
The previous use of `os.path.normpath(path)` also cleaned up redundant separators and `..` segments. The new version only swaps backslashes for `/`, so we lose that normalization. To keep normalized paths while still avoiding backslashes in the rendered command, consider `PureWindowsPath(path).as_posix()` or `os.path.normpath(path).replace("\\", "/")`.
</issue_to_address>
### Comment 2
<location path="astrbot/core/provider/sources/openai_source.py" line_range="273-278" />
<code_context>
path = unquote(parsed.path or "")
if re.fullmatch(r"[A-Za-z]:", netloc):
- return str(Path(f"{netloc}{path}"))
+ return Path(f"{netloc}{path}").as_posix()
if re.match(r"^/[A-Za-z]:/", path):
path = path[1:]
if netloc and netloc != "localhost":
path = f"//{netloc}{path}"
- return str(Path(path))
+ return Path(path).as_posix()
async def _image_ref_to_data_url(
</code_context>
<issue_to_address>
**question (bug_risk):** Clarify intent of always returning POSIX-style paths from `_file_uri_to_path`.
Using `.as_posix()` changes this function to always return forward-slash paths, even on Windows. That’s fine for most Python file operations, but could break callers that expect OS-native paths (e.g., for Windows UIs or external tools that require `\`). If this normalization is intentional, consider making that explicit at call sites or in variable names so it’s clear these are logical/normalized paths rather than native OS paths.
</issue_to_address>
### Comment 3
<location path="tests/test_openai_source.py" line_range="1066-1069" />
<code_context>
PILImage.new("RGBA", (1, 1), (255, 0, 0, 255)).save(image_path)
- localhost_uri = f"file://localhost{image_path.as_posix()}"
+ posix_path = image_path.as_posix()
+ if not posix_path.startswith("/"):
+ posix_path = "/" + posix_path
+ localhost_uri = f"file://localhost{posix_path}"
payloads, _ = await provider._prepare_chat_payload(
prompt=None,
</code_context>
<issue_to_address>
**suggestion (testing):** Add explicit test coverage for Windows-style paths where `as_posix()` lacks a leading slash
This branch is intended for Windows-style paths where `image_path.as_posix()` yields `C:/path/to/file` (no leading slash), but this test may not hit that case on the current platform. To properly exercise it, parameterize (or add) a test that builds `posix_path` explicitly in both forms (with and without a leading `/`) and asserts the resulting `localhost_uri`. You can use a fake path or a monkeypatched `Path`-like object to deterministically check `file://localhost/C:/...` vs `file://localhost/...` formats.
</issue_to_address>
### Comment 4
<location path="tests/test_pip_helper_modules.py" line_range="445-446" />
<code_context>
-def test_iter_requirements_supports_direct_line_input():
+def test_iter_requirements_supports_direct_line_input(monkeypatch):
+ monkeypatch.setattr("sys.platform", "linux")
parsed = list(
requirements_utils.iter_requirements(
</code_context>
<issue_to_address>
**suggestion (testing):** Clarify platform-dependent expectations and consider adding a complementary Windows-case test
Since `sys.platform` is forced to `"linux"` here for determinism, please also add a test that sets `sys.platform` to `"win32"` and asserts the expected inclusion/exclusion of the Windows-only requirement. This will exercise both branches of the marker logic independent of the CI OS.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| def test_iter_requirements_supports_direct_line_input(monkeypatch): | ||
| monkeypatch.setattr("sys.platform", "linux") |
There was a problem hiding this comment.
suggestion (testing): Clarify platform-dependent expectations and consider adding a complementary Windows-case test
Since sys.platform is forced to "linux" here for determinism, please also add a test that sets sys.platform to "win32" and asserts the expected inclusion/exclusion of the Windows-only requirement. This will exercise both branches of the marker logic independent of the CI OS.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
修复 AstrBot 在 Windows 上运行 uv run pytest tests/ 时出现的测试失败,均为 Windows 兼容性问题。
Modifications / 改动点
源码
computer_client.py—str(remote_zip)改为remote_zip.as_posix()openai_source.py—_file_uri_to_path两处str(Path(...))改为.as_posix(),修复 file URI 解析错误whisper_api_source.py—open()改为with open(),修复 Windows 文件锁导致临时文件无法删除skill_manager.py—os.path.normpath改为path.replace(chr(92), "/"),修复type命令路径格式错误utils/__init__.py— 新建空文件,修复 Windows 上子模块无法导入测试
.replace("\r\n", "\n")/,修复file://localhostC:/...格式错误monkeypatch.setattr("sys.platform", "linux"),确保 marker 过滤行为一致Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Improve Windows compatibility for file handling and testing across core components and test suite.
Bug Fixes:
Tests: