import contextlib import io import os import shutil import tempfile import unittest import zipfile import packassets def run_main(*arguments): output = io.StringIO() errors = io.StringIO() with contextlib.redirect_stdout(output), contextlib.redirect_stderr(errors): code = packassets.main(list(arguments)) return code, output.getvalue(), errors.getvalue() class PackAssetsTests(unittest.TestCase): def setUp(self): self.directory = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, self.directory, True) self.assets = self.path("assets") os.makedirs(self.assets) def path(self, *parts): return os.path.join(self.directory, *parts) def put(self, relative, content=b"data"): full = os.path.join(self.assets, *relative.split("/")) os.makedirs(os.path.dirname(full), exist_ok=True) with open(full, "wb") as file: file.write(content) def test_files_are_named_relative_to_the_folder_with_forward_slashes_and_sorted(self): self.put("b.png") self.put("Sounds/a.ogg") self.put("a.png") packassets.pack(self.assets, self.path("out.zip")) with zipfile.ZipFile(self.path("out.zip")) as archive: self.assertEqual(["Sounds/a.ogg", "a.png", "b.png"], archive.namelist()) def test_every_file_is_stored_uncompressed_with_its_bytes(self): content = b"x" * 1000 self.put("a.txt", content) packassets.pack(self.assets, self.path("out.zip")) with zipfile.ZipFile(self.path("out.zip")) as archive: info = archive.getinfo("a.txt") self.assertEqual(zipfile.ZIP_STORED, info.compress_type) self.assertEqual(1000, info.compress_size) self.assertEqual(content, archive.read("a.txt")) def test_folders_get_no_entries(self): self.put("Sounds/a.ogg") packassets.pack(self.assets, self.path("out.zip")) with zipfile.ZipFile(self.path("out.zip")) as archive: self.assertEqual(["Sounds/a.ogg"], archive.namelist()) def test_packing_twice_gives_the_same_bytes(self): self.put("a.png", b"one") self.put("b.png", b"two") packassets.pack(self.assets, self.path("first.zip")) packassets.pack(self.assets, self.path("second.zip")) with open(self.path("first.zip"), "rb") as first, open(self.path("second.zip"), "rb") as second: self.assertEqual(first.read(), second.read()) def test_packing_returns_how_many_files_it_holds(self): self.put("a.png") self.put("b.png") self.assertEqual(2, packassets.pack(self.assets, self.path("out.zip"))) def test_an_existing_archive_is_replaced(self): self.put("a.png") with open(self.path("out.zip"), "wb") as file: file.write(b"old") packassets.pack(self.assets, self.path("out.zip")) with zipfile.ZipFile(self.path("out.zip")) as archive: self.assertEqual(["a.png"], archive.namelist()) def test_no_temporary_file_is_left_behind(self): self.put("a.png") packassets.pack(self.assets, self.path("out.zip")) self.assertFalse(os.path.exists(self.path("out.zip.tmp"))) def test_an_empty_folder_fails_and_writes_nothing(self): code, _, errors = run_main(self.assets, self.path("out.zip")) self.assertEqual(1, code) self.assertIn("holds no files", errors) self.assertFalse(os.path.exists(self.path("out.zip"))) def test_a_missing_folder_fails(self): code, _, errors = run_main(self.path("nope"), self.path("out.zip")) self.assertEqual(1, code) self.assertIn("isn't a folder", errors) def test_an_unwritable_target_fails_and_leaves_no_temporary_file(self): self.put("a.png") code, _, errors = run_main(self.assets, self.path("missing-folder", "out.zip")) self.assertEqual(1, code) self.assertIn("error:", errors) def test_main_reports_the_count(self): self.put("a.png") code, output, _ = run_main(self.assets, self.path("out.zip")) self.assertEqual(0, code) self.assertIn("Packed 1 files", output) if __name__ == "__main__": unittest.main()