164 lines
5.8 KiB
Python
164 lines
5.8 KiB
Python
"""Tests for lightgui.build (Phase 4): syntax check, run capture,
|
|
and the build console window.
|
|
|
|
Logic tests run anywhere. BuildConsole widget tests need a display
|
|
and are skipped when Gtk cannot initialise (run under Xvfb, :99).
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
from lightgui.build import (
|
|
BuildConsole, BuildResult, command_str, repo_root, run_capture,
|
|
run_env, syntax_check)
|
|
|
|
try:
|
|
import gi
|
|
gi.require_version("Gtk", "3.0")
|
|
from gi.repository import Gtk
|
|
_GTK_OK = Gtk.init_check()[0]
|
|
except Exception:
|
|
Gtk = None
|
|
_GTK_OK = False
|
|
|
|
GOOD_SCRIPT = """import sys
|
|
print("hello from generated app")
|
|
print("second line")
|
|
"""
|
|
|
|
BAD_SCRIPT = """def broken(
|
|
print("never reached")
|
|
"""
|
|
|
|
FAILING_SCRIPT = """import sys
|
|
print("before the failure")
|
|
raise RuntimeError("boom at runtime")
|
|
"""
|
|
|
|
|
|
def _write_script(folder, name, text):
|
|
path = os.path.join(folder, name)
|
|
with open(path, "w", encoding="utf-8") as handle:
|
|
handle.write(text)
|
|
return path
|
|
|
|
|
|
class BuildLogicTests(unittest.TestCase):
|
|
def test_command_str(self):
|
|
self.assertEqual(command_str(["python3", "-m", "py_compile",
|
|
"/tmp/a b.py"]),
|
|
"python3 -m py_compile '/tmp/a b.py'")
|
|
|
|
def test_repo_root_has_package(self):
|
|
self.assertTrue(os.path.isdir(os.path.join(repo_root(),
|
|
"lightgui")))
|
|
|
|
def test_run_env_adds_repo_root(self):
|
|
env = run_env()
|
|
self.assertIn(repo_root(), env["PYTHONPATH"].split(os.pathsep))
|
|
|
|
def test_syntax_check_good_script(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = _write_script(folder, "good.py", GOOD_SCRIPT)
|
|
result = syntax_check(path)
|
|
self.assertTrue(result.ok)
|
|
self.assertEqual(result.returncode, 0)
|
|
self.assertIn("py_compile", result.command)
|
|
self.assertIn(path, result.command)
|
|
self.assertTrue(result.output.startswith("$ "))
|
|
|
|
def test_syntax_check_bad_script_reports_error(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = _write_script(folder, "bad.py", BAD_SCRIPT)
|
|
result = syntax_check(path)
|
|
self.assertFalse(result.ok)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertIn("SyntaxError", result.output)
|
|
|
|
def test_run_capture_good_script(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = _write_script(folder, "good.py", GOOD_SCRIPT)
|
|
result = run_capture(path)
|
|
self.assertTrue(result.ok)
|
|
self.assertIn("hello from generated app", result.output)
|
|
self.assertIn("second line", result.output)
|
|
# The console shows the exact command run.
|
|
self.assertIn("$ %s %s" % (sys.executable, path),
|
|
result.output)
|
|
|
|
def test_run_capture_failing_script_captures_stderr(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = _write_script(folder, "fail.py", FAILING_SCRIPT)
|
|
result = run_capture(path)
|
|
self.assertFalse(result.ok)
|
|
self.assertIn("before the failure", result.output)
|
|
self.assertIn("RuntimeError: boom at runtime", result.output)
|
|
self.assertIn("Traceback", result.output)
|
|
self.assertEqual(result.returncode, 1)
|
|
|
|
def test_run_capture_timeout(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = _write_script(folder, "slow.py",
|
|
"import time\ntime.sleep(30)\n")
|
|
result = run_capture(path, timeout=1)
|
|
self.assertFalse(result.ok)
|
|
self.assertIn("timed out", result.output)
|
|
|
|
|
|
class BuildConsoleTests(unittest.TestCase):
|
|
def setUp(self):
|
|
if not _GTK_OK:
|
|
self.skipTest("needs a display (run under Xvfb)")
|
|
|
|
def test_log_area_fills_the_dialog(self):
|
|
"""The cb95f5f fix: the scrolled log area is packed expanding
|
|
(expand + fill), so it fills the dialog instead of sitting at
|
|
~2 lines pinned to the top."""
|
|
console = BuildConsole(title="Build")
|
|
content = console.get_content_area()
|
|
children = content.get_children()
|
|
scrolled = [c for c in children
|
|
if isinstance(c, Gtk.ScrolledWindow)]
|
|
self.assertEqual(len(scrolled), 1)
|
|
pack = content.query_child_packing(scrolled[0])
|
|
self.assertTrue(pack[0], "log area must expand")
|
|
self.assertTrue(pack[1], "log area must fill")
|
|
console.destroy()
|
|
|
|
def test_append_and_text(self):
|
|
console = BuildConsole(title="Build")
|
|
console.set_text("$ python3 app.py\n")
|
|
console.append("hello\n")
|
|
console.append("world\n")
|
|
self.assertEqual(console.get_text(),
|
|
"$ python3 app.py\nhello\nworld\n")
|
|
console.destroy()
|
|
|
|
def test_run_command_streams_output(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = _write_script(folder, "good.py", GOOD_SCRIPT)
|
|
console = BuildConsole(title="Run")
|
|
console.show()
|
|
self.assertTrue(
|
|
console.run_command([sys.executable, path]))
|
|
while Gtk.events_pending():
|
|
Gtk.main_iteration()
|
|
# Wait for the child to exit (bounded spin).
|
|
for _ in range(200):
|
|
if console.proc is None:
|
|
break
|
|
while Gtk.events_pending():
|
|
Gtk.main_iteration()
|
|
import time
|
|
time.sleep(0.01)
|
|
text = console.get_text()
|
|
self.assertIn("hello from generated app", text)
|
|
self.assertIn("[exited with code 0]", text)
|
|
console.destroy()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|