Skip to content

Commit

Permalink
♻️ Coveralls config gets populated from kwargs
Browse files Browse the repository at this point in the history
Patching the env is not actually required as we can explicitly pass the
config as kwargs.
  • Loading branch information
AndreMiras committed Dec 1, 2020
1 parent d853282 commit 8ead214
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 51 deletions.
29 changes: 10 additions & 19 deletions src/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import sys
from contextlib import contextmanager
from enum import Enum
from unittest import mock

import requests
from coveralls.api import Coveralls, CoverallsException
Expand Down Expand Up @@ -48,20 +47,6 @@ def cd(newdir):
os.chdir(prevdir)


def patch_os_environ(repo_token, parallel, flag_name):
"""
Temporarily updates the environment variable to satisfy coveralls Python API.
That is because the coveralls package API consumes mostly environment variables.
"""
# https://github.com/coveralls-clients/coveralls-python/blob/2.0.0/coveralls/api.py#L146
parallel = "true" if parallel else ""
environ = {"COVERALLS_REPO_TOKEN": repo_token, "COVERALLS_PARALLEL": parallel}
if flag_name:
environ["COVERALLS_FLAG_NAME"] = flag_name
log.debug(f"Patching os.environ with: {environ}")
return mock.patch.dict("os.environ", environ)


def run_coveralls(
repo_token,
parallel=Default.PARALLEL,
Expand All @@ -78,13 +63,19 @@ def run_coveralls(
# sets `service_job_id` key so it exists, refs:
# https://github.com/coveralls-clients/coveralls-python/pull/241/files#r532248047
service_job_id = None
kwargs = {
"repo_token": repo_token,
"service_job_id": service_job_id,
}
if parallel:
kwargs.update({"parallel": parallel})
if flag_name is not None:
kwargs.update({"flag_name": flag_name})
result = None
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), cd(base_path):
coveralls = Coveralls(
service_name=service_name, service_job_id=service_job_id
)
with cd(base_path):
coveralls = Coveralls(service_name=service_name, **kwargs)
try:
result = coveralls.wear()
break
Expand Down
32 changes: 0 additions & 32 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ def test_run_coveralls_github_token(self):
assert json_file["repo_token"] == token
assert m_log.method_calls == [
mock.call.info("Trying submitting coverage with service_name: github..."),
mock.call.debug(
"Patching os.environ with: "
"{'COVERALLS_REPO_TOKEN': 'TOKEN', 'COVERALLS_PARALLEL': ''}"
),
mock.call.info("cd ."),
mock.call.info(mock.ANY),
mock.call.debug(json_response),
Expand All @@ -136,10 +132,6 @@ def test_run_coveralls_parallel(self):
assert json_file["parallel"] == parallel
assert m_log.method_calls == [
mock.call.info("Trying submitting coverage with service_name: github..."),
mock.call.debug(
"Patching os.environ with: "
"{'COVERALLS_REPO_TOKEN': 'TOKEN', 'COVERALLS_PARALLEL': 'true'}"
),
mock.call.info("cd ."),
mock.call.info(mock.ANY),
mock.call.debug(json_response),
Expand All @@ -155,10 +147,6 @@ def test_run_coveralls_parallel(self):
assert "parallel" not in json_file
assert m_log.method_calls == [
mock.call.info("Trying submitting coverage with service_name: github..."),
mock.call.debug(
"Patching os.environ with: "
"{'COVERALLS_REPO_TOKEN': 'TOKEN', 'COVERALLS_PARALLEL': ''}"
),
mock.call.info("cd ."),
mock.call.info(mock.ANY),
mock.call.debug(json_response),
Expand All @@ -183,14 +171,6 @@ def test_run_coveralls_flag_name(self):
assert json_file["flag_name"] == flag_name
assert m_log.method_calls == [
mock.call.info("Trying submitting coverage with service_name: github..."),
mock.call.debug(
"Patching os.environ with: "
"{"
"'COVERALLS_REPO_TOKEN': 'TOKEN', "
"'COVERALLS_PARALLEL': '', "
"'COVERALLS_FLAG_NAME': 'Unit'"
"}"
),
mock.call.info("cd ."),
mock.call.info(mock.ANY),
mock.call.debug(json_response),
Expand All @@ -206,10 +186,6 @@ def test_run_coveralls_flag_name(self):
assert "flag_name" not in json_file
assert m_log.method_calls == [
mock.call.info("Trying submitting coverage with service_name: github..."),
mock.call.debug(
"Patching os.environ with: "
"{'COVERALLS_REPO_TOKEN': 'TOKEN', 'COVERALLS_PARALLEL': ''}"
),
mock.call.info("cd ."),
mock.call.info(mock.ANY),
mock.call.debug(json_response),
Expand All @@ -229,10 +205,6 @@ def test_run_coveralls_wear_error_once(self):
assert m_wear.call_args_list == [mock.call(), mock.call()]
assert m_log.method_calls == [
mock.call.info("Trying submitting coverage with service_name: github..."),
mock.call.debug(
"Patching os.environ with: "
"{'COVERALLS_REPO_TOKEN': 'TOKEN', 'COVERALLS_PARALLEL': ''}"
),
mock.call.info("cd ."),
mock.call.warning(
"Failed submitting coverage with service_name: github",
Expand All @@ -242,10 +214,6 @@ def test_run_coveralls_wear_error_once(self):
mock.call.info(
"Trying submitting coverage with service_name: github-actions..."
),
mock.call.debug(
"Patching os.environ with: "
"{'COVERALLS_REPO_TOKEN': 'TOKEN', 'COVERALLS_PARALLEL': ''}"
),
mock.call.info("cd ."),
mock.call.info(mock.ANY),
mock.call.debug(side_effect[1]),
Expand Down

0 comments on commit 8ead214

Please sign in to comment.