From bcd0bdf133c10be427019da38d7f3b7c88d51a07 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 8 Oct 2021 20:41:54 -0400 Subject: [PATCH] jjb: binutils-gdb: replace sum2junit script with Python implementation --- scripts/binutils-gdb/build.sh | 171 +++++++++++++++++----------------- 1 file changed, 85 insertions(+), 86 deletions(-) diff --git a/scripts/binutils-gdb/build.sh b/scripts/binutils-gdb/build.sh index 726cc1a..a34c2ff 100755 --- a/scripts/binutils-gdb/build.sh +++ b/scripts/binutils-gdb/build.sh @@ -29,90 +29,86 @@ sum2junit() { local infile="$1" local outfile="$2" - local tool - local skipped - local passes - local failures - local total - local s2jtmpfile - - local result - local name - local message - - set +x - - tool=$(grep "tests ===" "$infile" | tr -s ' ' | cut -d ' ' -f 2) - - # Get the counts for tests that didn't work properly - skipped=$(grep -E -c '^UNRESOLVED|^UNTESTED|^UNSUPPORTED' "$infile" || true) - if test x"${skipped}" = x; then - skipped=0 - fi - - # The total of successful results are PASS and XFAIL - passes=$(grep -E -c '^PASS|XFAIL' "$infile" || true) - if test x"${passes}" = x; then - passes=0 - fi - - # The total of failed results are FAIL and XPASS - failures=$(grep -E -c '^FAIL|XPASS' "$infile" || true) - if test x"${failures}" = x; then - failures=0 - fi - - # Calculate the total number of test cases - total=$((passes + failures)) - total=$((total + skipped)) - - cat < "$outfile" - - - - - +cat < sum2junit.py +import sys +from datetime import datetime +import re +from xml.etree.ElementTree import ElementTree, Element, SubElement + +line_re = re.compile( + r"^(PASS|XPASS|FAIL|XFAIL|KFAIL|DUPLICATE|UNTESTED|UNSUPPORTED|UNRESOLVED): (.*?\.exp): (.*)" +) + +pass_count = 0 +fail_count = 0 +skip_count = 0 +error_count = 0 +now = datetime.now().isoformat(timespec="seconds") + +testsuites = Element( + "testsuites", + { + "xmlns": "https://raw.githubusercontent.com/windyroad/JUnit-Schema/master/JUnit.xsd" + }, +) +testsuite = SubElement( + testsuites, + "testsuite", + { + "name": "GDB", + "package": "package", + "id": "0", + "time": "1", + "timestamp": now, + "hostname": "hostname", + }, +) +SubElement(testsuite, "properties") + +for line in sys.stdin: + m = line_re.match(line) + if not m: + continue + + state, exp_filename, test_name = m.groups() + + testcase_name = "{} - {}".format(exp_filename, test_name) + + testcase = SubElement( + testsuite, + "testcase", + {"name": testcase_name, "classname": "classname", "time": "0"}, + ) + + if state in ("PASS", "XFAIL", "KFAIL"): + pass_count += 1 + elif state in ("FAIL", "XPASS"): + fail_count += 1 + SubElement(testcase, "failure", {"type": state}) + elif state in ("UNRESOLVED", "DUPLICATE"): + error_count += 1 + SubElement(testcase, "error", {"type": state}) + elif state in ("UNTESTED", "UNSUPPORTED"): + skip_count += 1 + SubElement(testcase, "skipped") + else: + assert False + +testsuite.attrib["tests"] = str(pass_count + fail_count + skip_count) +testsuite.attrib["failures"] = str(fail_count) +testsuite.attrib["skipped"] = str(skip_count) +testsuite.attrib["errors"] = str(error_count) + +SubElement(testsuite, "system-out") +SubElement(testsuite, "system-err") + +et = ElementTree(testsuites) +et.write(sys.stdout, encoding="unicode") + +sys.exit(1 if fail_count > 0 or error_count > 0 else 0) EOF - s2jtmpfile="$(mktemp)" - grep -E 'PASS|XPASS|FAIL|UNTESTED|UNSUPPORTED|UNRESOLVED' "$infile" > "$s2jtmpfile" || true - - while read -r line - do - echo -n "." - result=$(echo "$line" | cut -d ' ' -f 1 | tr -d ':') - name=$(echo "$line" | cut -d ' ' -f 2 | tr -d '\"><;:\[\]^\\&?@') - message=$(echo "$line" | cut -d ' ' -f 3-50 | tr -d '\"><;:\[\]^\\&?@') - - echo " " >> "$outfile" - case "${result}" in - PASS|XFAIL|KFAIL) - # No message for successful tests in junit - ;; - UNSUPPORTED|UNTESTED) - if test x"${message}" != x; then - echo -n " " >> "$outfile" - else - echo -n " " >> "$outfile" - fi - ;; - XPASS|UNRESOLVED|DUPLICATE) - echo -n " " >> "$outfile" - ;; - *) - echo -n " " >> "$outfile" - esac - - echo " " >> "$outfile" - done < "$s2jtmpfile" - - rm -f "$s2jtmpfile" - - # Write the closing tag for the test results - echo "" >> "$outfile" - echo "" >> "$outfile" - - set -x + python3 sum2junit.py < "$infile" > "$outfile" } # Required variables @@ -210,18 +206,21 @@ $MAKE -j "$($NPROC)" V=1 MAKEINFO=/bin/true # Install in the workspace $MAKE install DESTDIR="$WORKSPACE" -# Run tests, don't fail now, we want to run the archiving steps +# Run tests, don't fail now, we know that "make check" is going to fail, +# since some tests don't pass. # # Disable ASan leaks reporting, it might break some tests since it adds # unexpected output when GDB exits. -failed_tests=0 -ASAN_OPTIONS=detect_leaks=0 $MAKE -C gdb --keep-going check -j "$($NPROC)" || failed_tests=1 +ASAN_OPTIONS=detect_leaks=0 $MAKE -C gdb --keep-going check -j "$($NPROC)" || true # Copy the dejagnu test results for archiving before cleaning the build dir mkdir "${WORKSPACE}/results" cp gdb/testsuite/gdb.log "${WORKSPACE}/results/" cp gdb/testsuite/gdb.sum "${WORKSPACE}/results/" -sum2junit gdb/testsuite/gdb.sum "${WORKSPACE}/results/gdb.xml" + +# Convert results to JUnit format. +failed_tests=0 +sum2junit gdb/testsuite/gdb.sum "${WORKSPACE}/results/gdb.xml" || failed_tests=1 # Clean the build directory $MAKE clean -- 2.34.1