Phoenix Meadowlark | be1589d | 2020-10-06 21:48:40 -0700 | [diff] [blame] | 1 | # Lint as: python3 |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 2 | # Copyright 2020 The IREE Authors |
Phoenix Meadowlark | be1589d | 2020-10-06 21:48:40 -0700 | [diff] [blame] | 3 | # |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 4 | # Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 5 | # See https://llvm.org/LICENSE.txt for license information. |
| 6 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Phoenix Meadowlark | be1589d | 2020-10-06 21:48:40 -0700 | [diff] [blame] | 7 | |
| 8 | # pylint: disable=missing-docstring |
| 9 | |
| 10 | import argparse |
| 11 | import os |
| 12 | import re |
| 13 | import subprocess |
| 14 | from typing import Sequence |
| 15 | |
Phoenix Meadowlark | be1589d | 2020-10-06 21:48:40 -0700 | [diff] [blame] | 16 | |
| 17 | def create_markdown_table(rows: Sequence[Sequence[str]]): |
| 18 | """Converts a 2D array to a Markdown table.""" |
| 19 | return '\n'.join([' | '.join(row) for row in rows]) |
| 20 | |
| 21 | |
Geoffrey Martin-Noble | eed98f5 | 2020-10-15 18:44:49 -0700 | [diff] [blame] | 22 | def check_and_get_output_lines(command: Sequence[str], |
| 23 | dry_run: bool = False, |
Geoffrey Martin-Noble | 70f91f8 | 2020-10-28 20:46:22 -0700 | [diff] [blame] | 24 | log_stderr: bool = True): |
Phoenix Meadowlark | be1589d | 2020-10-06 21:48:40 -0700 | [diff] [blame] | 25 | print(f'Running: `{" ".join(command)}`') |
| 26 | if dry_run: |
| 27 | return None, None |
Phoenix Meadowlark | 01b507d | 2020-12-08 17:20:48 -0800 | [diff] [blame] | 28 | process = subprocess.run( |
| 29 | command, |
| 30 | # TODO(#4131) python>=3.7: Use capture_output=True. |
| 31 | stderr=subprocess.PIPE, |
| 32 | stdout=subprocess.PIPE, |
| 33 | # TODO(#4131) python>=3.7: Replace 'universal_newlines' with 'text'. |
| 34 | universal_newlines=True) |
Phoenix Meadowlark | be1589d | 2020-10-06 21:48:40 -0700 | [diff] [blame] | 35 | |
| 36 | if log_stderr: |
Geoffrey Martin-Noble | eed98f5 | 2020-10-15 18:44:49 -0700 | [diff] [blame] | 37 | for line in process.stderr.splitlines(): |
Geoffrey Martin-Noble | 70f91f8 | 2020-10-28 20:46:22 -0700 | [diff] [blame] | 38 | print(line) |
Phoenix Meadowlark | be1589d | 2020-10-06 21:48:40 -0700 | [diff] [blame] | 39 | |
Geoffrey Martin-Noble | eed98f5 | 2020-10-15 18:44:49 -0700 | [diff] [blame] | 40 | process.check_returncode() |
Phoenix Meadowlark | be1589d | 2020-10-06 21:48:40 -0700 | [diff] [blame] | 41 | |
Geoffrey Martin-Noble | eed98f5 | 2020-10-15 18:44:49 -0700 | [diff] [blame] | 42 | return process.stdout.splitlines() |
Phoenix Meadowlark | be1589d | 2020-10-06 21:48:40 -0700 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def get_test_targets(test_suite_path: str): |
| 46 | """Returns a list of test targets for the given test suite.""" |
| 47 | # Check if the suite exists (which may not be true for failing suites). |
| 48 | # We use two queries here because the return code for a failed query is |
| 49 | # unfortunately the same as the return code for a bazel configuration error. |
| 50 | target_dir = test_suite_path.split(':')[0] |
Geoffrey Martin-Noble | 70f91f8 | 2020-10-28 20:46:22 -0700 | [diff] [blame] | 51 | query = [ |
| 52 | 'bazel', 'query', '--ui_event_filters=-DEBUG', |
| 53 | '--noshow_loading_progress', '--noshow_progress', f'{target_dir}/...' |
| 54 | ] |
| 55 | targets = check_and_get_output_lines(query) |
Phoenix Meadowlark | be1589d | 2020-10-06 21:48:40 -0700 | [diff] [blame] | 56 | if test_suite_path not in targets: |
| 57 | return [] |
| 58 | |
Geoffrey Martin-Noble | 70f91f8 | 2020-10-28 20:46:22 -0700 | [diff] [blame] | 59 | query = [ |
| 60 | 'bazel', 'query', '--ui_event_filters=-DEBUG', |
| 61 | '--noshow_loading_progress', '--noshow_progress', |
| 62 | f'tests({test_suite_path})' |
| 63 | ] |
| 64 | tests = check_and_get_output_lines(query) |
Phoenix Meadowlark | be1589d | 2020-10-06 21:48:40 -0700 | [diff] [blame] | 65 | return tests |