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) { |
| 33 | runJavaLibraryTestCaseWithRegistrationCtxFunc(t, tc, func(ctx android.RegistrationContext) {}) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | func TestJavaLibrary(t *testing.T) { |
| 37 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 38 | description: "java_library with srcs, exclude_srcs and libs", |
| 39 | blueprint: `java_library { |
| 40 | name: "java-lib-1", |
| 41 | srcs: ["a.java", "b.java"], |
| 42 | exclude_srcs: ["b.java"], |
| 43 | libs: ["java-lib-2"], |
| 44 | bazel_module: { bp2build_available: true }, |
| 45 | } |
| 46 | |
| 47 | java_library { |
| 48 | name: "java-lib-2", |
| 49 | srcs: ["b.java"], |
| 50 | bazel_module: { bp2build_available: true }, |
| 51 | }`, |
| 52 | expectedBazelTargets: []string{ |
| 53 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 54 | "srcs": `["a.java"]`, |
| 55 | "deps": `[":java-lib-2"]`, |
| 56 | }), |
| 57 | makeBazelTarget("java_library", "java-lib-2", attrNameToString{ |
| 58 | "srcs": `["b.java"]`, |
| 59 | }), |
| 60 | }, |
| 61 | }) |
| 62 | } |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 63 | |
| 64 | func TestJavaLibraryConvertsStaticLibsToDepsAndExports(t *testing.T) { |
| 65 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 66 | blueprint: `java_library { |
| 67 | name: "java-lib-1", |
| 68 | srcs: ["a.java"], |
| 69 | libs: ["java-lib-2"], |
| 70 | static_libs: ["java-lib-3"], |
| 71 | bazel_module: { bp2build_available: true }, |
| 72 | } |
| 73 | |
| 74 | java_library { |
| 75 | name: "java-lib-2", |
| 76 | srcs: ["b.java"], |
| 77 | bazel_module: { bp2build_available: false }, |
| 78 | } |
| 79 | |
| 80 | java_library { |
| 81 | name: "java-lib-3", |
| 82 | srcs: ["c.java"], |
| 83 | bazel_module: { bp2build_available: false }, |
| 84 | }`, |
| 85 | expectedBazelTargets: []string{ |
| 86 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 87 | "srcs": `["a.java"]`, |
| 88 | "deps": `[ |
| 89 | ":java-lib-2", |
| 90 | ":java-lib-3", |
| 91 | ]`, |
| 92 | "exports": `[":java-lib-3"]`, |
| 93 | }), |
| 94 | }, |
| 95 | }) |
| 96 | } |
| 97 | |
| 98 | func TestJavaLibraryConvertsStaticLibsToExportsIfNoSrcs(t *testing.T) { |
| 99 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 100 | blueprint: `java_library { |
| 101 | name: "java-lib-1", |
| 102 | static_libs: ["java-lib-2"], |
| 103 | bazel_module: { bp2build_available: true }, |
| 104 | } |
| 105 | |
| 106 | java_library { |
| 107 | name: "java-lib-2", |
| 108 | srcs: ["a.java"], |
| 109 | bazel_module: { bp2build_available: false }, |
| 110 | }`, |
| 111 | expectedBazelTargets: []string{ |
| 112 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 113 | "exports": `[":java-lib-2"]`, |
| 114 | }), |
| 115 | }, |
| 116 | }) |
| 117 | } |
| 118 | |
| 119 | func TestJavaLibraryFailsToConvertLibsWithNoSrcs(t *testing.T) { |
| 120 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 121 | expectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."), |
| 122 | blueprint: `java_library { |
| 123 | name: "java-lib-1", |
| 124 | libs: ["java-lib-2"], |
| 125 | bazel_module: { bp2build_available: true }, |
| 126 | } |
| 127 | |
| 128 | java_library { |
| 129 | name: "java-lib-2", |
| 130 | srcs: ["a.java"], |
| 131 | bazel_module: { bp2build_available: false }, |
| 132 | }`, |
| 133 | expectedBazelTargets: []string{}, |
| 134 | }) |
| 135 | } |
Sam Delmerico | 77267c7 | 2022-03-18 14:11:07 +0000 | [diff] [blame] | 136 | |
| 137 | func TestJavaLibraryPlugins(t *testing.T) { |
| 138 | runJavaLibraryTestCaseWithRegistrationCtxFunc(t, bp2buildTestCase{ |
| 139 | blueprint: `java_library { |
| 140 | name: "java-lib-1", |
| 141 | plugins: ["java-plugin-1"], |
| 142 | bazel_module: { bp2build_available: true }, |
| 143 | } |
| 144 | |
| 145 | java_plugin { |
| 146 | name: "java-plugin-1", |
| 147 | srcs: ["a.java"], |
| 148 | bazel_module: { bp2build_available: false }, |
| 149 | }`, |
| 150 | expectedBazelTargets: []string{ |
| 151 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 152 | "plugins": `[":java-plugin-1"]`, |
| 153 | }), |
| 154 | }, |
| 155 | }, func(ctx android.RegistrationContext) { |
| 156 | ctx.RegisterModuleType("java_plugin", java.PluginFactory) |
| 157 | }) |
| 158 | } |