Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 1 | // Copyright 2022 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 bp2build |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/java" |
| 20 | |
| 21 | "testing" |
| 22 | ) |
| 23 | |
Zi Wang | 734266b | 2023-10-06 11:25:06 -0700 | [diff] [blame] | 24 | func runJavaImportTestCaseWithRegistrationCtxFunc(t *testing.T, tc Bp2buildTestCase, registrationCtxFunc func(ctx android.RegistrationContext)) { |
| 25 | t.Helper() |
| 26 | (&tc).ModuleTypeUnderTest = "java_import" |
| 27 | (&tc).ModuleTypeUnderTestFactory = java.ImportFactory |
| 28 | RunBp2BuildTestCase(t, registrationCtxFunc, tc) |
| 29 | } |
| 30 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 31 | func runJavaImportTestCase(t *testing.T, tc Bp2buildTestCase) { |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 32 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 33 | RunBp2BuildTestCase(t, registerJavaImportModuleTypes, tc) |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | func registerJavaImportModuleTypes(ctx android.RegistrationContext) { |
| 37 | } |
| 38 | |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 39 | func TestJavaImportMinimal(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 40 | runJavaImportTestCase(t, Bp2buildTestCase{ |
| 41 | Description: "Java import - simple example", |
| 42 | ModuleTypeUnderTest: "java_import", |
| 43 | ModuleTypeUnderTestFactory: java.ImportFactory, |
| 44 | Filesystem: map[string]string{ |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 45 | "import.jar": "", |
| 46 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 47 | Blueprint: ` |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 48 | java_import { |
| 49 | name: "example_import", |
| 50 | jars: ["import.jar"], |
| 51 | bazel_module: { bp2build_available: true }, |
| 52 | } |
| 53 | `, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 54 | ExpectedBazelTargets: []string{ |
Liz Kammer | 4e00b09 | 2023-10-11 15:24:24 -0400 | [diff] [blame^] | 55 | MakeBazelTarget("filegroup", "example_import-jars", AttrNameToString{ |
| 56 | "srcs": `["import.jar"]`, |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 57 | }), |
Liz Kammer | 4e00b09 | 2023-10-11 15:24:24 -0400 | [diff] [blame^] | 58 | MakeBazelTarget("java_import", "example_import", AttrNameToString{ |
| 59 | "jars": `[":example_import-jars"]`, |
| 60 | }), |
| 61 | MakeBazelTarget("java_import", "example_import-neverlink", AttrNameToString{ |
| 62 | "jars": `[":example_import-jars"]`, |
| 63 | "neverlink": `True`, |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 64 | }), |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 65 | }}) |
| 66 | } |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 67 | |
| 68 | func TestJavaImportArchVariant(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 69 | runJavaImportTestCase(t, Bp2buildTestCase{ |
| 70 | Description: "Java import - simple example", |
| 71 | ModuleTypeUnderTest: "java_import", |
| 72 | ModuleTypeUnderTestFactory: java.ImportFactory, |
| 73 | Filesystem: map[string]string{ |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 74 | "import.jar": "", |
| 75 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 76 | Blueprint: ` |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 77 | java_import { |
| 78 | name: "example_import", |
| 79 | target: { |
| 80 | android: { |
| 81 | jars: ["android.jar"], |
| 82 | }, |
| 83 | linux_glibc: { |
| 84 | jars: ["linux.jar"], |
| 85 | }, |
| 86 | }, |
| 87 | bazel_module: { bp2build_available: true }, |
| 88 | } |
| 89 | `, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 90 | ExpectedBazelTargets: []string{ |
Liz Kammer | 4e00b09 | 2023-10-11 15:24:24 -0400 | [diff] [blame^] | 91 | MakeBazelTarget("filegroup", "example_import-jars", AttrNameToString{ |
| 92 | "srcs": `select({ |
Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 93 | "//build/bazel_common_rules/platforms/os:android": ["android.jar"], |
| 94 | "//build/bazel_common_rules/platforms/os:linux_glibc": ["linux.jar"], |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 95 | "//conditions:default": [], |
| 96 | })`, |
| 97 | }), |
Liz Kammer | 4e00b09 | 2023-10-11 15:24:24 -0400 | [diff] [blame^] | 98 | MakeBazelTarget("java_import", "example_import", AttrNameToString{ |
| 99 | "jars": `[":example_import-jars"]`, |
| 100 | }), |
| 101 | MakeBazelTarget("java_import", "example_import-neverlink", AttrNameToString{ |
| 102 | "jars": `[":example_import-jars"]`, |
| 103 | "neverlink": `True`, |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 104 | }), |
| 105 | }}) |
| 106 | } |
| 107 | |
| 108 | func TestJavaImportHost(t *testing.T) { |
| 109 | runJavaImportTestCase(t, Bp2buildTestCase{ |
| 110 | Description: "Java import host- simple example", |
| 111 | ModuleTypeUnderTest: "java_import_host", |
| 112 | ModuleTypeUnderTestFactory: java.ImportFactory, |
| 113 | Filesystem: map[string]string{ |
| 114 | "import.jar": "", |
| 115 | }, |
| 116 | Blueprint: ` |
| 117 | java_import_host { |
| 118 | name: "example_import", |
| 119 | jars: ["import.jar"], |
| 120 | bazel_module: { bp2build_available: true }, |
| 121 | } |
| 122 | `, |
| 123 | ExpectedBazelTargets: []string{ |
Liz Kammer | 4e00b09 | 2023-10-11 15:24:24 -0400 | [diff] [blame^] | 124 | MakeBazelTarget("filegroup", "example_import-jars", AttrNameToString{ |
| 125 | "srcs": `["import.jar"]`, |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 126 | }), |
Liz Kammer | 4e00b09 | 2023-10-11 15:24:24 -0400 | [diff] [blame^] | 127 | MakeBazelTarget("java_import", "example_import", AttrNameToString{ |
| 128 | "jars": `[":example_import-jars"]`, |
| 129 | }), |
| 130 | MakeBazelTarget("java_import", "example_import-neverlink", AttrNameToString{ |
| 131 | "jars": `[":example_import-jars"]`, |
| 132 | "neverlink": `True`, |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 133 | }), |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 134 | }}) |
| 135 | } |
Zi Wang | 734266b | 2023-10-06 11:25:06 -0700 | [diff] [blame] | 136 | |
| 137 | func TestJavaImportSameNameAsJavaLibrary(t *testing.T) { |
| 138 | runJavaImportTestCaseWithRegistrationCtxFunc(t, Bp2buildTestCase{ |
| 139 | Description: "java_import has the same name as other package java_library's", |
| 140 | Filesystem: map[string]string{ |
| 141 | "foo/bar/Android.bp": simpleModule("java_library", "test_lib"), |
| 142 | "test.jar": "", |
| 143 | }, |
| 144 | Blueprint: `java_import { |
| 145 | name: "test_lib", |
| 146 | jars: ["test.jar"], |
| 147 | bazel_module: { bp2build_available: true }, |
| 148 | } |
| 149 | `, |
| 150 | ExpectedBazelTargets: []string{ |
Liz Kammer | 4e00b09 | 2023-10-11 15:24:24 -0400 | [diff] [blame^] | 151 | MakeBazelTarget("filegroup", "test_lib-jars", AttrNameToString{ |
| 152 | "srcs": `["test.jar"]`, |
Zi Wang | 734266b | 2023-10-06 11:25:06 -0700 | [diff] [blame] | 153 | }), |
Liz Kammer | 4e00b09 | 2023-10-11 15:24:24 -0400 | [diff] [blame^] | 154 | MakeBazelTarget("java_import", "test_lib", AttrNameToString{ |
| 155 | "jars": `[":test_lib-jars"]`, |
| 156 | }), |
| 157 | MakeBazelTarget("java_import", "test_lib-neverlink", AttrNameToString{ |
| 158 | "jars": `[":test_lib-jars"]`, |
| 159 | "neverlink": `True`, |
Zi Wang | 734266b | 2023-10-06 11:25:06 -0700 | [diff] [blame] | 160 | }), |
| 161 | }, |
| 162 | }, func(ctx android.RegistrationContext) { |
| 163 | ctx.RegisterModuleType("java_library", java.LibraryFactory) |
| 164 | }) |
| 165 | } |