blob: 8e885daa3e0e24b5aa1f71d6cb62feebae92ed3a [file] [log] [blame]
Jim Tang01b1b052019-11-26 16:53:51 +08001#!/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
18This is a unit test wrapper to run tests of aidegen, atest or both.
19"""
20
21
22from __future__ import print_function
23
24import argparse
25import os
26import shlex
27import subprocess
28import sys
29
30
31EXIT_ALL_CLEAN = 0
32EXIT_TEST_FAIL = 1
shinwang96b32012020-02-04 17:41:33 +080033ASUITE_PLUGIN_PATH = "tools/asuite/asuite_plugin"
Jim Tang01b1b052019-11-26 16:53:51 +080034# TODO: remove echo when atest migration has done.
35ATEST_CMD = "echo {}/tools/asuite/atest/atest_run_unittests.py".format(
36 os.getenv('ANDROID_BUILD_TOP'))
37AIDEGEN_CMD = "atest aidegen_unittests --host"
shinwang96b32012020-02-04 17:41:33 +080038GRADLE_TEST = "/gradlew test"
Jim Tang01b1b052019-11-26 16:53:51 +080039
40
41def 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 """
shinwang96b32012020-02-04 17:41:33 +080050 cmd_dict = {}
Jim Tang01b1b052019-11-26 16:53:51 +080051 for f in files:
52 if 'atest' in f:
shinwang96b32012020-02-04 17:41:33 +080053 cmd_dict.update({ATEST_CMD : None})
Jim Tang01b1b052019-11-26 16:53:51 +080054 if 'aidegen' in f:
shinwang96b32012020-02-04 17:41:33 +080055 cmd_dict.update({AIDEGEN_CMD : None})
56 if 'asuite_plugin' in f:
57 full_path = os.path.join(
58 os.getenv('ANDROID_BUILD_TOP'), ASUITE_PLUGIN_PATH)
59 cmd = full_path + GRADLE_TEST
60 cmd_dict.update({cmd : full_path})
Jim Tang01b1b052019-11-26 16:53:51 +080061 try:
shinwang96b32012020-02-04 17:41:33 +080062 for cmd, path in cmd_dict.items():
63 subprocess.check_call(shlex.split(cmd), cwd=path)
Jim Tang01b1b052019-11-26 16:53:51 +080064 except subprocess.CalledProcessError as error:
65 print('Unit test failed at:\n\n{}'.format(error.output))
66 raise
67 return True
68
69
70def get_files_to_upload():
71 """Parse args or modified files and return them as a list.
72
73 Returns:
74 A list of files to upload.
75 """
76 parser = argparse.ArgumentParser()
77 parser.add_argument('preupload_files', nargs='*', help='Files to upload.')
78 args = parser.parse_args()
79 files_to_upload = args.preupload_files
80 if not files_to_upload:
81 # When running by users directly, only consider:
82 # added(A), renamed(R) and modified(M) files
83 # and store them in files_to_upload.
84 cmd = "git status --short | egrep ^[ARM] | awk '{print $NF}'"
85 preupload_files = subprocess.check_output(cmd, shell=True,
86 encoding='utf-8').splitlines()
87 if preupload_files:
88 print('Files to upload: %s' % preupload_files)
89 files_to_upload = preupload_files
90 else:
91 sys.exit(EXIT_ALL_CLEAN)
92 return files_to_upload
93
94if __name__ == '__main__':
95 if not run_unittests(get_files_to_upload()):
96 sys.exit(EXIT_TEST_FAIL)