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 # TODO(ronshapiro): enable these with these instructions: 63 # https://docs.bazel.build/versions/master/be/android.html#android_local_test_examples 64 # We probably want to import all of Robolectric's dependencies into bazel-common because there 65 # are some differences (i.e. we both provide Guava). 66 pass 67 68def _GenTests( 69 library_rule_type, 70 test_rule_type, 71 name, 72 srcs, 73 deps, 74 test_only_deps = None, 75 plugins = None, 76 javacopts = None, 77 lib_javacopts = None, 78 test_javacopts = None, 79 functional = True, 80 test_kwargs = {}): 81 _gen_tests( 82 library_rule_type, 83 test_rule_type, 84 name, 85 srcs, 86 deps, 87 test_only_deps, 88 plugins, 89 javacopts, 90 lib_javacopts, 91 test_javacopts, 92 functional, 93 test_kwargs = test_kwargs, 94 ) 95 96 if functional: 97 for (variant_name, extra_javacopts) in BUILD_VARIANTS.items(): 98 variant_javacopts = (javacopts or []) + extra_javacopts 99 _gen_tests( 100 library_rule_type, 101 test_rule_type, 102 name, 103 srcs, 104 deps, 105 test_only_deps, 106 plugins, 107 variant_javacopts, 108 lib_javacopts, 109 test_javacopts, 110 functional, 111 variant_name, 112 test_kwargs = test_kwargs, 113 ) 114 115def _gen_tests( 116 library_rule_type, 117 test_rule_type, 118 name, 119 srcs, 120 deps, 121 test_only_deps, 122 plugins, 123 javacopts, 124 lib_javacopts, 125 test_javacopts, 126 functional, 127 variant_name = None, 128 test_kwargs = {}): 129 if variant_name: 130 suffix = "_" + variant_name 131 tags = [variant_name] 132 133 # Add jvm_flags so that the mode can be accessed from within tests. 134 jvm_flags = ["-Ddagger.mode=" + variant_name] 135 else: 136 suffix = "" 137 tags = [] 138 jvm_flags = [] 139 140 test_files = [] 141 supporting_files = [] 142 143 for src in srcs: 144 if src.endswith("Test.java"): 145 test_files.append(src) 146 else: 147 supporting_files.append(src) 148 149 if not test_only_deps: 150 test_only_deps = [] 151 152 test_deps = test_only_deps + deps 153 if supporting_files: 154 supporting_files_name = name + suffix + "_lib" 155 test_deps.append(":" + supporting_files_name) 156 library_rule_type( 157 name = supporting_files_name, 158 testonly = 1, 159 srcs = supporting_files, 160 javacopts = (javacopts or []) + (lib_javacopts or []), 161 plugins = plugins, 162 tags = tags, 163 deps = deps, 164 ) 165 if functional: 166 _hjar_test(supporting_files_name, tags) 167 168 for test_file in test_files: 169 test_name = test_file.replace(".java", "") 170 prefix_path = "src/test/java/" 171 package_name = native.package_name() 172 if package_name.find("javatests/") != -1: 173 prefix_path = "javatests/" 174 test_class = (package_name + "/" + test_name).rpartition(prefix_path)[2].replace("/", ".") 175 test_rule_type( 176 name = test_name + suffix, 177 srcs = [test_file], 178 javacopts = (javacopts or []) + (test_javacopts or []), 179 jvm_flags = jvm_flags, 180 plugins = plugins, 181 tags = tags, 182 test_class = test_class, 183 deps = test_deps, 184 **test_kwargs 185 ) 186 187def _hjar_test(name, tags): 188 pass 189