|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import tempfile |
| 4 | +from xml.etree import ElementTree |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from pytest_cpp.error import CppTestFailure |
| 9 | + |
| 10 | + |
| 11 | +class Catch2Facade(object): |
| 12 | + """ |
| 13 | + Facade for Catch2. |
| 14 | + """ |
| 15 | + |
| 16 | + @classmethod |
| 17 | + def is_test_suite(cls, executable): |
| 18 | + try: |
| 19 | + output = subprocess.check_output( |
| 20 | + [executable, "--help"], |
| 21 | + stderr=subprocess.STDOUT, |
| 22 | + universal_newlines=True, |
| 23 | + ) |
| 24 | + except (subprocess.CalledProcessError, OSError): |
| 25 | + return False |
| 26 | + else: |
| 27 | + return "--list-test-names-only" in output |
| 28 | + |
| 29 | + def list_tests(self, executable): |
| 30 | + """ |
| 31 | + Executes test with "--list-test-names-only" and gets list of tests |
| 32 | + parsing output like this: |
| 33 | +
|
| 34 | + 1: All test cases reside in other .cpp files (empty) |
| 35 | + 2: Factorial of 0 is 1 (fail) |
| 36 | + 2: Factorials of 1 and higher are computed (pass) |
| 37 | + """ |
| 38 | + # This will return an exit code with the number of tests available |
| 39 | + try: |
| 40 | + output = subprocess.check_output( |
| 41 | + [executable, "--list-test-names-only"], |
| 42 | + stderr=subprocess.STDOUT, |
| 43 | + universal_newlines=True, |
| 44 | + ) |
| 45 | + except subprocess.CalledProcessError as e: |
| 46 | + output = e.output |
| 47 | + |
| 48 | + result = output.strip().split("\n") |
| 49 | + |
| 50 | + return result |
| 51 | + |
| 52 | + def run_test(self, executable, test_id="", test_args=(), harness=None): |
| 53 | + harness = harness or [] |
| 54 | + xml_filename = self._get_temp_xml_filename() |
| 55 | + args = harness + [ |
| 56 | + executable, |
| 57 | + test_id, |
| 58 | + "--success", |
| 59 | + "--reporter=xml", |
| 60 | + "--out %s" % xml_filename, |
| 61 | + ] |
| 62 | + args.extend(test_args) |
| 63 | + |
| 64 | + try: |
| 65 | + output = subprocess.check_output( |
| 66 | + args, stderr=subprocess.STDOUT, universal_newlines=True |
| 67 | + ) |
| 68 | + except subprocess.CalledProcessError as e: |
| 69 | + output = e.output |
| 70 | + if e.returncode != 1: |
| 71 | + msg = ( |
| 72 | + "Internal Error: calling {executable} " |
| 73 | + "for test {test_id} failed (returncode={returncode}):\n" |
| 74 | + "{output}" |
| 75 | + ) |
| 76 | + failure = Catch2Failure( |
| 77 | + executable, |
| 78 | + 0, |
| 79 | + msg.format( |
| 80 | + executable=executable, |
| 81 | + test_id=test_id, |
| 82 | + output=e.output, |
| 83 | + returncode=e.returncode, |
| 84 | + ), |
| 85 | + ) |
| 86 | + |
| 87 | + return [failure], output |
| 88 | + |
| 89 | + results = self._parse_xml(xml_filename) |
| 90 | + os.remove(xml_filename) |
| 91 | + for (executed_test_id, failures, skipped) in results: |
| 92 | + if executed_test_id == test_id: |
| 93 | + if failures: |
| 94 | + return ( |
| 95 | + [ |
| 96 | + Catch2Failure(filename, linenum, lines) |
| 97 | + for (filename, linenum, lines) in failures |
| 98 | + ], |
| 99 | + output, |
| 100 | + ) |
| 101 | + elif skipped: |
| 102 | + pytest.skip() |
| 103 | + else: |
| 104 | + return None, output |
| 105 | + |
| 106 | + msg = "Internal Error: could not find test " "{test_id} in results:\n{results}" |
| 107 | + |
| 108 | + results_list = "\n".join("\n".join(x) for (n, x, f) in results) |
| 109 | + failure = Catch2Failure(msg.format(test_id=test_id, results=results_list)) |
| 110 | + return [failure], output |
| 111 | + |
| 112 | + def _get_temp_xml_filename(self): |
| 113 | + return tempfile.mktemp() |
| 114 | + |
| 115 | + def _parse_xml(self, xml_filename): |
| 116 | + root = ElementTree.parse(xml_filename) |
| 117 | + result = [] |
| 118 | + for test_suite in root.findall("Group"): |
| 119 | + test_suite_name = test_suite.attrib["name"] |
| 120 | + for test_case in test_suite.findall("TestCase"): |
| 121 | + test_name = test_case.attrib["name"] |
| 122 | + test_result = test_case.find("OverallResult") |
| 123 | + failures = [] |
| 124 | + if test_result.attrib["success"] == "false": |
| 125 | + test_checks = test_case.findall("Expression") |
| 126 | + for check in test_checks: |
| 127 | + file_name = check.attrib["filename"] |
| 128 | + line_num = check.attrib["line"] |
| 129 | + if check.attrib["success"] == "false": |
| 130 | + expected = check.find("Original").text |
| 131 | + actual = check.find("Expanded").text |
| 132 | + fail_msg = "Expected: {expected}\nActual: {actual}".format( |
| 133 | + expected=expected, actual=actual |
| 134 | + ) |
| 135 | + failures.append((file_name, line_num, fail_msg,)) |
| 136 | + skipped = False # TODO: skipped tests don't appear in the results |
| 137 | + result.append((test_name, failures, skipped)) |
| 138 | + |
| 139 | + return result |
| 140 | + |
| 141 | + |
| 142 | +class Catch2Failure(CppTestFailure): |
| 143 | + def __init__(self, filename, linenum, lines): |
| 144 | + self.lines = lines.splitlines() |
| 145 | + self.filename = filename |
| 146 | + self.linenum = int(linenum) |
| 147 | + |
| 148 | + def get_lines(self): |
| 149 | + m = ("red", "bold") |
| 150 | + return [(x, m) for x in self.lines] |
| 151 | + |
| 152 | + def get_file_reference(self): |
| 153 | + return self.filename, self.linenum |
0 commit comments