104 lines
3.2 KiB
Python
104 lines
3.2 KiB
Python
"""Tests for scripts/post_update.sh."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
def _bash_executable() -> str:
|
|
if os.name == "nt":
|
|
for candidate in (
|
|
Path(r"C:\Program Files\Git\bin\bash.exe"),
|
|
Path(r"C:\Program Files (x86)\Git\bin\bash.exe"),
|
|
):
|
|
if candidate.is_file():
|
|
return str(candidate)
|
|
return "bash"
|
|
|
|
|
|
def _bash_path_style() -> str:
|
|
"""Git Bash: /c/...; WSL: /mnt/c/..."""
|
|
probe = subprocess.run(
|
|
[_bash_executable(), "-c", 'if [ -d /mnt/c ]; then echo wsl; else echo gitbash; fi'],
|
|
capture_output=True,
|
|
text=True,
|
|
cwd=str(ROOT),
|
|
)
|
|
return (probe.stdout or "gitbash").strip()
|
|
|
|
|
|
def _bash_python_executable() -> str:
|
|
"""Путь к python для bash (WSL: /mnt/c/..., Git Bash: /c/...)."""
|
|
exe = Path(sys.executable).resolve()
|
|
if os.name != "nt":
|
|
return str(exe)
|
|
drive = exe.drive.rstrip(":").lower()
|
|
tail = str(exe).replace(exe.drive, "").replace("\\", "/")
|
|
if _bash_path_style() == "wsl":
|
|
return f"/mnt/{drive}{tail}"
|
|
return f"/{drive}{tail}"
|
|
|
|
|
|
class PostUpdateScriptTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self._tmp = tempfile.mkdtemp(prefix="wesp-post-")
|
|
self.base = Path(self._tmp)
|
|
data = self.base / "data"
|
|
data.mkdir(parents=True)
|
|
(data / "update_state.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"status": "pending_restart",
|
|
"target_version": "2.0.0",
|
|
"previous_version": "1.0.0",
|
|
"backup_path": str(self.base / "backups" / "b1"),
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
(data / ".pending_restart.json").write_text(
|
|
json.dumps({"backup_path": str(self.base / "backups" / "b1"), "previous_version": "1.0.0"}),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
def tearDown(self) -> None:
|
|
import shutil
|
|
|
|
shutil.rmtree(self._tmp, ignore_errors=True)
|
|
|
|
def _run_post(self, env: dict) -> subprocess.CompletedProcess:
|
|
e = os.environ.copy()
|
|
e.update(env)
|
|
# Run from real ROOT so scripts exist; point data via copying is hard — use WESP test root via symlink approach
|
|
# Simpler: invoke health cmd only path
|
|
cmd = [_bash_executable(), str(ROOT / "scripts" / "post_update.sh")]
|
|
return subprocess.run(cmd, capture_output=True, text=True, env=e, cwd=str(ROOT))
|
|
|
|
def test_health_success_exits_zero(self) -> None:
|
|
py = _bash_python_executable()
|
|
# Inline env: WSL/Git Bash may not inherit Windows subprocess env reliably.
|
|
cmd = (
|
|
f'WESP_SKIP_RESTART=1 WESP_HEALTH_CMD="exit 0" '
|
|
f'WESP_BASE="{ROOT.as_posix()}" WESP_PYTHON="{py}" '
|
|
f"bash scripts/post_update.sh"
|
|
)
|
|
r = subprocess.run(
|
|
[_bash_executable(), "-c", cmd],
|
|
capture_output=True,
|
|
text=True,
|
|
cwd=str(ROOT),
|
|
)
|
|
self.assertEqual(r.returncode, 0, r.stderr or r.stdout)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|