Skip to content

add more specific outputs #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp_linter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 17 additions & 9 deletions cpp_linter/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Comment on lines -64 to +68
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using bool(Globals.OUTPUT) always returns true since revision of comment contents (see changes in #32). So, this enhancement fixes that ambiguity while implementing the new outputs. 🚀

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
Expand Down Expand Up @@ -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.

Expand All @@ -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)
Expand All @@ -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("/")
Expand All @@ -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]]:
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down