1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-18 10:32:02 +00:00
trezor-firmware/tests/ui_tests/__init__.py

113 lines
3.3 KiB
Python
Raw Normal View History

2019-12-30 11:20:51 +00:00
import hashlib
import re
import shutil
from contextlib import contextmanager
from distutils.dir_util import copy_tree
2019-12-30 11:20:51 +00:00
from pathlib import Path
import pytest
from . import html
2019-12-30 11:20:51 +00:00
def _get_test_dirname(node):
# This composes the dirname from the test module name and test item name.
# Test item name is usually function name, but when parametrization is used,
# parameters are also part of the name. Some functions have very long parameter
# names (tx hashes etc) that run out of maximum allowable filename length, so
# we limit the name to first 100 chars. This is not a problem with txhashes.
node_name = re.sub(r"\W+", "_", node.name)[:100]
node_module_name = node.getparent(pytest.Module).name
2020-01-06 14:44:30 +00:00
return f"{node_module_name}_{node_name}"
2019-12-30 11:20:51 +00:00
def _check_fixture_directory(fixture_dir, screen_path):
# create the fixture dir if it does not exist
if not fixture_dir.exists():
fixture_dir.mkdir()
# delete old files
shutil.rmtree(screen_path, ignore_errors=True)
screen_path.mkdir()
def _process_recorded(screen_path):
# create hash
2020-01-06 14:44:30 +00:00
digest = _hash_files(screen_path)
(screen_path.parent / "hash.txt").write_text(digest)
2019-12-30 11:20:51 +00:00
_rename_records(screen_path)
def _rename_records(screen_path):
# rename screenshots
for index, record in enumerate(sorted(screen_path.iterdir())):
2020-01-06 14:44:30 +00:00
record.replace(screen_path / f"{index:08}.png")
2019-12-30 11:20:51 +00:00
2020-01-06 14:44:30 +00:00
def _hash_files(path):
files = path.iterdir()
2019-12-30 11:20:51 +00:00
hasher = hashlib.sha256()
for file in sorted(files):
2020-01-06 14:44:30 +00:00
hasher.update(file.read_bytes())
2019-12-30 11:20:51 +00:00
return hasher.digest().hex()
def _process_tested(fixture_test_path, test_name):
hash_file = fixture_test_path / "hash.txt"
if not hash_file.exists():
raise ValueError("File hash.txt not found.")
2020-01-06 14:44:30 +00:00
expected_hash = hash_file.read_text()
2019-12-30 11:20:51 +00:00
actual_path = fixture_test_path / "actual"
2020-01-06 14:44:30 +00:00
actual_hash = _hash_files(actual_path)
2019-12-30 11:20:51 +00:00
2020-01-06 14:44:30 +00:00
_rename_records(actual_path)
2019-12-30 11:20:51 +00:00
if actual_hash != expected_hash:
diff_file = html.diff_file(
fixture_test_path, test_name, actual_hash, expected_hash
)
2019-12-30 11:20:51 +00:00
pytest.fail(
"Hash of {} differs.\nExpected: {}\nActual: {}\nDiff file: {}".format(
test_name, expected_hash, actual_hash, diff_file
2019-12-30 11:20:51 +00:00
)
)
else:
copy_tree(
str(fixture_test_path / "actual"), str(fixture_test_path / "recorded")
)
2019-12-30 11:20:51 +00:00
if (fixture_test_path / "diff.html").exists():
(fixture_test_path / "diff.html").unlink()
@contextmanager
def screen_recording(client, request):
2020-01-07 09:13:08 +00:00
test_ui = request.config.getoption("ui")
2019-12-30 11:20:51 +00:00
test_name = _get_test_dirname(request.node)
2020-01-06 14:44:30 +00:00
fixture_test_path = Path(__file__).parent.resolve() / "fixtures" / test_name
2019-12-30 11:20:51 +00:00
2020-01-07 09:13:08 +00:00
if test_ui == "record":
2019-12-30 11:20:51 +00:00
screen_path = fixture_test_path / "recorded"
2020-01-07 09:13:08 +00:00
elif test_ui == "test":
2019-12-30 11:20:51 +00:00
screen_path = fixture_test_path / "actual"
else:
2020-01-07 09:13:08 +00:00
raise ValueError("Invalid 'ui' option.")
2019-12-30 11:20:51 +00:00
_check_fixture_directory(fixture_test_path, screen_path)
try:
client.debug.start_recording(str(screen_path))
yield
finally:
client.debug.stop_recording()
2020-01-07 09:13:08 +00:00
if test_ui == "record":
2019-12-30 11:20:51 +00:00
_process_recorded(screen_path)
2020-01-07 09:13:08 +00:00
elif test_ui == "test":
2019-12-30 11:20:51 +00:00
_process_tested(fixture_test_path, test_name)
else:
2020-01-07 09:13:08 +00:00
raise ValueError("Invalid 'ui' option.")