• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2020 The Pigweed Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5# use this file except in compliance with the License. You may obtain a copy of
6# the License at
7#
8#     https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations under
14# the License.
15"""The script that runs unit tests on lm3s6965evb-qemu targets."""
16
17import argparse
18import subprocess
19import sys
20
21_TARGET_QEMU_COMMAND = 'qemu-system-arm'
22_TESTS_STARTING_STRING = b'[==========] Running all tests.'
23_TESTS_DONE_STRING = b'[==========] Done running all tests.'
24_TEST_FAILURE_STRING = b'[  FAILED  ]'
25
26
27def handle_test_results(test_output):
28    """Parses test output to determine whether tests passed or failed."""
29    if test_output.find(_TESTS_STARTING_STRING) == -1:
30        return 1
31    if test_output.rfind(_TESTS_DONE_STRING) == -1:
32        return 1
33    if test_output.rfind(_TEST_FAILURE_STRING) != -1:
34        return 1
35    return 0
36
37
38def parse_args():
39    """Parses command-line arguments."""
40    parser = argparse.ArgumentParser(description=__doc__)
41    parser.add_argument('binary', help='The target test binary to run')
42    return parser.parse_args()
43
44
45def launch_tests(binary: str) -> int:
46    """Start a process that runs test on binary."""
47    cmd = [
48        _TARGET_QEMU_COMMAND, '-cpu', 'cortex-m3', '-machine', 'lm3s6965evb',
49        '-nographic', '-no-reboot', '-kernel', binary
50    ]
51    test_process = subprocess.run(cmd, stdout=subprocess.PIPE)
52    print(test_process.stdout.decode('utf-8'))
53    return handle_test_results(test_process.stdout)
54
55
56def main():
57    """Set up runner."""
58    args = parse_args()
59    return launch_tests(args.binary)
60
61
62if __name__ == '__main__':
63    sys.exit(main())
64