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