-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrun_tests.py
More file actions
247 lines (199 loc) · 7.25 KB
/
run_tests.py
File metadata and controls
247 lines (199 loc) · 7.25 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
#!/usr/bin/env python3
"""
Test runner for the drainage library.
This script provides a convenient way to run all tests for the drainage library,
including both Rust and Python tests.
"""
import sys
import os
import subprocess
import argparse
from pathlib import Path
def run_command(command, cwd=None, capture_output=False):
"""Run a command and return the result."""
try:
result = subprocess.run(
command,
shell=True,
cwd=cwd,
capture_output=capture_output,
text=True,
check=True
)
return result
except subprocess.CalledProcessError as e:
print(f"Command failed: {command}")
print(f"Error: {e}")
if e.stdout:
print(f"Stdout: {e.stdout}")
if e.stderr:
print(f"Stderr: {e.stderr}")
return None
def run_rust_tests():
"""Run Rust unit tests."""
print("Running Rust unit tests...")
print("=" * 50)
result = run_command("cargo test", capture_output=True)
if result is None:
print("❌ Rust tests failed")
return False
print("✅ Rust tests passed")
print(f"Output: {result.stdout}")
return True
def run_python_tests():
"""Run Python tests."""
print("Running Python tests...")
print("=" * 50)
# Check if pytest is available
try:
import pytest
except ImportError:
print("❌ pytest not available. Installing...")
result = run_command("pip install pytest pytest-mock", capture_output=True)
if result is None:
print("❌ Failed to install pytest")
return False
# Run pytest
result = run_command("python -m pytest tests/ -v", capture_output=True)
if result is None:
print("❌ Python tests failed")
return False
print("✅ Python tests passed")
print(f"Output: {result.stdout}")
return True
def run_integration_tests():
"""Run integration tests."""
print("Running integration tests...")
print("=" * 50)
# Check if drainage module is available
try:
import drainage
print("✅ drainage module is available")
except ImportError:
print("❌ drainage module not available. Building...")
result = run_command("maturin develop", capture_output=True)
if result is None:
print("❌ Failed to build drainage module")
return False
# Run integration tests
result = run_command("python -m pytest tests/ -m integration -v", capture_output=True)
if result is None:
print("❌ Integration tests failed")
return False
print("✅ Integration tests passed")
return True
def run_example_tests():
"""Run example tests."""
print("Running example tests...")
print("=" * 50)
examples_dir = Path("examples")
if not examples_dir.exists():
print("❌ Examples directory not found")
return False
# Test each example script
example_scripts = list(examples_dir.glob("*.py"))
if not example_scripts:
print("❌ No example scripts found")
return False
for script in example_scripts:
print(f"Testing {script.name}...")
# Test that the script can be imported and has a main function
try:
with open(script, 'r') as f:
content = f.read()
if 'def main(' in content or 'if __name__' in content:
print(f"✅ {script.name} has proper structure")
else:
print(f"⚠️ {script.name} may not have proper structure")
except Exception as e:
print(f"❌ Error reading {script.name}: {e}")
return False
print("✅ Example tests passed")
return True
def run_linting():
"""Run linting checks."""
print("Running linting checks...")
print("=" * 50)
# Check Rust linting
print("Checking Rust code...")
result = run_command("cargo clippy -- -D warnings", capture_output=True)
if result is None:
print("❌ Rust linting failed")
return False
print("✅ Rust code passed linting")
# Check Python linting (if flake8 is available)
try:
import flake8
print("Checking Python code...")
result = run_command("flake8 tests/ examples/ --max-line-length=100", capture_output=True)
if result is None:
print("❌ Python linting failed")
return False
print("✅ Python code passed linting")
except ImportError:
print("⚠️ flake8 not available, skipping Python linting")
return True
def run_formatting():
"""Run formatting checks."""
print("Running formatting checks...")
print("=" * 50)
# Check Rust formatting
print("Checking Rust formatting...")
result = run_command("cargo fmt -- --check", capture_output=True)
if result is None:
print("❌ Rust formatting failed")
return False
print("✅ Rust code is properly formatted")
# Check Python formatting (if black is available)
try:
import black
print("Checking Python formatting...")
result = run_command("black --check tests/ examples/", capture_output=True)
if result is None:
print("❌ Python formatting failed")
return False
print("✅ Python code is properly formatted")
except ImportError:
print("⚠️ black not available, skipping Python formatting check")
return True
def main():
"""Main test runner function."""
parser = argparse.ArgumentParser(description="Run tests for the drainage library")
parser.add_argument("--rust", action="store_true", help="Run only Rust tests")
parser.add_argument("--python", action="store_true", help="Run only Python tests")
parser.add_argument("--integration", action="store_true", help="Run only integration tests")
parser.add_argument("--examples", action="store_true", help="Run only example tests")
parser.add_argument("--lint", action="store_true", help="Run only linting checks")
parser.add_argument("--format", action="store_true", help="Run only formatting checks")
parser.add_argument("--all", action="store_true", help="Run all tests and checks")
args = parser.parse_args()
# If no specific tests are requested, run all
if not any([args.rust, args.python, args.integration, args.examples, args.lint, args.format]):
args.all = True
success = True
if args.all or args.rust:
if not run_rust_tests():
success = False
if args.all or args.python:
if not run_python_tests():
success = False
if args.all or args.integration:
if not run_integration_tests():
success = False
if args.all or args.examples:
if not run_example_tests():
success = False
if args.all or args.lint:
if not run_linting():
success = False
if args.all or args.format:
if not run_formatting():
success = False
if success:
print("\n🎉 All tests passed!")
sys.exit(0)
else:
print("\n❌ Some tests failed!")
sys.exit(1)
if __name__ == "__main__":
main()