"""Tests for lightgui.install_libs (Phase 5): dry-run only. Command construction, file list and target paths are checked here. No test performs a real privileged install (no pkexec/sudo run, nothing is written outside a temp dir). """ import os import tempfile import unittest from lightgui import install_libs as install class InstallLibsTests(unittest.TestCase): def test_site_packages_is_a_real_dir(self): site = install.site_packages() self.assertTrue(os.path.isdir(site)) self.assertTrue(site.endswith("dist-packages") or site.endswith("site-packages")) def test_package_target(self): self.assertEqual( install.package_target("/usr/lib/python3/dist-packages"), "/usr/lib/python3/dist-packages/lightgui") def test_launcher_text(self): text = install.launcher_text() self.assertTrue(text.startswith("#!/bin/sh\n")) self.assertIn("python3 -m lightgui", text) def test_desktop_text(self): text = install.desktop_text() self.assertIn("[Desktop Entry]", text) self.assertIn("Name=LightGUI", text) self.assertIn("Exec=lightgui", text) def test_install_files(self): files = install.install_files(root="/repo", site="/usr/lib/python3/x") dests = [dest for _src, dest in files] self.assertIn("/usr/lib/python3/x/lightgui", dests) self.assertIn(install.LAUNCHER_PATH, dests) self.assertIn(install.DESKTOP_PATH, dests) self.assertEqual(install.LAUNCHER_PATH, "/usr/local/bin/lightgui") self.assertEqual(install.DESKTOP_PATH, "/usr/local/share/applications/lightgui.desktop") def test_build_install_script(self): script = install.build_install_script( root="/repo", site="/usr/lib/python3/x") self.assertIn("set -e", script) self.assertIn("cp -r '/repo/lightgui' " "'/usr/lib/python3/x/lightgui'", script) self.assertIn("rm -rf '/usr/lib/python3/x/lightgui'", script) self.assertIn("__pycache__", script) self.assertIn("chmod 755 '/usr/local/bin/lightgui'", script) self.assertIn("chmod 644 '/usr/local/share/applications/" "lightgui.desktop'", script) # No password, no credential, nothing secret in the script. self.assertNotIn("password", script.lower()) self.assertNotIn("sudo", script) def test_privileged_runner(self): # pkexec is not installed on the server; sudo is. The test # asserts the shape, not a hardcoded runner, so it holds on # a machine with pkexec too. runner = install.privileged_runner() self.assertIn(runner, ("pkexec", "sudo", None)) if runner is not None: self.assertTrue(runner in ("pkexec", "sudo")) def test_install_command_construction(self): argv = install.install_command("/tmp/x.sh", runner="pkexec") self.assertEqual(argv, ["pkexec", "sh", "/tmp/x.sh"]) argv = install.install_command("/tmp/x.sh", runner="sudo") self.assertEqual(argv, ["sudo", "-S", "sh", "/tmp/x.sh"]) # With an explicit runner the command never contains a # password or a user name. self.assertNotIn("password", " ".join(argv)) self.assertIsNone(install.install_command("/tmp/x.sh", runner="bogus")) def test_install_command_uses_detected_runner(self): runner = install.privileged_runner() argv = install.install_command("/tmp/x.sh") if runner is None: self.assertIsNone(argv) else: self.assertEqual(argv[0], runner) self.assertEqual(argv[-1], "/tmp/x.sh") def test_run_install_without_runner_fails_cleanly(self): ok, output = install.run_install(runner=None) \ if install.privileged_runner() is None else (None, None) if install.privileged_runner() is None: self.assertFalse(ok) self.assertIn("pkexec", output) def test_run_install_sudo_without_password_refuses(self): if install.privileged_runner() != "sudo": self.skipTest("sudo is not the detected runner here") ok, output = install.run_install(password=None, runner="sudo") self.assertFalse(ok) self.assertIn("password", output) def test_write_install_script_roundtrip(self): with tempfile.TemporaryDirectory() as folder: path = os.path.join(folder, "inst.sh") written = install.write_install_script( path, root="/repo", site="/usr/lib/python3/x") self.assertEqual(written, path) with open(path, encoding="utf-8") as handle: self.assertEqual( handle.read(), install.build_install_script( root="/repo", site="/usr/lib/python3/x")) def test_is_installed_false_for_empty_site(self): with tempfile.TemporaryDirectory() as folder: self.assertFalse(install.is_installed(site=folder)) 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 if _GTK_OK: from lightgui.install_libs import InstallLibrariesDialog class InstallDialogTests(unittest.TestCase): def test_dialog_shows_dry_run_without_installing(self): if not _GTK_OK: self.skipTest("needs a display (run under Xvfb)") dialog = InstallLibrariesDialog(parent=None) try: text = dialog.buffer.get_text( *dialog.buffer.get_bounds(), True) self.assertIn("lightgui", text) self.assertIn("/usr/local/bin/lightgui", text) self.assertIn("cp -r", text) # Showing the dialog must not have installed anything. self.assertFalse(dialog._installing) finally: dialog.window.destroy() if __name__ == "__main__": unittest.main()