Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 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 | package java |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 19 | "android/soong/genrule" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 20 | "fmt" |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
Colin Cross | 227d436 | 2017-08-30 14:14:52 -0700 | [diff] [blame] | 24 | "reflect" |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 25 | "strconv" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 26 | "strings" |
| 27 | "testing" |
| 28 | ) |
| 29 | |
| 30 | var buildDir string |
| 31 | |
| 32 | func setUp() { |
| 33 | var err error |
| 34 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 35 | if err != nil { |
| 36 | panic(err) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func tearDown() { |
| 41 | os.RemoveAll(buildDir) |
| 42 | } |
| 43 | |
| 44 | func TestMain(m *testing.M) { |
| 45 | run := func() int { |
| 46 | setUp() |
| 47 | defer tearDown() |
| 48 | |
| 49 | return m.Run() |
| 50 | } |
| 51 | |
| 52 | os.Exit(run()) |
| 53 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 54 | func testJava(t *testing.T, bp string) *android.TestContext { |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 55 | return testJavaWithEnvFs(t, bp, nil, nil) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 58 | func testJavaWithEnvFs(t *testing.T, bp string, |
| 59 | env map[string]string, fs map[string][]byte) *android.TestContext { |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 60 | config := android.TestArchConfig(buildDir, env) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 61 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 62 | ctx := android.NewTestArchContext() |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 63 | ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory)) |
Colin Cross | a60ead8 | 2017-10-02 18:10:21 -0700 | [diff] [blame] | 64 | ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true))) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 65 | ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory)) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 66 | ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory)) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 67 | ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory)) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 68 | ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory)) |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 69 | ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory)) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 70 | ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory)) |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 71 | ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) |
| 72 | ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 73 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 74 | ctx.Register() |
| 75 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 76 | extraModules := []string{ |
| 77 | "core-oj", |
| 78 | "core-libart", |
| 79 | "framework", |
| 80 | "ext", |
| 81 | "okhttp", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 82 | "android_stubs_current", |
| 83 | "android_system_stubs_current", |
| 84 | "android_test_stubs_current", |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 85 | "kotlin-stdlib", |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 86 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 87 | |
| 88 | for _, extra := range extraModules { |
| 89 | bp += fmt.Sprintf(` |
| 90 | java_library { |
| 91 | name: "%s", |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 92 | srcs: ["a.java"], |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 93 | no_standard_libs: true, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 94 | system_modules: "core-system-modules", |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 95 | } |
| 96 | `, extra) |
| 97 | } |
| 98 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 99 | if config.TargetOpenJDK9() { |
| 100 | systemModules := []string{ |
| 101 | "core-system-modules", |
| 102 | "android_stubs_current_system_modules", |
| 103 | "android_system_stubs_current_system_modules", |
| 104 | "android_test_stubs_current_system_modules", |
| 105 | } |
| 106 | |
| 107 | for _, extra := range systemModules { |
| 108 | bp += fmt.Sprintf(` |
| 109 | java_system_modules { |
| 110 | name: "%s", |
| 111 | } |
| 112 | `, extra) |
| 113 | } |
| 114 | } |
| 115 | |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 116 | mockFS := map[string][]byte{ |
| 117 | "Android.bp": []byte(bp), |
| 118 | "a.java": nil, |
| 119 | "b.java": nil, |
| 120 | "c.java": nil, |
| 121 | "b.kt": nil, |
| 122 | "a.jar": nil, |
| 123 | "b.jar": nil, |
| 124 | "java-res/a": nil, |
| 125 | "java-res/b": nil, |
| 126 | "java-res2/a": nil, |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 127 | |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 128 | "prebuilts/sdk/14/android.jar": nil, |
| 129 | "prebuilts/sdk/14/framework.aidl": nil, |
| 130 | "prebuilts/sdk/current/android.jar": nil, |
| 131 | "prebuilts/sdk/current/framework.aidl": nil, |
| 132 | "prebuilts/sdk/system_current/android.jar": nil, |
| 133 | "prebuilts/sdk/system_current/framework.aidl": nil, |
| 134 | "prebuilts/sdk/test_current/android.jar": nil, |
| 135 | "prebuilts/sdk/test_current/framework.aidl": nil, |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 136 | } |
| 137 | |
| 138 | for k, v := range fs { |
| 139 | mockFS[k] = v |
| 140 | } |
| 141 | |
| 142 | ctx.MockFileSystem(mockFS) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 143 | |
Jeff Gaston | d3e141d | 2017-08-08 17:46:01 -0700 | [diff] [blame] | 144 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 145 | fail(t, errs) |
| 146 | _, errs = ctx.PrepareBuildActions(config) |
| 147 | fail(t, errs) |
| 148 | |
| 149 | return ctx |
| 150 | } |
| 151 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 152 | func moduleToPath(name string) string { |
| 153 | switch { |
| 154 | case name == `""`: |
| 155 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 156 | case strings.HasSuffix(name, ".jar"): |
| 157 | return name |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 158 | case name == "android_stubs_current" || name == "android_system_stubs_current" || |
| 159 | name == "android_test_stubs_current": |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 160 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 161 | default: |
| 162 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 166 | func TestSimple(t *testing.T) { |
| 167 | ctx := testJava(t, ` |
| 168 | java_library { |
| 169 | name: "foo", |
| 170 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 171 | libs: ["bar"], |
| 172 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | java_library { |
| 176 | name: "bar", |
| 177 | srcs: ["b.java"], |
| 178 | } |
| 179 | |
| 180 | java_library { |
| 181 | name: "baz", |
| 182 | srcs: ["c.java"], |
| 183 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 184 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 185 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 186 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 187 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 188 | |
| 189 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 190 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 191 | } |
| 192 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 193 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 194 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 195 | bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 196 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 197 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 198 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 201 | if !strings.Contains(javac.Args["classpath"], bazTurbine) { |
| 202 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 206 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 210 | func TestArchSpecific(t *testing.T) { |
| 211 | ctx := testJava(t, ` |
| 212 | java_library { |
| 213 | name: "foo", |
| 214 | srcs: ["a.java"], |
| 215 | target: { |
| 216 | android: { |
| 217 | srcs: ["b.java"], |
| 218 | }, |
| 219 | }, |
| 220 | } |
| 221 | `) |
| 222 | |
| 223 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 224 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 225 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 226 | } |
| 227 | } |
| 228 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 229 | var classpathTestcases = []struct { |
| 230 | name string |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 231 | moduleType string |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 232 | host android.OsClass |
| 233 | properties string |
| 234 | bootclasspath []string |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 235 | system string |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 236 | classpath []string |
| 237 | }{ |
| 238 | { |
| 239 | name: "default", |
| 240 | bootclasspath: []string{"core-oj", "core-libart"}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 241 | system: "core-system-modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 242 | classpath: []string{"ext", "framework", "okhttp"}, |
| 243 | }, |
| 244 | { |
| 245 | name: "blank sdk version", |
| 246 | properties: `sdk_version: "",`, |
| 247 | bootclasspath: []string{"core-oj", "core-libart"}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 248 | system: "core-system-modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 249 | classpath: []string{"ext", "framework", "okhttp"}, |
| 250 | }, |
| 251 | { |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 252 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 253 | name: "sdk v14", |
| 254 | properties: `sdk_version: "14",`, |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 255 | bootclasspath: []string{`""`}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 256 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 257 | classpath: []string{"prebuilts/sdk/14/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 258 | }, |
| 259 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 260 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 261 | name: "current", |
| 262 | properties: `sdk_version: "current",`, |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 263 | bootclasspath: []string{`""`}, |
| 264 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 265 | classpath: []string{"prebuilts/sdk/current/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 266 | }, |
| 267 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 268 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 269 | name: "system_current", |
| 270 | properties: `sdk_version: "system_current",`, |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 271 | bootclasspath: []string{`""`}, |
| 272 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 273 | classpath: []string{"prebuilts/sdk/system_current/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 274 | }, |
| 275 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 276 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 277 | name: "test_current", |
| 278 | properties: `sdk_version: "test_current",`, |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 279 | bootclasspath: []string{`""`}, |
| 280 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 281 | classpath: []string{"prebuilts/sdk/test_current/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 282 | }, |
| 283 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 284 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 285 | name: "nostdlib", |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 286 | properties: `no_standard_libs: true, system_modules: "none"`, |
| 287 | system: "none", |
| 288 | bootclasspath: []string{`""`}, |
| 289 | classpath: []string{}, |
| 290 | }, |
| 291 | { |
| 292 | |
| 293 | name: "nostdlib system_modules", |
| 294 | properties: `no_standard_libs: true, system_modules: "core-system-modules"`, |
| 295 | system: "core-system-modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 296 | bootclasspath: []string{`""`}, |
| 297 | classpath: []string{}, |
| 298 | }, |
| 299 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 300 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 301 | name: "host default", |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 302 | moduleType: "java_library_host", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 303 | properties: ``, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 304 | host: android.Host, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 305 | classpath: []string{}, |
| 306 | }, |
| 307 | { |
| 308 | name: "host nostdlib", |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 309 | moduleType: "java_library_host", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 310 | host: android.Host, |
| 311 | properties: `no_standard_libs: true`, |
| 312 | classpath: []string{}, |
| 313 | }, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 314 | { |
| 315 | |
| 316 | name: "host supported default", |
| 317 | host: android.Host, |
| 318 | properties: `host_supported: true,`, |
| 319 | classpath: []string{}, |
| 320 | }, |
| 321 | { |
| 322 | name: "host supported nostdlib", |
| 323 | host: android.Host, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 324 | properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 325 | classpath: []string{}, |
| 326 | }, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 327 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 328 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 329 | func TestClasspath(t *testing.T) { |
| 330 | for _, testcase := range classpathTestcases { |
| 331 | t.Run(testcase.name, func(t *testing.T) { |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 332 | moduleType := "java_library" |
| 333 | if testcase.moduleType != "" { |
| 334 | moduleType = testcase.moduleType |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 335 | } |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 336 | |
| 337 | bp := moduleType + ` { |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 338 | name: "foo", |
| 339 | srcs: ["a.java"], |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 340 | ` + testcase.properties + ` |
| 341 | }` |
| 342 | |
| 343 | variant := "android_common" |
| 344 | if testcase.host == android.Host { |
| 345 | variant = android.BuildOs.String() + "_common" |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 346 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 347 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 348 | convertModulesToPaths := func(cp []string) []string { |
| 349 | ret := make([]string, len(cp)) |
| 350 | for i, e := range cp { |
| 351 | ret[i] = moduleToPath(e) |
| 352 | } |
| 353 | return ret |
| 354 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 355 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 356 | bootclasspath := convertModulesToPaths(testcase.bootclasspath) |
| 357 | classpath := convertModulesToPaths(testcase.classpath) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 358 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 359 | bc := strings.Join(bootclasspath, ":") |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 360 | if bc != "" { |
| 361 | bc = "-bootclasspath " + bc |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 362 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 363 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 364 | c := strings.Join(classpath, ":") |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 365 | if c != "" { |
| 366 | c = "-classpath " + c |
| 367 | } |
| 368 | system := "" |
| 369 | if testcase.system == "none" { |
| 370 | system = "--system=none" |
| 371 | } else if testcase.system != "" { |
| 372 | system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/" |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 375 | t.Run("1.8", func(t *testing.T) { |
| 376 | // Test default javac 1.8 |
| 377 | ctx := testJava(t, bp) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 378 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 379 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 380 | |
| 381 | got := javac.Args["bootClasspath"] |
| 382 | if got != bc { |
| 383 | t.Errorf("bootclasspath expected %q != got %q", bc, got) |
| 384 | } |
| 385 | |
| 386 | got = javac.Args["classpath"] |
| 387 | if got != c { |
| 388 | t.Errorf("classpath expected %q != got %q", c, got) |
| 389 | } |
| 390 | |
| 391 | var deps []string |
| 392 | if len(bootclasspath) > 0 && bootclasspath[0] != `""` { |
| 393 | deps = append(deps, bootclasspath...) |
| 394 | } |
| 395 | deps = append(deps, classpath...) |
| 396 | |
| 397 | if !reflect.DeepEqual(javac.Implicits.Strings(), deps) { |
| 398 | t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings()) |
| 399 | } |
| 400 | }) |
| 401 | |
| 402 | // Test again with javac 1.9 |
| 403 | t.Run("1.9", func(t *testing.T) { |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 404 | ctx := testJavaWithEnvFs(t, bp, map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"}, nil) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 405 | |
| 406 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 407 | got := javac.Args["bootClasspath"] |
| 408 | expected := system |
| 409 | if testcase.system == "bootclasspath" { |
| 410 | expected = bc |
| 411 | } |
| 412 | if got != expected { |
| 413 | t.Errorf("bootclasspath expected %q != got %q", expected, got) |
| 414 | } |
| 415 | }) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 416 | }) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | func TestPrebuilts(t *testing.T) { |
| 422 | ctx := testJava(t, ` |
| 423 | java_library { |
| 424 | name: "foo", |
| 425 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 426 | libs: ["bar"], |
| 427 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 430 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 431 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 432 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 435 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 436 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 437 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 438 | } |
| 439 | `) |
| 440 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 441 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 442 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 443 | |
| 444 | bar := "a.jar" |
| 445 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 446 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 447 | } |
| 448 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 449 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" { |
| 450 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 454 | func TestDefaults(t *testing.T) { |
| 455 | ctx := testJava(t, ` |
| 456 | java_defaults { |
| 457 | name: "defaults", |
| 458 | srcs: ["a.java"], |
| 459 | libs: ["bar"], |
| 460 | static_libs: ["baz"], |
| 461 | } |
| 462 | |
| 463 | java_library { |
| 464 | name: "foo", |
| 465 | defaults: ["defaults"], |
| 466 | } |
| 467 | |
| 468 | java_library { |
| 469 | name: "bar", |
| 470 | srcs: ["b.java"], |
| 471 | } |
| 472 | |
| 473 | java_library { |
| 474 | name: "baz", |
| 475 | srcs: ["c.java"], |
| 476 | } |
| 477 | `) |
| 478 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 479 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 480 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 481 | |
| 482 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 483 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 484 | } |
| 485 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 486 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 487 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 488 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 491 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 492 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 493 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 497 | func TestResources(t *testing.T) { |
| 498 | var table = []struct { |
| 499 | name string |
| 500 | prop string |
| 501 | extra string |
| 502 | args string |
| 503 | }{ |
| 504 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 505 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 506 | name: "resource dirs", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 507 | prop: `java_resource_dirs: ["java-res"]`, |
| 508 | args: "-C java-res -f java-res/a -f java-res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 509 | }, |
| 510 | { |
| 511 | // Test that a module with java_resources includes the files |
| 512 | name: "resource files", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 513 | prop: `java_resources: ["java-res/a", "java-res/b"]`, |
| 514 | args: "-C . -f java-res/a -f java-res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 515 | }, |
| 516 | { |
| 517 | // Test that a module with a filegroup in java_resources includes the files with the |
| 518 | // path prefix |
| 519 | name: "resource filegroup", |
| 520 | prop: `java_resources: [":foo-res"]`, |
| 521 | extra: ` |
| 522 | filegroup { |
| 523 | name: "foo-res", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 524 | path: "java-res", |
| 525 | srcs: ["java-res/a", "java-res/b"], |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 526 | }`, |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 527 | args: "-C java-res -f java-res/a -f java-res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 528 | }, |
| 529 | { |
| 530 | // Test that a module with "include_srcs: true" includes its source files in the resources jar |
| 531 | name: "include sources", |
| 532 | prop: `include_srcs: true`, |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 533 | args: "-C . -f a.java -f b.java -f c.java", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 534 | }, |
| 535 | } |
| 536 | |
| 537 | for _, test := range table { |
| 538 | t.Run(test.name, func(t *testing.T) { |
| 539 | ctx := testJava(t, ` |
| 540 | java_library { |
| 541 | name: "foo", |
| 542 | srcs: [ |
| 543 | "a.java", |
| 544 | "b.java", |
| 545 | "c.java", |
| 546 | ], |
| 547 | `+test.prop+`, |
| 548 | } |
| 549 | `+test.extra) |
| 550 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 551 | foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") |
| 552 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 553 | |
| 554 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 555 | t.Errorf("foo combined jars %v does not contain %q", |
| 556 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 557 | } |
| 558 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 559 | if fooRes.Args["jarArgs"] != test.args { |
| 560 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 561 | fooRes.Args["jarArgs"], test.args) |
| 562 | } |
| 563 | }) |
| 564 | } |
| 565 | } |
| 566 | |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 567 | func TestExcludeResources(t *testing.T) { |
| 568 | ctx := testJava(t, ` |
| 569 | java_library { |
| 570 | name: "foo", |
| 571 | srcs: ["a.java"], |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 572 | java_resource_dirs: ["java-res", "java-res2"], |
| 573 | exclude_java_resource_dirs: ["java-res2"], |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | java_library { |
| 577 | name: "bar", |
| 578 | srcs: ["a.java"], |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 579 | java_resources: ["java-res/*"], |
| 580 | exclude_java_resources: ["java-res/b"], |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 581 | } |
| 582 | `) |
| 583 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 584 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 585 | |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 586 | expected := "-C java-res -f java-res/a -f java-res/b" |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 587 | if fooRes.Args["jarArgs"] != expected { |
| 588 | t.Errorf("foo resource jar args %q is not %q", |
| 589 | fooRes.Args["jarArgs"], expected) |
| 590 | |
| 591 | } |
| 592 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 593 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 594 | |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 595 | expected = "-C . -f java-res/a" |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 596 | if barRes.Args["jarArgs"] != expected { |
| 597 | t.Errorf("bar resource jar args %q is not %q", |
| 598 | barRes.Args["jarArgs"], expected) |
| 599 | |
| 600 | } |
| 601 | } |
| 602 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 603 | func TestGeneratedSources(t *testing.T) { |
| 604 | ctx := testJava(t, ` |
| 605 | java_library { |
| 606 | name: "foo", |
| 607 | srcs: [ |
| 608 | "a*.java", |
| 609 | ":gen", |
| 610 | "b*.java", |
| 611 | ], |
| 612 | } |
| 613 | |
| 614 | genrule { |
| 615 | name: "gen", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame^] | 616 | tool_files: ["java-res/a"], |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 617 | out: ["gen.java"], |
| 618 | } |
| 619 | `) |
| 620 | |
| 621 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 622 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 623 | |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 624 | if filepath.Base(genrule.Output.String()) != "gen.java" { |
| 625 | t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String()) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | if len(javac.Inputs) != 3 || |
| 629 | javac.Inputs[0].String() != "a.java" || |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 630 | javac.Inputs[1].String() != genrule.Output.String() || |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 631 | javac.Inputs[2].String() != "b.java" { |
| 632 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 633 | } |
| 634 | } |
| 635 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 636 | func TestKotlin(t *testing.T) { |
| 637 | ctx := testJava(t, ` |
| 638 | java_library { |
| 639 | name: "foo", |
| 640 | srcs: ["a.java", "b.kt"], |
| 641 | } |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 642 | |
| 643 | java_library { |
| 644 | name: "bar", |
| 645 | srcs: ["b.kt"], |
| 646 | } |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 647 | `) |
| 648 | |
| 649 | kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") |
| 650 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 651 | jar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 652 | |
| 653 | if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || |
| 654 | kotlinc.Inputs[1].String() != "b.kt" { |
| 655 | t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs) |
| 656 | } |
| 657 | |
| 658 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 659 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 660 | } |
| 661 | |
| 662 | if !strings.Contains(javac.Args["classpath"], kotlinc.Output.String()) { |
| 663 | t.Errorf("foo classpath %v does not contain %q", |
| 664 | javac.Args["classpath"], kotlinc.Output.String()) |
| 665 | } |
| 666 | |
| 667 | if !inList(kotlinc.Output.String(), jar.Inputs.Strings()) { |
| 668 | t.Errorf("foo jar inputs %v does not contain %q", |
| 669 | jar.Inputs.Strings(), kotlinc.Output.String()) |
| 670 | } |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 671 | |
| 672 | kotlinc = ctx.ModuleForTests("bar", "android_common").Rule("kotlinc") |
| 673 | jar = ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar") |
| 674 | |
| 675 | if len(kotlinc.Inputs) != 1 || kotlinc.Inputs[0].String() != "b.kt" { |
| 676 | t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, kotlinc.Inputs) |
| 677 | } |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 680 | func TestTurbine(t *testing.T) { |
| 681 | ctx := testJava(t, ` |
| 682 | java_library { |
| 683 | name: "foo", |
| 684 | srcs: ["a.java"], |
| 685 | } |
| 686 | |
| 687 | java_library { |
| 688 | name: "bar", |
| 689 | srcs: ["b.java"], |
| 690 | static_libs: ["foo"], |
| 691 | } |
| 692 | |
| 693 | java_library { |
| 694 | name: "baz", |
| 695 | srcs: ["c.java"], |
| 696 | libs: ["bar"], |
| 697 | sdk_version: "14", |
| 698 | } |
| 699 | `) |
| 700 | |
| 701 | fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine") |
| 702 | barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine") |
| 703 | barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac") |
| 704 | barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine") |
| 705 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 706 | |
| 707 | if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" { |
| 708 | t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs) |
| 709 | } |
| 710 | |
| 711 | fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 712 | if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) { |
| 713 | t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar) |
| 714 | } |
| 715 | if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) { |
| 716 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar) |
| 717 | } |
| 718 | if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar { |
| 719 | t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar) |
| 720 | } |
| 721 | if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/android.jar") { |
| 722 | t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"], |
| 723 | "prebuilts/sdk/14/android.jar") |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | func TestSharding(t *testing.T) { |
| 728 | ctx := testJava(t, ` |
| 729 | java_library { |
| 730 | name: "bar", |
| 731 | srcs: ["a.java","b.java","c.java"], |
| 732 | javac_shard_size: 1 |
| 733 | } |
| 734 | `) |
| 735 | |
| 736 | barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 737 | for i := 0; i < 3; i++ { |
| 738 | barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)) |
| 739 | if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) { |
| 740 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar) |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 745 | func fail(t *testing.T, errs []error) { |
| 746 | if len(errs) > 0 { |
| 747 | for _, err := range errs { |
| 748 | t.Error(err) |
| 749 | } |
| 750 | t.FailNow() |
| 751 | } |
| 752 | } |