gen_models --check: compare PNGs by their pixels, not their bytes (#92)
build / mod (push) Successful in 22m10s
build / mod (push) Successful in 22m10s
With Pillow installable again, build.yml failed one step later: on the Linux runner 171 generated PNGs (every item icon) differed from the committed files. Their pixels are identical (sha256 of the decoded rasters matches the macOS ones byte for byte); only the deflate stream differs, because the zlib inside the Pillow wheel is built per platform and Python version. The manifest's byte hashes are not portable, so the check could only pass where the assets had been generated. --check now treats a PNG whose bytes differ as unchanged when it decodes to the same mode, size and pixels, and only while the file still matches its manifest row: a hand-edited file stays a conflict. Writing (a run without --check) and the manifest stay byte-exact. Verified: a copy of airdef-mod with all 175 manifest PNGs re-encoded (stored blocks) and the manifest rows updated passes --check; the real tree still reports no differences; tools/test_gen_assets.py OK (12 tests, two new: a re-encoded icon is accepted and then rewritten by a real run, another picture and a hand-edited icon are still reported).
This commit is contained in:
@@ -189,5 +189,60 @@ class ReproducibilityTests(unittest.TestCase):
|
||||
self.assertEqual(outputs[0], outputs[1])
|
||||
|
||||
|
||||
class PortableCheckTests(unittest.TestCase):
|
||||
"""`--check` goes by the picture: another platform's Pillow compresses the same pixels into other bytes."""
|
||||
|
||||
ICON = "assets/airdef/textures/item/deploy_gepard.png"
|
||||
|
||||
def installed(self, root):
|
||||
source, manifest = root / "source-assets", root / "generated-manifest.txt"
|
||||
gen_models.regenerate(root, source, manifest)
|
||||
return source, manifest
|
||||
|
||||
def rewrite_with_manifest(self, root, manifest, rel, image):
|
||||
image.save(root / rel, compress_level=0)
|
||||
entries = pipeline.read_manifest(manifest)
|
||||
entries[rel] = pipeline.sha256(root / rel)
|
||||
manifest.write_text(pipeline.manifest_text(entries), encoding="utf-8")
|
||||
|
||||
def test_check_accepts_a_png_that_differs_from_the_generator_only_in_its_bytes(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp) / "mod"
|
||||
root.mkdir()
|
||||
source, manifest = self.installed(root)
|
||||
icon = root / self.ICON
|
||||
generated = icon.read_bytes()
|
||||
picture = Image.open(icon)
|
||||
picture.load()
|
||||
self.rewrite_with_manifest(root, manifest, self.ICON, picture)
|
||||
reencoded = icon.read_bytes()
|
||||
self.assertNotEqual(reencoded, generated) # stored blocks instead of this Pillow's deflate
|
||||
|
||||
self.assertFalse(gen_models.regenerate(root, source, manifest, check=True, report=True))
|
||||
self.assertEqual(icon.read_bytes(), reencoded)
|
||||
# Writing is still byte-exact: a real run installs this platform's encoding.
|
||||
self.assertTrue(gen_models.regenerate(root, source, manifest))
|
||||
self.assertEqual(icon.read_bytes(), generated)
|
||||
self.assertFalse(gen_models.regenerate(root, source, manifest, check=True))
|
||||
|
||||
def test_check_still_reports_another_picture_and_a_hand_edited_png(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp) / "mod"
|
||||
root.mkdir()
|
||||
source, manifest = self.installed(root)
|
||||
icon = root / self.ICON
|
||||
self.rewrite_with_manifest(root, manifest, self.ICON, Image.new("RGBA", (16, 16), (200, 10, 10, 255)))
|
||||
self.assertTrue(gen_models.regenerate(root, source, manifest, check=True, report=True))
|
||||
|
||||
picture = Image.open(icon)
|
||||
picture.load()
|
||||
self.rewrite_with_manifest(root, manifest, self.ICON, picture)
|
||||
entries = pipeline.read_manifest(manifest)
|
||||
entries[self.ICON] = "0" * 64 # the file no longer matches its manifest row: a hand edit
|
||||
manifest.write_text(pipeline.manifest_text(entries), encoding="utf-8")
|
||||
with self.assertRaisesRegex(RuntimeError, "КОНФЛИКТ"):
|
||||
gen_models.regenerate(root, source, manifest, check=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user