1# Copyright (C) 2017 The Dagger Authors. 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"""This file defines constants useful across the Dagger tests.""" 16 17load("@rules_java//java:defs.bzl", "java_library", "java_test") 18 19# Defines a set of build variants and the list of extra javacopts to build with. 20# The key will be appended to the generated test names to ensure uniqueness. 21BUILD_VARIANTS = { 22 "FastInit": ["-Adagger.fastInit=enabled"], 23} 24 25# TODO(ronshapiro): convert this to use bazel_common 26# TODO(bcorso): split into two functions for functional vs non-functional tests? 27def GenJavaTests( 28 name, 29 srcs, 30 deps, 31 test_only_deps = None, 32 plugins = None, 33 javacopts = None, 34 lib_javacopts = None, 35 test_javacopts = None, 36 functional = True): 37 _GenTests( 38 java_library, 39 java_test, 40 name, 41 srcs, 42 deps, 43 test_only_deps, 44 plugins, 45 javacopts, 46 lib_javacopts, 47 test_javacopts, 48 functional, 49 ) 50 51def GenRobolectricTests( 52 name, 53 srcs, 54 deps, 55 test_only_deps = None, 56 plugins = None, 57 javacopts = None, 58 lib_javacopts = None, 59 test_javacopts = None, 60 functional = True, 61 manifest_values = None): 62 deps = (deps or []) + ["//:android_local_test_exports"] 63 _GenTests( 64 native.android_library, 65 native.android_local_test, 66 name, 67 srcs, 68 deps, 69 test_only_deps, 70 plugins, 71 javacopts, 72 lib_javacopts, 73 test_javacopts, 74 functional, 75 test_kwargs = {"manifest_values": manifest_values}, 76 ) 77 78def _GenTests( 79 library_rule_type, 80 test_rule_type, 81 name, 82 srcs, 83 deps, 84 test_only_deps = None, 85 plugins = None, 86 javacopts = None, 87 lib_javacopts = None, 88 test_javacopts = None, 89 functional = True, 90 test_kwargs = {}): 91 _gen_tests( 92 library_rule_type, 93 test_rule_type, 94 name, 95 srcs, 96 deps, 97 test_only_deps, 98 plugins, 99 javacopts, 100 lib_javacopts, 101 test_javacopts, 102 functional, 103 test_kwargs = test_kwargs, 104 ) 105 106 if functional: 107 for (variant_name, extra_javacopts) in BUILD_VARIANTS.items(): 108 variant_javacopts = (javacopts or []) + extra_javacopts 109 _gen_tests( 110 library_rule_type, 111 test_rule_type, 112 name, 113 srcs, 114 deps, 115 test_only_deps, 116 plugins, 117 variant_javacopts, 118 lib_javacopts, 119 test_javacopts, 120 functional, 121 variant_name, 122 test_kwargs = test_kwargs, 123 ) 124 125def _gen_tests( 126 library_rule_type, 127 test_rule_type, 128 name, 129 srcs, 130 deps, 131 test_only_deps, 132 plugins, 133 javacopts, 134 lib_javacopts, 135 test_javacopts, 136 functional, 137 variant_name = None, 138 test_kwargs = {}): 139 if variant_name: 140 suffix = "_" + variant_name 141 tags = [variant_name] 142 143 # Add jvm_flags so that the mode can be accessed from within tests. 144 jvm_flags = ["-Ddagger.mode=" + variant_name] 145 else: 146 suffix = "" 147 tags = [] 148 jvm_flags = [] 149 150 test_files = [] 151 supporting_files = [] 152 153 for src in srcs: 154 if src.endswith("Test.java"): 155 test_files.append(src) 156 else: 157 supporting_files.append(src) 158 159 if not test_only_deps: 160 test_only_deps = [] 161 162 test_deps = test_only_deps + deps 163 if supporting_files: 164 supporting_files_name = name + suffix + "_lib" 165 test_deps.append(":" + supporting_files_name) 166 library_rule_type( 167 name = supporting_files_name, 168 testonly = 1, 169 srcs = supporting_files, 170 javacopts = (javacopts or []) + (lib_javacopts or []), 171 plugins = plugins, 172 tags = tags, 173 deps = deps, 174 ) 175 if functional: 176 _hjar_test(supporting_files_name, tags) 177 178 for test_file in test_files: 179 test_name = test_file.replace(".java", "") 180 prefix_path = "src/test/java/" 181 package_name = native.package_name() 182 if package_name.find("javatests/") != -1: 183 prefix_path = "javatests/" 184 test_class = (package_name + "/" + test_name).rpartition(prefix_path)[2].replace("/", ".") 185 test_rule_type( 186 name = test_name + suffix, 187 srcs = [test_file], 188 javacopts = (javacopts or []) + (test_javacopts or []), 189 jvm_flags = jvm_flags, 190 plugins = plugins, 191 tags = tags, 192 test_class = test_class, 193 deps = test_deps, 194 **test_kwargs 195 ) 196 197def _hjar_test(name, tags): 198 pass 199