"""Tests for the portable build helper.""" import os import sys import tempfile import unittest import zipfile ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "tools")) import build_portable class BuildPortableTests(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) self.python_zip = os.path.join(self.tmp.name, "python-embed.zip") with zipfile.ZipFile(self.python_zip, "w") as archive: archive.writestr("python.exe", b"MZ") archive.writestr("pythonw.exe", b"MZ") archive.writestr("python313.zip", b"stdlib") archive.writestr("python313._pth", b"python313.zip\n.\n") self.out = os.path.join(self.tmp.name, "SteamBase-portable") def test_build_folder_and_zip(self): out_dir, archive = build_portable.build( out_dir=self.out, python_zip=self.python_zip, make_zip=True, force=True, download=False) self.assertTrue(os.path.isdir(out_dir)) self.assertTrue(os.path.isfile(os.path.join(out_dir, "app", "web", "server.py"))) self.assertTrue(os.path.isfile(os.path.join(out_dir, "app", "tools", "import_steam.py"))) self.assertTrue(os.path.isfile(os.path.join(out_dir, "app", "VERSION"))) self.assertTrue(os.path.isfile(os.path.join(out_dir, "python", "python.exe"))) self.assertTrue(os.path.isfile(os.path.join(out_dir, "start_web.bat"))) self.assertTrue(os.path.isfile(os.path.join(out_dir, "update.bat"))) self.assertTrue(os.path.isdir(os.path.join(out_dir, "app", "data"))) self.assertTrue(os.path.isdir(os.path.join(out_dir, "app", "cache", "covers"))) self.assertFalse(os.path.exists(os.path.join(out_dir, "app", "steam.db"))) self.assertFalse(os.path.exists(os.path.join(out_dir, "app", "tests"))) with open(os.path.join(out_dir, "start_web.bat"), encoding="utf-8") as handle: start_bat = handle.read() self.assertIn("python.exe", start_bat) self.assertIn("restart_server.py", start_bat) with open(os.path.join(out_dir, "update.bat"), encoding="utf-8") as handle: update_bat = handle.read() self.assertIn("Load Steam Store descriptions?", update_bat) self.assertIn("How many descriptions to load?", update_bat) self.assertIn("fetch_details.py", update_bat) self.assertTrue(os.path.isfile(archive)) with zipfile.ZipFile(archive) as archive_zip: self.assertIn("app/web/server.py", archive_zip.namelist()) self.assertIn("python/python.exe", archive_zip.namelist()) def test_existing_output_requires_force(self): os.makedirs(self.out) with self.assertRaises(SystemExit): build_portable.build(out_dir=self.out, python_zip=self.python_zip, make_zip=False, force=False, download=False) def test_invalid_python_zip(self): bad = os.path.join(self.tmp.name, "bad.zip") with zipfile.ZipFile(bad, "w") as archive: archive.writestr("nope.txt", b"x") with self.assertRaises(SystemExit): build_portable.build(out_dir=self.out, python_zip=bad, make_zip=False, force=True, download=False) if __name__ == "__main__": unittest.main()