1#!/bin/bash 2 3# Copyright 2021 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# https://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# Fail on error and print out commands 18set -ex 19 20# By default we don't shard 21SHARD_COUNT=0 22SHARD_INDEX=0 23# By default we don't log 24LOG_FILE="" 25# By default we run tests on device 26DEVICE=true 27 28# Parse parameters 29for i in "$@"; do 30 case $i in 31 --shard-count=*) 32 SHARD_COUNT="${i#*=}" 33 shift 34 ;; 35 --unit-tests) 36 DEVICE=false 37 shift 38 ;; 39 --shard-index=*) 40 SHARD_INDEX="${i#*=}" 41 shift 42 ;; 43 --log-file=*) 44 LOG_FILE="${i#*=}" 45 shift 46 ;; 47 --run-affected) 48 RUN_AFFECTED=true 49 shift 50 ;; 51 --run-flaky-tests) 52 RUN_FLAKY=true 53 shift 54 ;; 55 --affected-base-ref=*) 56 BASE_REF="${i#*=}" 57 shift 58 ;; 59 *) 60 echo "Unknown option" 61 exit 1 62 ;; 63 esac 64done 65 66# Start logcat if we have a file to log to 67if [[ ! -z "$LOG_FILE" ]]; then 68 adb logcat >$LOG_FILE & 69fi 70 71FILTER_OPTS="" 72# Filter out flaky tests if we're not set to run them 73if [[ -z "$RUN_FLAKY" ]]; then 74 FILTER_OPTS="$FILTER_OPTS -Pandroid.testInstrumentationRunnerArguments.notAnnotation=androidx.test.filters.FlakyTest" 75fi 76 77# If we're set to only run affected test, update the Gradle task 78if [[ ! -z "$RUN_AFFECTED" ]]; then 79 if [ "$DEVICE" = true ]; then 80 TASK="runAffectedAndroidTests" 81 else 82 TASK="runAffectedUnitTests" 83 fi 84 TASK="$TASK -Paffected_module_detector.enable" 85 86 # If we have a base branch set, add the Gradle property 87 if [[ ! -z "$BASE_REF" ]]; then 88 TASK="$TASK -Paffected_base_ref=$BASE_REF" 89 fi 90fi 91 92# If we don't have a task yet, use the defaults 93if [[ -z "$TASK" ]]; then 94 if [ "$DEVICE" = true ]; then 95 TASK="connectedCheck" 96 else 97 TASK="testDebug" 98 fi 99fi 100 101SHARD_OPTS="" 102if [ "$SHARD_COUNT" -gt "0" ]; then 103 # If we have a shard count value, create the necessary Gradle property args. 104 # We assume that SHARD_INDEX has been set too 105 SHARD_OPTS="$SHARD_OPTS -Pandroid.testInstrumentationRunnerArguments.numShards=$SHARD_COUNT" 106 SHARD_OPTS="$SHARD_OPTS -Pandroid.testInstrumentationRunnerArguments.shardIndex=$SHARD_INDEX" 107fi 108 109./gradlew --scan --continue --no-configuration-cache --stacktrace $TASK $FILTER_OPTS $SHARD_OPTS 110