1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

tests/ui: code review fixes

This commit is contained in:
Tomas Susanka 2020-01-06 14:44:30 +00:00
parent fe690a6b6b
commit 56257eb6a5
5 changed files with 27 additions and 51 deletions

View File

@ -3,28 +3,19 @@ import shutil
from pathlib import Path from pathlib import Path
def _hash_files(files): def _hash_files(path):
files = path.iterdir()
hasher = hashlib.sha256() hasher = hashlib.sha256()
for file in sorted(files): for file in sorted(files):
with open(file, "rb") as f: hasher.update(file.read_bytes())
content = f.read()
hasher.update(content)
return hasher.digest().hex() return hasher.digest().hex()
def _compare_hash(test_dir, hash): fixture_root = Path().cwd() / "../tests/ui_tests/fixtures/"
with open(test_dir / "hash.txt", "r") as f:
content = f.read()
assert hash == content
for recorded_dir in fixture_root.glob("*/recorded"):
fixture_root = Path().cwd() / "../tests/ui_tests/fixtures" expected_hash = (recorded_dir.parent / "hash.txt").read_text()
actual_hash = _hash_files(recorded_dir)
for test_dir in fixture_root.iterdir(): assert expected_hash == actual_hash
if test_dir.is_dir(): shutil.make_archive("tmp/" + actual_hash, "zip", recorded_dir)
recorded_dir = test_dir / "recorded"
if recorded_dir.exists():
hash = _hash_files(recorded_dir.iterdir())
_compare_hash(test_dir, hash)
shutil.make_archive("tmp/" + hash, "zip", recorded_dir)

View File

@ -80,10 +80,10 @@ test_emu_click: ## run click tests
cd tests ; ./run_tests_click_emu.sh $(TESTOPTS) cd tests ; ./run_tests_click_emu.sh $(TESTOPTS)
test_emu_ui: ## run ui integration tests test_emu_ui: ## run ui integration tests
cd tests ; ./run_tests_device_emu.sh --test_screen=test -m "not skip_ui" $(TESTOPTS) cd tests ; ./run_tests_device_emu.sh --test-screen=test -m "not skip_ui" $(TESTOPTS)
test_emu_ui_record: ## record and hash screens for ui integration tests test_emu_ui_record: ## record and hash screens for ui integration tests
cd tests ; ./run_tests_device_emu.sh --test_screen=record -m "not skip_ui" $(TESTOPTS) cd tests ; ./run_tests_device_emu.sh --test-screen=record -m "not skip_ui" $(TESTOPTS)
pylint: ## run pylint on application sources and tests pylint: ## run pylint on application sources and tests
pylint -E $(shell find src tests -name *.py) pylint -E $(shell find src tests -name *.py)

View File

@ -140,7 +140,7 @@ def client(request):
def pytest_addoption(parser): def pytest_addoption(parser):
parser.addoption( parser.addoption(
"--test_screen", "--test-screen",
action="store", action="store",
default="", default="",
help="Enable UI intergration tests: 'record' or 'test'", help="Enable UI intergration tests: 'record' or 'test'",

View File

@ -18,7 +18,7 @@ def _get_test_dirname(node):
# we limit the name to first 100 chars. This is not a problem with txhashes. # 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_name = re.sub(r"\W+", "_", node.name)[:100]
node_module_name = node.getparent(pytest.Module).name node_module_name = node.getparent(pytest.Module).name
return "{}_{}".format(node_module_name, node_name) return f"{node_module_name}_{node_name}"
def _check_fixture_directory(fixture_dir, screen_path): def _check_fixture_directory(fixture_dir, screen_path):
@ -32,28 +32,24 @@ def _check_fixture_directory(fixture_dir, screen_path):
def _process_recorded(screen_path): def _process_recorded(screen_path):
records = sorted(screen_path.iterdir())
# create hash # create hash
digest = _hash_files(records) digest = _hash_files(screen_path)
with open(screen_path / "../hash.txt", "w") as f:
f.write(digest) (screen_path.parent / "hash.txt").write_text(digest)
_rename_records(screen_path) _rename_records(screen_path)
def _rename_records(screen_path): def _rename_records(screen_path):
# rename screenshots # rename screenshots
for index, record in enumerate(sorted(screen_path.iterdir())): for index, record in enumerate(sorted(screen_path.iterdir())):
filename = screen_path / "{:08}.png".format(index) record.replace(screen_path / f"{index:08}.png")
record.replace(filename)
def _hash_files(files): def _hash_files(path):
files = path.iterdir()
hasher = hashlib.sha256() hasher = hashlib.sha256()
for file in sorted(files): for file in sorted(files):
with open(file, "rb") as f: hasher.update(file.read_bytes())
content = f.read()
hasher.update(content)
return hasher.digest().hex() return hasher.digest().hex()
@ -64,14 +60,11 @@ def _process_tested(fixture_test_path, test_name):
if not hash_file.exists(): if not hash_file.exists():
raise ValueError("File hash.txt not found.") raise ValueError("File hash.txt not found.")
with open(hash_file, "r") as f: expected_hash = hash_file.read_text()
expected_hash = f.read()
actual_path = fixture_test_path / "actual" actual_path = fixture_test_path / "actual"
_rename_records(actual_path) actual_hash = _hash_files(actual_path)
records = sorted(actual_path.iterdir()) _rename_records(actual_path)
actual_hash = _hash_files(records)
if actual_hash != expected_hash: if actual_hash != expected_hash:
diff_file = html.diff_file( diff_file = html.diff_file(
@ -93,18 +86,13 @@ def _process_tested(fixture_test_path, test_name):
@contextmanager @contextmanager
def screen_recording(client, request): def screen_recording(client, request):
if not request.node.get_closest_marker("skip_ui"): test_screen = request.config.getoption("test_screen")
test_screen = request.config.getoption("test_screen") if request.node.get_closest_marker("skip_ui") or not test_screen:
else:
test_screen = ""
if not test_screen:
yield yield
return return
fixture_root = Path(__file__) / "../fixtures"
test_name = _get_test_dirname(request.node) test_name = _get_test_dirname(request.node)
fixture_test_path = fixture_root.resolve() / test_name fixture_test_path = Path(__file__).parent.resolve() / "fixtures" / test_name
if test_screen == "record": if test_screen == "record":
screen_path = fixture_test_path / "recorded" screen_path = fixture_test_path / "recorded"

View File

@ -61,8 +61,5 @@ def diff_file(fixture_test_path, test_name, actual_hash, expected_hash):
_image(r) _image(r)
_image(a) _image(a)
with open(fixture_test_path / "diff.html", "w") as f: (fixture_test_path / "diff.html").write_text(doc.render())
f.write(doc.render())
f.close()
return fixture_test_path / "diff.html" return fixture_test_path / "diff.html"