• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018, The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15ATEST_REL_DIR="tools/asuite/atest"
16
17_fetch_testable_modules() {
18    [[ -z $ANDROID_BUILD_TOP ]] && return 0
19    export ATEST_DIR="$ANDROID_BUILD_TOP/$ATEST_REL_DIR"
20    /usr/bin/env python3 - << END
21import os
22import pickle
23import sys
24
25from pathlib import Path
26
27sys.path.append(os.getenv('ATEST_DIR'))
28import constants
29
30index_dir = Path(os.getenv(constants.ANDROID_HOST_OUT)).joinpath('indexes')
31module_index = index_dir.joinpath(constants.MODULE_INDEX)
32if os.path.isfile(module_index):
33    with open(module_index, 'rb') as cache:
34        try:
35            print("\n".join(pickle.load(cache, encoding="utf-8")))
36        except:
37            print("\n".join(pickle.load(cache)))
38else:
39    print("")
40END
41    unset ATEST_DIR
42}
43
44# This function invoke get_args() and return each item
45# of the list for tab completion candidates.
46_fetch_atest_args() {
47    [[ -z $ANDROID_BUILD_TOP ]] && return 0
48    export ATEST_DIR="$ANDROID_BUILD_TOP/$ATEST_REL_DIR"
49    /usr/bin/env python3 - << END
50import os
51import sys
52
53atest_dir = os.path.join(os.getenv('ATEST_DIR'))
54sys.path.append(atest_dir)
55
56import atest_arg_parser
57
58parser = atest_arg_parser.AtestArgParser()
59parser.add_atest_args()
60print("\n".join(parser.get_args()))
61END
62    unset ATEST_DIR
63}
64
65# This function returns devices recognised by adb.
66_fetch_adb_devices() {
67    while read dev; do echo $dev | awk '{print $1}'; done < <(adb devices | egrep -v "^List|^$"||true)
68}
69
70# This function returns all paths contain TEST_MAPPING.
71_fetch_test_mapping_files() {
72    [[ -z $ANDROID_BUILD_TOP ]] && return 0
73    find -maxdepth 5 -type f -name TEST_MAPPING |sed 's/^.\///g'| xargs dirname 2>/dev/null
74}
75
76# The main tab completion function.
77_atest() {
78    local cur prev
79    COMPREPLY=()
80    cur="${COMP_WORDS[COMP_CWORD]}"
81    prev="${COMP_WORDS[COMP_CWORD-1]}"
82    _get_comp_words_by_ref -n : cur prev || true
83
84    case "$cur" in
85        -*)
86            COMPREPLY=($(compgen -W "$(_fetch_atest_args)" -- $cur))
87            ;;
88        */*)
89            ;;
90        *)
91            local candidate_args=$(ls; _fetch_testable_modules)
92            COMPREPLY=($(compgen -W "$candidate_args" -- $cur))
93            ;;
94    esac
95
96    case "$prev" in
97        --iterations|--retry-any-failure|--rerun-until-failure)
98            COMPREPLY=(10) ;;
99        --list-modules|-L)
100            # TODO: genetate the list automately when the API is available.
101            COMPREPLY=($(compgen -W "cts vts" -- $cur)) ;;
102        --serial|-s)
103            local adb_devices="$(_fetch_adb_devices)"
104            if [ -n "$adb_devices" ]; then
105                COMPREPLY=($(compgen -W "$(_fetch_adb_devices)" -- $cur))
106            else
107                # Don't complete files/dirs when there'is no devices.
108                compopt -o nospace
109                COMPREPLY=("")
110            fi ;;
111        --test-mapping|-p)
112            local mapping_files="$(_fetch_test_mapping_files)"
113            if [ -n "$mapping_files" ]; then
114                COMPREPLY=($(compgen -W "$mapping_files" -- $cur))
115            else
116                # Don't complete files/dirs when TEST_MAPPING wasn't found.
117                compopt -o nospace
118                COMPREPLY=("")
119            fi ;;
120    esac
121    __ltrim_colon_completions "$cur" "$prev" || true
122    return 0
123}
124
125function _atest_main() {
126    # Only use this in interactive mode.
127    # Warning: below check must be "return", not "exit". "exit" won't break the
128    # build in interactive shell(e.g VM), but will result in build breakage in
129    # non-interactive shell(e.g docker container); therefore, using "return"
130    # adapts both conditions.
131    [[ ! $- =~ 'i' ]] && return 0
132
133    local T="$(gettop)"
134
135    # Complete file/dir name first by using option "nosort".
136    # BASH version <= 4.3 doesn't have nosort option.
137    # Note that nosort has no effect for zsh.
138    local _atest_comp_options="-o default -o nosort"
139    local _atest_executables=(atest atest-dev atest-src atest-py3)
140    for exec in "${_atest_executables[*]}"; do
141        complete -F _atest $_atest_comp_options $exec 2>/dev/null || \
142        complete -F _atest -o default $exec
143    done
144
145    # Install atest-src for the convenience of debugging.
146    local atest_src="$T/$ATEST_REL_DIR/atest.py"
147    [[ -f "$atest_src" ]] && alias atest-src="$atest_src"
148
149    # Use prebuilt python3 for atest-dev
150    function atest-dev() {
151        atest_dev="$ANDROID_BUILD_TOP/out/host/$(uname -s | tr '[:upper:]' '[:lower:]')-x86/bin/atest-dev"
152        if [ ! -f $atest_dev ]; then
153            echo "Cannot find atest-dev. Run 'm atest' to generate one."
154            return 1
155        fi
156        PREBUILT_TOOLS_DIR="$ANDROID_BUILD_TOP/prebuilts/build-tools/path/linux-x86"
157        PATH=$PREBUILT_TOOLS_DIR:$PATH $atest_dev "$@"
158    }
159}
160
161_atest_main
162