blob: 4e3d5f8daeb2c619f88a6284e49aaf37adf19a25 [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
Ray Milkey8705cce2019-01-14 14:05:48 -080023load("//tools/build/bazel:deps_files.bzl", "deps_files")
24
Ray Milkey7dac7da2017-08-01 16:56:05 -070025def testIsExcluded(exclude_tests, test):
Ray Milkey0bcdfd12018-05-23 14:07:19 -070026 for excluded_test in exclude_tests:
27 normalized_excluded_test = excluded_test.replace(".", "/")
28 if test.endswith(normalized_excluded_test):
29 return True
30 return False
Ray Milkey7dac7da2017-08-01 16:56:05 -070031
Ray Milkey0bcdfd12018-05-23 14:07:19 -070032def generate_test_rules(
33 name,
34 test_files,
35 deps,
36 exclude_tests = [],
37 default_test_size = "small",
38 small_tests = [],
39 medium_tests = [],
40 large_tests = [],
41 enormous_tests = [],
42 resources = [],
43 flaky_tests = [],
44 tags = [],
45 prefix = "",
46 jvm_flags = ["-XX:MaxPermSize=128m"],
47 args = [],
48 visibility = None,
49 shard_count = 1):
50 for test in _get_test_names(test_files):
51 if testIsExcluded(exclude_tests, test):
52 continue
53 test_size = default_test_size
54 if test in small_tests:
55 test_size = "small"
56 if test in medium_tests:
57 test_size = "medium"
58 if test in large_tests:
59 test_size = "large"
60 if test in enormous_tests:
61 test_size = "enormous"
62 flaky = 0
63 if (test in flaky_tests) or ("flaky" in tags):
64 flaky = 1
65 java_class = _package_from_path(
Ray Milkey76c9bce2019-01-02 10:50:45 -080066 native.package_name() + "/" + _strip_right(test, ".java"),
Ray Milkey0bcdfd12018-05-23 14:07:19 -070067 )
68 package = java_class[:java_class.rfind(".")]
Ray Milkey8705cce2019-01-14 14:05:48 -080069
Ray Milkey0bcdfd12018-05-23 14:07:19 -070070 native.java_test(
Ray Milkey8705cce2019-01-14 14:05:48 -080071 data = ["@jacoco_agent_runtime//jar"],
Ray Milkey0bcdfd12018-05-23 14:07:19 -070072 name = prefix + test,
73 runtime_deps = deps,
74 resources = resources,
75 size = test_size,
Ray Milkey8705cce2019-01-14 14:05:48 -080076 #jvm_flags = jvm_flags,
Ray Milkey0bcdfd12018-05-23 14:07:19 -070077 args = args,
78 flaky = flaky,
79 tags = tags,
80 test_class = java_class,
81 visibility = visibility,
82 shard_count = shard_count,
Ray Milkey8705cce2019-01-14 14:05:48 -080083 jvm_flags = jvm_flags,
Ray Milkey0bcdfd12018-05-23 14:07:19 -070084 )
Ray Milkey7dac7da2017-08-01 16:56:05 -070085
Ray Milkey8705cce2019-01-14 14:05:48 -080086 jacoco_agent = "$(location @jacoco_agent_runtime//jar)"
87 native.java_test(
88 data = ["@jacoco_agent_runtime//jar"],
89 name = prefix + test + "-coverage",
90 runtime_deps = deps,
91 resources = resources,
92 size = "large",
93 #jvm_flags = jvm_flags,
94 args = args,
95 flaky = flaky,
96 tags = tags,
97 test_class = java_class,
98 visibility = visibility,
99 shard_count = shard_count,
100 jvm_flags = jvm_flags + ["-javaagent:" + jacoco_agent + "=destfile=/tmp/jacoco.exec"],
101 )
102 deps_files(name = name + "-deps", deps = deps)
103
Ray Milkey7dac7da2017-08-01 16:56:05 -0700104def _get_test_names(test_files):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700105 test_names = []
106 for test_file in test_files:
107 if not test_file.endswith("Test.java"):
108 continue
109 test_names += [test_file[:-5]]
110 return test_names
Ray Milkey7dac7da2017-08-01 16:56:05 -0700111
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700112def _package_from_path(package_path, src_impls = None):
113 src_impls = src_impls or ["javatests/", "java/"]
114 for src_impl in src_impls:
115 if not src_impl.endswith("/"):
116 src_impl += "/"
117 index = _index_of_end(package_path, src_impl)
118 if index >= 0:
119 package_path = package_path[index:]
120 break
121 return package_path.replace("/", ".")
Ray Milkey7dac7da2017-08-01 16:56:05 -0700122
123def _strip_right(str, suffix):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700124 """Returns str without the suffix if it ends with suffix."""
125 if str.endswith(suffix):
126 return str[0:len(str) - len(suffix)]
127 else:
128 return str
Ray Milkey7dac7da2017-08-01 16:56:05 -0700129
130def _index_of_end(str, part):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700131 """If part is in str, return the index of the first character after part.
132 Return -1 if part is not in str."""
133 index = str.find(part)
134 if index >= 0:
135 return index + len(part)
136 return -1