-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfind_untested_functions.py
More file actions
211 lines (165 loc) · 6.96 KB
/
find_untested_functions.py
File metadata and controls
211 lines (165 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
"""
Script to find functions in cosmotech/coal/ that don't have corresponding tests.
"""
import ast
import os
import re
from collections import defaultdict
from pathlib import Path
def get_functions_from_file(file_path):
"""Extract all function and class definitions from a Python file."""
with open(file_path, "r", encoding="utf-8") as f:
try:
tree = ast.parse(f.read())
except SyntaxError:
print(f"Syntax error in {file_path}")
return []
functions = []
# Get top-level functions
for node in ast.iter_child_nodes(tree):
if isinstance(node, ast.FunctionDef):
# Skip private functions (starting with _)
if not node.name.startswith("_"):
functions.append(node.name)
elif isinstance(node, ast.ClassDef):
# Get class methods
for class_node in ast.iter_child_nodes(node):
if isinstance(class_node, ast.FunctionDef):
# Skip private methods (starting with _)
if not class_node.name.startswith("_"):
functions.append(f"{node.name}.{class_node.name}")
return functions
def get_tests_from_file(file_path):
"""Extract all test functions from a Python test file."""
with open(file_path, "r", encoding="utf-8") as f:
try:
content = f.read()
tree = ast.parse(content)
except SyntaxError:
print(f"Syntax error in {file_path}")
return []
tests = []
# Look for test functions (test_*)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef) and node.name.startswith("test_"):
tests.append(node.name)
# Also look for function names in the content (for parameterized tests)
function_pattern = r"def\s+(test_\w+)"
tests.extend(re.findall(function_pattern, content))
return list(set(tests))
def map_module_to_test_file(module_path):
"""Map a module path to its corresponding test file path."""
# Convert module path to test path
# e.g., cosmotech/coal/aws/s3.py -> tests/unit/coal/test_aws/test_aws_s3.py
parts = module_path.parts
if len(parts) < 3 or parts[0] != "cosmotech" or parts[1] != "coal":
return None
# Skip __init__.py files
if parts[-1] == "__init__.py":
return None
# Get the module name without extension
module_name = parts[-1].replace(".py", "")
# Construct the test file path
test_dir = Path("tests/unit/coal")
module_parts = parts[2:-1] # Skip cosmotech/coal and the file name
# Create directory structure
for part in module_parts:
test_dir = test_dir / f"test_{part}"
# Create test file name with module path included
# For example, for cosmotech/coal/azure/adx/ingestion.py, the test file would be test_adx_ingestion.py
# For cosmotech/coal/azure/blob.py, the test file would be test_azure_blob.py
if module_parts:
test_file_name = f"test_{module_parts[-1]}_{module_name}.py"
else:
test_file_name = f"test_{module_name}.py"
test_file = test_dir / test_file_name
return test_file
def find_untested_functions():
"""Find functions in cosmotech/coal/ that don't have corresponding tests."""
coal_dir = Path("cosmotech/coal")
# Dictionary to store functions by module
module_functions = {}
# Dictionary to store tests by module
module_tests = defaultdict(list)
# Find all Python files in cosmotech/coal/
for root, _, files in os.walk(coal_dir):
for file in files:
if file.endswith(".py"):
file_path = Path(root) / file
module_path = file_path.relative_to(".")
# Skip __init__.py files
if file == "__init__.py":
continue
# Get functions from the module
functions = get_functions_from_file(file_path)
if functions:
module_functions[module_path] = functions
# Find all test files in tests/unit/coal/
test_dir = Path("tests/unit/coal")
if test_dir.exists():
for root, _, files in os.walk(test_dir):
for file in files:
if file.startswith("test_") and file.endswith(".py"):
test_file_path = Path(root) / file
tests = get_tests_from_file(test_file_path)
module_tests[test_file_path] = tests
# Check which functions don't have tests
untested_functions = {}
for module_path, functions in module_functions.items():
test_file = map_module_to_test_file(module_path)
if test_file is None:
# Skip modules that don't map to a test file
continue
if not test_file.exists():
# If the test file doesn't exist, all functions are untested
untested_functions[module_path] = functions
continue
# Get tests for this module
tests = module_tests.get(test_file, [])
# Check which functions don't have corresponding tests
untested = []
for func in functions:
# Check if there's a test for this function
has_test = False
for test in tests:
# Look for test_function_name or test_class_function_name
# Also check for test patterns like test_class_method_name_additional_info
# For class methods, also check for test_method_name (without the class name)
if (
test == f"test_{func}"
or test == f"test_{func.replace('.', '_')}"
or test.startswith(f"test_{func}_")
or test.startswith(f"test_{func.replace('.', '_')}_")
):
has_test = True
break
# Special case for class methods: check if there's a test for just the method name
if "." in func:
class_name, method_name = func.split(".")
if test == f"test_{method_name}" or test.startswith(f"test_{method_name}_"):
has_test = True
break
if not has_test:
untested.append(func)
if untested:
untested_functions[module_path] = untested
return untested_functions
def main():
"""Main function."""
untested_functions = find_untested_functions()
if not untested_functions:
print("All functions have tests!")
return
print("Functions without tests:")
print("=======================")
for module, functions in sorted(untested_functions.items()):
if functions:
print(f"\n{module}:")
for func in sorted(functions):
print(f" - {func}")
# Print summary
total_untested = sum(len(funcs) for funcs in untested_functions.values())
print(f"\nTotal untested functions: {total_untested}")
if __name__ == "__main__":
main()