Add detail about coverage.py

This commit is contained in:
Saurabh Misra 2023-12-04 12:05:51 -08:00
parent 481e66c5e3
commit 314cad098b

View file

@ -110,7 +110,9 @@ def process_test_files(
test_framework = cfg.test_framework
function_to_test_map = defaultdict(list)
jedi_project = jedi.Project(path=project_root_path)
code_graph_traverser = CodeGraphFunctionsTraverser(jedi_project=jedi_project, project_root_path=project_root_path, depth=2)
code_graph_traverser = CodeGraphFunctionsTraverser(
jedi_project=jedi_project, project_root_path=project_root_path, depth=2
)
for test_file, functions in file_to_test_map.items():
script = jedi.Script(path=test_file, project=jedi_project)
@ -149,17 +151,22 @@ def process_test_files(
scope = m.group(1)
index = test_functions_raw.index(scope) if scope in test_functions_raw else -1
if index >= 0:
scope_test_function = test_functions_list[index].function_name
scope_test_suite = test_functions_list[index].test_suite_name
test_function_names[TestFunction(scope_test_function, scope_test_suite)].append(name)
test_function_names[TestFunction(scope_test_function, scope_test_suite)].append(
name
)
# TODO: Memoize a list of visited_functions and don't visit them again
for test_function, names in test_function_names.items():
functions_discovered = code_graph_traverser.start_search((names, script), test_function.function_name)
functions_discovered = code_graph_traverser.start_search(
(names, script), test_function.function_name
)
for function in functions_discovered:
function_to_test_map[function].append(
TestsInFile(test_file, None, test_function.function_name, test_function.test_suite_name)
)
TestsInFile(
test_file, None, test_function.function_name, test_function.test_suite_name
)
)
deduped_function_to_test_map = {}
for function, tests in function_to_test_map.items():
deduped_function_to_test_map[function] = list(set(tests))
@ -167,7 +174,7 @@ def process_test_files(
class CodeGraphFunctionsTraverser:
# TODO: Check out pytest-testmon and pytest-incremental which aim to replicate the same
# TODO: Check out coverage.py, pytest-testmon and pytest-incremental which aim to replicate the same
def __init__(self, jedi_project, project_root_path, depth=1):
self.jedi_project = jedi_project
self.project_root_path = project_root_path
@ -180,8 +187,12 @@ class CodeGraphFunctionsTraverser:
def start_search(self, names_or_path, starting_function) -> set[str]:
return self.find_functions(names_or_path, starting_function, self.depth)
def find_functions(self, names_or_path: Union[str, tuple[List[jedi.api.classes.Name], jedi.Script]],
function_scope: str, depth: int=1) -> set[str]:
def find_functions(
self,
names_or_path: Union[str, tuple[List[jedi.api.classes.Name], jedi.Script]],
function_scope: str,
depth: int = 1,
) -> set[str]:
if depth == 0:
return set([])
if isinstance(names_or_path, str) and os.path.exists(names_or_path):
@ -220,18 +231,20 @@ class CodeGraphFunctionsTraverser:
definition_path = str(definition[0].module_path)
# The definition is part of this project + site-packages and not defined within the original function
if (
definition_path.startswith(str(self.project_root_path) + os.sep)
and not path_belongs_to_site_packages(definition_path)
and definition[0].module_name != name.module_name
definition_path.startswith(str(self.project_root_path) + os.sep)
and not path_belongs_to_site_packages(definition_path)
and definition[0].module_name != name.module_name
):
functions_discovered.append(definition[0].full_name)
only_function_name = definition[0].full_name.split('.')[-1]
only_function_name = definition[0].full_name.split(".")[-1]
next_function_scope.add((only_function_name, definition_path))
# function_to_test_map[definition[0].full_name].append(
# TestsInFile(test_file, None, scope_test_function, scope_test_suite)
# )
for only_function_name, function_path in next_function_scope:
functions_discovered += self.find_functions(str(function_path), only_function_name, depth=depth-1)
functions_discovered += self.find_functions(
str(function_path), only_function_name, depth=depth - 1
)
return set(functions_discovered)