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" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 25 | "strings" |
| 26 | "testing" |
| 27 | ) |
| 28 | |
| 29 | var buildDir string |
| 30 | |
| 31 | func setUp() { |
| 32 | var err error |
| 33 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 34 | if err != nil { |
| 35 | panic(err) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func tearDown() { |
| 40 | os.RemoveAll(buildDir) |
| 41 | } |
| 42 | |
| 43 | func TestMain(m *testing.M) { |
| 44 | run := func() int { |
| 45 | setUp() |
| 46 | defer tearDown() |
| 47 | |
| 48 | return m.Run() |
| 49 | } |
| 50 | |
| 51 | os.Exit(run()) |
| 52 | } |
| 53 | |
| 54 | func testJava(t *testing.T, bp string) *android.TestContext { |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 55 | config := android.TestArchConfig(buildDir) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 56 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 57 | ctx := android.NewTestArchContext() |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 58 | ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory)) |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 59 | ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory)) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 60 | ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory)) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 61 | ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory)) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 62 | ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory)) |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame^] | 63 | ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory)) |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 64 | ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) |
| 65 | ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 66 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 67 | ctx.Register() |
| 68 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 69 | extraModules := []string{ |
| 70 | "core-oj", |
| 71 | "core-libart", |
| 72 | "framework", |
| 73 | "ext", |
| 74 | "okhttp", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 75 | "android_stubs_current", |
| 76 | "android_system_stubs_current", |
| 77 | "android_test_stubs_current", |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 78 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 79 | |
| 80 | for _, extra := range extraModules { |
| 81 | bp += fmt.Sprintf(` |
| 82 | java_library { |
| 83 | name: "%s", |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 84 | srcs: ["a.java"], |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 85 | no_standard_libs: true, |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 86 | } |
| 87 | `, extra) |
| 88 | } |
| 89 | |
| 90 | ctx.MockFileSystem(map[string][]byte{ |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 91 | "Android.bp": []byte(bp), |
| 92 | "a.java": nil, |
| 93 | "b.java": nil, |
| 94 | "c.java": nil, |
| 95 | "a.jar": nil, |
| 96 | "b.jar": nil, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame^] | 97 | "res/a": nil, |
| 98 | "res/b": nil, |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 99 | "prebuilts/sdk/14/android.jar": nil, |
| 100 | "prebuilts/sdk/14/framework.aidl": nil, |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 101 | }) |
| 102 | |
| 103 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
| 104 | fail(t, errs) |
| 105 | _, errs = ctx.PrepareBuildActions(config) |
| 106 | fail(t, errs) |
| 107 | |
| 108 | return ctx |
| 109 | } |
| 110 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 111 | func moduleToPath(name string) string { |
| 112 | switch { |
| 113 | case name == `""`: |
| 114 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 115 | case strings.HasSuffix(name, ".jar"): |
| 116 | return name |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 117 | default: |
Colin Cross | 8649b26 | 2017-09-27 18:03:17 -0700 | [diff] [blame] | 118 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "classes-compiled.jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 122 | func TestSimple(t *testing.T) { |
| 123 | ctx := testJava(t, ` |
| 124 | java_library { |
| 125 | name: "foo", |
| 126 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 127 | libs: ["bar"], |
| 128 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | java_library { |
| 132 | name: "bar", |
| 133 | srcs: ["b.java"], |
| 134 | } |
| 135 | |
| 136 | java_library { |
| 137 | name: "baz", |
| 138 | srcs: ["c.java"], |
| 139 | } |
| 140 | `) |
| 141 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 142 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 143 | combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 144 | |
| 145 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 146 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 147 | } |
| 148 | |
Colin Cross | 8649b26 | 2017-09-27 18:03:17 -0700 | [diff] [blame] | 149 | bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar") |
| 150 | baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 151 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 152 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 153 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 154 | } |
| 155 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 156 | if !strings.Contains(javac.Args["classpath"], baz) { |
| 157 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], baz) |
| 158 | } |
| 159 | |
| 160 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 161 | 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] | 162 | } |
| 163 | } |
| 164 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 165 | var classpathTestcases = []struct { |
| 166 | name string |
| 167 | host android.OsClass |
| 168 | properties string |
| 169 | bootclasspath []string |
| 170 | classpath []string |
| 171 | }{ |
| 172 | { |
| 173 | name: "default", |
| 174 | bootclasspath: []string{"core-oj", "core-libart"}, |
| 175 | classpath: []string{"ext", "framework", "okhttp"}, |
| 176 | }, |
| 177 | { |
| 178 | name: "blank sdk version", |
| 179 | properties: `sdk_version: "",`, |
| 180 | bootclasspath: []string{"core-oj", "core-libart"}, |
| 181 | classpath: []string{"ext", "framework", "okhttp"}, |
| 182 | }, |
| 183 | { |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 184 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 185 | name: "sdk v14", |
| 186 | properties: `sdk_version: "14",`, |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 187 | bootclasspath: []string{`""`}, |
| 188 | classpath: []string{"prebuilts/sdk/14/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 189 | }, |
| 190 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 191 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 192 | name: "current", |
| 193 | properties: `sdk_version: "current",`, |
| 194 | bootclasspath: []string{"android_stubs_current"}, |
| 195 | classpath: []string{}, |
| 196 | }, |
| 197 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 198 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 199 | name: "system_current", |
| 200 | properties: `sdk_version: "system_current",`, |
| 201 | bootclasspath: []string{"android_system_stubs_current"}, |
| 202 | classpath: []string{}, |
| 203 | }, |
| 204 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 205 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 206 | name: "test_current", |
| 207 | properties: `sdk_version: "test_current",`, |
| 208 | bootclasspath: []string{"android_test_stubs_current"}, |
| 209 | classpath: []string{}, |
| 210 | }, |
| 211 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 212 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 213 | name: "nostdlib", |
| 214 | properties: `no_standard_libs: true`, |
| 215 | bootclasspath: []string{`""`}, |
| 216 | classpath: []string{}, |
| 217 | }, |
| 218 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 219 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 220 | name: "host default", |
| 221 | host: android.Host, |
| 222 | properties: ``, |
| 223 | classpath: []string{}, |
| 224 | }, |
| 225 | { |
| 226 | name: "host nostdlib", |
| 227 | host: android.Host, |
| 228 | properties: `no_standard_libs: true`, |
| 229 | classpath: []string{}, |
| 230 | }, |
| 231 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 232 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 233 | func TestClasspath(t *testing.T) { |
| 234 | for _, testcase := range classpathTestcases { |
| 235 | t.Run(testcase.name, func(t *testing.T) { |
| 236 | hostExtra := "" |
| 237 | if testcase.host == android.Host { |
| 238 | hostExtra = "_host" |
| 239 | } |
| 240 | ctx := testJava(t, ` |
| 241 | java_library`+hostExtra+` { |
| 242 | name: "foo", |
| 243 | srcs: ["a.java"], |
| 244 | `+testcase.properties+` |
| 245 | } |
| 246 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 247 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 248 | convertModulesToPaths := func(cp []string) []string { |
| 249 | ret := make([]string, len(cp)) |
| 250 | for i, e := range cp { |
| 251 | ret[i] = moduleToPath(e) |
| 252 | } |
| 253 | return ret |
| 254 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 255 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 256 | bootclasspath := convertModulesToPaths(testcase.bootclasspath) |
| 257 | classpath := convertModulesToPaths(testcase.classpath) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 258 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 259 | variant := "android_common" |
| 260 | if testcase.host == android.Host { |
| 261 | variant = android.BuildOs.String() + "_common" |
| 262 | } |
| 263 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 264 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 265 | got := strings.TrimPrefix(javac.Args["bootClasspath"], "-bootclasspath ") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 266 | bc := strings.Join(bootclasspath, ":") |
| 267 | if got != bc { |
| 268 | t.Errorf("bootclasspath expected %q != got %q", bc, got) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 269 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 270 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 271 | got = strings.TrimPrefix(javac.Args["classpath"], "-classpath ") |
| 272 | c := strings.Join(classpath, ":") |
| 273 | if got != c { |
| 274 | t.Errorf("classpath expected %q != got %q", c, got) |
| 275 | } |
| 276 | |
| 277 | var deps []string |
| 278 | if len(bootclasspath) > 0 && bootclasspath[0] != `""` { |
| 279 | deps = append(deps, bootclasspath...) |
| 280 | } |
| 281 | deps = append(deps, classpath...) |
| 282 | |
| 283 | if !reflect.DeepEqual(javac.Implicits.Strings(), deps) { |
| 284 | t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings()) |
| 285 | } |
| 286 | }) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | func TestPrebuilts(t *testing.T) { |
| 292 | ctx := testJava(t, ` |
| 293 | java_library { |
| 294 | name: "foo", |
| 295 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 296 | libs: ["bar"], |
| 297 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 300 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 301 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 302 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 305 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 306 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 307 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 308 | } |
| 309 | `) |
| 310 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 311 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 312 | combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 313 | |
| 314 | bar := "a.jar" |
| 315 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 316 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 317 | } |
| 318 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 319 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" { |
| 320 | 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] | 321 | } |
| 322 | } |
| 323 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 324 | func TestDefaults(t *testing.T) { |
| 325 | ctx := testJava(t, ` |
| 326 | java_defaults { |
| 327 | name: "defaults", |
| 328 | srcs: ["a.java"], |
| 329 | libs: ["bar"], |
| 330 | static_libs: ["baz"], |
| 331 | } |
| 332 | |
| 333 | java_library { |
| 334 | name: "foo", |
| 335 | defaults: ["defaults"], |
| 336 | } |
| 337 | |
| 338 | java_library { |
| 339 | name: "bar", |
| 340 | srcs: ["b.java"], |
| 341 | } |
| 342 | |
| 343 | java_library { |
| 344 | name: "baz", |
| 345 | srcs: ["c.java"], |
| 346 | } |
| 347 | `) |
| 348 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 349 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 350 | combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 351 | |
| 352 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 353 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 354 | } |
| 355 | |
Colin Cross | 8649b26 | 2017-09-27 18:03:17 -0700 | [diff] [blame] | 356 | bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 357 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 358 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 359 | } |
| 360 | |
Colin Cross | 8649b26 | 2017-09-27 18:03:17 -0700 | [diff] [blame] | 361 | baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 362 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 363 | 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] | 364 | } |
| 365 | } |
| 366 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame^] | 367 | func TestResources(t *testing.T) { |
| 368 | var table = []struct { |
| 369 | name string |
| 370 | prop string |
| 371 | extra string |
| 372 | args string |
| 373 | }{ |
| 374 | { |
| 375 | // Test that a module with java_resource_dirs includes a file list file |
| 376 | name: "resource dirs", |
| 377 | prop: `java_resource_dirs: ["res"]`, |
| 378 | args: "-C res -l ", |
| 379 | }, |
| 380 | { |
| 381 | // Test that a module with java_resources includes the files |
| 382 | name: "resource files", |
| 383 | prop: `java_resources: ["res/a", "res/b"]`, |
| 384 | args: "-C . -f res/a -C . -f res/b", |
| 385 | }, |
| 386 | { |
| 387 | // Test that a module with a filegroup in java_resources includes the files with the |
| 388 | // path prefix |
| 389 | name: "resource filegroup", |
| 390 | prop: `java_resources: [":foo-res"]`, |
| 391 | extra: ` |
| 392 | filegroup { |
| 393 | name: "foo-res", |
| 394 | path: "res", |
| 395 | srcs: ["res/a", "res/b"], |
| 396 | }`, |
| 397 | args: "-C res -f res/a -C res -f res/b", |
| 398 | }, |
| 399 | { |
| 400 | // Test that a module with "include_srcs: true" includes its source files in the resources jar |
| 401 | name: "include sources", |
| 402 | prop: `include_srcs: true`, |
| 403 | args: "-C . -f a.java -C . -f b.java -C . -f c.java", |
| 404 | }, |
| 405 | } |
| 406 | |
| 407 | for _, test := range table { |
| 408 | t.Run(test.name, func(t *testing.T) { |
| 409 | ctx := testJava(t, ` |
| 410 | java_library { |
| 411 | name: "foo", |
| 412 | srcs: [ |
| 413 | "a.java", |
| 414 | "b.java", |
| 415 | "c.java", |
| 416 | ], |
| 417 | `+test.prop+`, |
| 418 | } |
| 419 | `+test.extra) |
| 420 | |
| 421 | foo := ctx.ModuleForTests("foo", "android_common").Output("classes.jar") |
| 422 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar") |
| 423 | |
| 424 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 425 | t.Errorf("foo combined jars %v does not contain %q", |
| 426 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 427 | } |
| 428 | |
| 429 | if !strings.Contains(fooRes.Args["jarArgs"], test.args) { |
| 430 | t.Errorf("foo resource jar args %q does not contain %q", |
| 431 | fooRes.Args["jarArgs"], test.args) |
| 432 | } |
| 433 | }) |
| 434 | } |
| 435 | } |
| 436 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 437 | func fail(t *testing.T, errs []error) { |
| 438 | if len(errs) > 0 { |
| 439 | for _, err := range errs { |
| 440 | t.Error(err) |
| 441 | } |
| 442 | t.FailNow() |
| 443 | } |
| 444 | } |