test(webui): make folder picker tests portable

This commit is contained in:
Xubin Ren
2026-08-14 03:51:58 +09:00
parent 2f64aaf21f
commit 20aca92311
+11 -11
View File
@@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import sys
from pathlib import Path from pathlib import Path
import pytest import pytest
@@ -7,22 +8,21 @@ import pytest
from nanobot.webui import native_folder_picker as picker from nanobot.webui import native_folder_picker as picker
def _picker_script(tmp_path: Path, body: str) -> Path: def _picker_command(tmp_path: Path, body: str) -> picker._PickerCommand:
script = tmp_path / "picker.sh" script = tmp_path / "picker.py"
script.write_text(f"#!/bin/sh\n{body}\n", encoding="utf-8") script.write_text(f"{body}\n", encoding="utf-8")
script.chmod(0o755) return picker._PickerCommand((sys.executable, str(script)), frozenset({1}))
return script
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_pick_native_folder_returns_selected_directory(tmp_path, monkeypatch) -> None: async def test_pick_native_folder_returns_selected_directory(tmp_path, monkeypatch) -> None:
selected = tmp_path / "project" selected = tmp_path / "project"
selected.mkdir() selected.mkdir()
script = _picker_script(tmp_path, f"printf '%s' '{selected}'") command = _picker_command(tmp_path, f"print({str(selected)!r}, end='')")
monkeypatch.setattr( monkeypatch.setattr(
picker, picker,
"_picker_command", "_picker_command",
lambda: picker._PickerCommand((str(script),), frozenset({1})), lambda: command,
) )
assert await picker.pick_native_folder() == str(selected) assert await picker.pick_native_folder() == str(selected)
@@ -30,11 +30,11 @@ async def test_pick_native_folder_returns_selected_directory(tmp_path, monkeypat
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_pick_native_folder_maps_dialog_cancel_to_none(tmp_path, monkeypatch) -> None: async def test_pick_native_folder_maps_dialog_cancel_to_none(tmp_path, monkeypatch) -> None:
script = _picker_script(tmp_path, "exit 1") command = _picker_command(tmp_path, "raise SystemExit(1)")
monkeypatch.setattr( monkeypatch.setattr(
picker, picker,
"_picker_command", "_picker_command",
lambda: picker._PickerCommand((str(script),), frozenset({1})), lambda: command,
) )
assert await picker.pick_native_folder() is None assert await picker.pick_native_folder() is None
@@ -43,11 +43,11 @@ async def test_pick_native_folder_maps_dialog_cancel_to_none(tmp_path, monkeypat
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_pick_native_folder_rejects_non_directory_result(tmp_path, monkeypatch) -> None: async def test_pick_native_folder_rejects_non_directory_result(tmp_path, monkeypatch) -> None:
missing = tmp_path / "missing" missing = tmp_path / "missing"
script = _picker_script(tmp_path, f"printf '%s' '{missing}'") command = _picker_command(tmp_path, f"print({str(missing)!r}, end='')")
monkeypatch.setattr( monkeypatch.setattr(
picker, picker,
"_picker_command", "_picker_command",
lambda: picker._PickerCommand((str(script),), frozenset({1})), lambda: command,
) )
with pytest.raises(picker.NativeFolderPickerError, match="invalid directory"): with pytest.raises(picker.NativeFolderPickerError, match="invalid directory"):