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