33
44import pytest
55from coveralls .api import CoverallsException
6+ from requests .models import Response
67
78import entrypoint
89
@@ -23,11 +24,12 @@ def patch_sys_argv(argv):
2324 return mock .patch ("sys.argv" , argv )
2425
2526
26- def patch_requests_post (json_response = None ):
27- new_mock = mock .Mock ()
28- if json_response :
29- new_mock .return_value .json .return_value = json_response
30- return mock .patch ("entrypoint.requests.post" , new_mock )
27+ def patch_requests_post (json_response = mock .Mock (), status_code = 200 ):
28+ response = Response ()
29+ response .status_code = status_code
30+ response .json = lambda : json_response
31+ m_post = mock .Mock (return_value = response )
32+ return mock .patch ("entrypoint.requests.post" , m_post )
3133
3234
3335class TestEntryPoint :
@@ -147,6 +149,37 @@ def test_run_coveralls_wear_error_twice(self):
147149 entrypoint .run_coveralls (repo_token = "TOKEN" )
148150 assert ex_info .value .args == (entrypoint .ExitCode .FAILURE ,)
149151
152+ def test_status_code_422 (self ):
153+ """
154+ Makes sure the coveralls package retries on "422 Unprocessable Entry" error
155+ rather than crashing while trying to access the `service_job_id` key, refs:
156+ https://github.com/coveralls-clients/coveralls-python/pull/241/files#r532248047
157+ """
158+ status_code = 422
159+ with patch_requests_post (status_code = status_code ) as m_post , pytest .raises (
160+ SystemExit
161+ ), patch_log () as m_log :
162+ entrypoint .run_coveralls (repo_token = "TOKEN" )
163+ # coveralls package will retry once per service we call it with
164+ assert m_post .call_count == 4
165+ assert m_log .error .call_args_list == [
166+ mock .call ("Failed to submit coverage" , exc_info = None )
167+ ]
168+ assert m_log .warning .call_args_list == [
169+ mock .call (
170+ "Failed submitting coverage with service_name: github" ,
171+ exc_info = CoverallsException (
172+ "Could not submit coverage: 422 Client Error: None for url: None"
173+ ),
174+ ),
175+ mock .call (
176+ "Failed submitting coverage with service_name: github-actions" ,
177+ exc_info = CoverallsException (
178+ "Could not submit coverage: 422 Client Error: None for url: None"
179+ ),
180+ ),
181+ ]
182+
150183 def test_post_webhook (self ):
151184 """
152185 Tests different uses cases:
0 commit comments