Skip to content

Commit

Permalink
🐛 Fixes service_job_id KeyError with coveralls==2.2.0
Browse files Browse the repository at this point in the history
It seems like `coveralls==2.2.0` requires `service_job_id` to internally
resubmit payload on 422 Unprocessable Entry error, refs:
https://github.com/coveralls-clients/coveralls-python/pull/241/files#r532248047
  • Loading branch information
AndreMiras committed Nov 29, 2020
1 parent bb2d6b0 commit 50af55e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Change Log

## [Unreleased]
- Improves exception logging
- Fixes KeyError with `coveralls==2.2.0`
- Fixes entrypoint assertion error ([dfm](https://github.com/dfm)), refs #5
- Fixes parallel mode ([johanneswilm](https://github.com/johanneswilm)), refs #8
- Improves exception logging

## [v20200413]
- Made `github-token` parameter optional
Expand Down
7 changes: 6 additions & 1 deletion src/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ def run_coveralls(repo_token, parallel=False, flag_name=False, base_path=False):
# for some reasons the "service_name" can be one or the other
# (depending on where it's ran from?)
service_names = ("github", "github-actions")
# sets `service_job_id` key so it exists, refs:
# https://github.com/coveralls-clients/coveralls-python/pull/241/files#r532248047
service_job_id = None
result = None
if base_path and os.path.exists(base_path):
os.chdir(base_path)
for service_name in service_names:
log.info(f"Trying submitting coverage with service_name: {service_name}...")
with patch_os_environ(repo_token, parallel, flag_name):
coveralls = Coveralls(service_name=service_name)
coveralls = Coveralls(
service_name=service_name, service_job_id=service_job_id
)
try:
result = coveralls.wear()
break
Expand Down
43 changes: 38 additions & 5 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
from coveralls.api import CoverallsException
from requests.models import Response

import entrypoint

Expand All @@ -23,11 +24,12 @@ def patch_sys_argv(argv):
return mock.patch("sys.argv", argv)


def patch_requests_post(json_response=None):
new_mock = mock.Mock()
if json_response:
new_mock.return_value.json.return_value = json_response
return mock.patch("entrypoint.requests.post", new_mock)
def patch_requests_post(json_response=mock.Mock(), status_code=200):
response = Response()
response.status_code = status_code
response.json = lambda: json_response
m_post = mock.Mock(return_value=response)
return mock.patch("entrypoint.requests.post", m_post)


class TestEntryPoint:
Expand Down Expand Up @@ -147,6 +149,37 @@ def test_run_coveralls_wear_error_twice(self):
entrypoint.run_coveralls(repo_token="TOKEN")
assert ex_info.value.args == (entrypoint.ExitCode.FAILURE,)

def test_status_code_422(self):
"""
Makes sure the coveralls package retries on "422 Unprocessable Entry" error
rather than crashing while trying to access the `service_job_id` key, refs:
https://github.com/coveralls-clients/coveralls-python/pull/241/files#r532248047
"""
status_code = 422
with patch_requests_post(status_code=status_code) as m_post, pytest.raises(
SystemExit
), patch_log() as m_log:
entrypoint.run_coveralls(repo_token="TOKEN")
# coveralls package will retry once per service we call it with
assert m_post.call_count == 4
assert m_log.error.call_args_list == [
mock.call("Failed to submit coverage", exc_info=None)
]
assert m_log.warning.call_args_list == [
mock.call(
"Failed submitting coverage with service_name: github",
exc_info=CoverallsException(
"Could not submit coverage: 422 Client Error: None for url: None"
),
),
mock.call(
"Failed submitting coverage with service_name: github-actions",
exc_info=CoverallsException(
"Could not submit coverage: 422 Client Error: None for url: None"
),
),
]

def test_post_webhook(self):
"""
Tests different uses cases:
Expand Down

0 comments on commit 50af55e

Please sign in to comment.