22
33import runpy
44import unittest
5- from unittest .mock import MagicMock , patch
5+ from unittest .mock import MagicMock , call , patch
66
77import contributors as contributors_module
88from contributor_stats import ContributorStats
9- from contributors import get_all_contributors , get_contributors
109
1110
1211class TestContributors (unittest .TestCase ):
@@ -27,7 +26,7 @@ def test_get_contributors(self, mock_contributor_stats):
2726 mock_repo .contributors .return_value = [mock_user ]
2827 mock_repo .full_name = "owner/repo"
2928
30- get_contributors (mock_repo , "2022-01-01" , "2022-12-31" , "" )
29+ contributors_module . get_contributors (mock_repo , "2022-01-01" , "2022-12-31" , "" )
3130
3231 mock_contributor_stats .assert_called_once_with (
3332 "user" ,
@@ -60,8 +59,8 @@ def test_get_all_contributors_with_organization(self, mock_get_contributors):
6059 ]
6160 ghe = ""
6261
63- result = get_all_contributors (
64- "org" , "" , "2022-01-01" , "2022-12-31" , mock_github_connection , ghe
62+ result = contributors_module . get_all_contributors (
63+ "org" , [] , "2022-01-01" , "2022-12-31" , mock_github_connection , ghe
6564 )
6665
6766 self .assertEqual (
@@ -99,7 +98,7 @@ def test_get_all_contributors_with_repository(self, mock_get_contributors):
9998 ]
10099 ghe = ""
101100
102- result = get_all_contributors (
101+ result = contributors_module . get_all_contributors (
103102 "" , ["owner/repo" ], "2022-01-01" , "2022-12-31" , mock_github_connection , ghe
104103 )
105104
@@ -135,14 +134,22 @@ def test_get_contributors_skip_users_with_no_commits(self, mock_contributor_stat
135134 mock_user2 .avatar_url = "https://avatars.githubusercontent.com/u/12345679?v=4"
136135 mock_user2 .contributions_count = 102
137136
138- mock_repo .contributors .return_value = [mock_user ]
137+ mock_repo .contributors .return_value = [mock_user , mock_user2 ]
139138 mock_repo .full_name = "owner/repo"
140- mock_repo .get_commits .side_effect = StopIteration
139+ mock_repo .commits .side_effect = [
140+ iter ([object ()]), # user has commits in range
141+ iter ([]), # user2 has no commits in range and should be skipped
142+ ]
141143 ghe = ""
142144
143- get_contributors (mock_repo , "2022-01-01" , "2022-12-31" , ghe )
145+ contributors_module . get_contributors (mock_repo , "2022-01-01" , "2022-12-31" , ghe )
144146
145- # Note that only user is returned and user2 is not returned here because there were no commits in the date range
147+ mock_repo .commits .assert_has_calls (
148+ [
149+ call (author = "user" , since = "2022-01-01" , until = "2022-12-31" ),
150+ call (author = "user2" , since = "2022-01-01" , until = "2022-12-31" ),
151+ ]
152+ )
146153 mock_contributor_stats .assert_called_once_with (
147154 "user" ,
148155 False ,
@@ -168,10 +175,10 @@ def test_get_contributors_skip_bot(self, mock_contributor_stats):
168175 mock_repo .get_commits .side_effect = StopIteration
169176 ghe = ""
170177
171- get_contributors (mock_repo , "2022-01-01" , "2022-12-31" , ghe )
178+ contributors_module . get_contributors (mock_repo , "2022-01-01" , "2022-12-31" , ghe )
172179
173- # Note that only user is returned and user2 is not returned here because there were no commits in the date range
174- mock_contributor_stats .isEmpty ()
180+ # Ensure that the bot user is skipped and ContributorStats is never instantiated
181+ mock_contributor_stats .assert_not_called ()
175182
176183 @patch ("contributors.contributor_stats.ContributorStats" )
177184 def test_get_contributors_no_commit_end_date (self , mock_contributor_stats ):
@@ -189,7 +196,7 @@ def test_get_contributors_no_commit_end_date(self, mock_contributor_stats):
189196 mock_repo .get_commits .side_effect = StopIteration
190197 ghe = ""
191198
192- get_contributors (mock_repo , "2022-01-01" , "" , ghe )
199+ contributors_module . get_contributors (mock_repo , "2022-01-01" , "" , ghe )
193200
194201 # Note that only user is returned and user2 is not returned here because there were no commits in the date range
195202 mock_contributor_stats .assert_called_once_with (
@@ -212,7 +219,9 @@ def test_get_contributors_skips_when_no_commits_in_range(self):
212219 mock_repo .full_name = "owner/repo"
213220 mock_repo .commits .return_value = iter ([])
214221
215- result = get_contributors (mock_repo , "2022-01-01" , "2022-12-31" , "" )
222+ result = contributors_module .get_contributors (
223+ mock_repo , "2022-01-01" , "2022-12-31" , ""
224+ )
216225
217226 self .assertEqual (result , [])
218227
@@ -230,7 +239,9 @@ def __iter__(self):
230239 mock_repo .contributors .return_value = BoomIterable ()
231240
232241 with patch ("builtins.print" ) as mock_print :
233- result = get_contributors (mock_repo , "2022-01-01" , "2022-12-31" , "" )
242+ result = contributors_module .get_contributors (
243+ mock_repo , "2022-01-01" , "2022-12-31" , ""
244+ )
234245
235246 self .assertIsNone (result )
236247 mock_print .assert_any_call (
0 commit comments