73 lines
2.8 KiB
Python
73 lines
2.8 KiB
Python
"""Exercise the WSL bridge using disposable commands, never real services."""
|
|
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
class WindowsRestartTests(unittest.TestCase):
|
|
def run_bridge(self, exit_code, port_state):
|
|
with tempfile.TemporaryDirectory(prefix="planet-windows-restart-") as temporary:
|
|
root = Path(temporary) / "project space ' 地球"
|
|
bridge = root / "scripts/windows/restart.zsh"
|
|
bridge.parent.mkdir(parents=True)
|
|
shutil.copyfile(ROOT / "scripts/windows/restart.zsh", bridge)
|
|
commands = root / "bin"
|
|
commands.mkdir()
|
|
log = root / "calls.log"
|
|
state = root / "state"
|
|
state.mkdir()
|
|
(state / "ports.env").write_text(port_state)
|
|
scripts = {
|
|
root
|
|
/ "planet.sh": '#!/bin/sh\nprintf "restart:%s:%s\\n" "$PWD" "$*" >> "$TEST_LOG"\nexit '
|
|
+ str(exit_code),
|
|
commands
|
|
/ "powershell.exe": '#!/bin/sh\nprintf "browser:%s\\n" "$PLANET_LAUNCH_URL" >> "$TEST_LOG"',
|
|
commands / "zsh": '#!/bin/sh\nprintf "shell:%s\\n" "$*" >> "$TEST_LOG"',
|
|
}
|
|
for path, body in scripts.items():
|
|
path.write_text(body + "\n")
|
|
path.chmod(0o755)
|
|
result = subprocess.run(
|
|
["/usr/bin/zsh", str(bridge)],
|
|
cwd="/tmp",
|
|
capture_output=True,
|
|
text=True,
|
|
env={
|
|
**os.environ,
|
|
"PATH": str(commands) + ":/usr/bin:/bin",
|
|
"TEST_LOG": str(log),
|
|
"PLANET_STATE_DIR": str(state),
|
|
},
|
|
timeout=15,
|
|
)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
calls = log.read_text()
|
|
self.assertIn(f"restart:{root}:restart --allow-lan", calls)
|
|
self.assertIn("shell:-l", calls)
|
|
return calls, result.stdout
|
|
|
|
def test_success_uses_actual_frontend_port_and_scheme(self):
|
|
calls, output = self.run_bridge(0, "FRONTEND_PORT=4177\nFRONTEND_SCHEME=https\n")
|
|
self.assertIn("browser:https://localhost:4177/earth", calls)
|
|
self.assertIn("successfully", output)
|
|
|
|
def test_failure_keeps_shell_without_opening_browser(self):
|
|
calls, output = self.run_bridge(7, "FRONTEND_PORT=4177\n")
|
|
self.assertNotIn("browser:", calls)
|
|
self.assertIn("failed with exit code 7", output)
|
|
|
|
def test_port_state_is_not_executed_as_shell_code(self):
|
|
calls, _ = self.run_bridge(0, "FRONTEND_PORT=$(exit 91)\nFRONTEND_SCHEME=javascript\n")
|
|
self.assertIn("browser:http://localhost:3000/earth", calls)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|