xref: /libCEED/tests/junit_common.py (revision 83ebc4c489f36457f06b28184a0de964e4323c95)
11b16049aSZach Atkinsfrom abc import ABC, abstractmethod
21b16049aSZach Atkinsimport argparse
369ef23b6SZach Atkinsimport csv
41b16049aSZach Atkinsfrom dataclasses import dataclass, field
51b16049aSZach Atkinsimport difflib
61b16049aSZach Atkinsfrom enum import Enum
71b16049aSZach Atkinsfrom math import isclose
81b16049aSZach Atkinsimport os
91b16049aSZach Atkinsfrom pathlib import Path
101b16049aSZach Atkinsimport re
111b16049aSZach Atkinsimport subprocess
1219868e18SZach Atkinsimport multiprocessing as mp
1319868e18SZach Atkinsfrom itertools import product
141b16049aSZach Atkinsimport sys
151b16049aSZach Atkinsimport time
1678cb100bSJames Wrightfrom typing import Optional, Tuple, List
171b16049aSZach Atkins
181b16049aSZach Atkinssys.path.insert(0, str(Path(__file__).parent / "junit-xml"))
191b16049aSZach Atkinsfrom junit_xml import TestCase, TestSuite, to_xml_report_string  # nopep8
201b16049aSZach Atkins
211b16049aSZach Atkins
221b16049aSZach Atkinsclass CaseInsensitiveEnumAction(argparse.Action):
231b16049aSZach Atkins    """Action to convert input values to lower case prior to converting to an Enum type"""
241b16049aSZach Atkins
251b16049aSZach Atkins    def __init__(self, option_strings, dest, type, default, **kwargs):
261b16049aSZach Atkins        if not (issubclass(type, Enum) and issubclass(type, str)):
271b16049aSZach Atkins            raise ValueError(f"{type} must be a StrEnum or str and Enum")
281b16049aSZach Atkins        # store provided enum type
291b16049aSZach Atkins        self.enum_type = type
301b16049aSZach Atkins        if isinstance(default, str):
311b16049aSZach Atkins            default = self.enum_type(default.lower())
321b16049aSZach Atkins        else:
331b16049aSZach Atkins            default = [self.enum_type(v.lower()) for v in default]
341b16049aSZach Atkins        # prevent automatic type conversion
351b16049aSZach Atkins        super().__init__(option_strings, dest, default=default, **kwargs)
361b16049aSZach Atkins
371b16049aSZach Atkins    def __call__(self, parser, namespace, values, option_string=None):
381b16049aSZach Atkins        if isinstance(values, str):
391b16049aSZach Atkins            values = self.enum_type(values.lower())
401b16049aSZach Atkins        else:
411b16049aSZach Atkins            values = [self.enum_type(v.lower()) for v in values]
421b16049aSZach Atkins        setattr(namespace, self.dest, values)
431b16049aSZach Atkins
441b16049aSZach Atkins
451b16049aSZach Atkins@dataclass
461b16049aSZach Atkinsclass TestSpec:
471b16049aSZach Atkins    """Dataclass storing information about a single test case"""
481b16049aSZach Atkins    name: str
498938a869SZach Atkins    only: List = field(default_factory=list)
508938a869SZach Atkins    args: List = field(default_factory=list)
511b16049aSZach Atkins
521b16049aSZach Atkins
531b16049aSZach Atkinsclass RunMode(str, Enum):
541b16049aSZach Atkins    """Enumeration of run modes, either `RunMode.TAP` or `RunMode.JUNIT`"""
551b16049aSZach Atkins    __str__ = str.__str__
561b16049aSZach Atkins    __format__ = str.__format__
571b16049aSZach Atkins    TAP: str = 'tap'
581b16049aSZach Atkins    JUNIT: str = 'junit'
591b16049aSZach Atkins
601b16049aSZach Atkins
611b16049aSZach Atkinsclass SuiteSpec(ABC):
621b16049aSZach Atkins    """Abstract Base Class defining the required interface for running a test suite"""
631b16049aSZach Atkins    @abstractmethod
641b16049aSZach Atkins    def get_source_path(self, test: str) -> Path:
651b16049aSZach Atkins        """Compute path to test source file
661b16049aSZach Atkins
671b16049aSZach Atkins        Args:
681b16049aSZach Atkins            test (str): Name of test
691b16049aSZach Atkins
701b16049aSZach Atkins        Returns:
711b16049aSZach Atkins            Path: Path to source file
721b16049aSZach Atkins        """
731b16049aSZach Atkins        raise NotImplementedError
741b16049aSZach Atkins
751b16049aSZach Atkins    @abstractmethod
761b16049aSZach Atkins    def get_run_path(self, test: str) -> Path:
771b16049aSZach Atkins        """Compute path to built test executable file
781b16049aSZach Atkins
791b16049aSZach Atkins        Args:
801b16049aSZach Atkins            test (str): Name of test
811b16049aSZach Atkins
821b16049aSZach Atkins        Returns:
831b16049aSZach Atkins            Path: Path to test executable
841b16049aSZach Atkins        """
851b16049aSZach Atkins        raise NotImplementedError
861b16049aSZach Atkins
871b16049aSZach Atkins    @abstractmethod
881b16049aSZach Atkins    def get_output_path(self, test: str, output_file: str) -> Path:
891b16049aSZach Atkins        """Compute path to expected output file
901b16049aSZach Atkins
911b16049aSZach Atkins        Args:
921b16049aSZach Atkins            test (str): Name of test
931b16049aSZach Atkins            output_file (str): File name of output file
941b16049aSZach Atkins
951b16049aSZach Atkins        Returns:
961b16049aSZach Atkins            Path: Path to expected output file
971b16049aSZach Atkins        """
981b16049aSZach Atkins        raise NotImplementedError
991b16049aSZach Atkins
100*83ebc4c4SJeremy L Thompson    def get_cgns_tol(self) -> float:
101*83ebc4c4SJeremy L Thompson        """retrieve CGNS test tolerance.
102*83ebc4c4SJeremy L Thompson
103*83ebc4c4SJeremy L Thompson        Returns:
104*83ebc4c4SJeremy L Thompson            tolerance (float): Test tolerance
105*83ebc4c4SJeremy L Thompson        """
106*83ebc4c4SJeremy L Thompson        return self.cgns_tol if hasattr(self, 'cgns_tol') else 1.0e-12
107*83ebc4c4SJeremy L Thompson
1081b16049aSZach Atkins    def post_test_hook(self, test: str, spec: TestSpec) -> None:
1091b16049aSZach Atkins        """Function callback ran after each test case
1101b16049aSZach Atkins
1111b16049aSZach Atkins        Args:
1121b16049aSZach Atkins            test (str): Name of test
1131b16049aSZach Atkins            spec (TestSpec): Test case specification
1141b16049aSZach Atkins        """
1151b16049aSZach Atkins        pass
1161b16049aSZach Atkins
1171b16049aSZach Atkins    def check_pre_skip(self, test: str, spec: TestSpec, resource: str, nproc: int) -> Optional[str]:
1181b16049aSZach Atkins        """Check if a test case should be skipped prior to running, returning the reason for skipping
1191b16049aSZach Atkins
1201b16049aSZach Atkins        Args:
1211b16049aSZach Atkins            test (str): Name of test
1221b16049aSZach Atkins            spec (TestSpec): Test case specification
1231b16049aSZach Atkins            resource (str): libCEED backend
1241b16049aSZach Atkins            nproc (int): Number of MPI processes to use when running test case
1251b16049aSZach Atkins
1261b16049aSZach Atkins        Returns:
1271b16049aSZach Atkins            Optional[str]: Skip reason, or `None` if test case should not be skipped
1281b16049aSZach Atkins        """
1291b16049aSZach Atkins        return None
1301b16049aSZach Atkins
1311b16049aSZach Atkins    def check_post_skip(self, test: str, spec: TestSpec, resource: str, stderr: str) -> Optional[str]:
1321b16049aSZach Atkins        """Check if a test case should be allowed to fail, based on its stderr output
1331b16049aSZach Atkins
1341b16049aSZach Atkins        Args:
1351b16049aSZach Atkins            test (str): Name of test
1361b16049aSZach Atkins            spec (TestSpec): Test case specification
1371b16049aSZach Atkins            resource (str): libCEED backend
1381b16049aSZach Atkins            stderr (str): Standard error output from test case execution
1391b16049aSZach Atkins
1401b16049aSZach Atkins        Returns:
14119868e18SZach Atkins            Optional[str]: Skip reason, or `None` if unexpected error
1421b16049aSZach Atkins        """
1431b16049aSZach Atkins        return None
1441b16049aSZach Atkins
14578cb100bSJames Wright    def check_required_failure(self, test: str, spec: TestSpec, resource: str, stderr: str) -> Tuple[str, bool]:
1461b16049aSZach Atkins        """Check whether a test case is expected to fail and if it failed expectedly
1471b16049aSZach Atkins
1481b16049aSZach Atkins        Args:
1491b16049aSZach Atkins            test (str): Name of test
1501b16049aSZach Atkins            spec (TestSpec): Test case specification
1511b16049aSZach Atkins            resource (str): libCEED backend
1521b16049aSZach Atkins            stderr (str): Standard error output from test case execution
1531b16049aSZach Atkins
1541b16049aSZach Atkins        Returns:
1551b16049aSZach Atkins            tuple[str, bool]: Tuple of the expected failure string and whether it was present in `stderr`
1561b16049aSZach Atkins        """
1571b16049aSZach Atkins        return '', True
1581b16049aSZach Atkins
1591b16049aSZach Atkins    def check_allowed_stdout(self, test: str) -> bool:
1601b16049aSZach Atkins        """Check whether a test is allowed to print console output
1611b16049aSZach Atkins
1621b16049aSZach Atkins        Args:
1631b16049aSZach Atkins            test (str): Name of test
1641b16049aSZach Atkins
1651b16049aSZach Atkins        Returns:
1661b16049aSZach Atkins            bool: True if the test is allowed to print console output
1671b16049aSZach Atkins        """
1681b16049aSZach Atkins        return False
1691b16049aSZach Atkins
1701b16049aSZach Atkins
1711b16049aSZach Atkinsdef has_cgnsdiff() -> bool:
1721b16049aSZach Atkins    """Check whether `cgnsdiff` is an executable program in the current environment
1731b16049aSZach Atkins
1741b16049aSZach Atkins    Returns:
1751b16049aSZach Atkins        bool: True if `cgnsdiff` is found
1761b16049aSZach Atkins    """
1771b16049aSZach Atkins    my_env: dict = os.environ.copy()
1781b16049aSZach Atkins    proc = subprocess.run('cgnsdiff',
1791b16049aSZach Atkins                          shell=True,
1801b16049aSZach Atkins                          stdout=subprocess.PIPE,
1811b16049aSZach Atkins                          stderr=subprocess.PIPE,
1821b16049aSZach Atkins                          env=my_env)
1831b16049aSZach Atkins    return 'not found' not in proc.stderr.decode('utf-8')
1841b16049aSZach Atkins
1851b16049aSZach Atkins
18678cb100bSJames Wrightdef contains_any(base: str, substrings: List[str]) -> bool:
1871b16049aSZach Atkins    """Helper function, checks if any of the substrings are included in the base string
1881b16049aSZach Atkins
1891b16049aSZach Atkins    Args:
1901b16049aSZach Atkins        base (str): Base string to search in
1918938a869SZach Atkins        substrings (List[str]): List of potential substrings
1921b16049aSZach Atkins
1931b16049aSZach Atkins    Returns:
1941b16049aSZach Atkins        bool: True if any substrings are included in base string
1951b16049aSZach Atkins    """
1961b16049aSZach Atkins    return any((sub in base for sub in substrings))
1971b16049aSZach Atkins
1981b16049aSZach Atkins
19978cb100bSJames Wrightdef startswith_any(base: str, prefixes: List[str]) -> bool:
2001b16049aSZach Atkins    """Helper function, checks if the base string is prefixed by any of `prefixes`
2011b16049aSZach Atkins
2021b16049aSZach Atkins    Args:
2031b16049aSZach Atkins        base (str): Base string to search
2048938a869SZach Atkins        prefixes (List[str]): List of potential prefixes
2051b16049aSZach Atkins
2061b16049aSZach Atkins    Returns:
2071b16049aSZach Atkins        bool: True if base string is prefixed by any of the prefixes
2081b16049aSZach Atkins    """
2091b16049aSZach Atkins    return any((base.startswith(prefix) for prefix in prefixes))
2101b16049aSZach Atkins
2111b16049aSZach Atkins
2121b16049aSZach Atkinsdef parse_test_line(line: str) -> TestSpec:
2131b16049aSZach Atkins    """Parse a single line of TESTARGS and CLI arguments into a `TestSpec` object
2141b16049aSZach Atkins
2151b16049aSZach Atkins    Args:
2161b16049aSZach Atkins        line (str): String containing TESTARGS specification and CLI arguments
2171b16049aSZach Atkins
2181b16049aSZach Atkins    Returns:
2191b16049aSZach Atkins        TestSpec: Parsed specification of test case
2201b16049aSZach Atkins    """
22178cb100bSJames Wright    args: List[str] = re.findall("(?:\".*?\"|\\S)+", line.strip())
2221b16049aSZach Atkins    if args[0] == 'TESTARGS':
2231b16049aSZach Atkins        return TestSpec(name='', args=args[1:])
2241b16049aSZach Atkins    raw_test_args: str = args[0][args[0].index('TESTARGS(') + 9:args[0].rindex(')')]
2251b16049aSZach Atkins    # transform 'name="myname",only="serial,int32"' into {'name': 'myname', 'only': 'serial,int32'}
2261b16049aSZach Atkins    test_args: dict = dict([''.join(t).split('=') for t in re.findall(r"""([^,=]+)(=)"([^"]*)\"""", raw_test_args)])
227f85e4a7bSJeremy L Thompson    name: str = test_args.get('name', '')
22878cb100bSJames Wright    constraints: List[str] = test_args['only'].split(',') if 'only' in test_args else []
2291b16049aSZach Atkins    if len(args) > 1:
230f85e4a7bSJeremy L Thompson        return TestSpec(name=name, only=constraints, args=args[1:])
2311b16049aSZach Atkins    else:
232f85e4a7bSJeremy L Thompson        return TestSpec(name=name, only=constraints)
2331b16049aSZach Atkins
2341b16049aSZach Atkins
23578cb100bSJames Wrightdef get_test_args(source_file: Path) -> List[TestSpec]:
2361b16049aSZach Atkins    """Parse all test cases from a given source file
2371b16049aSZach Atkins
2381b16049aSZach Atkins    Args:
2391b16049aSZach Atkins        source_file (Path): Path to source file
2401b16049aSZach Atkins
2411b16049aSZach Atkins    Raises:
2421b16049aSZach Atkins        RuntimeError: Errors if source file extension is unsupported
2431b16049aSZach Atkins
2441b16049aSZach Atkins    Returns:
2458938a869SZach Atkins        List[TestSpec]: List of parsed `TestSpec` objects, or a list containing a single, default `TestSpec` if none were found
2461b16049aSZach Atkins    """
2471b16049aSZach Atkins    comment_str: str = ''
2488c81f8b0SPeter Munch    if source_file.suffix in ['.c', '.cc', '.cpp']:
2491b16049aSZach Atkins        comment_str = '//'
2501b16049aSZach Atkins    elif source_file.suffix in ['.py']:
2511b16049aSZach Atkins        comment_str = '#'
2521b16049aSZach Atkins    elif source_file.suffix in ['.usr']:
2531b16049aSZach Atkins        comment_str = 'C_'
2541b16049aSZach Atkins    elif source_file.suffix in ['.f90']:
2551b16049aSZach Atkins        comment_str = '! '
2561b16049aSZach Atkins    else:
2571b16049aSZach Atkins        raise RuntimeError(f'Unrecognized extension for file: {source_file}')
2581b16049aSZach Atkins
2591b16049aSZach Atkins    return [parse_test_line(line.strip(comment_str))
2601b16049aSZach Atkins            for line in source_file.read_text().splitlines()
2611b16049aSZach Atkins            if line.startswith(f'{comment_str}TESTARGS')] or [TestSpec('', args=['{ceed_resource}'])]
2621b16049aSZach Atkins
2631b16049aSZach Atkins
2641b16049aSZach Atkinsdef diff_csv(test_csv: Path, true_csv: Path, zero_tol: float = 3e-10, rel_tol: float = 1e-2) -> str:
2651b16049aSZach Atkins    """Compare CSV results against an expected CSV file with tolerances
2661b16049aSZach Atkins
2671b16049aSZach Atkins    Args:
2681b16049aSZach Atkins        test_csv (Path): Path to output CSV results
2691b16049aSZach Atkins        true_csv (Path): Path to expected CSV results
2701b16049aSZach Atkins        zero_tol (float, optional): Tolerance below which values are considered to be zero. Defaults to 3e-10.
2711b16049aSZach Atkins        rel_tol (float, optional): Relative tolerance for comparing non-zero values. Defaults to 1e-2.
2721b16049aSZach Atkins
2731b16049aSZach Atkins    Returns:
2741b16049aSZach Atkins        str: Diff output between result and expected CSVs
2751b16049aSZach Atkins    """
27678cb100bSJames Wright    test_lines: List[str] = test_csv.read_text().splitlines()
27778cb100bSJames Wright    true_lines: List[str] = true_csv.read_text().splitlines()
27869ef23b6SZach Atkins    # Files should not be empty
27969ef23b6SZach Atkins    if len(test_lines) == 0:
28069ef23b6SZach Atkins        return f'No lines found in test output {test_csv}'
28169ef23b6SZach Atkins    if len(true_lines) == 0:
28269ef23b6SZach Atkins        return f'No lines found in test source {true_csv}'
2831b16049aSZach Atkins
28469ef23b6SZach Atkins    test_reader: csv.DictReader = csv.DictReader(test_lines)
28569ef23b6SZach Atkins    true_reader: csv.DictReader = csv.DictReader(true_lines)
28669ef23b6SZach Atkins    if test_reader.fieldnames != true_reader.fieldnames:
2871b16049aSZach Atkins        return ''.join(difflib.unified_diff([f'{test_lines[0]}\n'], [f'{true_lines[0]}\n'],
2881b16049aSZach Atkins                       tofile='found CSV columns', fromfile='expected CSV columns'))
2891b16049aSZach Atkins
29069ef23b6SZach Atkins    if len(test_lines) != len(true_lines):
29169ef23b6SZach Atkins        return f'Number of lines in {test_csv} and {true_csv} do not match'
29278cb100bSJames Wright    diff_lines: List[str] = list()
29369ef23b6SZach Atkins    for test_line, true_line in zip(test_reader, true_reader):
29469ef23b6SZach Atkins        for key in test_reader.fieldnames:
29569ef23b6SZach Atkins            # Check if the value is numeric
29669ef23b6SZach Atkins            try:
29769ef23b6SZach Atkins                true_val: float = float(true_line[key])
29869ef23b6SZach Atkins                test_val: float = float(test_line[key])
2991b16049aSZach Atkins                true_zero: bool = abs(true_val) < zero_tol
3001b16049aSZach Atkins                test_zero: bool = abs(test_val) < zero_tol
3011b16049aSZach Atkins                fail: bool = False
3021b16049aSZach Atkins                if true_zero:
3031b16049aSZach Atkins                    fail = not test_zero
3041b16049aSZach Atkins                else:
3051b16049aSZach Atkins                    fail = not isclose(test_val, true_val, rel_tol=rel_tol)
3061b16049aSZach Atkins                if fail:
30769ef23b6SZach Atkins                    diff_lines.append(f'column: {key}, expected: {true_val}, got: {test_val}')
30869ef23b6SZach Atkins            except ValueError:
30969ef23b6SZach Atkins                if test_line[key] != true_line[key]:
31069ef23b6SZach Atkins                    diff_lines.append(f'column: {key}, expected: {true_line[key]}, got: {test_line[key]}')
31169ef23b6SZach Atkins
3121b16049aSZach Atkins    return '\n'.join(diff_lines)
3131b16049aSZach Atkins
3141b16049aSZach Atkins
315*83ebc4c4SJeremy L Thompsondef diff_cgns(test_cgns: Path, true_cgns: Path, cgns_tol: float = 1e-12) -> str:
3161b16049aSZach Atkins    """Compare CGNS results against an expected CGSN file with tolerance
3171b16049aSZach Atkins
3181b16049aSZach Atkins    Args:
3191b16049aSZach Atkins        test_cgns (Path): Path to output CGNS file
3201b16049aSZach Atkins        true_cgns (Path): Path to expected CGNS file
321*83ebc4c4SJeremy L Thompson        cgns_tol (float, optional): Tolerance for comparing floating-point values
3221b16049aSZach Atkins
3231b16049aSZach Atkins    Returns:
3241b16049aSZach Atkins        str: Diff output between result and expected CGNS files
3251b16049aSZach Atkins    """
3261b16049aSZach Atkins    my_env: dict = os.environ.copy()
3271b16049aSZach Atkins
328*83ebc4c4SJeremy L Thompson    run_args: List[str] = ['cgnsdiff', '-d', '-t', f'{cgns_tol}', str(test_cgns), str(true_cgns)]
3291b16049aSZach Atkins    proc = subprocess.run(' '.join(run_args),
3301b16049aSZach Atkins                          shell=True,
3311b16049aSZach Atkins                          stdout=subprocess.PIPE,
3321b16049aSZach Atkins                          stderr=subprocess.PIPE,
3331b16049aSZach Atkins                          env=my_env)
3341b16049aSZach Atkins
3351b16049aSZach Atkins    return proc.stderr.decode('utf-8') + proc.stdout.decode('utf-8')
3361b16049aSZach Atkins
3371b16049aSZach Atkins
338e17e35bbSJames Wrightdef test_case_output_string(test_case: TestCase, spec: TestSpec, mode: RunMode,
339e17e35bbSJames Wright                            backend: str, test: str, index: int) -> str:
340e17e35bbSJames Wright    output_str = ''
341e17e35bbSJames Wright    if mode is RunMode.TAP:
342e17e35bbSJames Wright        # print incremental output if TAP mode
343e17e35bbSJames Wright        if test_case.is_skipped():
344e17e35bbSJames Wright            output_str += f'    ok {index} - {spec.name}, {backend} # SKIP {test_case.skipped[0]["message"]}\n'
345e17e35bbSJames Wright        elif test_case.is_failure() or test_case.is_error():
346e17e35bbSJames Wright            output_str += f'    not ok {index} - {spec.name}, {backend}\n'
347e17e35bbSJames Wright        else:
348e17e35bbSJames Wright            output_str += f'    ok {index} - {spec.name}, {backend}\n'
349e17e35bbSJames Wright        output_str += f'      ---\n'
350e17e35bbSJames Wright        if spec.only:
351e17e35bbSJames Wright            output_str += f'      only: {",".join(spec.only)}\n'
352e17e35bbSJames Wright        output_str += f'      args: {test_case.args}\n'
353e17e35bbSJames Wright        if test_case.is_error():
354e17e35bbSJames Wright            output_str += f'      error: {test_case.errors[0]["message"]}\n'
355e17e35bbSJames Wright        if test_case.is_failure():
356e17e35bbSJames Wright            output_str += f'      num_failures: {len(test_case.failures)}\n'
357e17e35bbSJames Wright            for i, failure in enumerate(test_case.failures):
358e17e35bbSJames Wright                output_str += f'      failure_{i}: {failure["message"]}\n'
359e17e35bbSJames Wright                output_str += f'        message: {failure["message"]}\n'
360e17e35bbSJames Wright                if failure["output"]:
361e17e35bbSJames Wright                    out = failure["output"].strip().replace('\n', '\n          ')
362e17e35bbSJames Wright                    output_str += f'        output: |\n          {out}\n'
363e17e35bbSJames Wright        output_str += f'      ...\n'
364e17e35bbSJames Wright    else:
365e17e35bbSJames Wright        # print error or failure information if JUNIT mode
366e17e35bbSJames Wright        if test_case.is_error() or test_case.is_failure():
367e17e35bbSJames Wright            output_str += f'Test: {test} {spec.name}\n'
368e17e35bbSJames Wright            output_str += f'  $ {test_case.args}\n'
369e17e35bbSJames Wright            if test_case.is_error():
370e17e35bbSJames Wright                output_str += 'ERROR: {}\n'.format((test_case.errors[0]['message'] or 'NO MESSAGE').strip())
371e17e35bbSJames Wright                output_str += 'Output: \n{}\n'.format((test_case.errors[0]['output'] or 'NO MESSAGE').strip())
372e17e35bbSJames Wright            if test_case.is_failure():
373e17e35bbSJames Wright                for failure in test_case.failures:
374e17e35bbSJames Wright                    output_str += 'FAIL: {}\n'.format((failure['message'] or 'NO MESSAGE').strip())
375e17e35bbSJames Wright                    output_str += 'Output: \n{}\n'.format((failure['output'] or 'NO MESSAGE').strip())
376e17e35bbSJames Wright    return output_str
377e17e35bbSJames Wright
378e17e35bbSJames Wright
37919868e18SZach Atkinsdef run_test(index: int, test: str, spec: TestSpec, backend: str,
38019868e18SZach Atkins             mode: RunMode, nproc: int, suite_spec: SuiteSpec) -> TestCase:
38119868e18SZach Atkins    """Run a single test case and backend combination
3821b16049aSZach Atkins
3831b16049aSZach Atkins    Args:
3848938a869SZach Atkins        index (int): Index of backend for current spec
38519868e18SZach Atkins        test (str): Path to test
38619868e18SZach Atkins        spec (TestSpec): Specification of test case
38719868e18SZach Atkins        backend (str): CEED backend
38819868e18SZach Atkins        mode (RunMode): Output mode
38919868e18SZach Atkins        nproc (int): Number of MPI processes to use when running test case
39019868e18SZach Atkins        suite_spec (SuiteSpec): Specification of test suite
3911b16049aSZach Atkins
3921b16049aSZach Atkins    Returns:
39319868e18SZach Atkins        TestCase: Test case result
3941b16049aSZach Atkins    """
3951b16049aSZach Atkins    source_path: Path = suite_spec.get_source_path(test)
3968938a869SZach Atkins    run_args: List = [f'{suite_spec.get_run_path(test)}', *map(str, spec.args)]
3971b16049aSZach Atkins
3981b16049aSZach Atkins    if '{ceed_resource}' in run_args:
39919868e18SZach Atkins        run_args[run_args.index('{ceed_resource}')] = backend
4008938a869SZach Atkins    for i, arg in enumerate(run_args):
4018938a869SZach Atkins        if '{ceed_resource}' in arg:
4028938a869SZach Atkins            run_args[i] = arg.replace('{ceed_resource}', backend.replace('/', '-'))
4031b16049aSZach Atkins    if '{nproc}' in run_args:
4041b16049aSZach Atkins        run_args[run_args.index('{nproc}')] = f'{nproc}'
4051b16049aSZach Atkins    elif nproc > 1 and source_path.suffix != '.py':
4061b16049aSZach Atkins        run_args = ['mpiexec', '-n', f'{nproc}', *run_args]
4071b16049aSZach Atkins
4081b16049aSZach Atkins    # run test
40919868e18SZach Atkins    skip_reason: str = suite_spec.check_pre_skip(test, spec, backend, nproc)
4101b16049aSZach Atkins    if skip_reason:
41119868e18SZach Atkins        test_case: TestCase = TestCase(f'{test}, "{spec.name}", n{nproc}, {backend}',
4121b16049aSZach Atkins                                       elapsed_sec=0,
4131b16049aSZach Atkins                                       timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime()),
4141b16049aSZach Atkins                                       stdout='',
4158938a869SZach Atkins                                       stderr='',
4168938a869SZach Atkins                                       category=spec.name,)
4171b16049aSZach Atkins        test_case.add_skipped_info(skip_reason)
4181b16049aSZach Atkins    else:
4191b16049aSZach Atkins        start: float = time.time()
4201b16049aSZach Atkins        proc = subprocess.run(' '.join(str(arg) for arg in run_args),
4211b16049aSZach Atkins                              shell=True,
4221b16049aSZach Atkins                              stdout=subprocess.PIPE,
4231b16049aSZach Atkins                              stderr=subprocess.PIPE,
4241b16049aSZach Atkins                              env=my_env)
4251b16049aSZach Atkins
42619868e18SZach Atkins        test_case = TestCase(f'{test}, "{spec.name}", n{nproc}, {backend}',
4271b16049aSZach Atkins                             classname=source_path.parent,
4281b16049aSZach Atkins                             elapsed_sec=time.time() - start,
4291b16049aSZach Atkins                             timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(start)),
4301b16049aSZach Atkins                             stdout=proc.stdout.decode('utf-8'),
4311b16049aSZach Atkins                             stderr=proc.stderr.decode('utf-8'),
4328938a869SZach Atkins                             allow_multiple_subelements=True,
4338938a869SZach Atkins                             category=spec.name,)
43478cb100bSJames Wright        ref_csvs: List[Path] = []
4358938a869SZach Atkins        output_files: List[str] = [arg for arg in run_args if 'ascii:' in arg]
43697fab443SJeremy L Thompson        if output_files:
4371b16049aSZach Atkins            ref_csvs = [suite_spec.get_output_path(test, file.split('ascii:')[-1]) for file in output_files]
43878cb100bSJames Wright        ref_cgns: List[Path] = []
4398938a869SZach Atkins        output_files = [arg for arg in run_args if 'cgns:' in arg]
44097fab443SJeremy L Thompson        if output_files:
4411b16049aSZach Atkins            ref_cgns = [suite_spec.get_output_path(test, file.split('cgns:')[-1]) for file in output_files]
4421b16049aSZach Atkins        ref_stdout: Path = suite_spec.get_output_path(test, test + '.out')
4431b16049aSZach Atkins        suite_spec.post_test_hook(test, spec)
4441b16049aSZach Atkins
4451b16049aSZach Atkins    # check allowed failures
4461b16049aSZach Atkins    if not test_case.is_skipped() and test_case.stderr:
44719868e18SZach Atkins        skip_reason: str = suite_spec.check_post_skip(test, spec, backend, test_case.stderr)
4481b16049aSZach Atkins        if skip_reason:
4491b16049aSZach Atkins            test_case.add_skipped_info(skip_reason)
4501b16049aSZach Atkins
4511b16049aSZach Atkins    # check required failures
4521b16049aSZach Atkins    if not test_case.is_skipped():
4532fee3251SSebastian Grimberg        required_message, did_fail = suite_spec.check_required_failure(
45419868e18SZach Atkins            test, spec, backend, test_case.stderr)
4551b16049aSZach Atkins        if required_message and did_fail:
4561b16049aSZach Atkins            test_case.status = f'fails with required: {required_message}'
4571b16049aSZach Atkins        elif required_message:
4581b16049aSZach Atkins            test_case.add_failure_info(f'required failure missing: {required_message}')
4591b16049aSZach Atkins
4601b16049aSZach Atkins    # classify other results
4611b16049aSZach Atkins    if not test_case.is_skipped() and not test_case.status:
4621b16049aSZach Atkins        if test_case.stderr:
4631b16049aSZach Atkins            test_case.add_failure_info('stderr', test_case.stderr)
4641b16049aSZach Atkins        if proc.returncode != 0:
4651b16049aSZach Atkins            test_case.add_error_info(f'returncode = {proc.returncode}')
4661b16049aSZach Atkins        if ref_stdout.is_file():
4671b16049aSZach Atkins            diff = list(difflib.unified_diff(ref_stdout.read_text().splitlines(keepends=True),
4681b16049aSZach Atkins                                             test_case.stdout.splitlines(keepends=True),
4691b16049aSZach Atkins                                             fromfile=str(ref_stdout),
4701b16049aSZach Atkins                                             tofile='New'))
4711b16049aSZach Atkins            if diff:
4721b16049aSZach Atkins                test_case.add_failure_info('stdout', output=''.join(diff))
4731b16049aSZach Atkins        elif test_case.stdout and not suite_spec.check_allowed_stdout(test):
4741b16049aSZach Atkins            test_case.add_failure_info('stdout', output=test_case.stdout)
4751b16049aSZach Atkins        # expected CSV output
4761b16049aSZach Atkins        for ref_csv in ref_csvs:
4778938a869SZach Atkins            csv_name = ref_csv.name
4788938a869SZach Atkins            if not ref_csv.is_file():
4798938a869SZach Atkins                # remove _{ceed_backend} from path name
4808938a869SZach Atkins                ref_csv = (ref_csv.parent / ref_csv.name.rsplit('_', 1)[0]).with_suffix('.csv')
4811b16049aSZach Atkins            if not ref_csv.is_file():
4821b16049aSZach Atkins                test_case.add_failure_info('csv', output=f'{ref_csv} not found')
4831b16049aSZach Atkins            else:
4848938a869SZach Atkins                diff: str = diff_csv(Path.cwd() / csv_name, ref_csv)
4851b16049aSZach Atkins                if diff:
4861b16049aSZach Atkins                    test_case.add_failure_info('csv', output=diff)
4871b16049aSZach Atkins                else:
4888938a869SZach Atkins                    (Path.cwd() / csv_name).unlink()
4891b16049aSZach Atkins        # expected CGNS output
4901b16049aSZach Atkins        for ref_cgn in ref_cgns:
4918938a869SZach Atkins            cgn_name = ref_cgn.name
4928938a869SZach Atkins            if not ref_cgn.is_file():
4938938a869SZach Atkins                # remove _{ceed_backend} from path name
4948938a869SZach Atkins                ref_cgn = (ref_cgn.parent / ref_cgn.name.rsplit('_', 1)[0]).with_suffix('.cgns')
4951b16049aSZach Atkins            if not ref_cgn.is_file():
4961b16049aSZach Atkins                test_case.add_failure_info('cgns', output=f'{ref_cgn} not found')
4971b16049aSZach Atkins            else:
498*83ebc4c4SJeremy L Thompson                diff = diff_cgns(Path.cwd() / cgn_name, ref_cgn, cgns_tol=suite_spec.get_cgns_tol())
4991b16049aSZach Atkins                if diff:
5001b16049aSZach Atkins                    test_case.add_failure_info('cgns', output=diff)
5011b16049aSZach Atkins                else:
5028938a869SZach Atkins                    (Path.cwd() / cgn_name).unlink()
5031b16049aSZach Atkins
5041b16049aSZach Atkins    # store result
5051b16049aSZach Atkins    test_case.args = ' '.join(str(arg) for arg in run_args)
506e17e35bbSJames Wright    output_str = test_case_output_string(test_case, spec, mode, backend, test, index)
50719868e18SZach Atkins
50819868e18SZach Atkins    return test_case, output_str
50919868e18SZach Atkins
51019868e18SZach Atkins
51119868e18SZach Atkinsdef init_process():
51219868e18SZach Atkins    """Initialize multiprocessing process"""
51319868e18SZach Atkins    # set up error handler
51419868e18SZach Atkins    global my_env
51519868e18SZach Atkins    my_env = os.environ.copy()
51619868e18SZach Atkins    my_env['CEED_ERROR_HANDLER'] = 'exit'
51719868e18SZach Atkins
51819868e18SZach Atkins
51978cb100bSJames Wrightdef run_tests(test: str, ceed_backends: List[str], mode: RunMode, nproc: int,
52019868e18SZach Atkins              suite_spec: SuiteSpec, pool_size: int = 1) -> TestSuite:
52119868e18SZach Atkins    """Run all test cases for `test` with each of the provided `ceed_backends`
52219868e18SZach Atkins
52319868e18SZach Atkins    Args:
52419868e18SZach Atkins        test (str): Name of test
5258938a869SZach Atkins        ceed_backends (List[str]): List of libCEED backends
52619868e18SZach Atkins        mode (RunMode): Output mode, either `RunMode.TAP` or `RunMode.JUNIT`
52719868e18SZach Atkins        nproc (int): Number of MPI processes to use when running each test case
52819868e18SZach Atkins        suite_spec (SuiteSpec): Object defining required methods for running tests
52919868e18SZach Atkins        pool_size (int, optional): Number of processes to use when running tests in parallel. Defaults to 1.
53019868e18SZach Atkins
53119868e18SZach Atkins    Returns:
53219868e18SZach Atkins        TestSuite: JUnit `TestSuite` containing results of all test cases
53319868e18SZach Atkins    """
53478cb100bSJames Wright    test_specs: List[TestSpec] = get_test_args(suite_spec.get_source_path(test))
53519868e18SZach Atkins    if mode is RunMode.TAP:
5368938a869SZach Atkins        print('TAP version 13')
5378938a869SZach Atkins        print(f'1..{len(test_specs)}')
53819868e18SZach Atkins
53919868e18SZach Atkins    with mp.Pool(processes=pool_size, initializer=init_process) as pool:
5408938a869SZach Atkins        async_outputs: List[List[mp.AsyncResult]] = [
5418938a869SZach Atkins            [pool.apply_async(run_test, (i, test, spec, backend, mode, nproc, suite_spec))
5428938a869SZach Atkins             for (i, backend) in enumerate(ceed_backends, start=1)]
5438938a869SZach Atkins            for spec in test_specs
5448938a869SZach Atkins        ]
54519868e18SZach Atkins
54619868e18SZach Atkins        test_cases = []
5478938a869SZach Atkins        for (i, subtest) in enumerate(async_outputs, start=1):
5488938a869SZach Atkins            is_new_subtest = True
5498938a869SZach Atkins            subtest_ok = True
5508938a869SZach Atkins            for async_output in subtest:
55119868e18SZach Atkins                test_case, print_output = async_output.get()
55219868e18SZach Atkins                test_cases.append(test_case)
5538938a869SZach Atkins                if is_new_subtest and mode == RunMode.TAP:
5548938a869SZach Atkins                    is_new_subtest = False
5558938a869SZach Atkins                    print(f'# Subtest: {test_case.category}')
5568938a869SZach Atkins                    print(f'    1..{len(ceed_backends)}')
55719868e18SZach Atkins                print(print_output, end='')
5588938a869SZach Atkins                if test_case.is_failure() or test_case.is_error():
5598938a869SZach Atkins                    subtest_ok = False
5608938a869SZach Atkins            if mode == RunMode.TAP:
5618938a869SZach Atkins                print(f'{"" if subtest_ok else "not "}ok {i} - {test_case.category}')
5621b16049aSZach Atkins
5631b16049aSZach Atkins    return TestSuite(test, test_cases)
5641b16049aSZach Atkins
5651b16049aSZach Atkins
5661b16049aSZach Atkinsdef write_junit_xml(test_suite: TestSuite, output_file: Optional[Path], batch: str = '') -> None:
5671b16049aSZach Atkins    """Write a JUnit XML file containing the results of a `TestSuite`
5681b16049aSZach Atkins
5691b16049aSZach Atkins    Args:
5701b16049aSZach Atkins        test_suite (TestSuite): JUnit `TestSuite` to write
5711b16049aSZach Atkins        output_file (Optional[Path]): Path to output file, or `None` to generate automatically as `build/{test_suite.name}{batch}.junit`
5721b16049aSZach Atkins        batch (str): Name of JUnit batch, defaults to empty string
5731b16049aSZach Atkins    """
5741b16049aSZach Atkins    output_file: Path = output_file or Path('build') / (f'{test_suite.name}{batch}.junit')
5751b16049aSZach Atkins    output_file.write_text(to_xml_report_string([test_suite]))
5761b16049aSZach Atkins
5771b16049aSZach Atkins
5781b16049aSZach Atkinsdef has_failures(test_suite: TestSuite) -> bool:
5791b16049aSZach Atkins    """Check whether any test cases in a `TestSuite` failed
5801b16049aSZach Atkins
5811b16049aSZach Atkins    Args:
5821b16049aSZach Atkins        test_suite (TestSuite): JUnit `TestSuite` to check
5831b16049aSZach Atkins
5841b16049aSZach Atkins    Returns:
5851b16049aSZach Atkins        bool: True if any test cases failed
5861b16049aSZach Atkins    """
5871b16049aSZach Atkins    return any(c.is_failure() or c.is_error() for c in test_suite.test_cases)
588