-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.py
More file actions
279 lines (216 loc) · 8.58 KB
/
integration_test.py
File metadata and controls
279 lines (216 loc) · 8.58 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
#!/usr/bin/env python3
"""
Comprehensive integration test for the static analysis workflow automation.
This test creates temporary repositories with different configurations and
verifies the automation handles them correctly.
"""
import sys
import tempfile
from pathlib import Path
from test_utils import run_command
def create_test_repo(path, branch_name="main"):
"""Create a test git repository."""
path.mkdir(parents=True, exist_ok=True)
# Initialize git repo
run_command("git init", cwd=path)
run_command("git config user.email 'test@example.com'", cwd=path)
run_command("git config user.name 'Test User'", cwd=path)
# Create initial commit
readme = path / "README.md"
readme.write_text("# Test Repository\n")
run_command("git add README.md", cwd=path)
run_command("git commit -m 'Initial commit'", cwd=path)
# Get the actual initial branch name (could be master or main)
returncode, stdout, stderr = run_command("git rev-parse --abbrev-ref HEAD", cwd=path)
current_branch = stdout.strip()
# Rename branch if needed
if branch_name != current_branch:
run_command(f"git branch -m {current_branch} {branch_name}", cwd=path)
return path
def test_basic_workflow_creation():
"""Test creating workflow in a fresh repository."""
print("\nTest: Basic workflow creation")
print("-" * 80)
with tempfile.TemporaryDirectory() as tmpdir:
repo_path = Path(tmpdir) / "test-repo"
create_test_repo(repo_path)
# Run the script
script_path = Path(__file__).parent / "add-static-analysis-workflow.py"
returncode, stdout, stderr = run_command(
[sys.executable, str(script_path), str(repo_path)]
)
if returncode != 0:
print(f"FAIL: Script failed: {stderr}")
return False
# Verify workflow file exists
workflow_file = repo_path / ".github" / "workflows" / "static-analysis.yaml"
if not workflow_file.exists():
print("FAIL: Workflow file not created")
return False
# Verify content
content = workflow_file.read_text()
if "static-analysis:" not in content or "main" not in content:
print("FAIL: Workflow content incorrect")
return False
print("PASS: Basic workflow creation works")
return True
def test_custom_branch():
"""Test with a repository using a non-main branch."""
print("\nTest: Custom branch detection")
print("-" * 80)
with tempfile.TemporaryDirectory() as tmpdir:
repo_path = Path(tmpdir) / "test-repo"
create_test_repo(repo_path, branch_name="develop")
# Run the script
script_path = Path(__file__).parent / "add-static-analysis-workflow.py"
returncode, stdout, stderr = run_command(
[sys.executable, str(script_path), str(repo_path)]
)
if returncode != 0:
print(f"FAIL: Script failed: {stderr}")
return False
# Verify workflow uses correct branch
workflow_file = repo_path / ".github" / "workflows" / "static-analysis.yaml"
content = workflow_file.read_text()
if "develop" not in content:
print(f"FAIL: Workflow doesn't use correct branch. Content:\n{content}")
return False
print("PASS: Custom branch detection works")
return True
def test_idempotency():
"""Test that running twice doesn't cause issues."""
print("\nTest: Idempotency")
print("-" * 80)
with tempfile.TemporaryDirectory() as tmpdir:
repo_path = Path(tmpdir) / "test-repo"
create_test_repo(repo_path)
script_path = Path(__file__).parent / "add-static-analysis-workflow.py"
# Run once
returncode1, stdout1, stderr1 = run_command(
[sys.executable, str(script_path), str(repo_path)]
)
if returncode1 != 0:
print(f"FAIL: First run failed: {stderr1}")
return False
# Get original content
workflow_file = repo_path / ".github" / "workflows" / "static-analysis.yaml"
original_content = workflow_file.read_text()
# Run again
returncode2, stdout2, stderr2 = run_command(
[sys.executable, str(script_path), str(repo_path)]
)
if returncode2 != 0:
print(f"FAIL: Second run failed: {stderr2}")
return False
# Verify content unchanged
new_content = workflow_file.read_text()
if original_content != new_content:
print("FAIL: Content changed on second run")
return False
# Verify it was skipped
if "skipped" not in stdout2.lower():
print("FAIL: Second run didn't skip")
return False
print("PASS: Idempotency works")
return True
def test_batch_processing():
"""Test batch processing multiple repositories."""
print("\nTest: Batch processing")
print("-" * 80)
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)
# Create multiple repos
repos = []
for i in range(3):
repo_path = tmpdir / f"repo{i}"
create_test_repo(repo_path)
repos.append(repo_path)
# Create repos file
repos_file = tmpdir / "repos.txt"
repos_file.write_text("\n".join(str(r) for r in repos))
# Run batch script
script_path = Path(__file__).parent / "batch-add-workflows.py"
output_file = tmpdir / "batch-report.json"
returncode, stdout, stderr = run_command([
sys.executable,
str(script_path),
"--repo-file", str(repos_file),
"--output", str(output_file)
])
if returncode != 0:
print(f"FAIL: Batch script failed: {stderr}")
return False
# Verify all repos have workflow
for repo in repos:
workflow_file = repo / ".github" / "workflows" / "static-analysis.yaml"
if not workflow_file.exists():
print(f"FAIL: Workflow not created in {repo}")
return False
# Verify report exists
if not output_file.exists():
print("FAIL: Batch report not created")
return False
print("PASS: Batch processing works")
return True
def test_yaml_validation():
"""Test that YAML validation prevents invalid files."""
print("\nTest: YAML validation")
print("-" * 80)
with tempfile.TemporaryDirectory() as tmpdir:
repo_path = Path(tmpdir) / "test-repo"
create_test_repo(repo_path)
# Run the script
script_path = Path(__file__).parent / "add-static-analysis-workflow.py"
returncode, stdout, stderr = run_command(
[sys.executable, str(script_path), str(repo_path)]
)
if returncode != 0:
print(f"FAIL: Script failed: {stderr}")
return False
# Verify YAML is valid
workflow_file = repo_path / ".github" / "workflows" / "static-analysis.yaml"
try:
import yaml
with open(workflow_file, 'r') as f:
yaml.safe_load(f)
print("PASS: YAML validation works")
return True
except yaml.YAMLError as e:
print(f"FAIL: Generated YAML is invalid: {e}")
return False
def main():
"""Run all integration tests."""
print("=" * 80)
print("Integration Tests for Static Analysis Workflow Automation")
print("=" * 80)
tests = [
test_basic_workflow_creation,
test_custom_branch,
test_idempotency,
test_batch_processing,
test_yaml_validation,
]
results = []
for test in tests:
try:
result = test()
results.append(result)
except Exception as e:
print(f"FAIL: Exception: {e}")
import traceback
traceback.print_exc()
results.append(False)
print("\n" + "=" * 80)
print("INTEGRATION TEST SUMMARY")
print("=" * 80)
passed = sum(results)
total = len(results)
print(f"Passed: {passed}/{total}")
if passed == total:
print("\n✓ All integration tests passed!")
return 0
else:
print("\n✗ Some integration tests failed")
return 1
if __name__ == "__main__":
sys.exit(main())