Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 1 | // Copyright 2023 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 | "testing" |
| 19 | |
| 20 | "github.com/google/blueprint/bootstrap" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | func runGoTests(t *testing.T, tc Bp2buildTestCase) { |
| 26 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) { |
| 27 | tCtx := ctx.(*android.TestContext) |
| 28 | bootstrap.RegisterGoModuleTypes(tCtx.Context.Context) // android.TestContext --> android.Context --> blueprint.Context |
| 29 | }, tc) |
| 30 | } |
| 31 | |
| 32 | func TestConvertGoPackage(t *testing.T) { |
| 33 | bp := ` |
| 34 | bootstrap_go_package { |
| 35 | name: "foo", |
| 36 | pkgPath: "android/foo", |
| 37 | deps: [ |
| 38 | "bar", |
| 39 | ], |
| 40 | srcs: [ |
| 41 | "foo1.go", |
| 42 | "foo2.go", |
| 43 | ], |
| 44 | linux: { |
| 45 | srcs: [ |
| 46 | "foo_linux.go", |
| 47 | ], |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 48 | testSrcs: [ |
| 49 | "foo_linux_test.go", |
| 50 | ], |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 51 | }, |
| 52 | darwin: { |
| 53 | srcs: [ |
| 54 | "foo_darwin.go", |
| 55 | ], |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 56 | testSrcs: [ |
| 57 | "foo_darwin_test.go", |
| 58 | ], |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 59 | }, |
| 60 | testSrcs: [ |
| 61 | "foo1_test.go", |
| 62 | "foo2_test.go", |
| 63 | ], |
| 64 | } |
| 65 | ` |
| 66 | depBp := ` |
| 67 | bootstrap_go_package { |
| 68 | name: "bar", |
| 69 | } |
| 70 | ` |
| 71 | t.Parallel() |
| 72 | runGoTests(t, Bp2buildTestCase{ |
| 73 | Description: "Convert bootstrap_go_package to go_library", |
| 74 | ModuleTypeUnderTest: "bootrstap_go_package", |
| 75 | Blueprint: bp, |
| 76 | Filesystem: map[string]string{ |
| 77 | "bar/Android.bp": depBp, // Put dep in Android.bp to reduce boilerplate in ExpectedBazelTargets |
| 78 | }, |
| 79 | ExpectedBazelTargets: []string{makeBazelTargetHostOrDevice("go_library", "foo", |
| 80 | AttrNameToString{ |
| 81 | "deps": `["//bar:bar"]`, |
| 82 | "importpath": `"android/foo"`, |
| 83 | "srcs": `[ |
Spandan Das | 0a8a275 | 2023-06-21 01:50:33 +0000 | [diff] [blame] | 84 | "foo1.go", |
| 85 | "foo2.go", |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 86 | ] + select({ |
Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame^] | 87 | "//build/bazel_common_rules/platforms/os:darwin": ["foo_darwin.go"], |
| 88 | "//build/bazel_common_rules/platforms/os:linux_glibc": ["foo_linux.go"], |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 89 | "//conditions:default": [], |
| 90 | })`, |
| 91 | }, |
| 92 | android.HostSupported, |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 93 | ), |
| 94 | makeBazelTargetHostOrDevice("go_test", "foo-test", |
| 95 | AttrNameToString{ |
| 96 | "embed": `[":foo"]`, |
| 97 | "srcs": `[ |
| 98 | "foo1_test.go", |
| 99 | "foo2_test.go", |
| 100 | ] + select({ |
Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame^] | 101 | "//build/bazel_common_rules/platforms/os:darwin": ["foo_darwin_test.go"], |
| 102 | "//build/bazel_common_rules/platforms/os:linux_glibc": ["foo_linux_test.go"], |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 103 | "//conditions:default": [], |
| 104 | })`, |
| 105 | }, |
| 106 | android.HostSupported, |
| 107 | )}, |
Spandan Das | ea2abba | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 108 | }) |
| 109 | } |
Spandan Das | de62329 | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 110 | |
| 111 | func TestConvertGoBinaryWithTransitiveDeps(t *testing.T) { |
| 112 | bp := ` |
| 113 | blueprint_go_binary { |
| 114 | name: "foo", |
| 115 | srcs: ["main.go"], |
| 116 | deps: ["bar"], |
| 117 | } |
| 118 | ` |
| 119 | depBp := ` |
| 120 | bootstrap_go_package { |
| 121 | name: "bar", |
| 122 | deps: ["baz"], |
| 123 | } |
| 124 | bootstrap_go_package { |
| 125 | name: "baz", |
| 126 | } |
| 127 | ` |
| 128 | t.Parallel() |
| 129 | runGoTests(t, Bp2buildTestCase{ |
| 130 | Description: "Convert blueprint_go_binary to go_binary", |
| 131 | Blueprint: bp, |
| 132 | Filesystem: map[string]string{ |
| 133 | "bar/Android.bp": depBp, // Put dep in Android.bp to reduce boilerplate in ExpectedBazelTargets |
| 134 | }, |
| 135 | ExpectedBazelTargets: []string{makeBazelTargetHostOrDevice("go_binary", "foo", |
| 136 | AttrNameToString{ |
| 137 | "deps": `[ |
| 138 | "//bar:bar", |
| 139 | "//bar:baz", |
| 140 | ]`, |
Spandan Das | 0a8a275 | 2023-06-21 01:50:33 +0000 | [diff] [blame] | 141 | "srcs": `["main.go"]`, |
| 142 | }, |
| 143 | android.HostSupported, |
| 144 | )}, |
| 145 | }) |
| 146 | } |
| 147 | |
Spandan Das | 682e786 | 2023-06-22 22:22:11 +0000 | [diff] [blame] | 148 | func TestConvertGoBinaryWithTestSrcs(t *testing.T) { |
| 149 | bp := ` |
| 150 | blueprint_go_binary { |
| 151 | name: "foo", |
| 152 | srcs: ["main.go"], |
| 153 | testSrcs: ["main_test.go"], |
| 154 | } |
| 155 | ` |
| 156 | t.Parallel() |
| 157 | runGoTests(t, Bp2buildTestCase{ |
| 158 | Description: "Convert blueprint_go_binary with testSrcs", |
| 159 | Blueprint: bp, |
| 160 | ExpectedBazelTargets: []string{ |
| 161 | makeBazelTargetHostOrDevice("go_binary", "foo", |
| 162 | AttrNameToString{ |
| 163 | "deps": `[]`, |
| 164 | "embed": `[":foo-source"]`, |
| 165 | }, |
| 166 | android.HostSupported, |
| 167 | ), |
| 168 | makeBazelTargetHostOrDevice("go_source", "foo-source", |
| 169 | AttrNameToString{ |
| 170 | "deps": `[]`, |
| 171 | "srcs": `["main.go"]`, |
| 172 | }, |
| 173 | android.HostSupported, |
| 174 | ), |
| 175 | makeBazelTargetHostOrDevice("go_test", "foo-test", |
| 176 | AttrNameToString{ |
| 177 | "embed": `[":foo-source"]`, |
| 178 | "srcs": `["main_test.go"]`, |
| 179 | }, |
| 180 | android.HostSupported, |
| 181 | ), |
| 182 | }, |
| 183 | }) |
| 184 | } |
| 185 | |
Spandan Das | 0a8a275 | 2023-06-21 01:50:33 +0000 | [diff] [blame] | 186 | func TestConvertGoBinaryWithSrcInDifferentPackage(t *testing.T) { |
| 187 | bp := ` |
| 188 | blueprint_go_binary { |
| 189 | name: "foo", |
| 190 | srcs: ["subdir/main.go"], |
| 191 | } |
| 192 | ` |
| 193 | t.Parallel() |
| 194 | runGoTests(t, Bp2buildTestCase{ |
| 195 | Description: "Convert blueprint_go_binary with src in different package", |
| 196 | Blueprint: bp, |
| 197 | Filesystem: map[string]string{ |
| 198 | "subdir/Android.bp": "", |
| 199 | }, |
| 200 | ExpectedBazelTargets: []string{makeBazelTargetHostOrDevice("go_binary", "foo", |
| 201 | AttrNameToString{ |
| 202 | "deps": `[]`, |
| 203 | "srcs": `["//subdir:main.go"]`, |
Spandan Das | de62329 | 2023-06-14 21:30:38 +0000 | [diff] [blame] | 204 | }, |
| 205 | android.HostSupported, |
| 206 | )}, |
| 207 | }) |
| 208 | } |