blob: c8c3976132af06a510aea69d92ca5387ccc1175a [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):
24 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
29
30def generate_test_rules(name,
31 test_files,
32 deps,
33 exclude_tests=[],
34 default_test_size="small",
35 small_tests=[],
36 medium_tests=[],
37 large_tests=[],
38 enormous_tests=[],
39 resources=[],
40 flaky_tests=[],
41 tags=[],
42 prefix="",
43 jvm_flags=["-XX:MaxPermSize=128m"],
44 args=[],
45 visibility=None,
46 shard_count=1):
47 for test in _get_test_names(test_files):
48 if testIsExcluded(exclude_tests, test):
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 flaky = 0
60 if (test in flaky_tests) or ("flaky" in tags):
61 flaky = 1
62 java_class = _package_from_path(
63 PACKAGE_NAME + "/" + _strip_right(test, ".java"))
64 package = java_class[:java_class.rfind(".")]
65 native.java_test(name = prefix + test,
66 runtime_deps = deps,
67 resources = resources,
68 size = test_size,
69 jvm_flags = jvm_flags,
70 args = args,
71 flaky = flaky,
72 tags = tags,
73 test_class = java_class,
74 visibility = visibility,
75 shard_count = shard_count)
76
77
78def _get_test_names(test_files):
79 test_names = []
80 for test_file in test_files:
81 if not test_file.endswith("Test.java"):
82 continue
83 test_names += [test_file[:-5]]
84 return test_names
85
86
87def _package_from_path(package_path, src_impls=None):
88 src_impls = src_impls or ['javatests/', 'java/']
89 for src_impl in src_impls:
90 if not src_impl.endswith('/'):
91 src_impl += '/'
92 index = _index_of_end(package_path, src_impl)
93 if index >= 0:
94 package_path = package_path[index:]
95 break
96 return package_path.replace('/', '.')
97
98
99def _strip_right(str, suffix):
100 """Returns str without the suffix if it ends with suffix."""
101 if str.endswith(suffix):
102 return str[0: len(str) - len(suffix)]
103 else:
104 return str
105
106
107def _index_of_end(str, part):
108 """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