mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-21 23:18:13 +00:00
fix(tests): decoding PNG files to fix Linux/Mac differences in encoding
This commit is contained in:
parent
5bc3e353e0
commit
1b932a20e8
@ -1,27 +1,19 @@
|
|||||||
import hashlib
|
|
||||||
import json
|
|
||||||
import shutil
|
import shutil
|
||||||
|
import sys
|
||||||
from pathlib import Path
|
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):
|
read_fixtures()
|
||||||
files = path.iterdir()
|
from tests.ui_tests import _hash_files, FILE_HASHES, SCREENS_DIR # isort:skip
|
||||||
hasher = hashlib.sha256()
|
|
||||||
for file in sorted(files):
|
|
||||||
hasher.update(file.read_bytes())
|
|
||||||
|
|
||||||
return hasher.digest().hex()
|
|
||||||
|
|
||||||
|
|
||||||
root = Path(__file__).parent / ".."
|
for test_case in FILE_HASHES.keys():
|
||||||
screens = root / "tests/ui_tests/screens"
|
recorded_dir = SCREENS_DIR / test_case / "recorded"
|
||||||
fixtures = root / "tests/ui_tests/fixtures.json"
|
expected_hash = FILE_HASHES[test_case]
|
||||||
|
|
||||||
hashes = json.loads(fixtures.read_text())
|
|
||||||
|
|
||||||
for test_case in hashes.keys():
|
|
||||||
recorded_dir = screens / test_case / "recorded"
|
|
||||||
expected_hash = hashes[test_case]
|
|
||||||
actual_hash = _hash_files(recorded_dir)
|
actual_hash = _hash_files(recorded_dir)
|
||||||
assert expected_hash == actual_hash
|
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
|
import pytest
|
||||||
from _pytest.outcomes import Failed
|
from _pytest.outcomes import Failed
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
from .reporting import testreport
|
from .reporting import testreport
|
||||||
|
|
||||||
UI_TESTS_DIR = Path(__file__).parent.resolve()
|
UI_TESTS_DIR = Path(__file__).parent.resolve()
|
||||||
|
SCREENS_DIR = UI_TESTS_DIR / "screens"
|
||||||
HASH_FILE = UI_TESTS_DIR / "fixtures.json"
|
HASH_FILE = UI_TESTS_DIR / "fixtures.json"
|
||||||
SUGGESTION_FILE = UI_TESTS_DIR / "fixtures.suggestion.json"
|
SUGGESTION_FILE = UI_TESTS_DIR / "fixtures.suggestion.json"
|
||||||
FILE_HASHES = {}
|
FILE_HASHES = {}
|
||||||
@ -45,15 +47,24 @@ def _rename_records(screen_path):
|
|||||||
record.replace(screen_path / f"{index:08}.png")
|
record.replace(screen_path / f"{index:08}.png")
|
||||||
|
|
||||||
|
|
||||||
def _hash_files(path):
|
def _hash_files(path: Path) -> str:
|
||||||
files = path.iterdir()
|
files = path.iterdir()
|
||||||
hasher = hashlib.sha256()
|
hasher = hashlib.sha256()
|
||||||
for file in sorted(files):
|
for file in sorted(files):
|
||||||
hasher.update(file.read_bytes())
|
hasher.update(_get_bytes_from_png(str(file)))
|
||||||
|
|
||||||
return hasher.digest().hex()
|
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):
|
def _process_tested(fixture_test_path, test_name):
|
||||||
PROCESSED.add(test_name)
|
PROCESSED.add(test_name)
|
||||||
|
|
||||||
@ -85,7 +96,7 @@ def _process_tested(fixture_test_path, test_name):
|
|||||||
def screen_recording(client, request):
|
def screen_recording(client, request):
|
||||||
test_ui = request.config.getoption("ui")
|
test_ui = request.config.getoption("ui")
|
||||||
test_name = get_test_name(request.node.nodeid)
|
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":
|
if test_ui == "record":
|
||||||
screen_path = screens_test_path / "recorded"
|
screen_path = screens_test_path / "recorded"
|
||||||
@ -144,7 +155,7 @@ def _get_fixtures_content(fixtures: dict, remove_missing: bool):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
read_fixtures()
|
read_fixtures()
|
||||||
for record in (UI_TESTS_DIR / "screens").iterdir():
|
for record in SCREENS_DIR.iterdir():
|
||||||
if not (record / "actual").exists():
|
if not (record / "actual").exists():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user