From a2875ac00e6f1dc3eb4ac19712c7a241b5a76e83 Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Mon, 4 Dec 2023 08:19:02 -0800 Subject: [PATCH] add more specific outputs (#37) satisfies feature requested in cpp-linter/cpp-linter-action#168 --- cpp_linter/__init__.py | 4 ++++ cpp_linter/run.py | 26 +++++++++++++++++--------- tests/test_misc.py | 9 ++++++--- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/cpp_linter/__init__.py b/cpp_linter/__init__.py index dfc31fa9..c1527dfa 100644 --- a/cpp_linter/__init__.py +++ b/cpp_linter/__init__.py @@ -72,6 +72,10 @@ class Globals: """The parsed JSON of the event payload.""" response_buffer: Response = Response() """A shared response object for `requests` module.""" + format_failed_count: int = 0 + """A total count of clang-format concerns""" + tidy_failed_count: int = 0 + """A total count of clang-tidy concerns""" class GlobalParser: diff --git a/cpp_linter/run.py b/cpp_linter/run.py index 36f7896e..1fdd6663 100644 --- a/cpp_linter/run.py +++ b/cpp_linter/run.py @@ -61,10 +61,18 @@ def set_exit_code(override: Optional[int] = None) -> int: The exit code that was used. If the ``override`` parameter was not passed, then this value will describe (like a bool value) if any checks failed. """ - exit_code = override if override is not None else bool(Globals.OUTPUT) + exit_code = ( + override + if override is not None + else (Globals.format_failed_count + Globals.tidy_failed_count) + ) try: with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as env_file: env_file.write(f"checks-failed={exit_code}\n") + env_file.write( + f"clang-format-checks-failed={Globals.format_failed_count}\n" + ) + env_file.write(f"clang-tidy-checks-failed={Globals.tidy_failed_count}\n") except (KeyError, FileNotFoundError): # pragma: no cover # not executed on a github CI runner; ignore this error when executed locally pass @@ -563,7 +571,7 @@ def post_results( def make_annotations( style: str, file_annotations: bool, lines_changed_only: int -) -> bool: +) -> int: """Use github log commands to make annotations from clang-format and clang-tidy output. @@ -581,7 +589,6 @@ def make_annotations( :returns: A boolean describing if any annotations were made. """ - count = 0 files = ( Globals.FILES if GITHUB_EVENT_NAME == "pull_request" or isinstance(Globals.FILES, list) @@ -594,7 +601,7 @@ def make_annotations( if output is not None: if file_annotations: log_commander.info(output) - count += 1 + Globals.format_failed_count += 1 for note in GlobalParser.tidy_notes: if lines_changed_only: filename = note.filename.replace("\\", "/").lstrip("/") @@ -606,18 +613,19 @@ def make_annotations( List[int], range_of_changed_lines(file, lines_changed_only) ) break - else: # filename match not found; treat line_filter as empty list + else: # filename match not found; treat line_filter as empty list continue if note.line in line_filter or not line_filter: - count += 1 + Globals.tidy_failed_count += 1 if file_annotations: log_commander.info(note.log_command()) else: - count += 1 + Globals.tidy_failed_count += 1 if file_annotations: log_commander.info(note.log_command()) + count = Globals.format_failed_count + Globals.format_failed_count logger.info("%d checks-failed", count) - return bool(count) + return count def parse_ignore_option(paths: str) -> Tuple[List[str], List[str]]: @@ -730,7 +738,7 @@ def main(): checks_failed = make_annotations( args.style, args.file_annotations, args.lines_changed_only ) - set_exit_code(int(checks_failed)) + set_exit_code(checks_failed) if GITHUB_EVENT_PATH and "private" in Globals.EVENT_PAYLOAD["repository"]: thread_comments_allowed = ( Globals.EVENT_PAYLOAD["repository"]["private"] is not True diff --git a/tests/test_misc.py b/tests/test_misc.py index 2e9a1bcd..020fa76a 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -23,13 +23,16 @@ def test_exit_override(tmp_path: Path): env_file = tmp_path / "GITHUB_OUTPUT" os.environ["GITHUB_OUTPUT"] = str(env_file) assert 1 == set_exit_code(1) - assert env_file.read_text(encoding="utf-8") == "checks-failed=1\n" + assert env_file.read_text(encoding="utf-8").startswith("checks-failed=1\n") def test_exit_implicit(): """Test the exit code issued when a thread comment is to be made.""" - Globals.OUTPUT = "TEST" # fake content for a thread comment - assert 1 == set_exit_code() + # fake values for total checks-failed + Globals.tidy_failed_count = 1 + Globals.format_failed_count = 1 + assert 2 == set_exit_code() + # see https://github.com/pytest-dev/pytest/issues/5997