Skip to content

Commit

Permalink
test: test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ideepu committed Apr 13, 2024
1 parent b7b9484 commit b4e5570
Show file tree
Hide file tree
Showing 15 changed files with 2,204 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ lint:
pipenv run pre-commit run --all-files

test:
pipenv run pytest tests/* --cov
pipenv run pytest tests/* --cov=codecov --cov-report=term-missing

report:
pipenv run pytest tests --cov-branch --cov=codecov --cov-report=json:/tmp/report.json
pipenv run pytest tests --cov-branch --cov=codecov --cov-report=term-missing --cov-report=json:/tmp/report.json

build:
python3 -m build
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,5 @@ For example, if your project has a run.py file, you can run it using the followi
That's it! You have successfully set up your local environment using Pipenv.

This project is inspired by the concepts of [py-cov-action/python-coverage-comment-action](https://github.com/py-cov-action/python-coverage-comment-action.git).
This project is almost copy of [py-cov-action/python-coverage-comment-action]
(<https://github.com/py-cov-action/python-coverage-comment-action.git>) with few modifications.
4 changes: 2 additions & 2 deletions codecov/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from codecov import github_client, groups, log, settings

GITHUB_ACTIONS_LOGIN = 'CI-codecov[bot]'
GITHUB_CODECOV_LOGIN = 'CI-codecov[bot]'


class CannotDeterminePR(Exception):
Expand Down Expand Up @@ -83,7 +83,7 @@ def get_my_login(github: github_client.GitHub) -> str:
# The GitHub actions user cannot access its own details
# and I'm not sure there's a way to see that we're using
# the GitHub actions user except noting that it fails
return GITHUB_ACTIONS_LOGIN
return GITHUB_CODECOV_LOGIN

return response.login

Expand Down
6 changes: 2 additions & 4 deletions codecov/github_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import annotations

Expand All @@ -9,7 +8,7 @@


class _Executable:
def __init__(self, _gh, _method, _path):
def __init__(self, _gh: GitHub, _method: str, _path: str):
self._gh = _gh
self._method = _method
self._path = _path
Expand Down Expand Up @@ -37,7 +36,6 @@ def __getattr__(self, attr):


class GitHub:

"""
GitHub client.
"""
Expand All @@ -48,7 +46,7 @@ def __init__(self, session: httpx.Client):
def __getattr__(self, attr):
return _Callable(self, f'/{attr}')

def _http(self, method, path, *, use_bytes=False, use_text=False, **kw):
def _http(self, method: str, path: str, *, use_bytes: bool = False, use_text: bool = False, **kw):
_method = method.lower()
requests_kwargs = {}
headers = kw.pop('headers', {})
Expand Down
17 changes: 7 additions & 10 deletions codecov/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ class InvalidAnnotationType(Exception):


def path_below(path_str: str | pathlib.Path) -> pathlib.Path:
try:
path = pathlib.Path(path_str).resolve()
if not (path.exists() and path.is_file()):
raise ValueError('Path does not exist')

if path.suffix != '.json':
raise ValueError('The file is not a JSON file.')
return path
except ValueError as exc:
raise ValueError('Path can not be resolved') from exc
path = pathlib.Path(path_str).resolve()
if not (path.exists() and path.is_file()):
raise ValueError('Path does not exist')

if path.suffix != '.json':
raise ValueError('The file is not a JSON file.')
return path


def str_to_bool(value: str) -> bool:
Expand Down
35 changes: 4 additions & 31 deletions codecov/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,14 @@
import hashlib
import itertools
import pathlib
from collections.abc import Callable
from importlib import resources

import jinja2
from jinja2.sandbox import SandboxedEnvironment

from codecov import badge, coverage as coverage_module, diff_grouper

MARKER = """<!-- This comment was generated by CI codecov {id_part} -->"""


def uptodate():
return True


class CommentLoader(jinja2.BaseLoader):
def __init__(self, base_template: str):
self.base_template = base_template

# fmt: off
def get_source(self, environment: jinja2.Environment, template: str) -> tuple[str, str | None, Callable[..., bool]]: # pylint: disable=unused-argument
# fmt: on
if template == 'base':
return (
self.base_template,
'codecov/template_files/comment.md.j2',
uptodate,
)

raise jinja2.TemplateNotFound(template)
MARKER = """<!-- This comment was generated by CI codecov{id_part} -->"""


class MissingMarker(Exception):
Expand All @@ -58,10 +36,6 @@ def pluralize(number, singular='', plural='s'):
return plural


def sign(val: int | decimal.Decimal) -> str:
return '+' if val > 0 else '' if val < 0 else '±'


def remove_exponent(val: decimal.Decimal) -> decimal.Decimal:
# From https://docs.python.org/3/library/decimal.html#decimal-faq
return val.quantize(decimal.Decimal(1)) if val == val.to_integral() else val.normalize()
Expand All @@ -71,7 +45,7 @@ def percentage_value(val: decimal.Decimal, precision: int = 2) -> decimal.Decima
return remove_exponent(
(decimal.Decimal('100') * val).quantize(
decimal.Decimal('1.' + ('0' * precision)),
rounding=decimal.ROUND_CEILING,
rounding=decimal.ROUND_DOWN,
)
)

Expand Down Expand Up @@ -112,8 +86,7 @@ def get_comment_markdown( # pylint: disable=too-many-arguments,too-many-locals
complete_project_report: bool = False,
coverage_report_url: str | None = None,
):
loader = CommentLoader(base_template=base_template)
env = SandboxedEnvironment(loader=loader)
env = SandboxedEnvironment()
env.filters['pct'] = pct
env.filters['x100'] = x100
env.filters['generate_badge'] = badge.get_static_badge_url
Expand Down Expand Up @@ -143,7 +116,7 @@ def get_comment_markdown( # pylint: disable=too-many-arguments,too-many-locals
)
}
try:
comment = env.get_template('base').render(
comment = env.from_string(base_template).render(
coverage=coverage,
diff_coverage=diff_coverage,
max_files=max_files,
Expand Down
Loading

0 comments on commit b4e5570

Please sign in to comment.