-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_github_stats.py
More file actions
466 lines (401 loc) · 15.1 KB
/
Copy pathtest_github_stats.py
File metadata and controls
466 lines (401 loc) · 15.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
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
import asyncio
import subprocess
import sys
import types
import unittest
from unittest import mock
sys.modules.setdefault(
"aiohttp",
types.SimpleNamespace(
ClientError=Exception,
ClientSession=object,
),
)
sys.modules.setdefault(
"requests",
types.SimpleNamespace(
get=lambda *args, **kwargs: None,
post=lambda *args, **kwargs: None,
),
)
from github_stats import Stats # noqa: E402
class _FakeResponse:
def __init__(self, status: int):
self.status = status
class _FakeSession:
async def get(self, *args, **kwargs):
return _FakeResponse(202)
class _FakeQueries:
def __init__(self, responses=None):
self.access_token = "token"
self.semaphore = asyncio.Semaphore(10)
self.session = _FakeSession()
self._responses = responses or {}
async def query_rest(self, path, params=None):
responses = self._responses.get(path, [])
if responses:
value = responses.pop(0)
if isinstance(value, tuple):
return value[1]
return value
return {}
async def query_rest_response(
self,
path,
params=None,
max_attempts=10,
retry_statuses=None,
verbose=True,
):
responses = self._responses.get(path, [])
if responses:
value = responses.pop(0)
if isinstance(value, tuple):
return value
return 200, value
return 200, {}
async def _run_immediately(func, *args, **kwargs):
return func(*args, **kwargs)
class StatsTests(unittest.IsolatedAsyncioTestCase):
async def test_fetch_lines_changed_uses_cached_login_when_username_missing(self):
stats = Stats(None, "token", None)
stats._login = "octocat"
stats.queries = _FakeQueries(
responses={
"/repos/owner/repo/stats/contributors": [
(
200,
[
{
"author": {"login": "octocat"},
"weeks": [{"a": 5, "d": 2}],
}
],
)
],
}
)
result = await stats._fetch_lines_changed("owner/repo")
self.assertEqual(result, (5, 2, "api"))
async def test_fetch_lines_changed_treats_no_content_as_zero_api_success(self):
stats = Stats("octocat", "token", None)
stats.queries = _FakeQueries(
responses={
"/repos/owner/repo/stats/contributors": [(204, {})],
}
)
result = await stats._fetch_lines_changed("owner/repo")
self.assertEqual(result, (0, 0, "api"))
async def test_lines_changed_falls_back_to_git_when_stats_api_never_finishes(self):
stats = Stats("octocat", "token", None)
stats._repos = {"owner/repo"}
stats.queries = _FakeQueries(
responses={
"/repos/owner/repo/stats/contributors": [(202, {})] * 10,
}
)
stats._get_lines_changed_from_git = mock.AsyncMock(
return_value=(7, 3, "git_fallback")
)
with mock.patch("github_stats.asyncio.sleep", new=mock.AsyncMock()):
result = await stats.lines_changed
self.assertEqual(result, (7, 3))
stats._get_lines_changed_from_git.assert_awaited_once_with("owner/repo")
async def test_lines_changed_summary_counts_api_fallback_and_failures(self):
stats = Stats("octocat", "token", None)
stats._repos = {"owner/api", "owner/fallback", "owner/fail"}
stats.queries = _FakeQueries(
responses={
"/repos/owner/api/stats/contributors": [
(
200,
[
{
"author": {"login": "octocat"},
"weeks": [{"a": 4, "d": 1}],
}
],
)
],
"/repos/owner/fallback/stats/contributors": [(202, {})] * 10,
"/repos/owner/fail/stats/contributors": [(500, {"message": "boom"})],
}
)
stats._get_lines_changed_from_git = mock.AsyncMock(
return_value=(3, 2, "git_fallback")
)
with mock.patch("github_stats.asyncio.sleep", new=mock.AsyncMock()):
result = await stats.lines_changed
summary = await stats.lines_changed_summary
self.assertEqual(result, (7, 3))
self.assertEqual(
summary,
{
"api_success": 1,
"git_fallback_success": 1,
"failed": 1,
"git_unavailable": 0,
"clone_failed": 0,
"git_log_failed": 0,
"other_api_error": 1,
},
)
async def test_lines_changed_summary_treats_no_content_as_api_success(self):
stats = Stats("octocat", "token", None)
stats._repos = {"owner/repo"}
stats.queries = _FakeQueries(
responses={
"/repos/owner/repo/stats/contributors": [(204, {})],
}
)
result = await stats.lines_changed
summary = await stats.lines_changed_summary
self.assertEqual(result, (0, 0))
self.assertEqual(
summary,
{
"api_success": 1,
"git_fallback_success": 0,
"failed": 0,
"git_unavailable": 0,
"clone_failed": 0,
"git_log_failed": 0,
"other_api_error": 0,
},
)
async def test_lines_changed_summary_redacts_repository_names(self):
stats = Stats("octocat", "token", None)
stats._lines_changed = (7, 3)
stats._lines_changed_summary = {
"api_success": 2,
"git_fallback_success": 1,
"failed": 1,
"git_unavailable": 0,
"clone_failed": 1,
"git_log_failed": 0,
"other_api_error": 0,
}
summary = await stats.lines_changed_summary_text
self.assertEqual(
summary,
"Lines changed sources: API 2 | git fallback 1 | failed 1",
)
self.assertNotIn("owner/api", summary)
self.assertNotIn("owner/fallback", summary)
async def test_lines_changed_failure_summary_text_only_reports_nonzero_causes(self):
stats = Stats("octocat", "token", None)
stats._lines_changed_summary = {
"api_success": 1,
"git_fallback_success": 0,
"failed": 3,
"git_unavailable": 1,
"clone_failed": 1,
"git_log_failed": 0,
"other_api_error": 1,
}
failure_summary = await stats.lines_changed_failure_summary_text
self.assertEqual(
failure_summary,
"Lines changed failure causes: "
"git unavailable 1 | clone failed 1 | other/api error 1",
)
async def test_lines_changed_summary_recovers_from_missing_summary_cache(self):
stats = Stats("octocat", "token", None)
stats._repos = {"owner/repo"}
stats._lines_changed = (99, 99)
stats._lines_changed_summary = None
stats._fetch_lines_changed = mock.AsyncMock(return_value=(2, 1, "api"))
summary = await stats.lines_changed_summary
self.assertEqual(
summary,
{
"api_success": 1,
"git_fallback_success": 0,
"failed": 0,
"git_unavailable": 0,
"clone_failed": 0,
"git_log_failed": 0,
"other_api_error": 0,
},
)
self.assertEqual(stats._lines_changed, (2, 1))
stats._fetch_lines_changed.assert_awaited_once_with("owner/repo")
async def test_recompute_lines_changed_cache_replaces_stale_values(self):
stats = Stats("octocat", "token", None)
stats._repos = {"owner/repo"}
stats._lines_changed = (99, 99)
stats._lines_changed_summary = {
"api_success": 0,
"git_fallback_success": 0,
"failed": 1,
"git_unavailable": 1,
"clone_failed": 0,
"git_log_failed": 0,
"other_api_error": 0,
}
stats._fetch_lines_changed = mock.AsyncMock(return_value=(4, 2, "api"))
await stats._recompute_lines_changed_cache()
self.assertEqual(stats._lines_changed, (4, 2))
self.assertEqual(
stats._lines_changed_summary,
{
"api_success": 1,
"git_fallback_success": 0,
"failed": 0,
"git_unavailable": 0,
"clone_failed": 0,
"git_log_failed": 0,
"other_api_error": 0,
},
)
stats._fetch_lines_changed.assert_awaited_once_with("owner/repo")
async def test_lines_changed_result_is_reused_after_first_calculation(self):
stats = Stats("octocat", "token", None)
stats._repos = {"owner/repo"}
stats._fetch_lines_changed = mock.AsyncMock(return_value=(1, 1, "api"))
first = await stats.lines_changed
summary = await stats.lines_changed_summary
second = await stats.lines_changed
self.assertEqual(first, (1, 1))
self.assertEqual(
summary,
{
"api_success": 1,
"git_fallback_success": 0,
"failed": 0,
"git_unavailable": 0,
"clone_failed": 0,
"git_log_failed": 0,
"other_api_error": 0,
},
)
self.assertEqual(second, (1, 1))
stats._fetch_lines_changed.assert_awaited_once_with("owner/repo")
async def test_get_user_emails_falls_back_to_noreply_address(self):
stats = Stats("octocat", "token", None)
stats.queries = _FakeQueries(
responses={
"/user/emails": [(403, {"message": "Forbidden"})],
}
)
result = await stats._get_user_emails()
self.assertEqual(result, ["octocat@users.noreply.github.com"])
async def test_git_fallback_sums_numstat_for_all_known_emails(self):
stats = Stats("octocat", "token", None)
with mock.patch.object(
stats,
"_get_user_emails",
new=mock.AsyncMock(return_value=["one@example.com", "two@example.com"]),
), mock.patch(
"github_stats.shutil.which", return_value="/usr/bin/git"
), mock.patch(
"github_stats.asyncio.to_thread", side_effect=_run_immediately
), mock.patch(
"github_stats.subprocess.run"
) as run_mock:
run_mock.side_effect = [
subprocess.CompletedProcess(
args=[], returncode=0, stdout="", stderr=""
),
subprocess.CompletedProcess(
args=[],
returncode=0,
stdout="5\t3\tfoo.py\n-\t4\tbin.dat\n2\t1\tbar.py\n",
stderr="",
),
]
result = await stats._get_lines_changed_from_git("owner/repo")
self.assertEqual(result, (7, 8, "git_fallback"))
log_command = run_mock.call_args_list[1].args[0]
self.assertEqual(log_command[:4], ["git", "-C", log_command[2], "log"])
self.assertEqual(log_command.count("--author"), 2)
self.assertIn("one@example.com", log_command)
self.assertIn("two@example.com", log_command)
async def test_git_fallback_uses_cached_login_when_username_missing(self):
stats = Stats(None, "token", None)
stats._login = "octocat"
with mock.patch.object(
stats,
"_get_user_emails",
new=mock.AsyncMock(return_value=["one@example.com"]),
), mock.patch(
"github_stats.shutil.which", return_value="/usr/bin/git"
), mock.patch(
"github_stats.asyncio.to_thread", side_effect=_run_immediately
), mock.patch(
"github_stats.subprocess.run"
) as run_mock:
run_mock.side_effect = [
subprocess.CompletedProcess(
args=[], returncode=0, stdout="", stderr=""
),
subprocess.CompletedProcess(
args=[],
returncode=0,
stdout="1\t1\tfoo.py\n",
stderr="",
),
]
result = await stats._get_lines_changed_from_git("owner/repo")
self.assertEqual(result, (1, 1, "git_fallback"))
clone_command = run_mock.call_args_list[0].args[0]
self.assertIn("https://octocat:token@github.com/owner/repo.git", clone_command)
async def test_git_fallback_returns_failed_when_git_is_unavailable(self):
stats = Stats("octocat", "token", None)
with mock.patch("github_stats.shutil.which", return_value=None):
result = await stats._get_lines_changed_from_git("owner/repo")
self.assertEqual(result, (0, 0, "git_unavailable"))
async def test_lines_changed_summary_counts_failed_git_fallback(self):
stats = Stats("octocat", "token", None)
stats._repos = {"owner/repo"}
stats.queries = _FakeQueries(
responses={
"/repos/owner/repo/stats/contributors": [(202, {})] * 10,
}
)
stats._get_lines_changed_from_git = mock.AsyncMock(
return_value=(0, 0, "failed")
)
with mock.patch("github_stats.asyncio.sleep", new=mock.AsyncMock()):
result = await stats.lines_changed
summary = await stats.lines_changed_summary
self.assertEqual(result, (0, 0))
self.assertEqual(
summary,
{
"api_success": 0,
"git_fallback_success": 0,
"failed": 1,
"git_unavailable": 0,
"clone_failed": 0,
"git_log_failed": 0,
"other_api_error": 1,
},
)
async def test_lines_changed_summary_counts_failed_git_unavailable(self):
stats = Stats("octocat", "token", None)
stats._repos = {"owner/repo"}
stats.queries = _FakeQueries(
responses={
"/repos/owner/repo/stats/contributors": [(202, {})] * 10,
}
)
stats._get_lines_changed_from_git = mock.AsyncMock(
return_value=(0, 0, "git_unavailable")
)
with mock.patch("github_stats.asyncio.sleep", new=mock.AsyncMock()):
summary = await stats.lines_changed_summary
self.assertEqual(
summary,
{
"api_success": 0,
"git_fallback_success": 0,
"failed": 1,
"git_unavailable": 1,
"clone_failed": 0,
"git_log_failed": 0,
"other_api_error": 0,
},
)
if __name__ == "__main__":
unittest.main()