1# Copyright (C) 2021 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 15"""Rules used to run device tests""" 16 17_TEST_SRCDIR = "${TEST_SRCDIR}" 18_BAZEL_WORK_DIR = "%s/${TEST_WORKSPACE}/" % _TEST_SRCDIR 19_PY_TOOLCHAIN = "@bazel_tools//tools/python:toolchain_type" 20_TOOLCHAINS = [_PY_TOOLCHAIN] 21 22DeviceEnvironment = provider( 23 "Represents the environment a test will run under. Concretely this is an " + 24 "executable and any runfiles required to trigger execution in the " + 25 "environment.", 26 fields = { 27 "runner": "depset of executable to to setup test environment and execute test.", 28 "data": "runfiles of all needed artifacts in the executable.", 29 }, 30) 31 32def device_test_impl(ctx): 33 runner_script = _BAZEL_WORK_DIR + ctx.attr.run_with[DeviceEnvironment].runner.to_list()[0].short_path 34 test_script = _BAZEL_WORK_DIR + ctx.file.test.short_path 35 script = ctx.actions.declare_file("device_test_%s.sh" % ctx.label.name) 36 path_additions = [] 37 38 ctx.actions.expand_template( 39 template = ctx.file._device_test_template, 40 output = script, 41 is_executable = True, 42 substitutions = { 43 "{runner}": runner_script, 44 "{test_script}": test_script, 45 }, 46 ) 47 48 test_runfiles = ctx.runfiles().merge( 49 ctx.attr.test[DefaultInfo].default_runfiles, 50 ) 51 device_runfiles = ctx.runfiles().merge( 52 ctx.attr.run_with[DeviceEnvironment].data, 53 ) 54 all_runfiles = test_runfiles.merge_all([device_runfiles]) 55 return [DefaultInfo( 56 executable = script, 57 runfiles = all_runfiles, 58 )] 59 60device_test = rule( 61 attrs = { 62 "run_with": attr.label(default = "//bazel/rules:target_device"), 63 "test": attr.label( 64 allow_single_file = True, 65 ), 66 "_device_test_template": attr.label( 67 default = "//bazel/rules:device_test.sh.template", 68 allow_single_file = True, 69 ), 70 }, 71 test = True, 72 implementation = device_test_impl, 73 doc = "Runs a test under a device environment", 74) 75