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 | |
| 25 | func runJavaLibraryTestCase(t *testing.T, tc bp2buildTestCase) { |
| 26 | t.Helper() |
| 27 | (&tc).moduleTypeUnderTest = "java_library" |
| 28 | (&tc).moduleTypeUnderTestFactory = java.LibraryFactory |
| 29 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) |
| 30 | } |
| 31 | |
| 32 | func TestJavaLibrary(t *testing.T) { |
| 33 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 34 | description: "java_library with srcs, exclude_srcs and libs", |
| 35 | blueprint: `java_library { |
| 36 | name: "java-lib-1", |
| 37 | srcs: ["a.java", "b.java"], |
| 38 | exclude_srcs: ["b.java"], |
| 39 | libs: ["java-lib-2"], |
| 40 | bazel_module: { bp2build_available: true }, |
| 41 | } |
| 42 | |
| 43 | java_library { |
| 44 | name: "java-lib-2", |
| 45 | srcs: ["b.java"], |
| 46 | bazel_module: { bp2build_available: true }, |
| 47 | }`, |
| 48 | expectedBazelTargets: []string{ |
| 49 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 50 | "srcs": `["a.java"]`, |
| 51 | "deps": `[":java-lib-2"]`, |
| 52 | }), |
| 53 | makeBazelTarget("java_library", "java-lib-2", attrNameToString{ |
| 54 | "srcs": `["b.java"]`, |
| 55 | }), |
| 56 | }, |
| 57 | }) |
| 58 | } |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 59 | |
| 60 | func TestJavaLibraryConvertsStaticLibsToDepsAndExports(t *testing.T) { |
| 61 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 62 | blueprint: `java_library { |
| 63 | name: "java-lib-1", |
| 64 | srcs: ["a.java"], |
| 65 | libs: ["java-lib-2"], |
| 66 | static_libs: ["java-lib-3"], |
| 67 | bazel_module: { bp2build_available: true }, |
| 68 | } |
| 69 | |
| 70 | java_library { |
| 71 | name: "java-lib-2", |
| 72 | srcs: ["b.java"], |
| 73 | bazel_module: { bp2build_available: false }, |
| 74 | } |
| 75 | |
| 76 | java_library { |
| 77 | name: "java-lib-3", |
| 78 | srcs: ["c.java"], |
| 79 | bazel_module: { bp2build_available: false }, |
| 80 | }`, |
| 81 | expectedBazelTargets: []string{ |
| 82 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 83 | "srcs": `["a.java"]`, |
| 84 | "deps": `[ |
| 85 | ":java-lib-2", |
| 86 | ":java-lib-3", |
| 87 | ]`, |
| 88 | "exports": `[":java-lib-3"]`, |
| 89 | }), |
| 90 | }, |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | func TestJavaLibraryConvertsStaticLibsToExportsIfNoSrcs(t *testing.T) { |
| 95 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 96 | blueprint: `java_library { |
| 97 | name: "java-lib-1", |
| 98 | static_libs: ["java-lib-2"], |
| 99 | bazel_module: { bp2build_available: true }, |
| 100 | } |
| 101 | |
| 102 | java_library { |
| 103 | name: "java-lib-2", |
| 104 | srcs: ["a.java"], |
| 105 | bazel_module: { bp2build_available: false }, |
| 106 | }`, |
| 107 | expectedBazelTargets: []string{ |
| 108 | makeBazelTarget("java_library", "java-lib-1", attrNameToString{ |
| 109 | "exports": `[":java-lib-2"]`, |
| 110 | }), |
| 111 | }, |
| 112 | }) |
| 113 | } |
| 114 | |
| 115 | func TestJavaLibraryFailsToConvertLibsWithNoSrcs(t *testing.T) { |
| 116 | runJavaLibraryTestCase(t, bp2buildTestCase{ |
| 117 | expectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."), |
| 118 | blueprint: `java_library { |
| 119 | name: "java-lib-1", |
| 120 | libs: ["java-lib-2"], |
| 121 | bazel_module: { bp2build_available: true }, |
| 122 | } |
| 123 | |
| 124 | java_library { |
| 125 | name: "java-lib-2", |
| 126 | srcs: ["a.java"], |
| 127 | bazel_module: { bp2build_available: false }, |
| 128 | }`, |
| 129 | expectedBazelTargets: []string{}, |
| 130 | }) |
| 131 | } |