Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 1 | # -*- 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. |
| 18 | Instead of having to create one test rule per test in the BUILD file, this rule |
| 19 | provides a handy way to create a bunch of test rules for the specified test |
| 20 | files. |
| 21 | """ |
| 22 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 23 | load("//tools/build/bazel:deps_files.bzl", "deps_files") |
| 24 | |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 25 | def testIsExcluded(exclude_tests, test): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 26 | 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 Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 31 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 32 | def 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 Milkey | 76c9bce | 2019-01-02 10:50:45 -0800 | [diff] [blame] | 66 | native.package_name() + "/" + _strip_right(test, ".java"), |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 67 | ) |
| 68 | package = java_class[:java_class.rfind(".")] |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 69 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 70 | native.java_test( |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 71 | data = ["@jacoco_agent_runtime//jar"], |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 72 | name = prefix + test, |
| 73 | runtime_deps = deps, |
| 74 | resources = resources, |
| 75 | size = test_size, |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 76 | #jvm_flags = jvm_flags, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 77 | args = args, |
| 78 | flaky = flaky, |
| 79 | tags = tags, |
| 80 | test_class = java_class, |
| 81 | visibility = visibility, |
| 82 | shard_count = shard_count, |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 83 | jvm_flags = jvm_flags, |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 84 | ) |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 85 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 86 | 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 Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 104 | def _get_test_names(test_files): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 105 | 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 Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 111 | |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 112 | def _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 Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 122 | |
| 123 | def _strip_right(str, suffix): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 124 | """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 Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 129 | |
| 130 | def _index_of_end(str, part): |
Ray Milkey | 0bcdfd1 | 2018-05-23 14:07:19 -0700 | [diff] [blame] | 131 | """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 |