mikepenz / action-junit-report

Reports junit test results as GitHub Pull Request Check
https://blog.mikepenz.dev
Apache License 2.0
321 stars 118 forks source link

Different line number representation (xunit) #779

Open KD-7 opened 1 year ago

KD-7 commented 1 year ago

Hello @mikepenz

When parsing xunit output the tool works except for the line numberings. For the following output no line numbers are found hence it defaults to 1:

<?xml version="1.0" ?>
<testsuite errors="544" failures="0" name="prospector-dodgy-mccabe-profile-validator-pycodestyle-pyflakes-pylint" tests="544" time="33.46">
    <properties/>
    <system-out><![CDATA[]]></system-out>
    <system-err><![CDATA[]]></system-err>
    <testcase name="truelearn_experiments/analyse_results.py-54">
        <error message="String statement has no effect" type="pylint Error"><![CDATA[truelearn_experiments/analyse_results.py:54: [pointless-string-statement(pylint), None] String statement has no effect]]></error>
    </testcase>

Shown as Github annotations: image

The format of this output is as follows:

  for message in sorted(self.messages):
            testcase_el = xml_doc.createElement("testcase")
            testcase_el.setAttribute("name", f"{self._make_path(message.location.path)}-{message.location.line}")

            failure_el = xml_doc.createElement("error")
            failure_el.setAttribute("message", message.message.strip())
            failure_el.setAttribute("type", "%s Error" % message.source)
            template = "%(path)s:%(line)s: [%(code)s(%(source)s), %(function)s] %(message)s"
            cdata = template % {
                "path": self._make_path(message.location.path),
                "line": message.location.line,
                "source": message.source,
                "code": message.code,
                "function": message.location.function,
                "message": message.message.strip(),
            }

As per the xunit output of prospector: https://github.com/PyCQA/prospector/blob/master/prospector/formatters/xunit.py

mikepenz commented 1 year ago

Thank you for the report. Looks this project you reference decided yet another format for line numbers.

The action itself supports already the most common variants of line numbers, via either line attributes on the testcase or failure node: https://github.com/mikepenz/action-junit-report/blob/main/src/testParser.ts#L312-L321

Alternative it supports the common format of : as delimiter after the filename: https://github.com/mikepenz/action-junit-report/blob/main/src/testParser.ts#L66-L87

Do you have the chance to adjust the format? The most reliable would be to have line as it's own attribute, as it would not depend on a regex to try to resolve it.

KD-7 commented 1 year ago

Hello @mikepenz

When parsing xunit output the tool works except for the line numberings. For the following output no line numbers are found hence it defaults to 1:

<?xml version="1.0" ?>
<testsuite errors="544" failures="0" name="prospector-dodgy-mccabe-profile-validator-pycodestyle-pyflakes-pylint" tests="544" time="33.46">
  <properties/>
  <system-out><![CDATA[]]></system-out>
  <system-err><![CDATA[]]></system-err>
  <testcase name="truelearn_experiments/analyse_results.py-54">
      <error message="String statement has no effect" type="pylint Error"><![CDATA[truelearn_experiments/analyse_results.py:54: [pointless-string-statement(pylint), None] String statement has no effect]]></error>
  </testcase>

Shown as Github annotations: image

The format of this output is as follows:

  for message in sorted(self.messages):
            testcase_el = xml_doc.createElement("testcase")
            testcase_el.setAttribute("name", f"{self._make_path(message.location.path)}-{message.location.line}")

            failure_el = xml_doc.createElement("error")
            failure_el.setAttribute("message", message.message.strip())
            failure_el.setAttribute("type", "%s Error" % message.source)
            template = "%(path)s:%(line)s: [%(code)s(%(source)s), %(function)s] %(message)s"
            cdata = template % {
                "path": self._make_path(message.location.path),
                "line": message.location.line,
                "source": message.source,
                "code": message.code,
                "function": message.location.function,
                "message": message.message.strip(),
            }

As per the xunit output of prospector: https://github.com/PyCQA/prospector/blob/master/prospector/formatters/xunit.py

After reviewing this, I can also see that the file paths arent matched correctly as well

KD-7 commented 1 year ago

Thank you for the report. Looks this project you reference decided yet another format for line numbers.

The action itself supports already the most common variants of line numbers, via either line attributes on the testcase or failure node: https://github.com/mikepenz/action-junit-report/blob/main/src/testParser.ts#L312-L321

Alternative it supports the common format of : as delimiter after the filename: https://github.com/mikepenz/action-junit-report/blob/main/src/testParser.ts#L66-L87

Do you have the chance to adjust the format? The most reliable would be to have line as it's own attribute, as it would not depend on a regex to try to resolve it.

Changing the xml file format isnt really an option in this scenario, I was wondering if from the name attribute the - can be used to split the file path and line numberings to give the desired behaviour. I have also tested a sample XML file where I add the line= attribute, however the file path isnt correctly matched:

<?xml version="1.0" ?>
<testsuite errors="544" failures="0" name="prospector-dodgy-mccabe-profile-validator-pycodestyle-pyflakes-pylint" tests="544" time="33.46">
    <properties/>
    <system-out><![CDATA[]]></system-out>
    <system-err><![CDATA[]]></system-err>
    <testcase name="truelearn_experiments/analyse_results.py" line="54">
        <error message="String statement has no effect" name="pylint Error"><![CDATA[truelearn_experiments/analyse_results.py:54: [pointless-string-statement(pylint), None] String statement has no effect]]></error>
    </testcase>
</testsuite>
CI