Jim Tang | 01b1b05 | 2019-11-26 16:53:51 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2019, The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://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, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | """asuite_run_unittests |
| 17 | |
| 18 | This is a unit test wrapper to run tests of aidegen, atest or both. |
| 19 | """ |
| 20 | |
| 21 | |
| 22 | from __future__ import print_function |
| 23 | |
| 24 | import argparse |
| 25 | import os |
| 26 | import shlex |
| 27 | import subprocess |
| 28 | import sys |
| 29 | |
Jim Tang | c4b5921 | 2020-12-01 01:27:13 +0800 | [diff] [blame] | 30 | ASUITE_HOME = os.path.dirname(os.path.realpath(__file__)) |
| 31 | ASUITE_PLUGIN_PATH = os.path.join(ASUITE_HOME, "asuite_plugin") |
| 32 | ATEST_CMD = os.path.join(ASUITE_HOME, "atest", "atest_run_unittests.py") |
| 33 | ATEST2_CMD = os.path.join(ASUITE_HOME, "atest-py2", "atest_run_unittests.py") |
Jim Tang | 01b1b05 | 2019-11-26 16:53:51 +0800 | [diff] [blame] | 34 | AIDEGEN_CMD = "atest aidegen_unittests --host" |
shinwang | d766b6d | 2020-07-15 16:46:05 +0800 | [diff] [blame] | 35 | PLUGIN_LIB_CMD = "atest plugin_lib_unittests --host" |
shinwang | 96b3201 | 2020-02-04 17:41:33 +0800 | [diff] [blame] | 36 | GRADLE_TEST = "/gradlew test" |
Jim Tang | c4b5921 | 2020-12-01 01:27:13 +0800 | [diff] [blame] | 37 | # Definition of exit codes. |
| 38 | EXIT_ALL_CLEAN = 0 |
| 39 | EXIT_TEST_FAIL = 1 |
Jim Tang | 01b1b05 | 2019-11-26 16:53:51 +0800 | [diff] [blame] | 40 | |
| 41 | def run_unittests(files): |
| 42 | """Parse modified files and tell if they belong to aidegen, atest or both. |
| 43 | |
| 44 | Args: |
| 45 | files: a list of files. |
| 46 | |
| 47 | Returns: |
| 48 | True if subprocess.check_call() returns 0. |
| 49 | """ |
shinwang | 96b3201 | 2020-02-04 17:41:33 +0800 | [diff] [blame] | 50 | cmd_dict = {} |
Jim Tang | 01b1b05 | 2019-11-26 16:53:51 +0800 | [diff] [blame] | 51 | for f in files: |
| 52 | if 'atest' in f: |
shinwang | d766b6d | 2020-07-15 16:46:05 +0800 | [diff] [blame] | 53 | cmd_dict.update({ATEST_CMD: None}) |
Jim Tang | c4b5921 | 2020-12-01 01:27:13 +0800 | [diff] [blame] | 54 | if 'atest-py2' in f: |
| 55 | cmd_dict.update({ATEST2_CMD: None}) |
Jim Tang | 01b1b05 | 2019-11-26 16:53:51 +0800 | [diff] [blame] | 56 | if 'aidegen' in f: |
shinwang | d766b6d | 2020-07-15 16:46:05 +0800 | [diff] [blame] | 57 | cmd_dict.update({AIDEGEN_CMD: None}) |
| 58 | if 'plugin_lib' in f: |
| 59 | cmd_dict.update({PLUGIN_LIB_CMD: None}) |
shinwang | 96b3201 | 2020-02-04 17:41:33 +0800 | [diff] [blame] | 60 | if 'asuite_plugin' in f: |
Jim Tang | c4b5921 | 2020-12-01 01:27:13 +0800 | [diff] [blame] | 61 | cmd = ASUITE_PLUGIN_PATH + GRADLE_TEST |
| 62 | cmd_dict.update({cmd : ASUITE_PLUGIN_PATH}) |
Jim Tang | 01b1b05 | 2019-11-26 16:53:51 +0800 | [diff] [blame] | 63 | try: |
shinwang | 96b3201 | 2020-02-04 17:41:33 +0800 | [diff] [blame] | 64 | for cmd, path in cmd_dict.items(): |
| 65 | subprocess.check_call(shlex.split(cmd), cwd=path) |
Jim Tang | 01b1b05 | 2019-11-26 16:53:51 +0800 | [diff] [blame] | 66 | except subprocess.CalledProcessError as error: |
| 67 | print('Unit test failed at:\n\n{}'.format(error.output)) |
| 68 | raise |
| 69 | return True |
| 70 | |
| 71 | |
| 72 | def get_files_to_upload(): |
| 73 | """Parse args or modified files and return them as a list. |
| 74 | |
| 75 | Returns: |
| 76 | A list of files to upload. |
| 77 | """ |
| 78 | parser = argparse.ArgumentParser() |
| 79 | parser.add_argument('preupload_files', nargs='*', help='Files to upload.') |
| 80 | args = parser.parse_args() |
| 81 | files_to_upload = args.preupload_files |
| 82 | if not files_to_upload: |
| 83 | # When running by users directly, only consider: |
| 84 | # added(A), renamed(R) and modified(M) files |
| 85 | # and store them in files_to_upload. |
| 86 | cmd = "git status --short | egrep ^[ARM] | awk '{print $NF}'" |
| 87 | preupload_files = subprocess.check_output(cmd, shell=True, |
| 88 | encoding='utf-8').splitlines() |
| 89 | if preupload_files: |
| 90 | print('Files to upload: %s' % preupload_files) |
| 91 | files_to_upload = preupload_files |
| 92 | else: |
| 93 | sys.exit(EXIT_ALL_CLEAN) |
| 94 | return files_to_upload |
| 95 | |
| 96 | if __name__ == '__main__': |
| 97 | if not run_unittests(get_files_to_upload()): |
| 98 | sys.exit(EXIT_TEST_FAIL) |