From 41e07769e2c7071c059d907295f4805e9cf31265 Mon Sep 17 00:00:00 2001 From: grdddj Date: Wed, 8 Mar 2023 11:38:52 +0100 Subject: [PATCH] feat(tests): allow for coverage report even when emulator is controlled by the tests Allowing tests to specify the profiling wrapper over the emulator it is starting. [no changelog] --- ci/test.yml | 4 ++-- tests/conftest.py | 22 +++++++++++++++++++++- tests/emulators.py | 4 +++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/ci/test.yml b/ci/test.yml index fa5adcd3e..ad75cd9ae 100644 --- a/ci/test.yml +++ b/ci/test.yml @@ -57,12 +57,12 @@ core device test: needs: - core unix frozen debug build variables: - TREZOR_PROFILING: 1 + TREZOR_PROFILING: 1 # so that we get coverage data MULTICORE: 4 # more could interfere with other jobs script: - $NIX_SHELL --run "poetry run make -C core test_emu_ui_multicore | ts -s" after_script: - - mv core/src/.coverage core/.coverage.test_emu + - mv core/src/.coverage.* core # there will be more coverage files (one per core) - mv tests/ui_tests/reports/test/ test_ui_report - $NIX_SHELL --run "poetry run python ci/prepare_ui_artifacts.py | ts -s" - diff -u tests/ui_tests/fixtures.json tests/ui_tests/fixtures.suggestion.json diff --git a/tests/conftest.py b/tests/conftest.py index abd364b97..7aeaba179 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,6 +17,7 @@ from __future__ import annotations import os +from pathlib import Path from typing import TYPE_CHECKING, Generator import pytest @@ -37,6 +38,10 @@ if TYPE_CHECKING: from _pytest.config.argparsing import Parser from _pytest.terminal import TerminalReporter + +HERE = Path(__file__).resolve().parent + + # So that we see details of failed asserts from this module pytest.register_assert_rewrite("tests.common") @@ -83,8 +88,23 @@ def emulator(request: pytest.FixtureRequest) -> Generator["Emulator", None, None # 1. normal link, 2. debug link and 3. webauthn fake interface return 20000 + int(worker_id[2:]) * 3 + # So that we can generate coverage reports + profiling = os.environ.get("TREZOR_PROFILING") == "1" + if profiling: + core_dir = HERE.parent / "core" + profiling_wrapper = core_dir / "prof" / "prof.py" + main_args = [str(profiling_wrapper)] + # So that the coverage reports have the correct paths + os.environ["TREZOR_SRC"] = str(core_dir / "src") + else: + main_args = ["-m", "main"] + with EmulatorWrapper( - model, port=_get_port(), headless=True, auto_interact=not interact + model, + port=_get_port(), + headless=True, + auto_interact=not interact, + main_args=main_args, ) as emu: yield emu diff --git a/tests/emulators.py b/tests/emulators.py index 9fe1048f8..9a227c7b7 100644 --- a/tests/emulators.py +++ b/tests/emulators.py @@ -17,7 +17,7 @@ import tempfile from collections import defaultdict from pathlib import Path -from typing import Dict, List, Optional, Tuple +from typing import Dict, List, Optional, Sequence, Tuple from trezorlib._internal.emulator import CoreEmulator, Emulator, LegacyEmulator @@ -75,6 +75,7 @@ class EmulatorWrapper: port: Optional[int] = None, headless: bool = True, auto_interact: bool = True, + main_args: Sequence[str] = ("-m", "main"), ) -> None: if tag is not None: executable = filename_from_tag(gen, tag) @@ -107,6 +108,7 @@ class EmulatorWrapper: port=port, headless=headless, auto_interact=auto_interact, + main_args=main_args, ) else: raise ValueError(