slither.utils.tests_pattern
1from pathlib import Path 2from typing import TYPE_CHECKING 3 4if TYPE_CHECKING: 5 from slither.core.declarations.contract import Contract 6 7_TESTS_PATTERNS = ["Test", "test", "Mock", "mock"] 8TESTS_PATTERNS = _TESTS_PATTERNS + [x + "s" for x in _TESTS_PATTERNS] 9 10 11def _is_test_pattern(txt: str, pattern: str) -> bool: 12 """ 13 Check if the txt starts with the pattern, or ends with it 14 :param pattern: 15 :return: 16 """ 17 if txt.endswith(pattern): 18 return True 19 if not txt.startswith(pattern): 20 return False 21 length = len(pattern) 22 if len(txt) <= length: 23 return True 24 return txt[length] == "_" or txt[length].isupper() 25 26 27def is_test_file(path: Path) -> bool: 28 """ 29 Check if the given path points to a test/mock file 30 :param path: 31 :return: 32 """ 33 return any((test_pattern in path.parts for test_pattern in TESTS_PATTERNS)) 34 35 36def is_test_contract(contract: "Contract") -> bool: 37 """ 38 Check if the contract is a test/mock 39 :param contract: 40 :return: 41 """ 42 return ( 43 _is_test_pattern(contract.name, "Test") 44 or _is_test_pattern(contract.name, "Mock") 45 or ( 46 contract.source_mapping.filename.absolute 47 and is_test_file(Path(contract.source_mapping.filename.absolute)) 48 ) 49 )
TESTS_PATTERNS =
['Test', 'test', 'Mock', 'mock', 'Tests', 'tests', 'Mocks', 'mocks']
def
is_test_file(path: pathlib.Path) -> bool:
28def is_test_file(path: Path) -> bool: 29 """ 30 Check if the given path points to a test/mock file 31 :param path: 32 :return: 33 """ 34 return any((test_pattern in path.parts for test_pattern in TESTS_PATTERNS))
Check if the given path points to a test/mock file
Parameters
- path:
Returns
37def is_test_contract(contract: "Contract") -> bool: 38 """ 39 Check if the contract is a test/mock 40 :param contract: 41 :return: 42 """ 43 return ( 44 _is_test_pattern(contract.name, "Test") 45 or _is_test_pattern(contract.name, "Mock") 46 or ( 47 contract.source_mapping.filename.absolute 48 and is_test_file(Path(contract.source_mapping.filename.absolute)) 49 ) 50 )
Check if the contract is a test/mock
Parameters
- contract: