xref: /aosp_15_r20/external/tink/java_src/tools/gen_java_test_rules.bzl (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1# Copyright 2017 Google Inc. 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################################################################################
16
17"""Generate Java test rules from given test_files.
18
19Instead of having to create one test rule per test in the BUILD file, this rule
20provides a handy way to create a bunch of test rules for the specified test
21files.
22
23"""
24
25def gen_java_test_rules(
26        test_files,
27        deps,
28        data = [],
29        exclude_tests = [],
30        default_test_size = "small",
31        small_tests = [],
32        medium_tests = [],
33        large_tests = [],
34        enormous_tests = [],
35        flaky_tests = [],
36        manual_tests = [],
37        notsan_tests = [],
38        no_rbe_tests = [],
39        resources = [],
40        tags = [],
41        prefix = "",
42        jvm_flags = [],
43        args = [],
44        visibility = None,
45        shard_count = 1):
46    for test in _get_test_names(test_files):
47        if test in exclude_tests:
48            continue
49        test_size = default_test_size
50        if test in small_tests:
51            test_size = "small"
52        if test in medium_tests:
53            test_size = "medium"
54        if test in large_tests:
55            test_size = "large"
56        if test in enormous_tests:
57            test_size = "enormous"
58        manual = []
59        if test in manual_tests:
60            manual = ["manual"]
61        notsan = []
62        if test in notsan_tests:
63            notsan = ["notsan"]
64        no_rbe = []
65        if test in no_rbe_tests:
66            no_rbe = ["no_rbe"]
67        flaky = 0
68        if (test in flaky_tests) or ("flaky" in tags):
69            flaky = 1
70        java_class = _package_from_path(
71            native.package_name() + "/" + _strip_right(test, ".java"),
72        )
73        native.java_test(
74            name = prefix + test,
75            runtime_deps = deps,
76            data = data,
77            resources = resources,
78            size = test_size,
79            jvm_flags = jvm_flags,
80            args = args,
81            flaky = flaky,
82            tags = tags + manual + notsan + no_rbe,
83            test_class = java_class,
84            visibility = visibility,
85            shard_count = shard_count,
86        )
87
88def _get_test_names(test_files):
89    test_names = []
90    for test_file in test_files:
91        if not test_file.endswith("Test.java"):
92            continue
93        test_names += [test_file[:-5]]
94    return test_names
95
96def _package_from_path(package_path, src_impls = None):
97    src_impls = src_impls or ["src/test/java/", "javatests/", "java/"]
98    for src_impl in src_impls:
99        if not src_impl.endswith("/"):
100            src_impl += "/"
101        index = _index_of_end(package_path, src_impl)
102        if index >= 0:
103            package_path = package_path[index:]
104            break
105    return package_path.replace("/", ".")
106
107def _strip_right(s, suffix):
108    if s.endswith(suffix):
109        return s[0:len(s) - len(suffix)]
110    else:
111        return s
112
113def _index_of_end(s, part):
114    index = s.find(part)
115    if index >= 0:
116        return index + len(part)
117    return -1
118