From 01b78a75e937c62677b731e718b7b4862acb302c Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Wed, 6 Jul 2022 20:47:33 +0100 Subject: [PATCH] Log the last line of the exception, not the first --- python/publish/github_action.py | 3 ++- python/publish_unit_test_results.py | 13 ++++++++---- python/test/test_action_script.py | 32 +++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/python/publish/github_action.py b/python/publish/github_action.py index a967264a..b98d9e12 100644 --- a/python/publish/github_action.py +++ b/python/publish/github_action.py @@ -65,7 +65,8 @@ def warning(self, message: str, file: Optional[str] = None, line: Optional[int] def _exception(self, te: traceback.TracebackException): def exception_str(te: traceback.TracebackException) -> str: - return ''.join(te.format_exception_only()).split('\n')[0] + # we take the last line, which ends with a newline, that we strip + return list(te.format_exception_only())[-1].split('\n')[0] self.error('{te}{caused}{context}'.format( te=exception_str(te), diff --git a/python/publish_unit_test_results.py b/python/publish_unit_test_results.py index 5f678c09..f12539ea 100644 --- a/python/publish_unit_test_results.py +++ b/python/publish_unit_test_results.py @@ -16,13 +16,14 @@ import publish.github_action from publish import available_annotations, default_annotations, \ pull_request_build_modes, fail_on_modes, fail_on_mode_errors, fail_on_mode_failures, \ - comment_mode_off, comment_mode_always, comment_modes, punctuation_space + comment_mode_always, comment_modes, punctuation_space from publish.github_action import GithubAction from publish.junit import parse_junit_xml_files, process_junit_xml_elems from publish.progress import progress_logger from publish.publisher import Publisher, Settings from publish.retry import GitHubRetry -from publish.unittestresults import get_test_results, get_stats, ParsedUnitTestResults, ParsedUnitTestResultsWithCommit +from publish.unittestresults import get_test_results, get_stats, ParsedUnitTestResults, ParsedUnitTestResultsWithCommit, \ + ParseError logger = logging.getLogger('publish') @@ -123,6 +124,11 @@ def parse_files(settings: Settings, gha: GithubAction) -> ParsedUnitTestResultsW return process_junit_xml_elems(elems, settings.time_factor).with_commit(settings.commit) +def log_parse_errors(errors: List[ParseError], gha: GithubAction): + [gha.error(message=f'Error processing result file: {error.message}', file=error.file, line=error.line, column=error.column, exception=error.exception) + for error in errors] + + def main(settings: Settings, gha: GithubAction) -> None: # we cannot create a check run or pull request comment when running on pull_request event from a fork # when event_file is given we assume proper setup as in README.md#support-fork-repositories-and-dependabot-branches @@ -144,8 +150,7 @@ def main(settings: Settings, gha: GithubAction) -> None: # get the unit test results parsed = parse_files(settings, gha) - [gha.error(message=f'Error processing result file: {error.message}', file=error.file, line=error.line, column=error.column, exception=error.exception) - for error in parsed.errors] + log_parse_errors(parsed.errors, gha) # process the parsed results results = get_test_results(parsed, settings.dedup_classes_by_file_name) diff --git a/python/test/test_action_script.py b/python/test/test_action_script.py index a69bafa3..fbf0b37a 100644 --- a/python/test/test_action_script.py +++ b/python/test/test_action_script.py @@ -1,3 +1,5 @@ +import io +import re import json import logging import os @@ -6,15 +8,16 @@ import tempfile import unittest from typing import Optional, Union, List, Type + import mock from publish import pull_request_build_mode_merge, fail_on_mode_failures, fail_on_mode_errors, \ - fail_on_mode_nothing, comment_modes, comment_mode_off, comment_mode_always, \ + fail_on_mode_nothing, comment_modes, comment_mode_always, \ pull_request_build_modes, punctuation_space from publish.github_action import GithubAction from publish.unittestresults import ParsedUnitTestResults, ParseError from publish_unit_test_results import get_conclusion, get_commit_sha, get_var, \ - check_var, check_var_condition, deprecate_var, deprecate_val, \ + check_var, check_var_condition, deprecate_var, deprecate_val, log_parse_errors, \ get_settings, get_annotations_config, Settings, get_files, throttle_gh_request_raw, is_float, parse_files, main from test_utils import chdir @@ -803,8 +806,7 @@ def test_parse_files(self): trx_files_glob=str(test_files_path / 'trx' / '**' / '*.trx')) actual = parse_files(settings, gha) - gha.warning.assert_not_called() - gha.error.assert_not_called() + self.assertEqual([], gha.method_calls) self.assertEqual(65, actual.files) self.assertEqual(6, len(actual.errors)) @@ -817,6 +819,28 @@ def test_parse_files(self): self.assertEqual(1858, len(actual.cases)) self.assertEqual('commit', actual.commit) + with io.StringIO() as string: + gha = GithubAction(file=string) + with mock.patch('publish.github_action.logger') as m: + log_parse_errors(actual.errors, gha) + self.assertEqual( + sorted([ + "::error::lxml.etree.XMLSyntaxError: Start tag expected, '<' not found, line 1, column 1", + "::error file=non-xml.xml::Error processing result file: Start tag expected, '<' not found, line 1, column 1 (non-xml.xml, line 1)", + "::error::Exception: File is empty.", + "::error file=empty.xml::Error processing result file: File is empty.", + "::error::lxml.etree.XMLSyntaxError: Premature end of data in tag skipped line 9, line 11, column 22", + "::error file=corrupt-xml.xml::Error processing result file: Premature end of data in tag skipped line 9, line 11, column 22 (corrupt-xml.xml, line 11)", + "::error::junitparser.junitparser.JUnitXmlError: Invalid format.", + "::error file=non-junit.xml::Error processing result file: Invalid format.", + "::error::lxml.etree.XMLSyntaxError: Char 0x0 out of allowed range, line 33, column 16", + "::error file=NUnit-issue17521.xml::Error processing result file: Char 0x0 out of allowed range, line 33, column 16 (NUnit-issue17521.xml, line 33)", + "::error::lxml.etree.XMLSyntaxError: attributes construct error, line 5, column 109", + "::error file=NUnit-issue47367.xml::Error processing result file: attributes construct error, line 5, column 109 (NUnit-issue47367.xml, line 5)" + ]), sorted([re.sub(r'file=.*[/\\]', 'file=', line) for line in string.getvalue().split(os.linesep) if line]) + ) + # self.assertEqual([], m.method_calls) + def test_parse_files_no_matches(self): gha = mock.MagicMock() with tempfile.TemporaryDirectory() as path: