blob: 17f811e33f56f5157f17c72b93926a6073bea74f [file] [log] [blame]
Ray Milkey7dac7da2017-08-01 16:56:05 -07001# -*- mode:python; -*-
2#
3# Copyright 2016 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"""Generate java test rules from given test_files.
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
23def testIsExcluded(exclude_tests, test):
Ray Milkey0bcdfd12018-05-23 14:07:19 -070024 for excluded_test in exclude_tests:
25 normalized_excluded_test = excluded_test.replace(".", "/")
26 if test.endswith(normalized_excluded_test):
27 return True
28 return False
Ray Milkey7dac7da2017-08-01 16:56:05 -070029
Ray Milkey0bcdfd12018-05-23 14:07:19 -070030def generate_test_rules(
31 name,
32 test_files,
33 deps,
34 exclude_tests = [],
35 default_test_size = "small",
36 small_tests = [],
37 medium_tests = [],
38 large_tests = [],
39 enormous_tests = [],
40 resources = [],
41 flaky_tests = [],
42 tags = [],
43 prefix = "",
44 jvm_flags = ["-XX:MaxPermSize=128m"],
45 args = [],
46 visibility = None,
47 shard_count = 1):
48 for test in _get_test_names(test_files):
49 if testIsExcluded(exclude_tests, test):
50 continue
51 test_size = default_test_size
52 if test in small_tests:
53 test_size = "small"
54 if test in medium_tests:
55 test_size = "medium"
56 if test in large_tests:
57 test_size = "large"
58 if test in enormous_tests:
59 test_size = "enormous"
60 flaky = 0
61 if (test in flaky_tests) or ("flaky" in tags):
62 flaky = 1
63 java_class = _package_from_path(
Ray Milkey1a106302019-01-02 10:50:45 -080064 native.package_name() + "/" + _strip_right(test, ".java"),
Ray Milkey0bcdfd12018-05-23 14:07:19 -070065 )
66 package = java_class[:java_class.rfind(".")]
67 native.java_test(
68 name = prefix + test,
69 runtime_deps = deps,
70 resources = resources,
71 size = test_size,
72 jvm_flags = jvm_flags,
73 args = args,
74 flaky = flaky,
75 tags = tags,
76 test_class = java_class,
77 visibility = visibility,
78 shard_count = shard_count,
79 )
Ray Milkey7dac7da2017-08-01 16:56:05 -070080
81def _get_test_names(test_files):
Ray Milkey0bcdfd12018-05-23 14:07:19 -070082 test_names = []
83 for test_file in test_files:
84 if not test_file.endswith("Test.java"):
85 continue
86 test_names += [test_file[:-5]]
87 return test_names
Ray Milkey7dac7da2017-08-01 16:56:05 -070088
Ray Milkey0bcdfd12018-05-23 14:07:19 -070089def _package_from_path(package_path, src_impls = None):
90 src_impls = src_impls or ["javatests/", "java/"]
91 for src_impl in src_impls:
92 if not src_impl.endswith("/"):
93 src_impl += "/"
94 index = _index_of_end(package_path, src_impl)
95 if index >= 0:
96 package_path = package_path[index:]
97 break
98 return package_path.replace("/", ".")
Ray Milkey7dac7da2017-08-01 16:56:05 -070099
100def _strip_right(str, suffix):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700101 """Returns str without the suffix if it ends with suffix."""
102 if str.endswith(suffix):
103 return str[0:len(str) - len(suffix)]
104 else:
105 return str
Ray Milkey7dac7da2017-08-01 16:56:05 -0700106
107def _index_of_end(str, part):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700108 """If part is in str, return the index of the first character after part.
109 Return -1 if part is not in str."""
110 index = str.find(part)
111 if index >= 0:
112 return index + len(part)
113 return -1