Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [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 | package bp2build |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
| 18 | "testing" |
| 19 | |
| 20 | "android/soong/cc" |
| 21 | ) |
| 22 | |
| 23 | func runCcPrebuiltBinaryTestCase(t *testing.T, testCase Bp2buildTestCase) { |
| 24 | t.Helper() |
| 25 | description := fmt.Sprintf("cc_prebuilt_binary: %s", testCase.Description) |
| 26 | testCase.ModuleTypeUnderTest = "cc_prebuilt_binary" |
| 27 | testCase.ModuleTypeUnderTestFactory = cc.PrebuiltBinaryFactory |
| 28 | testCase.Description = description |
| 29 | t.Run(description, func(t *testing.T) { |
| 30 | t.Helper() |
| 31 | RunBp2BuildTestCaseSimple(t, testCase) |
| 32 | }) |
| 33 | } |
| 34 | |
| 35 | func TestPrebuiltBinary(t *testing.T) { |
| 36 | runCcPrebuiltBinaryTestCase(t, |
| 37 | Bp2buildTestCase{ |
| 38 | Description: "simple", |
| 39 | Filesystem: map[string]string{ |
| 40 | "bin": "", |
| 41 | }, |
| 42 | Blueprint: ` |
| 43 | cc_prebuilt_binary { |
| 44 | name: "bintest", |
| 45 | srcs: ["bin"], |
| 46 | bazel_module: { bp2build_available: true }, |
| 47 | }`, |
| 48 | ExpectedBazelTargets: []string{ |
| 49 | MakeBazelTarget("cc_prebuilt_binary", "bintest", AttrNameToString{ |
| 50 | "src": `"bin"`, |
| 51 | })}, |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | func TestPrebuiltBinaryWithStrip(t *testing.T) { |
| 56 | runCcPrebuiltBinaryTestCase(t, |
| 57 | Bp2buildTestCase{ |
| 58 | Description: "with strip", |
| 59 | Filesystem: map[string]string{ |
| 60 | "bin": "", |
| 61 | }, |
| 62 | Blueprint: ` |
| 63 | cc_prebuilt_binary { |
| 64 | name: "bintest", |
| 65 | srcs: ["bin"], |
| 66 | strip: { all: true }, |
| 67 | bazel_module: { bp2build_available: true }, |
| 68 | }`, ExpectedBazelTargets: []string{ |
| 69 | MakeBazelTarget("cc_prebuilt_binary", "bintest", AttrNameToString{ |
| 70 | "src": `"bin"`, |
| 71 | "strip": `{ |
| 72 | "all": True, |
| 73 | }`, |
| 74 | }), |
| 75 | }, |
| 76 | }) |
| 77 | } |
| 78 | |
| 79 | func TestPrebuiltBinaryWithArchVariance(t *testing.T) { |
| 80 | runCcPrebuiltBinaryTestCase(t, |
| 81 | Bp2buildTestCase{ |
| 82 | Description: "with arch variance", |
| 83 | Filesystem: map[string]string{ |
| 84 | "bina": "", |
| 85 | "binb": "", |
| 86 | }, |
| 87 | Blueprint: ` |
| 88 | cc_prebuilt_binary { |
| 89 | name: "bintest", |
| 90 | arch: { |
| 91 | arm64: { srcs: ["bina"], }, |
| 92 | arm: { srcs: ["binb"], }, |
| 93 | }, |
| 94 | bazel_module: { bp2build_available: true }, |
| 95 | }`, ExpectedBazelTargets: []string{ |
| 96 | MakeBazelTarget("cc_prebuilt_binary", "bintest", AttrNameToString{ |
| 97 | "src": `select({ |
| 98 | "//build/bazel/platforms/arch:arm": "binb", |
| 99 | "//build/bazel/platforms/arch:arm64": "bina", |
| 100 | "//conditions:default": None, |
| 101 | })`, |
| 102 | }), |
| 103 | }, |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | func TestPrebuiltBinaryMultipleSrcsFails(t *testing.T) { |
| 108 | runCcPrebuiltBinaryTestCase(t, |
| 109 | Bp2buildTestCase{ |
| 110 | Description: "fails because multiple sources", |
| 111 | Filesystem: map[string]string{ |
| 112 | "bina": "", |
| 113 | "binb": "", |
| 114 | }, |
| 115 | Blueprint: ` |
| 116 | cc_prebuilt_binary { |
| 117 | name: "bintest", |
| 118 | srcs: ["bina", "binb"], |
| 119 | bazel_module: { bp2build_available: true }, |
| 120 | }`, |
| 121 | ExpectedErr: fmt.Errorf("Expected at most one source file"), |
| 122 | }) |
| 123 | } |
| 124 | |
| 125 | // TODO: nosrcs test |