Repository navigation
Expand file tree
/
Copy pathmodule-05-validation-script.py
More file actions
372 lines (310 loc) · 12.1 KB
/
Copy pathmodule-05-validation-script.py
File metadata and controls
372 lines (310 loc) · 12.1 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python3
"""
Module 05 Exercise Validation Script
Validates completion of documentation and code quality exercises
"""
import ast
import sys
from pathlib import Path
from typing import Dict, List, Tuple, Optional
import subprocess
import json
import importlib.util
class ExerciseValidator:
"""Validate Module 05 exercise completion."""
def __init__(self):
self.results = {
"exercise1": {"passed": 0, "total": 0, "details": []},
"exercise2": {"passed": 0, "total": 0, "details": []},
"exercise3": {"passed": 0, "total": 0, "details": []},
}
def validate_all(self) -> bool:
"""Run all validations and return overall pass/fail."""
print("🔍 Module 05 Exercise Validator")
print("=" * 50)
# Check Exercise 1: Documentation Generator
print("\n📝 Exercise 1: Documentation Generator")
self.validate_exercise1()
# Check Exercise 2: Refactoring Assistant
print("\n🔨 Exercise 2: Refactoring Assistant")
self.validate_exercise2()
# Check Exercise 3: Quality Automation System
print("\n📊 Exercise 3: Quality Automation System")
self.validate_exercise3()
# Summary
self.print_summary()
# Return True if all exercises pass
return all(
ex["passed"] == ex["total"]
for ex in self.results.values()
)
def validate_exercise1(self):
"""Validate Documentation Generator exercise."""
exercise_path = Path("exercises/exercise1-easy/solution")
# Check 1: Main file exists
main_file = exercise_path / "doc_generator.py"
self.check(
"exercise1",
"Main file exists",
main_file.exists()
)
if not main_file.exists():
return
# Check 2: Can parse the file
try:
tree = ast.parse(main_file.read_text())
self.check("exercise1", "Valid Python syntax", True)
except SyntaxError:
self.check("exercise1", "Valid Python syntax", False)
return
# Check 3: Required classes exist
classes = [node.name for node in ast.walk(tree) if isinstance(node, ast.ClassDef)]
self.check(
"exercise1",
"DocumentationGenerator class exists",
"DocumentationGenerator" in classes
)
# Check 4: Required methods exist
if "DocumentationGenerator" in classes:
methods = self.get_class_methods(tree, "DocumentationGenerator")
required_methods = [
"parse_file",
"extract_functions",
"generate_docstring",
"generate_readme"
]
for method in required_methods:
self.check(
"exercise1",
f"Method {method} exists",
method in methods
)
# Check 5: Tests exist
test_file = exercise_path / "tests" / "test_doc_generator.py"
self.check(
"exercise1",
"Test file exists",
test_file.exists()
)
# Check 6: Can import and run basic test
try:
sys.path.insert(0, str(exercise_path))
from doc_generator import DocumentationGenerator, DocStyle
# Test instantiation
gen = DocumentationGenerator(style=DocStyle.GOOGLE)
self.check("exercise1", "Can instantiate DocumentationGenerator", True)
# Test basic functionality
test_code = '''
def hello(name: str) -> str:
return f"Hello, {name}!"
'''
tree = ast.parse(test_code)
functions = gen.extract_functions(tree)
self.check(
"exercise1",
"Can extract functions",
len(functions) > 0
)
except Exception as e:
self.check(
"exercise1",
"Can import and run",
False,
f"Error: {str(e)}"
)
finally:
sys.path.pop(0)
def validate_exercise2(self):
"""Validate Refactoring Assistant exercise."""
exercise_path = Path("exercises/exercise2-medium/solution")
# Check 1: Main file exists
main_file = exercise_path / "refactoring_assistant.py"
self.check(
"exercise2",
"Main file exists",
main_file.exists()
)
if not main_file.exists():
return
# Check 2: Required classes and enums
try:
tree = ast.parse(main_file.read_text())
classes = [node.name for node in ast.walk(tree) if isinstance(node, ast.ClassDef)]
self.check(
"exercise2",
"RefactoringAssistant class exists",
"RefactoringAssistant" in classes
)
self.check(
"exercise2",
"SmellType enum exists",
"SmellType" in classes
)
self.check(
"exercise2",
"CodeSmell class exists",
"CodeSmell" in classes
)
except SyntaxError:
self.check("exercise2", "Valid Python syntax", False)
return
# Check 3: Smell detection methods
if "RefactoringAssistant" in classes:
methods = self.get_class_methods(tree, "RefactoringAssistant")
smell_methods = [
"_detect_long_methods",
"_detect_long_parameter_lists",
"_detect_duplicate_code",
"_detect_complex_conditionals"
]
for method in smell_methods:
self.check(
"exercise2",
f"Smell detector {method} exists",
method in methods
)
# Check 4: Refactoring implementations
refactoring_dir = exercise_path / "refactorings"
self.check(
"exercise2",
"Refactorings directory exists",
refactoring_dir.exists()
)
if refactoring_dir.exists():
refactoring_files = list(refactoring_dir.glob("*.py"))
self.check(
"exercise2",
"At least 3 refactoring implementations",
len(refactoring_files) >= 3
)
def validate_exercise3(self):
"""Validate Quality Automation System exercise."""
exercise_path = Path("exercises/exercise3-hard/solution")
# Check 1: Main system file
main_file = exercise_path / "quality_system.py"
self.check(
"exercise3",
"Main system file exists",
main_file.exists()
)
if not main_file.exists():
return
# Check 2: Component directories
components = ["monitors", "reporters", "integrations", "dashboards"]
for component in components:
component_dir = exercise_path / component
self.check(
"exercise3",
f"{component} directory exists",
component_dir.exists()
)
# Check 3: Monitor implementations
monitors_dir = exercise_path / "monitors"
if monitors_dir.exists():
monitor_files = list(monitors_dir.glob("*_monitor.py"))
self.check(
"exercise3",
"At least 4 quality monitors",
len(monitor_files) >= 4
)
# Check 4: CI/CD integration
ci_file = exercise_path / "integrations" / "github_actions.py"
self.check(
"exercise3",
"GitHub Actions integration exists",
ci_file.exists()
)
# Check 5: Dashboard implementation
dashboard_file = exercise_path / "dashboards" / "quality_dashboard.py"
self.check(
"exercise3",
"Dashboard implementation exists",
dashboard_file.exists()
)
# Check 6: Configuration support
config_file = exercise_path / "config" / "quality_config.py"
self.check(
"exercise3",
"Configuration system exists",
config_file.exists() or (exercise_path / "config.py").exists()
)
# Check 7: Docker deployment
docker_file = exercise_path.parent / "deployment" / "docker-compose.yml"
self.check(
"exercise3",
"Docker deployment configuration",
docker_file.exists() or (exercise_path / "Dockerfile").exists()
)
def check(self, exercise: str, test_name: str, passed: bool, details: str = ""):
"""Record a test result."""
self.results[exercise]["total"] += 1
if passed:
self.results[exercise]["passed"] += 1
print(f" ✅ {test_name}")
else:
print(f" ❌ {test_name}")
if details:
print(f" {details}")
self.results[exercise]["details"].append({
"test": test_name,
"passed": passed,
"details": details
})
def get_class_methods(self, tree: ast.Module, class_name: str) -> List[str]:
"""Extract method names from a class."""
methods = []
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef) and node.name == class_name:
for item in node.body:
if isinstance(item, ast.FunctionDef):
methods.append(item.name)
return methods
def print_summary(self):
"""Print validation summary."""
print("\n" + "=" * 50)
print("📊 VALIDATION SUMMARY")
print("=" * 50)
total_passed = 0
total_tests = 0
for exercise, results in self.results.items():
passed = results["passed"]
total = results["total"]
total_passed += passed
total_tests += total
percentage = (passed / total * 100) if total > 0 else 0
status = "✅ PASS" if passed == total else "❌ FAIL"
print(f"\n{exercise.upper()}:")
print(f" Status: {status}")
print(f" Passed: {passed}/{total} ({percentage:.1f}%)")
print("\n" + "-" * 50)
overall_percentage = (total_passed / total_tests * 100) if total_tests > 0 else 0
overall_status = "✅ ALL EXERCISES COMPLETE!" if total_passed == total_tests else "❌ INCOMPLETE"
print(f"OVERALL: {overall_status}")
print(f"Total: {total_passed}/{total_tests} ({overall_percentage:.1f}%)")
if total_passed < total_tests:
print("\n💡 Tips:")
print("- Review the failed checks above")
print("- Ensure all files are in the correct locations")
print("- Check that all required methods are implemented")
print("- Run tests locally to debug issues")
def export_results(self, filename: str = "validation_results.json"):
"""Export results to JSON file."""
with open(filename, 'w') as f:
json.dump(self.results, f, indent=2)
print(f"\n📄 Results exported to {filename}")
def main():
"""Run the validation script."""
validator = ExerciseValidator()
# Check if we're in the right directory
if not Path("exercises").exists():
print("❌ Error: Must run from module-05 directory")
print(" Current directory:", Path.cwd())
sys.exit(1)
# Run validation
all_passed = validator.validate_all()
# Export results
validator.export_results()
# Exit with appropriate code
sys.exit(0 if all_passed else 1)
if __name__ == "__main__":
main()