1# Copyright 2021 The Bazel Authors. All rights reserved.
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""" Bazel rules that test the Android Local Test rule.
16
17launcher_test: Asserts that the executable is contained in the target's runfiles.
18"""
19
20load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
21load("@bazel_skylib//lib:sets.bzl", "sets")
22
23def _android_local_test_default_launcher(ctx):
24    env = analysistest.begin(ctx)
25    target_under_test = analysistest.target_under_test(env)
26    expected_runfile = getattr(env.ctx.attr, "expected_runfile")
27
28    runfiles = sets.make([f.short_path for f in target_under_test[DefaultInfo].default_runfiles.files.to_list()])
29    asserts.true(env, sets.contains(runfiles, expected_runfile), "Expect runfiles to contains {0}".format(expected_runfile))
30
31    return analysistest.end(env)
32
33android_local_test_default_launcher_test = analysistest.make(
34    _android_local_test_default_launcher,
35    attrs = {
36        "expected_runfile": attr.string(),
37    },
38)
39
40def android_local_test_launcher_test_suite(name, expected_executable):
41    android_local_test_default_launcher_test(
42        name = "android_local_test_default_launcher",
43        target_under_test = ":sample_test_default_launcher",
44        expected_runfile = expected_executable,
45    )
46
47    native.test_suite(
48        name = name,
49        tests = [":android_local_test_default_launcher"],
50    )
51