-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_markdown_writer.py
More file actions
469 lines (439 loc) · 20.3 KB
/
test_markdown_writer.py
File metadata and controls
469 lines (439 loc) · 20.3 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
"""Unit tests for the write_to_markdown and write_step_summary functions in markdown_writer.py"""
import os
import unittest
from unittest.mock import call, mock_open, patch
from markdown_writer import write_step_summary, write_to_markdown
class TestWriteToMarkdown(unittest.TestCase):
"""Test the write_to_markdown function"""
def test_write_with_all_counts_and_no_users_to_remove(self):
"""Test that the function writes the correct markdown when there are no users to remove"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_to_markdown(0, 0, 2, 3, {}, [])
mock_file().write.assert_called_once_with(
"# Cleanowners Report\n\n"
"## Overall Stats\n"
"0 Users to Remove\n"
"0 Pull Requests created\n"
"2 Repositories missing or empty CODEOWNERS files\n"
"3 Repositories with CODEOWNERS file\n"
)
def test_write_with_repos_and_users_with_users_to_remove(self):
"""Test that the function writes the correct markdown when there are users to remove"""
mock_file = mock_open()
repo_users = {"repo1": ["user1", "user2"], "repo2": ["user3"]}
with patch("builtins.open", mock_file):
write_to_markdown(1, 2, 3, 4, repo_users, [])
calls = [
call(
"# Cleanowners Report\n\n"
"## Overall Stats\n"
"1 Users to Remove\n"
"2 Pull Requests created\n"
"3 Repositories missing or empty CODEOWNERS files\n"
"4 Repositories with CODEOWNERS file\n"
),
call("## Repositories and Users to Remove\n"),
call("repo1\n"),
call("- user1\n"),
call("- user2\n"),
call("\n"),
call("repo2\n"),
call("- user3\n"),
call("\n"),
]
mock_file().write.assert_has_calls(calls, any_order=False)
def test_write_with_repos_missing_codeowners(self):
"""Test that the function writes the correct markdown when there are repos missing CODEOWNERS"""
mock_file = mock_open()
repos_missing_codeowners = ["repo1", "repo2"]
with patch("builtins.open", mock_file):
write_to_markdown(0, 0, 2, 0, {}, repos_missing_codeowners)
calls = [
call(
"# Cleanowners Report\n\n"
"## Overall Stats\n"
"0 Users to Remove\n"
"0 Pull Requests created\n"
"2 Repositories missing or empty CODEOWNERS files\n"
"0 Repositories with CODEOWNERS file\n"
),
call("## Repositories Missing or Empty CODEOWNERS\n"),
call("- repo1\n"),
call("- repo2\n"),
call("\n"),
]
mock_file().write.assert_has_calls(calls, any_order=False)
def test_write_with_empty_inputs(self):
"""Test that the function writes the correct markdown when all inputs are 0"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_to_markdown(0, 0, 0, 0, {}, [])
mock_file().write.assert_called_once_with(
"# Cleanowners Report\n\n"
"## Overall Stats\n"
"0 Users to Remove\n"
"0 Pull Requests created\n"
"0 Repositories missing or empty CODEOWNERS files\n"
"0 Repositories with CODEOWNERS file\n"
)
class TestWriteStepSummary(unittest.TestCase):
"""Test the write_step_summary function"""
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_writes_basic_stats(self):
"""Test that basic stats are written correctly"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=0,
eligble_for_pr_count=0,
no_codeowners_count=2,
codeowners_count=3,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
enable_github_actions_step_summary=True,
)
mock_file.assert_called_once_with(
"/tmp/test_summary.md", "a", encoding="utf-8"
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn("# Cleanowners Report", written)
self.assertIn("Found 0 users to remove", written)
self.assertIn("Created 0 pull requests successfully", written)
self.assertIn(
"Found 2 repositories missing or empty CODEOWNERS files", written
)
self.assertIn("Processed 3 repositories with a CODEOWNERS file", written)
self.assertIn("No pull requests were needed", written)
self.assertIn("60.0% of repositories had CODEOWNERS files", written)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_writes_pr_percentage(self):
"""Test that PR percentage is calculated correctly"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=5,
eligble_for_pr_count=10,
no_codeowners_count=2,
codeowners_count=3,
users_count=4,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn(
"50.0% of eligible repositories had pull requests created", written
)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_writes_no_repositories_processed(self):
"""Test output when no repositories were processed"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=0,
eligble_for_pr_count=0,
no_codeowners_count=0,
codeowners_count=0,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn("No pull requests were needed", written)
self.assertIn("No repositories were processed", written)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_writes_users_to_remove(self):
"""Test that users to remove section is included"""
mock_file = mock_open()
repo_users = {"org/repo1": ["user1", "user2"], "org/repo2": ["user3"]}
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=2,
eligble_for_pr_count=2,
no_codeowners_count=0,
codeowners_count=2,
users_count=3,
repo_and_users_to_remove=repo_users,
repos_missing_codeowners=[],
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn("## Repositories and Users to Remove", written)
self.assertIn("org/repo1", written)
self.assertIn("- user1", written)
self.assertIn("- user2", written)
self.assertIn("org/repo2", written)
self.assertIn("- user3", written)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_writes_repos_missing_codeowners(self):
"""Test that repos missing CODEOWNERS section is included"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=0,
eligble_for_pr_count=0,
no_codeowners_count=2,
codeowners_count=3,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=["org/repo1", "org/repo2"],
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn("## Repositories Missing or Empty CODEOWNERS", written)
self.assertIn("- org/repo1", written)
self.assertIn("- org/repo2", written)
@patch.dict(os.environ, {}, clear=False)
def test_noop_when_env_var_not_set(self):
"""Test that nothing happens when GITHUB_STEP_SUMMARY is not set"""
# Remove the env var if it exists
with patch.dict(os.environ, {}, clear=False):
os.environ.pop("GITHUB_STEP_SUMMARY", None)
mock_file = mock_open()
with (
patch("builtins.open", mock_file),
patch("builtins.print") as mock_print,
):
write_step_summary(
pull_count=0,
eligble_for_pr_count=0,
no_codeowners_count=0,
codeowners_count=0,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
enable_github_actions_step_summary=True,
)
mock_file.assert_not_called()
mock_print.assert_called_once_with(
"GITHUB_STEP_SUMMARY not set, skipping step summary"
)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_noop_when_not_enabled(self):
"""Test that nothing happens when enable_github_actions_step_summary is False"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=0,
eligble_for_pr_count=0,
no_codeowners_count=0,
codeowners_count=0,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
enable_github_actions_step_summary=False,
)
mock_file.assert_not_called()
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_writes_error_when_provided(self):
"""Test that an error section is included when an error occurred"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=1,
eligble_for_pr_count=2,
no_codeowners_count=0,
codeowners_count=3,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
error="API rate limit exceeded",
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn("## Error", written)
self.assertIn("API rate limit exceeded", written)
# Stats should still be present (partial results)
self.assertIn("Processed 3 repositories with a CODEOWNERS file", written)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_no_error_section_when_none(self):
"""Test that no error section appears when error is None"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=0,
eligble_for_pr_count=0,
no_codeowners_count=0,
codeowners_count=3,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
error=None,
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertNotIn("## Error", written)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_writes_pull_request_urls(self):
"""Test that PR URLs are included as clickable links"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=2,
eligble_for_pr_count=2,
no_codeowners_count=0,
codeowners_count=2,
users_count=1,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
pull_request_urls=[
"https://github.com/org/repo1/pull/42",
"https://github.com/org/repo2/pull/99",
],
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn("## Pull Requests Created", written)
self.assertIn("https://github.com/org/repo1/pull/42", written)
self.assertIn("https://github.com/org/repo2/pull/99", written)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_no_pull_requests_section_when_empty(self):
"""Test that no PR section appears when no PRs were created"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=0,
eligble_for_pr_count=0,
no_codeowners_count=0,
codeowners_count=3,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
pull_request_urls=[],
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertNotIn("## Pull Requests Created", written)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_check_mark_when_all_clean(self):
"""Test that check mark emoji appears on Overall Stats when no issues found"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=0,
eligble_for_pr_count=0,
no_codeowners_count=0,
codeowners_count=5,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn("## Overall Stats :white_check_mark:", written)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_warning_on_overall_stats_when_issues_found(self):
"""Test that warning emoji appears on Overall Stats when there are issues"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=0,
eligble_for_pr_count=0,
no_codeowners_count=2,
codeowners_count=3,
users_count=1,
repo_and_users_to_remove={"org/repo1": ["user1"]},
repos_missing_codeowners=[],
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertNotIn(":white_check_mark:", written)
self.assertIn("## Overall Stats :warning:", written)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_warning_on_users_to_remove_without_error(self):
"""Test that warning emoji appears on users to remove section when no error"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=1,
eligble_for_pr_count=1,
no_codeowners_count=0,
codeowners_count=2,
users_count=2,
repo_and_users_to_remove={"org/repo1": ["user1", "user2"]},
repos_missing_codeowners=[],
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn("## Repositories and Users to Remove :warning:", written)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_warning_on_missing_codeowners_without_error(self):
"""Test that warning emoji appears on missing codeowners section when no error"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=0,
eligble_for_pr_count=0,
no_codeowners_count=2,
codeowners_count=3,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=["org/repo1", "org/repo2"],
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn(
"## Repositories Missing or Empty CODEOWNERS :warning:", written
)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_no_warning_on_sections_with_error(self):
"""Test that warning emoji does not appear on detail sections when there is an error"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=1,
eligble_for_pr_count=2,
no_codeowners_count=1,
codeowners_count=3,
users_count=1,
repo_and_users_to_remove={"org/repo1": ["user1"]},
repos_missing_codeowners=["org/repo2"],
error="API rate limit exceeded",
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertNotIn("## Repositories and Users to Remove :warning:", written)
self.assertNotIn(
"## Repositories Missing or Empty CODEOWNERS :warning:", written
)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_link_on_pull_requests_created(self):
"""Test that link emoji appears on Pull Requests Created section"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=1,
eligble_for_pr_count=1,
no_codeowners_count=0,
codeowners_count=1,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
pull_request_urls=["https://github.com/org/repo1/pull/42"],
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn("## Pull Requests Created :link:", written)
@patch.dict(os.environ, {"GITHUB_STEP_SUMMARY": "/tmp/test_summary.md"})
def test_x_on_error(self):
"""Test that x emoji appears on Error section"""
mock_file = mock_open()
with patch("builtins.open", mock_file):
write_step_summary(
pull_count=0,
eligble_for_pr_count=0,
no_codeowners_count=0,
codeowners_count=1,
users_count=0,
repo_and_users_to_remove={},
repos_missing_codeowners=[],
error="Something went wrong",
enable_github_actions_step_summary=True,
)
written = "".join(c.args[0] for c in mock_file().write.call_args_list)
self.assertIn("## Error :x:", written)
if __name__ == "__main__":
unittest.main()