1#!/bin/bash 2# 3# Copyright 2023 The Bazel Authors. All rights reserved. 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# http://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# --- begin runfiles.bash initialization v2 --- 18# Copy-pasted from the Bazel Bash runfiles library v2. 19set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash 20source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ 21 source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ 22 source "$0.runfiles/$f" 2>/dev/null || \ 23 source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ 24 source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ 25 { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e 26# --- end runfiles.bash initialization v2 --- 27 28source "$(rlocation rules_android/test/bashunit/unittest.bash)" || \ 29 (echo >&2 "Failed to locate bashunit.sh" && exit 1) 30 31source "$(rlocation rules_android/test/rules/android_sdk_repository/android_helper.sh)" || \ 32 (echo >&2 "Failed to locate android_helper.sh" && exit 1) 33 34# Actual tests for Android Sdk Repository 35 36# Test that the dummy SDK exists. 37function test_dummy_sdk() { 38 # Create android SDK 39 local sdk_path="$(create_android_sdk_basic)" 40 41 cat >> WORKSPACE <<EOF 42android_sdk_repository( 43 name = "androidsdk", 44 path = "${sdk_path}", 45) 46EOF 47 48 "${BIT_BAZEL_BINARY}" query @androidsdk//:sdk-dummy >& $TEST_log || fail "Dummy SDK missing" 49} 50 51# Check that the empty BUILD file was created. 52function test_android_sdk_repository_no_path_or_android_home() { 53 cat >> WORKSPACE <<EOF 54android_sdk_repository( 55 name = "androidsdk", 56) 57EOF 58 59 verify_no_android_sdk 60 "${BIT_BAZEL_BINARY}" build @androidsdk//:files >& $TEST_log && fail "Should have failed" || true 61 expect_log "Either the path attribute of android_sdk_repository" 62} 63 64function test_android_sdk_repository_path_from_attribute() { 65 # Create android SDK 66 local sdk_path="$(create_android_sdk_basic)" 67 68 # Add to repository. 69 cat >> WORKSPACE <<EOF 70android_sdk_repository( 71 name = "androidsdk", 72 path = "${sdk_path}", 73) 74EOF 75 76 # Verify the SDK is created correctly. 77 verify_android_sdk 78} 79 80function test_android_sdk_repository_path_from_environment() { 81 # Create android SDK 82 local sdk_path="$(create_android_sdk_basic)" 83 84 # Add to repository. 85 cat >> WORKSPACE <<EOF 86android_sdk_repository( 87 name = "androidsdk", 88) 89EOF 90 91 export ANDROID_HOME="${sdk_path}" 92 # Verify the SDK is created correctly. 93 verify_android_sdk 94} 95 96function test_android_sdk_repository_fails_invalid_path() { 97 # Create an empty SDK directory. 98 mkdir -p "$TEST_TMPDIR/android_sdk" 99 100 # Add to repository with the invalid path 101 cat >> WORKSPACE <<EOF 102android_sdk_repository( 103 name = "androidsdk", 104 path = "$TEST_TMPDIR/android_sdk", 105) 106EOF 107 108 "${BIT_BAZEL_BINARY}" query @androidsdk//:files >& $TEST_log && fail "Should have failed" || true 109 expect_log "No Android SDK apis found in the Android SDK" 110} 111 112function test_build_tools_largest() { 113 # create several build tools 114 local sdk_path="$(create_android_sdk)" 115 add_platforms "${sdk_path}" 31 116 add_build_tools "${sdk_path}" 10.1.2 20.2.3 30.3.4 117 118 # Add to repository. 119 cat >> WORKSPACE <<EOF 120android_sdk_repository( 121 name = "androidsdk", 122 path = "${sdk_path}", 123) 124EOF 125 126 check_android_sdk_provider 127 expect_log "build_tools_version: 30.3.4" 128} 129 130function test_api_level_default() { 131 if [[ "${ENABLE_PLATFORMS:-false}" == "true" ]]; then 132 # TODO(katre): Fix API selection with platforms. 133 return 134 fi 135 # create several api levels 136 local sdk_path="$(create_android_sdk)" 137 add_platforms "${sdk_path}" 31 23 45 138 add_build_tools "${sdk_path}" 30.3.4 139 140 # Add to repository. 141 cat >> WORKSPACE <<EOF 142android_sdk_repository( 143 name = "androidsdk", 144 path = "${sdk_path}", 145) 146EOF 147 148 # Should be the largest API level available 149 check_android_sdk_provider 150 expect_log "api_level: 45" 151} 152 153function test_api_level_specific() { 154 if [[ "${ENABLE_PLATFORMS:-false}" == "true" ]]; then 155 # TODO(katre): Fix API selection with platforms. 156 return 157 fi 158 # create several api levels 159 local sdk_path="$(create_android_sdk)" 160 add_platforms "${sdk_path}" 31 23 45 161 add_build_tools "${sdk_path}" 30.3.4 162 163 # Add to repository. 164 cat >> WORKSPACE <<EOF 165android_sdk_repository( 166 name = "androidsdk", 167 path = "${sdk_path}", 168 api_level = 31, 169) 170EOF 171 172 check_android_sdk_provider 173 expect_log "api_level: 31" 174} 175 176function test_api_level_specific_missing() { 177 # create several api levels 178 local sdk_path="$(create_android_sdk)" 179 add_platforms "${sdk_path}" 31 23 45 180 add_build_tools "${sdk_path}" 30.3.4 181 182 # Add to repository. 183 cat >> WORKSPACE <<EOF 184android_sdk_repository( 185 name = "androidsdk", 186 path = "${sdk_path}", 187 api_level = 30, 188) 189EOF 190 191 "${BIT_BAZEL_BINARY}" query @androidsdk//:files >& $TEST_log && fail "Should have failed" || true 192 expect_log "Android SDK api level 30 was requested but it is not installed" 193} 194 195function test_api_level_flag() { 196 # create several api levels 197 local sdk_path="$(create_android_sdk)" 198 add_platforms "${sdk_path}" 31 23 45 199 add_build_tools "${sdk_path}" 30.3.4 200 201 # Add to repository. 202 cat >> WORKSPACE <<EOF 203android_sdk_repository( 204 name = "androidsdk", 205 path = "${sdk_path}", 206) 207EOF 208 209 check_android_sdk_provider --@androidsdk//:api_level=31 210 expect_log "api_level: 31" 211} 212