From ef00b5078ae8d51a05c5992c18809a98421aed89 Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Mon, 12 Jun 2023 16:30:06 +0200 Subject: [PATCH 01/16] build: ignore cpython files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1c6ff859..967cd489 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .DS_Store .coverage dev-env-vars +*.pyc \ No newline at end of file From 3272ddd73ba5ee337b9ecf929d57b989cee59892 Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Mon, 12 Jun 2023 16:42:22 +0200 Subject: [PATCH 02/16] feat: annotations use diff coverage --- coverage_comment/annotations.py | 10 ++++++---- coverage_comment/main.py | 9 ++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/coverage_comment/annotations.py b/coverage_comment/annotations.py index 9b06524c..080028ca 100644 --- a/coverage_comment/annotations.py +++ b/coverage_comment/annotations.py @@ -4,15 +4,17 @@ MISSING_LINES_GROUP_TITLE = "Annotations of lines with missing coverage" -def create_pr_annotations(annotation_type: str, coverage: coverage_module.Coverage): +def create_pr_annotations(annotation_type: str, coverage: coverage_module.DiffCoverage): github.send_workflow_command( command="group", command_value=MISSING_LINES_GROUP_TITLE ) - for filename, file_coverage in coverage.files.items(): - for missing_line in file_coverage.missing_lines: + for filepath, file_coverage in coverage.files.items(): + for missing_line in file_coverage.violation_lines: github.create_missing_coverage_annotation( - annotation_type=annotation_type, file=filename, line=missing_line + annotation_type=annotation_type, + file=filepath.name, + line=missing_line, ) github.send_workflow_command(command="endgroup", command_value="") diff --git a/coverage_comment/main.py b/coverage_comment/main.py index e9f481b7..f68332ca 100644 --- a/coverage_comment/main.py +++ b/coverage_comment/main.py @@ -5,9 +5,10 @@ import httpx -from coverage_comment import annotations, comment_file, communication -from coverage_comment import coverage as coverage_module from coverage_comment import ( + annotations, + comment_file, + communication, files, github, github_client, @@ -18,6 +19,7 @@ subprocess, template, ) +from coverage_comment import coverage as coverage_module def main(): @@ -75,8 +77,9 @@ def action( coverage = coverage_module.get_coverage_info(merge=config.MERGE_COVERAGE_FILES) if event_name == "pull_request": if config.ANNOTATE_MISSING_LINES: + diff_coverage = coverage_module.get_diff_coverage_info(base_ref=config.GITHUB_BASE_REF) annotations.create_pr_annotations( - annotation_type=config.ANNOTATION_TYPE, coverage=coverage + annotation_type=config.ANNOTATION_TYPE, coverage=diff_coverage ) return generate_comment( From 715af48ec3570cdea02a851e29bc2de5a331956a Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Mon, 12 Jun 2023 16:42:34 +0200 Subject: [PATCH 03/16] build: expand gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 967cd489..873c347a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ .DS_Store .coverage dev-env-vars -*.pyc \ No newline at end of file +*.pyc +.venv +.vscode/settings.json From 8bb0cce1b2b3590e2cc08a799536b425583e3325 Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Mon, 12 Jun 2023 16:57:42 +0200 Subject: [PATCH 04/16] tests: adapt to using diff coverage obj --- tests/conftest.py | 3 ++- tests/unit/test_annotations.py | 18 ++++++------------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index e616776f..26689e43 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,6 +4,7 @@ import io import os import zipfile +from pathlib import Path import httpx import pytest @@ -281,7 +282,7 @@ def diff_coverage_obj(): total_percent_covered=decimal.Decimal("0.8"), num_changed_lines=39, files={ - "codebase/code.py": coverage_module.FileDiffCoverage( + Path("codebase/code.py"): coverage_module.FileDiffCoverage( path="codebase/code.py", percent_covered=decimal.Decimal("0.8"), violation_lines=[7, 9], diff --git a/tests/unit/test_annotations.py b/tests/unit/test_annotations.py index 306bdcab..e9fc825e 100644 --- a/tests/unit/test_annotations.py +++ b/tests/unit/test_annotations.py @@ -1,8 +1,8 @@ from coverage_comment import annotations -def test_annotations(coverage_obj, capsys): - annotations.create_pr_annotations(annotation_type="warning", coverage=coverage_obj) +def test_annotations(diff_coverage_obj, capsys): + annotations.create_pr_annotations(annotation_type="warning", coverage=diff_coverage_obj) expected = """::group::Annotations of lines with missing coverage ::warning file=codebase/code.py,line=7::This line has no coverage @@ -12,20 +12,14 @@ def test_annotations(coverage_obj, capsys): assert output.out.strip() == expected -def test_annotations_several_files(coverage_obj_many_missing_lines, capsys): +def test_annotations_several_files(diff_coverage_obj, capsys): annotations.create_pr_annotations( - annotation_type="notice", coverage=coverage_obj_many_missing_lines + annotation_type="notice", coverage=diff_coverage_obj ) expected = """::group::Annotations of lines with missing coverage -::notice file=codebase/main.py,line=3::This line has no coverage -::notice file=codebase/main.py,line=7::This line has no coverage -::notice file=codebase/main.py,line=13::This line has no coverage -::notice file=codebase/main.py,line=21::This line has no coverage -::notice file=codebase/main.py,line=123::This line has no coverage -::notice file=codebase/caller.py,line=13::This line has no coverage -::notice file=codebase/caller.py,line=21::This line has no coverage -::notice file=codebase/caller.py,line=212::This line has no coverage +::notice file=codebase/code.py,line=7::This line has no coverage +::notice file=codebase/code.py,line=9::This line has no coverage ::endgroup::""" output = capsys.readouterr() assert output.out.strip() == expected From 4f037f35eac311bbc9b6a978a0d106bb45ab69ff Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Mon, 12 Jun 2023 16:57:46 +0200 Subject: [PATCH 05/16] misc. --- coverage_comment/annotations.py | 2 +- coverage_comment/main.py | 1 + pyproject.toml | 1 - setup.cfg | 5 ++--- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/coverage_comment/annotations.py b/coverage_comment/annotations.py index 080028ca..6f714ce1 100644 --- a/coverage_comment/annotations.py +++ b/coverage_comment/annotations.py @@ -13,7 +13,7 @@ def create_pr_annotations(annotation_type: str, coverage: coverage_module.DiffCo for missing_line in file_coverage.violation_lines: github.create_missing_coverage_annotation( annotation_type=annotation_type, - file=filepath.name, + file=str(filepath), line=missing_line, ) diff --git a/coverage_comment/main.py b/coverage_comment/main.py index f68332ca..8fecb1c7 100644 --- a/coverage_comment/main.py +++ b/coverage_comment/main.py @@ -78,6 +78,7 @@ def action( if event_name == "pull_request": if config.ANNOTATE_MISSING_LINES: diff_coverage = coverage_module.get_diff_coverage_info(base_ref=config.GITHUB_BASE_REF) + annotations.create_pr_annotations( annotation_type=config.ANNOTATION_TYPE, coverage=diff_coverage ) diff --git a/pyproject.toml b/pyproject.toml index 3684882f..723e8ce1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,6 @@ flake8 = "*" isort = "*" mypy = "*" pytest = "*" -pytest-cov = "^4.0.0" pytest-mock = "*" tenacity = "^8.1.0" diff --git a/setup.cfg b/setup.cfg index adf8c25d..5b4fe3e8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,9 +22,8 @@ exclude_lines = \.\.\. [tool:pytest] -addopts = - --cov-report term-missing --cov-branch --cov-report html --cov-report term - --cov=coverage_comment -vv --strict-markers -rfE +addopts = + -vv --strict-markers -rfE --ignore=tests/end_to_end/repo testpaths = tests/unit From 8a435b890f42a693fb2b493c313c26bb25db2eee Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 12 Jun 2023 15:00:13 +0000 Subject: [PATCH 06/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- coverage_comment/main.py | 12 ++++++------ setup.cfg | 2 +- tests/unit/test_annotations.py | 4 +++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/coverage_comment/main.py b/coverage_comment/main.py index 8fecb1c7..18ef5af6 100644 --- a/coverage_comment/main.py +++ b/coverage_comment/main.py @@ -5,10 +5,9 @@ import httpx +from coverage_comment import annotations, comment_file, communication +from coverage_comment import coverage as coverage_module from coverage_comment import ( - annotations, - comment_file, - communication, files, github, github_client, @@ -19,7 +18,6 @@ subprocess, template, ) -from coverage_comment import coverage as coverage_module def main(): @@ -77,8 +75,10 @@ def action( coverage = coverage_module.get_coverage_info(merge=config.MERGE_COVERAGE_FILES) if event_name == "pull_request": if config.ANNOTATE_MISSING_LINES: - diff_coverage = coverage_module.get_diff_coverage_info(base_ref=config.GITHUB_BASE_REF) - + diff_coverage = coverage_module.get_diff_coverage_info( + base_ref=config.GITHUB_BASE_REF + ) + annotations.create_pr_annotations( annotation_type=config.ANNOTATION_TYPE, coverage=diff_coverage ) diff --git a/setup.cfg b/setup.cfg index 5b4fe3e8..144133f7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,7 +22,7 @@ exclude_lines = \.\.\. [tool:pytest] -addopts = +addopts = -vv --strict-markers -rfE --ignore=tests/end_to_end/repo testpaths = diff --git a/tests/unit/test_annotations.py b/tests/unit/test_annotations.py index e9fc825e..8b1937f3 100644 --- a/tests/unit/test_annotations.py +++ b/tests/unit/test_annotations.py @@ -2,7 +2,9 @@ def test_annotations(diff_coverage_obj, capsys): - annotations.create_pr_annotations(annotation_type="warning", coverage=diff_coverage_obj) + annotations.create_pr_annotations( + annotation_type="warning", coverage=diff_coverage_obj + ) expected = """::group::Annotations of lines with missing coverage ::warning file=codebase/code.py,line=7::This line has no coverage From 083f4361eacb7ed038c7ef63ef2ce1e519e00972 Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Mon, 12 Jun 2023 17:00:51 +0200 Subject: [PATCH 07/16] build: revert changes to setup.cfg required for vscode debugger to function --- pyproject.toml | 1 + setup.cfg | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 723e8ce1..3684882f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ flake8 = "*" isort = "*" mypy = "*" pytest = "*" +pytest-cov = "^4.0.0" pytest-mock = "*" tenacity = "^8.1.0" diff --git a/setup.cfg b/setup.cfg index 5b4fe3e8..adf8c25d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,8 +22,9 @@ exclude_lines = \.\.\. [tool:pytest] -addopts = - -vv --strict-markers -rfE +addopts = + --cov-report term-missing --cov-branch --cov-report html --cov-report term + --cov=coverage_comment -vv --strict-markers -rfE --ignore=tests/end_to_end/repo testpaths = tests/unit From 0327047400fb090bce0e45b007e04a3072cca3fd Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Tue, 13 Jun 2023 09:48:55 +0200 Subject: [PATCH 08/16] fix: change annotation in DiffCoverage --- coverage_comment/coverage.py | 2 +- tests/conftest.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/coverage_comment/coverage.py b/coverage_comment/coverage.py index 25c13847..888896cd 100644 --- a/coverage_comment/coverage.py +++ b/coverage_comment/coverage.py @@ -58,7 +58,7 @@ class DiffCoverage: total_num_violations: int total_percent_covered: decimal.Decimal num_changed_lines: int - files: dict[pathlib.Path, FileDiffCoverage] + files: dict[str, FileDiffCoverage] def compute_coverage(num_covered: int, num_total: int) -> decimal.Decimal: diff --git a/tests/conftest.py b/tests/conftest.py index 26689e43..015467b6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -273,7 +273,6 @@ def coverage_obj_many_missing_lines(): }, ) - @pytest.fixture def diff_coverage_obj(): return coverage_module.DiffCoverage( @@ -282,7 +281,7 @@ def diff_coverage_obj(): total_percent_covered=decimal.Decimal("0.8"), num_changed_lines=39, files={ - Path("codebase/code.py"): coverage_module.FileDiffCoverage( + "codebase/code.py": coverage_module.FileDiffCoverage( path="codebase/code.py", percent_covered=decimal.Decimal("0.8"), violation_lines=[7, 9], From 8b6a8f4aec32cc6d1b824eea6f22e2545f17ea41 Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Tue, 13 Jun 2023 09:49:23 +0200 Subject: [PATCH 09/16] tests: remove multi-line test for diff_coverage Identical to prior tests --- tests/conftest.py | 59 ---------------------------------- tests/unit/test_annotations.py | 15 +-------- 2 files changed, 1 insertion(+), 73 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 015467b6..eb614cc1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -214,65 +214,6 @@ def coverage_obj_no_branch(): }, ) - -@pytest.fixture -def coverage_obj_many_missing_lines(): - return coverage_module.Coverage( - meta=coverage_module.CoverageMetadata( - version="1.2.3", - timestamp=datetime.datetime(2000, 1, 1), - branch_coverage=True, - show_contexts=False, - ), - info=coverage_module.CoverageInfo( - covered_lines=7, - num_statements=10, - percent_covered=decimal.Decimal("0.8"), - missing_lines=12, - excluded_lines=0, - num_branches=2, - num_partial_branches=1, - covered_branches=1, - missing_branches=1, - ), - files={ - "codebase/main.py": coverage_module.FileCoverage( - path="codebase/main.py", - executed_lines=[1, 2, 5, 6, 9], - missing_lines=[3, 7, 13, 21, 123], - excluded_lines=[], - info=coverage_module.CoverageInfo( - covered_lines=5, - num_statements=10, - percent_covered=decimal.Decimal("0.5"), - missing_lines=5, - excluded_lines=0, - num_branches=2, - num_partial_branches=1, - covered_branches=1, - missing_branches=1, - ), - ), - "codebase/caller.py": coverage_module.FileCoverage( - path="codebase/caller.py", - executed_lines=[1, 2, 5], - missing_lines=[13, 21, 212], - excluded_lines=[], - info=coverage_module.CoverageInfo( - covered_lines=3, - num_statements=6, - percent_covered=decimal.Decimal("0.5"), - missing_lines=3, - excluded_lines=0, - num_branches=2, - num_partial_branches=1, - covered_branches=1, - missing_branches=1, - ), - ), - }, - ) - @pytest.fixture def diff_coverage_obj(): return coverage_module.DiffCoverage( diff --git a/tests/unit/test_annotations.py b/tests/unit/test_annotations.py index 8b1937f3..f0d3f262 100644 --- a/tests/unit/test_annotations.py +++ b/tests/unit/test_annotations.py @@ -11,17 +11,4 @@ def test_annotations(diff_coverage_obj, capsys): ::warning file=codebase/code.py,line=9::This line has no coverage ::endgroup::""" output = capsys.readouterr() - assert output.out.strip() == expected - - -def test_annotations_several_files(diff_coverage_obj, capsys): - annotations.create_pr_annotations( - annotation_type="notice", coverage=diff_coverage_obj - ) - - expected = """::group::Annotations of lines with missing coverage -::notice file=codebase/code.py,line=7::This line has no coverage -::notice file=codebase/code.py,line=9::This line has no coverage -::endgroup::""" - output = capsys.readouterr() - assert output.out.strip() == expected + assert output.out.strip() == expected \ No newline at end of file From 65df6cfc723a906af799938631e23d7ccf68015a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Jun 2023 07:49:37 +0000 Subject: [PATCH 10/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/conftest.py | 1 + tests/unit/test_annotations.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index eb614cc1..09d668a2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -214,6 +214,7 @@ def coverage_obj_no_branch(): }, ) + @pytest.fixture def diff_coverage_obj(): return coverage_module.DiffCoverage( diff --git a/tests/unit/test_annotations.py b/tests/unit/test_annotations.py index f0d3f262..bf70ccb3 100644 --- a/tests/unit/test_annotations.py +++ b/tests/unit/test_annotations.py @@ -11,4 +11,4 @@ def test_annotations(diff_coverage_obj, capsys): ::warning file=codebase/code.py,line=9::This line has no coverage ::endgroup::""" output = capsys.readouterr() - assert output.out.strip() == expected \ No newline at end of file + assert output.out.strip() == expected From 1e10f71a560a3a20bd441c682e8be97210fe9d16 Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Tue, 13 Jun 2023 10:38:40 +0200 Subject: [PATCH 11/16] tests: allow diff to non-origin --- coverage_comment/coverage.py | 7 +++++-- coverage_comment/main.py | 21 ++++++++++++--------- coverage_comment/settings.py | 1 + tests/integration/test_main.py | 7 +++++-- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/coverage_comment/coverage.py b/coverage_comment/coverage.py index 888896cd..c99db530 100644 --- a/coverage_comment/coverage.py +++ b/coverage_comment/coverage.py @@ -191,14 +191,17 @@ def extract_info(data) -> Coverage: ) -def get_diff_coverage_info(base_ref: str) -> DiffCoverage: +def get_diff_coverage_info( + base_ref: str, compare_to_origin: bool = True +) -> DiffCoverage: subprocess.run("git", "fetch", "--depth=1000") subprocess.run("coverage", "xml") with tempfile.NamedTemporaryFile("r") as f: + ref_prefix = "origin/" if compare_to_origin else "" subprocess.run( "diff-cover", "coverage.xml", - f"--compare-branch=origin/{base_ref}", + f"--compare-branch={ref_prefix}{base_ref}", f"--json-report={f.name}", "--diff-range-notation=..", "--quiet", diff --git a/coverage_comment/main.py b/coverage_comment/main.py index 18ef5af6..ba942aa0 100644 --- a/coverage_comment/main.py +++ b/coverage_comment/main.py @@ -5,9 +5,10 @@ import httpx -from coverage_comment import annotations, comment_file, communication -from coverage_comment import coverage as coverage_module from coverage_comment import ( + annotations, + comment_file, + communication, files, github, github_client, @@ -18,6 +19,7 @@ subprocess, template, ) +from coverage_comment import coverage as coverage_module def main(): @@ -73,12 +75,14 @@ def action( if event_name in {"pull_request", "push"}: coverage = coverage_module.get_coverage_info(merge=config.MERGE_COVERAGE_FILES) + if event_name == "pull_request": - if config.ANNOTATE_MISSING_LINES: - diff_coverage = coverage_module.get_diff_coverage_info( - base_ref=config.GITHUB_BASE_REF - ) + diff_coverage = coverage_module.get_diff_coverage_info( + base_ref=config.GITHUB_BASE_REF, + compare_to_origin=config.COV_DIFF_TO_ORIGIN, + ) + if config.ANNOTATE_MISSING_LINES: annotations.create_pr_annotations( annotation_type=config.ANNOTATION_TYPE, coverage=diff_coverage ) @@ -86,6 +90,7 @@ def action( return generate_comment( config=config, coverage=coverage, + diff_coverage=diff_coverage, github_session=github_session, ) else: @@ -109,15 +114,13 @@ def action( def generate_comment( config: settings.Config, coverage: coverage_module.Coverage, + diff_coverage: coverage_module.DiffCoverage, github_session: httpx.Client, ) -> int: log.info("Generating comment for PR") gh = github_client.GitHub(session=github_session) - diff_coverage = coverage_module.get_diff_coverage_info( - base_ref=config.GITHUB_BASE_REF - ) previous_coverage_data_file = storage.get_datafile_contents( github=gh, repository=config.GITHUB_REPOSITORY, diff --git a/coverage_comment/settings.py b/coverage_comment/settings.py index 5f4e882a..1b92a69a 100644 --- a/coverage_comment/settings.py +++ b/coverage_comment/settings.py @@ -48,6 +48,7 @@ class Config: MERGE_COVERAGE_FILES: bool = False ANNOTATE_MISSING_LINES: bool = False ANNOTATION_TYPE: str = "warning" + COV_DIFF_TO_ORIGIN: bool = False VERBOSE: bool = False # Only for debugging, not exposed in the action: FORCE_WORKFLOW_RUN: bool = False diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index a4231862..8e0f03d7 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -3,6 +3,7 @@ import os import pathlib import subprocess +from typing import Callable import pytest @@ -28,7 +29,7 @@ def file_path(integration_dir): @pytest.fixture -def write_file(file_path): +def write_file(file_path) -> Callable: def _(*variables): content = "import os" for i, var in enumerate(variables): @@ -255,7 +256,9 @@ def test_action__pull_request__annotations( )(status_code=200) result = main.action( - config=pull_request_config(ANNOTATE_MISSING_LINES=True), + config=pull_request_config( + ANNOTATE_MISSING_LINES=True, COV_DIFF_TO_ORIGIN=False + ), github_session=session, http_session=session, git=None, From f25f538cc9ac26288eed558be30dc03685a73aab Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Tue, 13 Jun 2023 10:38:55 +0200 Subject: [PATCH 12/16] chore: a few extra annotations of pytest fixtures --- tests/conftest.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index eb614cc1..5d29e429 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,6 +5,7 @@ import os import zipfile from pathlib import Path +from typing import Callable import httpx import pytest @@ -44,7 +45,7 @@ def _(**kwargs): @pytest.fixture -def pull_request_config(base_config): +def pull_request_config(base_config) -> Callable: def _(**kwargs): defaults = { # GitHub stuff @@ -214,6 +215,7 @@ def coverage_obj_no_branch(): }, ) + @pytest.fixture def diff_coverage_obj(): return coverage_module.DiffCoverage( From eb87dc34fd2543fe51c8e4504b9ab66f6086707f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Jun 2023 08:39:22 +0000 Subject: [PATCH 13/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- coverage_comment/main.py | 6 ++---- tests/conftest.py | 2 +- tests/integration/test_main.py | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/coverage_comment/main.py b/coverage_comment/main.py index ba942aa0..6995d83e 100644 --- a/coverage_comment/main.py +++ b/coverage_comment/main.py @@ -5,10 +5,9 @@ import httpx +from coverage_comment import annotations, comment_file, communication +from coverage_comment import coverage as coverage_module from coverage_comment import ( - annotations, - comment_file, - communication, files, github, github_client, @@ -19,7 +18,6 @@ subprocess, template, ) -from coverage_comment import coverage as coverage_module def main(): diff --git a/tests/conftest.py b/tests/conftest.py index 5d29e429..2472a96f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,7 +5,7 @@ import os import zipfile from pathlib import Path -from typing import Callable +from collections.abc import Callable import httpx import pytest diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 8e0f03d7..950728a7 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -3,7 +3,7 @@ import os import pathlib import subprocess -from typing import Callable +from collections.abc import Callable import pytest From 6edb94631e5882f6923c38c6db29be7369359135 Mon Sep 17 00:00:00 2001 From: kieferro <81773954+kieferro@users.noreply.github.com> Date: Thu, 15 Jun 2023 19:09:26 +0200 Subject: [PATCH 14/16] [pre-commit] Remove unused import --- tests/conftest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2472a96f..4f8b7365 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,6 @@ import io import os import zipfile -from pathlib import Path from collections.abc import Callable import httpx From ce3a9157d67a8029e9737d87fb5fad7764e5a817 Mon Sep 17 00:00:00 2001 From: kieferro <81773954+kieferro@users.noreply.github.com> Date: Thu, 15 Jun 2023 19:56:47 +0200 Subject: [PATCH 15/16] Add second test for annotations --- tests/conftest.py | 32 ++++++++++++++++++++++++++++++++ tests/unit/test_annotations.py | 20 ++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 4f8b7365..b2870129 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -232,6 +232,38 @@ def diff_coverage_obj(): ) +@pytest.fixture +def diff_coverage_obj_many_missing_lines(): + return coverage_module.DiffCoverage( + total_num_lines=20, + total_num_violations=1, + total_percent_covered=decimal.Decimal("0.7"), + num_changed_lines=52, + files={ + "codebase/code.py": coverage_module.FileDiffCoverage( + path="codebase/code.py", + percent_covered=decimal.Decimal("0.8"), + violation_lines=[3, 5, 21, 111], + ), + "codebase/helper.py": coverage_module.FileDiffCoverage( + path="codebase/helper.py", + percent_covered=decimal.Decimal("0.6"), + violation_lines=[19, 22], + ), + "codebase/log.py": coverage_module.FileDiffCoverage( + path="codebase/log.py", + percent_covered=decimal.Decimal("0.9"), + violation_lines=[], + ), + "codebase/files.py": coverage_module.FileDiffCoverage( + path="codebase/files.py", + percent_covered=decimal.Decimal("0.8"), + violation_lines=[120, 121, 122], + ), + }, + ) + + @pytest.fixture def session(is_failed): """ diff --git a/tests/unit/test_annotations.py b/tests/unit/test_annotations.py index bf70ccb3..3b88d9fb 100644 --- a/tests/unit/test_annotations.py +++ b/tests/unit/test_annotations.py @@ -12,3 +12,23 @@ def test_annotations(diff_coverage_obj, capsys): ::endgroup::""" output = capsys.readouterr() assert output.out.strip() == expected + + +def test_annotations_several_files(diff_coverage_obj_many_missing_lines, capsys): + annotations.create_pr_annotations( + annotation_type="notice", coverage=diff_coverage_obj_many_missing_lines + ) + + expected = """::group::Annotations of lines with missing coverage +::notice file=codebase/code.py,line=3::This line has no coverage +::notice file=codebase/code.py,line=5::This line has no coverage +::notice file=codebase/code.py,line=21::This line has no coverage +::notice file=codebase/code.py,line=111::This line has no coverage +::notice file=codebase/helper.py,line=19::This line has no coverage +::notice file=codebase/helper.py,line=22::This line has no coverage +::notice file=codebase/files.py,line=120::This line has no coverage +::notice file=codebase/files.py,line=121::This line has no coverage +::notice file=codebase/files.py,line=122::This line has no coverage +::endgroup::""" + output = capsys.readouterr() + assert output.out.strip() == expected From 41afb915df9a18520573a64a977acece80f39043 Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Fri, 16 Jun 2023 10:39:01 +0200 Subject: [PATCH 16/16] Fix test --- tests/integration/test_main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 950728a7..89094402 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -264,7 +264,6 @@ def test_action__pull_request__annotations( git=None, ) expected = """::group::Annotations of lines with missing coverage -::warning file=foo.py,line=6::This line has no coverage ::endgroup::""" output = capsys.readouterr()