-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage_analysis.py
More file actions
282 lines (235 loc) · 9.69 KB
/
coverage_analysis.py
File metadata and controls
282 lines (235 loc) · 9.69 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python3
"""
Code Coverage Analysis Tool for FoundationUI
Analyzes source files and test files to estimate test coverage.
This tool uses repository-relative paths for cross-environment compatibility.
It can be executed from any directory and will resolve paths from the
repository root (identified by .git directory).
"""
import os
import sys
import re
import argparse
from pathlib import Path
from collections import defaultdict
def count_lines_of_code(file_path):
"""Count lines of code (excluding comments and blank lines)"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
code_lines = 0
in_multiline_comment = False
for line in lines:
stripped = line.strip()
# Skip blank lines
if not stripped:
continue
# Handle multiline comments
if '/*' in stripped:
in_multiline_comment = True
if '*/' in stripped:
in_multiline_comment = False
continue
if in_multiline_comment:
continue
# Skip single-line comments
if stripped.startswith('//'):
continue
# Count as code line
code_lines += 1
return code_lines, len(lines)
except Exception as e:
print(f"Error reading {file_path}: {e}")
return 0, 0
def analyze_file(file_path):
"""Analyze a Swift file for functions, structs, classes, etc."""
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Count various Swift constructs
funcs = len(re.findall(r'\bfunc\s+\w+', content))
structs = len(re.findall(r'\bstruct\s+\w+', content))
classes = len(re.findall(r'\bclass\s+\w+', content))
enums = len(re.findall(r'\benum\s+\w+', content))
extensions = len(re.findall(r'\bextension\s+\w+', content))
properties = len(re.findall(r'\b(var|let)\s+\w+', content))
return {
'functions': funcs,
'structs': structs,
'classes': classes,
'enums': enums,
'extensions': extensions,
'properties': properties
}
except Exception as e:
print(f"Error analyzing {file_path}: {e}")
return {}
def find_repo_root():
"""
Find the repository root by searching for .git directory.
Starts from the current working directory and traverses upward.
Falls back to script directory if .git is not found.
"""
current_path = Path.cwd().resolve()
# Try to find .git in current and parent directories
while current_path != current_path.parent:
if (current_path / '.git').exists():
return current_path
current_path = current_path.parent
# Fallback to script directory's parent
script_path = Path(__file__).resolve().parent
if (script_path / '.git').exists():
return script_path
# Ultimate fallback: current working directory
return Path.cwd().resolve()
def main():
# Parse command-line arguments
parser = argparse.ArgumentParser(
description='Analyze test coverage in the FoundationUI package.',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog='''
Examples:
python3 coverage_analysis.py # Analyze and report
python3 coverage_analysis.py --threshold 0.67 # Check if coverage meets threshold
python3 coverage_analysis.py --report coverage.txt # Write report to file
'''
)
parser.add_argument(
'--threshold',
type=float,
default=None,
help='Exit with non-zero code if coverage is below this threshold (0.0-1.0)'
)
parser.add_argument(
'--report',
type=str,
default=None,
help='Output report to file instead of stdout'
)
parser.add_argument(
'-v', '--verbose',
action='store_true',
help='Verbose output'
)
args = parser.parse_args()
# Find repository root
repo_root = find_repo_root()
base_path = repo_root / 'FoundationUI'
sources_path = base_path / 'Sources' / 'FoundationUI'
tests_path = base_path / 'Tests' / 'FoundationUITests'
if args.verbose:
print(f"Repository root: {repo_root}", file=sys.stderr)
print(f"FoundationUI path: {base_path}", file=sys.stderr)
print(f"Sources path: {sources_path}", file=sys.stderr)
print(f"Tests path: {tests_path}", file=sys.stderr)
print(file=sys.stderr)
# Layers to analyze
layers = {
'DesignTokens': 'Layer 0',
'Modifiers': 'Layer 1',
'Components': 'Layer 2',
'Patterns': 'Layer 3',
'Contexts': 'Layer 4',
'Utilities': 'Utilities'
}
results = {}
output_lines = []
for layer_dir, layer_name in layers.items():
layer_path = sources_path / layer_dir
if not layer_path.exists():
continue
source_files = list(layer_path.glob('*.swift'))
total_loc = 0
total_actual_lines = 0
total_constructs = defaultdict(int)
output_lines.append(f"\n{'='*80}")
output_lines.append(f"{layer_name}: {layer_dir}")
output_lines.append(f"{'='*80}")
for source_file in sorted(source_files):
loc, actual_lines = count_lines_of_code(source_file)
total_loc += loc
total_actual_lines += actual_lines
constructs = analyze_file(source_file)
for key, value in constructs.items():
total_constructs[key] += value
output_lines.append(f" {source_file.name:40s} {loc:5d} LOC ({actual_lines:5d} total)")
output_lines.append(f"\n {'TOTAL':40s} {total_loc:5d} LOC ({total_actual_lines:5d} total)")
output_lines.append(f"\n Constructs:")
output_lines.append(f" Functions: {total_constructs['functions']}")
output_lines.append(f" Structs: {total_constructs['structs']}")
output_lines.append(f" Classes: {total_constructs['classes']}")
output_lines.append(f" Enums: {total_constructs['enums']}")
output_lines.append(f" Extensions: {total_constructs['extensions']}")
output_lines.append(f" Properties: {total_constructs['properties']}")
# Analyze corresponding test files
test_subdirs = {
'DesignTokens': 'DesignTokensTests',
'Modifiers': 'ModifiersTests',
'Components': 'ComponentsTests',
'Patterns': 'PatternsTests',
'Contexts': 'ContextsTests',
'Utilities': 'UtilitiesTests'
}
test_dir = test_subdirs.get(layer_dir)
if test_dir:
test_layer_path = tests_path / test_dir
if test_layer_path.exists():
test_files = list(test_layer_path.glob('*Tests.swift'))
test_loc = 0
test_actual_lines = 0
output_lines.append(f"\n Test Files:")
for test_file in sorted(test_files):
loc, actual_lines = count_lines_of_code(test_file)
test_loc += loc
test_actual_lines += actual_lines
output_lines.append(f" {test_file.name:38s} {loc:5d} LOC ({actual_lines:5d} total)")
output_lines.append(f"\n {'TEST TOTAL':40s} {test_loc:5d} LOC ({test_actual_lines:5d} total)")
# Calculate test/code ratio
if total_loc > 0:
ratio = (test_loc / total_loc) * 100
output_lines.append(f"\n Test/Code Ratio: {ratio:.1f}%")
output_lines.append(f" Coverage Estimate: {'GOOD' if ratio > 50 else 'NEEDS IMPROVEMENT'}")
results[layer_name] = {
'source_loc': total_loc,
'test_loc': test_loc if 'test_loc' in locals() else 0,
'files': len(source_files)
}
# Summary
output_lines.append(f"\n{'='*80}")
output_lines.append("SUMMARY")
output_lines.append(f"{'='*80}")
total_source_loc = sum(r['source_loc'] for r in results.values())
total_test_loc = sum(r['test_loc'] for r in results.values())
output_lines.append(f"\nTotal Source LOC: {total_source_loc:,}")
output_lines.append(f"Total Test LOC: {total_test_loc:,}")
# Calculate overall coverage ratio
overall_ratio = 0.0
if total_source_loc > 0:
overall_ratio = (total_test_loc / total_source_loc)
output_lines.append(f"Overall Test/Code Ratio: {overall_ratio*100:.1f}%")
output_lines.append(f"\n{'Layer':<20s} {'Source LOC':>12s} {'Test LOC':>12s} {'Ratio':>10s}")
output_lines.append(f"{'-'*60}")
for layer_name, data in results.items():
ratio = (data['test_loc'] / data['source_loc'] * 100) if data['source_loc'] > 0 else 0
output_lines.append(f"{layer_name:<20s} {data['source_loc']:>12,d} {data['test_loc']:>12,d} {ratio:>9.1f}%")
# Output report
report_text = '\n'.join(output_lines)
if args.report:
with open(args.report, 'w') as f:
f.write(report_text)
f.write('\n')
if args.verbose:
print(f"Report written to: {args.report}", file=sys.stderr)
else:
print(report_text)
# Check threshold if specified
if args.threshold is not None:
if overall_ratio >= args.threshold:
if args.verbose:
print(f"\n✅ Coverage {overall_ratio*100:.1f}% meets threshold {args.threshold*100:.1f}%", file=sys.stderr)
sys.exit(0)
else:
print(f"\n❌ Coverage FAILED: {overall_ratio*100:.1f}% below threshold {args.threshold*100:.1f}%", file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()