blob: 1aff78aea617ef46c90068c7b8eb35a097632cb4 [file] [log] [blame]
Phoenix Meadowlarkbe1589d2020-10-06 21:48:40 -07001# Lint as: python3
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07002# Copyright 2020 The IREE Authors
Phoenix Meadowlarkbe1589d2020-10-06 21:48:40 -07003#
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07004# 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 Meadowlarkbe1589d2020-10-06 21:48:40 -07007
8# pylint: disable=missing-docstring
9
10import argparse
11import os
12import re
13import subprocess
14from typing import Sequence
15
Phoenix Meadowlarkbe1589d2020-10-06 21:48:40 -070016
17def 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-Nobleeed98f52020-10-15 18:44:49 -070022def check_and_get_output_lines(command: Sequence[str],
23 dry_run: bool = False,
Geoffrey Martin-Noble70f91f82020-10-28 20:46:22 -070024 log_stderr: bool = True):
Phoenix Meadowlarkbe1589d2020-10-06 21:48:40 -070025 print(f'Running: `{" ".join(command)}`')
26 if dry_run:
27 return None, None
Phoenix Meadowlark01b507d2020-12-08 17:20:48 -080028 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 Meadowlarkbe1589d2020-10-06 21:48:40 -070035
36 if log_stderr:
Geoffrey Martin-Nobleeed98f52020-10-15 18:44:49 -070037 for line in process.stderr.splitlines():
Geoffrey Martin-Noble70f91f82020-10-28 20:46:22 -070038 print(line)
Phoenix Meadowlarkbe1589d2020-10-06 21:48:40 -070039
Geoffrey Martin-Nobleeed98f52020-10-15 18:44:49 -070040 process.check_returncode()
Phoenix Meadowlarkbe1589d2020-10-06 21:48:40 -070041
Geoffrey Martin-Nobleeed98f52020-10-15 18:44:49 -070042 return process.stdout.splitlines()
Phoenix Meadowlarkbe1589d2020-10-06 21:48:40 -070043
44
45def 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-Noble70f91f82020-10-28 20:46:22 -070051 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 Meadowlarkbe1589d2020-10-06 21:48:40 -070056 if test_suite_path not in targets:
57 return []
58
Geoffrey Martin-Noble70f91f82020-10-28 20:46:22 -070059 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 Meadowlarkbe1589d2020-10-06 21:48:40 -070065 return tests