1#!/usr/bin/env python3 2# Copyright (c) 2023 Intel Corporation 3# 4# SPDX-License-Identifier: Apache-2.0 5 6'''Common fixtures for use in testing the twister tool.''' 7 8import logging 9import shutil 10import mock 11import os 12import pytest 13import sys 14 15ZEPHYR_BASE = os.getenv('ZEPHYR_BASE') 16TEST_DATA = os.path.join(ZEPHYR_BASE, 'scripts', 'tests', 17 'twister_blackbox', 'test_data') 18 19 20sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister")) 21sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts")) 22 23 24sample_filename_mock = mock.PropertyMock(return_value='test_sample.yaml') 25testsuite_filename_mock = mock.PropertyMock(return_value='test_data.yaml') 26sample_filename_mock = mock.PropertyMock(return_value='test_sample.yaml') 27 28def pytest_configure(config): 29 config.addinivalue_line("markers", "noclearlog: disable the clear_log autouse fixture") 30 config.addinivalue_line("markers", "noclearout: disable the provide_out autouse fixture") 31 32@pytest.fixture(name='zephyr_base') 33def zephyr_base_directory(): 34 return ZEPHYR_BASE 35 36 37@pytest.fixture(name='zephyr_test_data') 38def zephyr_test_directory(): 39 return TEST_DATA 40 41@pytest.fixture(autouse=True) 42def clear_log(request): 43 # As this fixture is autouse, one can use the pytest.mark.noclearlog decorator 44 # in order to be sure that this fixture's code will not fire. 45 if 'noclearlog' in request.keywords: 46 return 47 48 # clear_log is used by pytest fixture 49 # However, clear_log_in_test is prepared to be used directly in the code, wherever required 50 clear_log_in_test() 51 52def clear_log_in_test(): 53 # Required to fix the pytest logging error 54 # See: https://github.com/pytest-dev/pytest/issues/5502 55 loggers = [logging.getLogger()] \ 56 + list(logging.Logger.manager.loggerDict.values()) \ 57 + [logging.getLogger(name) for \ 58 name in logging.root.manager.loggerDict] 59 for logger in loggers: 60 handlers = getattr(logger, 'handlers', []) 61 for handler in handlers: 62 logger.removeHandler(handler) 63 64# This fixture provides blackbox tests with an `out_path` parameter 65# It should be used as the `-O` (`--out_dir`) parameter in blackbox tests 66# APPRECIATED: method of using this out_path wholly outside of test code 67@pytest.fixture(name='out_path', autouse=True) 68def provide_out(tmp_path, request): 69 # As this fixture is autouse, one can use the pytest.mark.noclearout decorator 70 # in order to be sure that this fixture's code will not fire. 71 # Most of the time, just omitting the `out_path` parameter is sufficient. 72 if 'noclearout' in request.keywords: 73 yield 74 return 75 76 # Before 77 out_container_path = tmp_path / 'blackbox-out-container' 78 out_container_path.mkdir() 79 out_path = os.path.join(out_container_path, "blackbox-out") 80 81 # Test 82 yield out_path 83 84 # After 85 # We're operating in temp, so it is not strictly necessary 86 # but the files can get large quickly as we do not need them after the test. 87 shutil.rmtree(out_container_path) 88