-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_cleanowners.py
More file actions
474 lines (387 loc) · 18.6 KB
/
test_cleanowners.py
File metadata and controls
474 lines (387 loc) · 18.6 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
470
471
472
473
474
"""Test the functions in the cleanowners module."""
import unittest
import uuid
from io import StringIO
from unittest.mock import MagicMock, patch
import github3
from cleanowners import (
build_default_codeowners,
cleanup_whitespace,
commit_changes,
get_codeowners_file,
get_org,
get_repos_iterator,
get_usernames_from_codeowners,
print_stats,
remove_username_from_content,
)
class TestCommitChanges(unittest.TestCase):
"""Test the commit_changes function in cleanowners.py"""
@patch("uuid.uuid4")
def test_commit_changes(self, mock_uuid):
"""Test the commit_changes function."""
mock_uuid.return_value = uuid.UUID(
"12345678123456781234567812345678"
) # Mock UUID generation
mock_repo = MagicMock() # Mock repo object
mock_repo.default_branch = "main"
mock_repo.ref.return_value.object.sha = "abc123" # Mock SHA for latest commit
mock_repo.create_ref.return_value = True
mock_repo.file_contents.return_value = MagicMock()
mock_repo.file_contents.update.return_value = True
mock_repo.create_pull.return_value = "MockPullRequest"
title = "Test Title"
body = "Test Body"
dependabot_file = "testing!"
branch_name = "codeowners-12345678-1234-5678-1234-567812345678"
commit_message = "Test commit message"
result = commit_changes(
title,
body,
mock_repo,
dependabot_file,
commit_message,
"CODEOWNERS",
)
# Assert that the methods were called with the correct arguments
mock_repo.create_ref.assert_called_once_with(
f"refs/heads/{branch_name}", "abc123"
)
mock_repo.file_contents.assert_called_once_with("CODEOWNERS")
mock_repo.create_pull.assert_called_once_with(
title=title,
body=body,
head=branch_name,
base="main",
)
# Assert that the function returned the expected result
self.assertEqual(result, "MockPullRequest")
@patch("uuid.uuid4")
def test_commit_changes_create_new_file(self, mock_uuid):
"""Test the commit_changes function when creating a new file."""
mock_uuid.return_value = uuid.UUID("12345678123456781234567812345678")
mock_repo = MagicMock()
mock_repo.default_branch = "main"
mock_repo.ref.return_value.object.sha = "abc123"
mock_repo.create_ref.return_value = True
mock_repo.create_file.return_value = True
mock_repo.create_pull.return_value = "MockPullRequest"
result = commit_changes(
"Test Title",
"Test Body",
mock_repo,
b"new content",
"Test commit message",
"CODEOWNERS",
create_new=True,
)
branch_name = "codeowners-12345678-1234-5678-1234-567812345678"
mock_repo.create_ref.assert_called_once_with(
f"refs/heads/{branch_name}", "abc123"
)
mock_repo.create_file.assert_called_once_with(
"CODEOWNERS",
"Test commit message",
b"new content",
branch=branch_name,
)
mock_repo.file_contents.assert_not_called()
self.assertEqual(result, "MockPullRequest")
class TestGetUsernamesFromCodeowners(unittest.TestCase):
"""Test the get_usernames_from_codeowners function in cleanowners.py"""
def test_get_usernames_from_codeowners_ignore_teams(self):
"""Test the get_usernames_from_codeowners function."""
codeowners_file_contents = """
# Comment
@user1
@user2
@org/team
# Another comment
@user3 @user4
""".encode("ASCII")
expected_usernames = ["user1", "user2", "user3", "user4"]
result = get_usernames_from_codeowners(codeowners_file_contents)
self.assertEqual(result, expected_usernames)
def test_get_usernames_from_codeowners_with_teams(self):
"""Test the get_usernames_from_codeowners function."""
codeowners_file_contents = """
# Comment
@user1
@user2
@org/team
# Another comment
@user3 @user4
""".encode("ASCII")
expected_usernames = ["user1", "user2", "org/team", "user3", "user4"]
result = get_usernames_from_codeowners(codeowners_file_contents, False)
self.assertEqual(result, expected_usernames)
def test_get_usernames_from_codeowners_with_raw_bytes(self):
"""Test that get_usernames_from_codeowners works with raw bytes (large file path).
Regression test for https://github.com/github-community-projects/cleanowners/issues/378
When a CODEOWNERS file is large, blob().decode_content() returns raw bytes
instead of a Contents object with a .decoded attribute.
"""
codeowners_file_contents = b"* @user1 @user2\ndocs/* @user3\n"
expected_usernames = ["user1", "user2", "user3"]
result = get_usernames_from_codeowners(codeowners_file_contents)
self.assertEqual(result, expected_usernames)
def test_multiple_username_removals_are_cumulative(self):
"""Test that removing multiple usernames preserves all removals.
Regression test for https://github.com/github-community-projects/cleanowners/issues/380
The removal loop must accumulate changes rather than replacing from the
original content each time, otherwise only the last removal survives.
"""
codeowners_decoded = b"* @alice @bob @charlie\ndocs/* @alice\n"
usernames_to_remove = ["alice", "bob"]
codeowners_file_contents_new = codeowners_decoded
changed_lines: set[int] = set()
for username in usernames_to_remove:
codeowners_file_contents_new = remove_username_from_content(
codeowners_file_contents_new, username, changed_lines
)
codeowners_file_contents_new = cleanup_whitespace(
codeowners_file_contents_new, changed_lines
)
remaining = get_usernames_from_codeowners(codeowners_file_contents_new)
self.assertEqual(remaining, ["charlie"])
self.assertNotIn(b"@alice", codeowners_file_contents_new)
self.assertNotIn(b"@bob", codeowners_file_contents_new)
def test_username_removal_does_not_corrupt_similar_names(self):
"""Test that removing @bob does not corrupt @bobsmith.
The removal must use word-boundary matching so that @bob only matches
the exact handle, not as a prefix of @bobsmith.
"""
codeowners_decoded = b"* @bobsmith @bob @charlie\n"
usernames_to_remove = ["bob"]
codeowners_file_contents_new = codeowners_decoded
changed_lines: set[int] = set()
for username in usernames_to_remove:
codeowners_file_contents_new = remove_username_from_content(
codeowners_file_contents_new, username, changed_lines
)
codeowners_file_contents_new = cleanup_whitespace(
codeowners_file_contents_new, changed_lines
)
remaining = get_usernames_from_codeowners(codeowners_file_contents_new)
self.assertEqual(remaining, ["bobsmith", "charlie"])
self.assertIn(b"@bobsmith", codeowners_file_contents_new)
self.assertNotIn(b"@bob ", codeowners_file_contents_new)
def test_username_removal_cleans_up_whitespace(self):
"""Test that removing usernames does not leave extra whitespace.
After removing a username from between two others, the resulting
double space should be collapsed to a single space, and trailing
whitespace should be stripped.
"""
codeowners_decoded = b"* @alice @bob @charlie\n"
usernames_to_remove = ["bob"]
codeowners_file_contents_new = codeowners_decoded
changed_lines: set[int] = set()
for username in usernames_to_remove:
codeowners_file_contents_new = remove_username_from_content(
codeowners_file_contents_new, username, changed_lines
)
codeowners_file_contents_new = cleanup_whitespace(
codeowners_file_contents_new, changed_lines
)
self.assertEqual(codeowners_file_contents_new, b"* @alice @charlie\n")
def test_username_removal_handles_crlf_line_endings(self):
"""Test that whitespace cleanup works with CRLF line endings.
Windows-style line endings use \\r\\n. The trailing whitespace
cleanup must strip spaces before \\r without consuming the \\r itself.
"""
codeowners_decoded = b"* @alice @bob @charlie\r\n"
usernames_to_remove = ["bob"]
codeowners_file_contents_new = codeowners_decoded
changed_lines: set[int] = set()
for username in usernames_to_remove:
codeowners_file_contents_new = remove_username_from_content(
codeowners_file_contents_new, username, changed_lines
)
codeowners_file_contents_new = cleanup_whitespace(
codeowners_file_contents_new, changed_lines
)
self.assertEqual(codeowners_file_contents_new, b"* @alice @charlie\r\n")
def test_whitespace_cleanup_scoped_to_changed_lines(self):
"""Test that whitespace cleanup only affects lines where usernames were removed.
Lines with intentional alignment spacing should not be modified
if no username was removed from them.
"""
codeowners_decoded = b"src/** @alice @bob @charlie\ndocs/** @dave\n"
usernames_to_remove = ["bob"]
codeowners_file_contents_new = codeowners_decoded
changed_lines: set[int] = set()
for username in usernames_to_remove:
codeowners_file_contents_new = remove_username_from_content(
codeowners_file_contents_new, username, changed_lines
)
codeowners_file_contents_new = cleanup_whitespace(
codeowners_file_contents_new, changed_lines
)
# src line should be normalized (removal happened there)
self.assertIn(b"src/** @alice @charlie", codeowners_file_contents_new)
# docs line should be untouched (no removal happened there)
self.assertIn(b"docs/** @dave", codeowners_file_contents_new)
class TestGetOrganization(unittest.TestCase):
"""Test the get_org function in cleanowners.py"""
@patch("github3.login")
def test_get_organization_succeeds(self, mock_github):
"""Test the organization is valid."""
organization = "my_organization"
github_connection = mock_github.return_value
mock_organization = MagicMock()
github_connection.organization.return_value = mock_organization
result = get_org(github_connection, organization)
github_connection.organization.assert_called_once_with(organization)
self.assertEqual(result, mock_organization)
@patch("github3.login")
def test_get_organization_fails(self, mock_github):
"""Test the organization is not valid."""
organization = "my_organization"
github_connection = mock_github.return_value
github_connection.organization.side_effect = github3.exceptions.NotFoundError(
resp=MagicMock(status_code=404)
)
result = get_org(github_connection, organization)
github_connection.organization.assert_called_once_with(organization)
self.assertIsNone(result)
class TestGetReposIterator(unittest.TestCase):
"""Test the get_repos_iterator function in evergreen.py"""
@patch("github3.login")
def test_get_repos_iterator_with_organization(self, mock_github):
"""Test the get_repos_iterator function with an organization"""
organization = "my_organization"
repository_list = []
github_connection = mock_github.return_value
mock_organization = MagicMock()
mock_repositories = MagicMock()
mock_organization.repositories.return_value = mock_repositories
github_connection.organization.return_value = mock_organization
result = get_repos_iterator(organization, repository_list, github_connection)
# Assert that the organization method was called with the correct argument
github_connection.organization.assert_called_once_with(organization)
# Assert that the repositories method was called on the organization object
mock_organization.repositories.assert_called_once()
# Assert that the function returned the expected result
self.assertEqual(result, mock_repositories)
@patch("github3.login")
def test_get_repos_iterator_with_repository_list(self, mock_github):
"""Test the get_repos_iterator function with a repository list"""
organization = None
repository_list = ["org/repo1", "org2/repo2"]
github_connection = mock_github.return_value
mock_repository = MagicMock()
mock_repository_list = [mock_repository, mock_repository]
github_connection.repository.side_effect = mock_repository_list
result = get_repos_iterator(organization, repository_list, github_connection)
# Assert that the repository method was called with the correct arguments for each repository in the list
expected_calls = [
unittest.mock.call("org", "repo1"),
unittest.mock.call("org2", "repo2"),
]
github_connection.repository.assert_has_calls(expected_calls)
# Assert that the function returned the expected result
self.assertEqual(result, mock_repository_list)
class TestPrintStats(unittest.TestCase):
"""Test the print_stats function in cleanowners.py"""
@patch("sys.stdout", new_callable=StringIO)
def test_print_stats_all_counts(self, mock_stdout):
"""Test the print_stats function with all counts."""
print_stats(5, 10, 2, 3, 4)
expected_output = (
"Found 4 users to remove\n"
"Created 5 pull requests successfully\n"
"Found 2 repositories missing or empty CODEOWNERS files\n"
"Processed 3 repositories with a CODEOWNERS file\n"
"50.0% of eligible repositories had pull requests created\n"
"60.0% of repositories had CODEOWNERS files\n"
)
self.assertEqual(mock_stdout.getvalue(), expected_output)
@patch("sys.stdout", new_callable=StringIO)
def test_print_stats_no_pull_requests_needed(self, mock_stdout):
"""Test the print_stats function with no pull requests needed."""
print_stats(0, 0, 2, 3, 4)
expected_output = (
"Found 4 users to remove\n"
"Created 0 pull requests successfully\n"
"Found 2 repositories missing or empty CODEOWNERS files\n"
"Processed 3 repositories with a CODEOWNERS file\n"
"No pull requests were needed\n"
"60.0% of repositories had CODEOWNERS files\n"
)
self.assertEqual(mock_stdout.getvalue(), expected_output)
@patch("sys.stdout", new_callable=StringIO)
def test_print_stats_no_repositories_processed(self, mock_stdout):
"""Test the print_stats function with no repositories processed."""
print_stats(0, 0, 0, 0, 0)
expected_output = (
"Found 0 users to remove\n"
"Created 0 pull requests successfully\n"
"Found 0 repositories missing or empty CODEOWNERS files\n"
"Processed 0 repositories with a CODEOWNERS file\n"
"No pull requests were needed\n"
"No repositories were processed\n"
)
self.assertEqual(mock_stdout.getvalue(), expected_output)
class TestGetCodeownersFile(unittest.TestCase):
"""Test the get_codeowners_file function in cleanowners.py"""
def setUp(self):
self.repo = MagicMock()
def test_codeowners_in_github_folder(self):
"""Test that a CODEOWNERS file in the .github folder is considered valid."""
self.repo.file_contents.side_effect = lambda path: (
MagicMock(size=1) if path == ".github/CODEOWNERS" else None
)
contents, path = get_codeowners_file(self.repo)
self.assertIsNotNone(contents)
self.assertEqual(path, ".github/CODEOWNERS")
def test_codeowners_in_root(self):
"""Test that a CODEOWNERS file in the root is considered valid."""
self.repo.file_contents.side_effect = lambda path: (
MagicMock(size=1) if path == "CODEOWNERS" else None
)
contents, path = get_codeowners_file(self.repo)
self.assertIsNotNone(contents)
self.assertEqual(path, "CODEOWNERS")
def test_codeowners_in_docs_folder(self):
"""Test that a CODEOWNERS file in a docs folder is considered valid."""
self.repo.file_contents.side_effect = lambda path: (
MagicMock(size=1) if path == "docs/CODEOWNERS" else None
)
contents, path = get_codeowners_file(self.repo)
self.assertIsNotNone(contents)
self.assertEqual(path, "docs/CODEOWNERS")
def test_codeowners_not_found(self):
"""Test that a missing CODEOWNERS file is not considered valid because it doesn't exist."""
self.repo.file_contents.side_effect = lambda path: None
contents, path = get_codeowners_file(self.repo)
self.assertIsNone(contents)
self.assertIsNone(path)
def test_codeowners_empty_file(self):
"""Test that an empty CODEOWNERS file is returned for further handling."""
self.repo.file_contents.side_effect = lambda path: MagicMock(size=0)
contents, path = get_codeowners_file(self.repo)
self.assertIsNotNone(contents)
self.assertEqual(path, ".github/CODEOWNERS")
def test_codeowners_not_found_then_found(self):
"""Test that a later path is used when earlier ones are not found."""
not_found = github3.exceptions.NotFoundError(resp=MagicMock(status_code=404))
self.repo.file_contents.side_effect = [not_found, MagicMock(size=1)]
contents, path = get_codeowners_file(self.repo)
self.assertIsNotNone(contents)
self.assertEqual(path, "CODEOWNERS")
class TestBuildDefaultCodeowners(unittest.TestCase):
"""Test the build_default_codeowners function in cleanowners.py"""
def test_build_default_codeowners_for_org(self):
"""Test placeholder uses org team handle."""
repo = MagicMock()
repo.owner.login = "my-org"
repo.owner.type = "Organization"
result = build_default_codeowners(repo)
self.assertIn(b"@my-org/REPLACE_WITH_TEAM", result)
def test_build_default_codeowners_for_user(self):
"""Test placeholder uses user handle."""
repo = MagicMock()
repo.owner.login = "my-user"
repo.owner.type = "User"
result = build_default_codeowners(repo)
self.assertIn(b"@my-user", result)