18eb2bee6SAlexander V. Chernikovimport pytest 28eb2bee6SAlexander V. Chernikovfrom atf_python.atf_pytest import ATFHandler 3513ce835SAlexander V. Chernikovfrom typing import Dict 48eb2bee6SAlexander V. Chernikov 58eb2bee6SAlexander V. Chernikov 68eb2bee6SAlexander V. ChernikovPLUGIN_ENABLED = False 78eb2bee6SAlexander V. ChernikovDEFAULT_HANDLER = None 88eb2bee6SAlexander V. Chernikov 98eb2bee6SAlexander V. Chernikov 106332ef89SAlexander V. Chernikovdef set_handler(config): 116332ef89SAlexander V. Chernikov global DEFAULT_HANDLER, PLUGIN_ENABLED 126332ef89SAlexander V. Chernikov DEFAULT_HANDLER = ATFHandler(report_file_name=config.option.atf_file) 136332ef89SAlexander V. Chernikov PLUGIN_ENABLED = True 146332ef89SAlexander V. Chernikov return DEFAULT_HANDLER 156332ef89SAlexander V. Chernikov 166332ef89SAlexander V. Chernikov 178eb2bee6SAlexander V. Chernikovdef get_handler(): 188eb2bee6SAlexander V. Chernikov return DEFAULT_HANDLER 198eb2bee6SAlexander V. Chernikov 208eb2bee6SAlexander V. Chernikov 218eb2bee6SAlexander V. Chernikovdef pytest_addoption(parser): 228eb2bee6SAlexander V. Chernikov """Add file output""" 238eb2bee6SAlexander V. Chernikov # Add meta-values 248eb2bee6SAlexander V. Chernikov group = parser.getgroup("general", "Running and selection options") 258eb2bee6SAlexander V. Chernikov group.addoption( 268eb2bee6SAlexander V. Chernikov "--atf-source-dir", 278eb2bee6SAlexander V. Chernikov type=str, 288eb2bee6SAlexander V. Chernikov dest="atf_source_dir", 298eb2bee6SAlexander V. Chernikov help="Path to the test source directory", 308eb2bee6SAlexander V. Chernikov ) 318eb2bee6SAlexander V. Chernikov group.addoption( 328eb2bee6SAlexander V. Chernikov "--atf-cleanup", 338eb2bee6SAlexander V. Chernikov default=False, 348eb2bee6SAlexander V. Chernikov action="store_true", 358eb2bee6SAlexander V. Chernikov dest="atf_cleanup", 368eb2bee6SAlexander V. Chernikov help="Call cleanup procedure for a given test", 378eb2bee6SAlexander V. Chernikov ) 388eb2bee6SAlexander V. Chernikov group = parser.getgroup("terminal reporting", "reporting", after="general") 398eb2bee6SAlexander V. Chernikov group.addoption( 408eb2bee6SAlexander V. Chernikov "--atf", 418eb2bee6SAlexander V. Chernikov default=False, 428eb2bee6SAlexander V. Chernikov action="store_true", 438eb2bee6SAlexander V. Chernikov help="Enable test listing/results output in atf format", 448eb2bee6SAlexander V. Chernikov ) 458eb2bee6SAlexander V. Chernikov group.addoption( 468eb2bee6SAlexander V. Chernikov "--atf-file", 478eb2bee6SAlexander V. Chernikov type=str, 488eb2bee6SAlexander V. Chernikov dest="atf_file", 498eb2bee6SAlexander V. Chernikov help="Path to the status file provided by atf runtime", 508eb2bee6SAlexander V. Chernikov ) 518eb2bee6SAlexander V. Chernikov 528eb2bee6SAlexander V. Chernikov 53513ce835SAlexander V. Chernikov@pytest.fixture(autouse=True, scope="session") 54513ce835SAlexander V. Chernikovdef atf_vars() -> Dict[str, str]: 55513ce835SAlexander V. Chernikov return ATFHandler.get_atf_vars() 56513ce835SAlexander V. Chernikov 57513ce835SAlexander V. Chernikov 58eaeebfcbSJose Luis Duran@pytest.hookimpl(trylast=True) 598eb2bee6SAlexander V. Chernikovdef pytest_configure(config): 608eb2bee6SAlexander V. Chernikov if config.option.help: 618eb2bee6SAlexander V. Chernikov return 628eb2bee6SAlexander V. Chernikov 638eb2bee6SAlexander V. Chernikov # Register markings anyway to avoid warnings 648eb2bee6SAlexander V. Chernikov config.addinivalue_line("markers", "require_user(name): user to run the test with") 658eb2bee6SAlexander V. Chernikov config.addinivalue_line( 668eb2bee6SAlexander V. Chernikov "markers", "require_arch(names): List[str] of support archs" 678eb2bee6SAlexander V. Chernikov ) 688eb2bee6SAlexander V. Chernikov # config.addinivalue_line("markers", "require_config(config): List[Tuple[str,Any]] of k=v pairs") 698eb2bee6SAlexander V. Chernikov config.addinivalue_line( 708eb2bee6SAlexander V. Chernikov "markers", "require_diskspace(amount): str with required diskspace" 718eb2bee6SAlexander V. Chernikov ) 728eb2bee6SAlexander V. Chernikov config.addinivalue_line( 738eb2bee6SAlexander V. Chernikov "markers", "require_files(space): List[str] with file paths" 748eb2bee6SAlexander V. Chernikov ) 758eb2bee6SAlexander V. Chernikov config.addinivalue_line( 768eb2bee6SAlexander V. Chernikov "markers", "require_machine(names): List[str] of support machine types" 778eb2bee6SAlexander V. Chernikov ) 788eb2bee6SAlexander V. Chernikov config.addinivalue_line( 798eb2bee6SAlexander V. Chernikov "markers", "require_memory(amount): str with required memory" 808eb2bee6SAlexander V. Chernikov ) 818eb2bee6SAlexander V. Chernikov config.addinivalue_line( 828eb2bee6SAlexander V. Chernikov "markers", "require_progs(space): List[str] with file paths" 838eb2bee6SAlexander V. Chernikov ) 848eb2bee6SAlexander V. Chernikov config.addinivalue_line( 858eb2bee6SAlexander V. Chernikov "markers", "timeout(dur): int/float with max duration in sec" 868eb2bee6SAlexander V. Chernikov ) 878eb2bee6SAlexander V. Chernikov 886332ef89SAlexander V. Chernikov if not config.option.atf: 898eb2bee6SAlexander V. Chernikov return 906332ef89SAlexander V. Chernikov handler = set_handler(config) 918eb2bee6SAlexander V. Chernikov 928eb2bee6SAlexander V. Chernikov if config.option.collectonly: 938eb2bee6SAlexander V. Chernikov # Need to output list of tests to stdout, hence override 948eb2bee6SAlexander V. Chernikov # standard reporter plugin 958eb2bee6SAlexander V. Chernikov reporter = config.pluginmanager.getplugin("terminalreporter") 968eb2bee6SAlexander V. Chernikov if reporter: 978eb2bee6SAlexander V. Chernikov config.pluginmanager.unregister(reporter) 986332ef89SAlexander V. Chernikov else: 996332ef89SAlexander V. Chernikov handler.setup_configure() 1008eb2bee6SAlexander V. Chernikov 1018eb2bee6SAlexander V. Chernikov 102*3e5d0784SAlexander V. Chernikovdef pytest_pycollect_makeitem(collector, name, obj): 103*3e5d0784SAlexander V. Chernikov if PLUGIN_ENABLED: 104*3e5d0784SAlexander V. Chernikov handler = get_handler() 105*3e5d0784SAlexander V. Chernikov return handler.expand_tests(collector, name, obj) 106*3e5d0784SAlexander V. Chernikov 107*3e5d0784SAlexander V. Chernikov 1088eb2bee6SAlexander V. Chernikovdef pytest_collection_modifyitems(session, config, items): 1098eb2bee6SAlexander V. Chernikov """If cleanup is requested, replace collected tests with their cleanups (if any)""" 110d9af4219SAlexander V. Chernikov if PLUGIN_ENABLED: 1118eb2bee6SAlexander V. Chernikov handler = get_handler() 112d9af4219SAlexander V. Chernikov handler.modify_tests(items, config) 1138eb2bee6SAlexander V. Chernikov 1148eb2bee6SAlexander V. Chernikov 1158eb2bee6SAlexander V. Chernikovdef pytest_collection_finish(session): 1168eb2bee6SAlexander V. Chernikov if PLUGIN_ENABLED and session.config.option.collectonly: 1178eb2bee6SAlexander V. Chernikov handler = get_handler() 1188eb2bee6SAlexander V. Chernikov handler.list_tests(session.items) 1198eb2bee6SAlexander V. Chernikov 1208eb2bee6SAlexander V. Chernikov 1216332ef89SAlexander V. Chernikovdef pytest_runtest_setup(item): 1226332ef89SAlexander V. Chernikov if PLUGIN_ENABLED: 1236332ef89SAlexander V. Chernikov handler = get_handler() 1246332ef89SAlexander V. Chernikov handler.setup_method_pre(item) 1256332ef89SAlexander V. Chernikov 1266332ef89SAlexander V. Chernikov 1278eb2bee6SAlexander V. Chernikovdef pytest_runtest_logreport(report): 1288eb2bee6SAlexander V. Chernikov if PLUGIN_ENABLED: 1298eb2bee6SAlexander V. Chernikov handler = get_handler() 1308eb2bee6SAlexander V. Chernikov handler.add_report(report) 1318eb2bee6SAlexander V. Chernikov 1328eb2bee6SAlexander V. Chernikov 1338eb2bee6SAlexander V. Chernikovdef pytest_unconfigure(config): 1348eb2bee6SAlexander V. Chernikov if PLUGIN_ENABLED and config.option.atf_file: 1358eb2bee6SAlexander V. Chernikov handler = get_handler() 1366332ef89SAlexander V. Chernikov handler.write_report() 137