Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 1 | // Copyright 2021 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 ( |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 18 | "fmt" |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/java" |
| 23 | ) |
| 24 | |
Sam Delmerico | 77267c7 | 2022-03-18 14:11:07 +0000 | [diff] [blame] | 25 | func runJavaLibraryTestCaseWithRegistrationCtxFunc(t *testing.T, tc bp2buildTestCase, registrationCtxFunc func(ctx android.RegistrationContext)) { |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 26 | t.Helper() |
| 27 | (&tc).moduleTypeUnderTest = "java_library" |
| 28 | (&tc).moduleTypeUnderTestFactory = java.LibraryFactory |
Sam Delmerico | 77267c7 | 2022-03-18 14:11:07 +0000 | [diff] [blame] | 29 | runBp2BuildTestCase(t, registrationCtxFunc, tc) |
| 30 | } |
| 31 | |
| 32 | func runJavaLibraryTestCase(t *testing.T, tc bp2buildTestCase) { |
Sam Delmerico | 58614c0 | 2022-03-15 21:02:09 +0000 | [diff] [blame^] | 33 | t.Helper() |
Sam Delmerico | 77267c7 | 2022-03-18 14:11:07 +0000 | [diff] [blame] | 34 | runJavaLibraryTestCaseWithRegistrationCtxFunc(t, tc, func(ctx android.RegistrationContext) {}) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | func TestJavaLibrary(t *testing.T) { |
| 38 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 39 | description: "java_library with srcs, exclude_srcs and libs", |
| 40 | blueprint: `java_library { |
| 41 | name: "java-lib-1", |
| 42 | srcs: ["a.java", "b.java"], |
| 43 | exclude_srcs: ["b.java"], |
| 44 | libs: ["java-lib-2"], |
| 45 | bazel_module: { bp2build_available: true }, |
| 46 | } |
| 47 | |
| 48 | java_library { |
| 49 | name: "java-lib-2", |
| 50 | srcs: ["b.java"], |
| 51 | bazel_module: { bp2build_available: true }, |
| 52 | }`, |
| 53 | expectedBazelTargets: []string{ |
| 54 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 55 | "srcs": `["a.java"]`, |
| 56 | "deps": `[":java-lib-2"]`, |
| 57 | }), |
| 58 | makeBazelTarget("java_library", "java-lib-2", attrNameToString{ |
| 59 | "srcs": `["b.java"]`, |
| 60 | }), |
| 61 | }, |
| 62 | }) |
| 63 | } |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 64 | |
| 65 | func TestJavaLibraryConvertsStaticLibsToDepsAndExports(t *testing.T) { |
| 66 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 67 | blueprint: `java_library { |
| 68 | name: "java-lib-1", |
| 69 | srcs: ["a.java"], |
| 70 | libs: ["java-lib-2"], |
| 71 | static_libs: ["java-lib-3"], |
| 72 | bazel_module: { bp2build_available: true }, |
| 73 | } |
| 74 | |
| 75 | java_library { |
| 76 | name: "java-lib-2", |
| 77 | srcs: ["b.java"], |
| 78 | bazel_module: { bp2build_available: false }, |
| 79 | } |
| 80 | |
| 81 | java_library { |
| 82 | name: "java-lib-3", |
| 83 | srcs: ["c.java"], |
| 84 | bazel_module: { bp2build_available: false }, |
| 85 | }`, |
| 86 | expectedBazelTargets: []string{ |
| 87 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 88 | "srcs": `["a.java"]`, |
| 89 | "deps": `[ |
| 90 | ":java-lib-2", |
| 91 | ":java-lib-3", |
| 92 | ]`, |
| 93 | "exports": `[":java-lib-3"]`, |
| 94 | }), |
| 95 | }, |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | func TestJavaLibraryConvertsStaticLibsToExportsIfNoSrcs(t *testing.T) { |
| 100 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 101 | blueprint: `java_library { |
| 102 | name: "java-lib-1", |
| 103 | static_libs: ["java-lib-2"], |
| 104 | bazel_module: { bp2build_available: true }, |
| 105 | } |
| 106 | |
| 107 | java_library { |
| 108 | name: "java-lib-2", |
| 109 | srcs: ["a.java"], |
| 110 | bazel_module: { bp2build_available: false }, |
| 111 | }`, |
| 112 | expectedBazelTargets: []string{ |
| 113 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 114 | "exports": `[":java-lib-2"]`, |
| 115 | }), |
| 116 | }, |
| 117 | }) |
| 118 | } |
| 119 | |
| 120 | func TestJavaLibraryFailsToConvertLibsWithNoSrcs(t *testing.T) { |
| 121 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 122 | expectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."), |
| 123 | blueprint: `java_library { |
| 124 | name: "java-lib-1", |
| 125 | libs: ["java-lib-2"], |
| 126 | bazel_module: { bp2build_available: true }, |
| 127 | } |
| 128 | |
| 129 | java_library { |
| 130 | name: "java-lib-2", |
| 131 | srcs: ["a.java"], |
| 132 | bazel_module: { bp2build_available: false }, |
| 133 | }`, |
| 134 | expectedBazelTargets: []string{}, |
| 135 | }) |
| 136 | } |
Sam Delmerico | 77267c7 | 2022-03-18 14:11:07 +0000 | [diff] [blame] | 137 | |
| 138 | func TestJavaLibraryPlugins(t *testing.T) { |
| 139 | runJavaLibraryTestCaseWithRegistrationCtxFunc(t, bp2buildTestCase{ |
| 140 | blueprint: `java_library { |
| 141 | name: "java-lib-1", |
| 142 | plugins: ["java-plugin-1"], |
| 143 | bazel_module: { bp2build_available: true }, |
| 144 | } |
| 145 | |
| 146 | java_plugin { |
| 147 | name: "java-plugin-1", |
| 148 | srcs: ["a.java"], |
| 149 | bazel_module: { bp2build_available: false }, |
| 150 | }`, |
| 151 | expectedBazelTargets: []string{ |
| 152 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 153 | "plugins": `[":java-plugin-1"]`, |
| 154 | }), |
| 155 | }, |
| 156 | }, func(ctx android.RegistrationContext) { |
| 157 | ctx.RegisterModuleType("java_plugin", java.PluginFactory) |
| 158 | }) |
| 159 | } |
Sam Delmerico | 58614c0 | 2022-03-15 21:02:09 +0000 | [diff] [blame^] | 160 | |
| 161 | func TestJavaLibraryErrorproneJavacflagsEnabledManually(t *testing.T) { |
| 162 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 163 | blueprint: `java_library { |
| 164 | name: "java-lib-1", |
| 165 | srcs: ["a.java"], |
| 166 | javacflags: ["-Xsuper-fast"], |
| 167 | errorprone: { |
| 168 | enabled: true, |
| 169 | javacflags: ["-Xep:SpeedLimit:OFF"], |
| 170 | }, |
| 171 | }`, |
| 172 | expectedBazelTargets: []string{ |
| 173 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 174 | "javacopts": `[ |
| 175 | "-Xsuper-fast", |
| 176 | "-Xep:SpeedLimit:OFF", |
| 177 | ]`, |
| 178 | "srcs": `["a.java"]`, |
| 179 | }), |
| 180 | }, |
| 181 | }) |
| 182 | } |
| 183 | |
| 184 | func TestJavaLibraryErrorproneJavacflagsErrorproneDisabledByDefault(t *testing.T) { |
| 185 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 186 | blueprint: `java_library { |
| 187 | name: "java-lib-1", |
| 188 | srcs: ["a.java"], |
| 189 | javacflags: ["-Xsuper-fast"], |
| 190 | errorprone: { |
| 191 | javacflags: ["-Xep:SpeedLimit:OFF"], |
| 192 | }, |
| 193 | }`, |
| 194 | expectedBazelTargets: []string{ |
| 195 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 196 | "javacopts": `["-Xsuper-fast"]`, |
| 197 | "srcs": `["a.java"]`, |
| 198 | }), |
| 199 | }, |
| 200 | }) |
| 201 | } |
| 202 | |
| 203 | func TestJavaLibraryErrorproneJavacflagsErrorproneDisabledManually(t *testing.T) { |
| 204 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 205 | blueprint: `java_library { |
| 206 | name: "java-lib-1", |
| 207 | srcs: ["a.java"], |
| 208 | javacflags: ["-Xsuper-fast"], |
| 209 | errorprone: { |
| 210 | enabled: false, |
| 211 | javacflags: ["-Xep:SpeedLimit:OFF"], |
| 212 | }, |
| 213 | }`, |
| 214 | expectedBazelTargets: []string{ |
| 215 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 216 | "javacopts": `["-Xsuper-fast"]`, |
| 217 | "srcs": `["a.java"]`, |
| 218 | }), |
| 219 | }, |
| 220 | }) |
| 221 | } |