fix(tests): decoding PNG files to fix Linux/Mac differences in encoding

pull/1801/head
grdddj 3 years ago committed by matejcik
parent 5bc3e353e0
commit 1b932a20e8

@ -1,27 +1,19 @@
import hashlib
import json
import shutil
import sys
from pathlib import Path
ROOT = Path(__file__).parent / ".."
sys.path.insert(0, str(ROOT))
# Needed for setup purposes, filling the FILE_HASHES dict
from tests.ui_tests import read_fixtures # isort:skip
def _hash_files(path):
files = path.iterdir()
hasher = hashlib.sha256()
for file in sorted(files):
hasher.update(file.read_bytes())
read_fixtures()
from tests.ui_tests import _hash_files, FILE_HASHES, SCREENS_DIR # isort:skip
return hasher.digest().hex()
root = Path(__file__).parent / ".."
screens = root / "tests/ui_tests/screens"
fixtures = root / "tests/ui_tests/fixtures.json"
hashes = json.loads(fixtures.read_text())
for test_case in hashes.keys():
recorded_dir = screens / test_case / "recorded"
expected_hash = hashes[test_case]
for test_case in FILE_HASHES.keys():
recorded_dir = SCREENS_DIR / test_case / "recorded"
expected_hash = FILE_HASHES[test_case]
actual_hash = _hash_files(recorded_dir)
assert expected_hash == actual_hash
shutil.make_archive(root / "ci/ui_test_records" / actual_hash, "zip", recorded_dir)
shutil.make_archive(ROOT / "ci/ui_test_records" / actual_hash, "zip", recorded_dir)

@ -7,10 +7,12 @@ from pathlib import Path
import pytest
from _pytest.outcomes import Failed
from PIL import Image
from .reporting import testreport
UI_TESTS_DIR = Path(__file__).parent.resolve()
SCREENS_DIR = UI_TESTS_DIR / "screens"
HASH_FILE = UI_TESTS_DIR / "fixtures.json"
SUGGESTION_FILE = UI_TESTS_DIR / "fixtures.suggestion.json"
FILE_HASHES = {}
@ -45,15 +47,24 @@ def _rename_records(screen_path):
record.replace(screen_path / f"{index:08}.png")
def _hash_files(path):
def _hash_files(path: Path) -> str:
files = path.iterdir()
hasher = hashlib.sha256()
for file in sorted(files):
hasher.update(file.read_bytes())
hasher.update(_get_bytes_from_png(str(file)))
return hasher.digest().hex()
def _get_bytes_from_png(png_file: str) -> bytes:
"""Decode a PNG file into bytes representing all the pixels.
Is necessary because Linux and Mac are using different PNG encoding libraries,
and we need the file hashes to be the same on both platforms.
"""
return Image.open(png_file).tobytes()
def _process_tested(fixture_test_path, test_name):
PROCESSED.add(test_name)
@ -85,7 +96,7 @@ def _process_tested(fixture_test_path, test_name):
def screen_recording(client, request):
test_ui = request.config.getoption("ui")
test_name = get_test_name(request.node.nodeid)
screens_test_path = UI_TESTS_DIR / "screens" / test_name
screens_test_path = SCREENS_DIR / test_name
if test_ui == "record":
screen_path = screens_test_path / "recorded"
@ -144,7 +155,7 @@ def _get_fixtures_content(fixtures: dict, remove_missing: bool):
def main():
read_fixtures()
for record in (UI_TESTS_DIR / "screens").iterdir():
for record in SCREENS_DIR.iterdir():
if not (record / "actual").exists():
continue

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save