| Rupert Shuttleworth | fb97fde | 2021-02-11 03:40:05 +0000 | [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 ( | 
| Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 18 | "testing" | 
|  | 19 |  | 
| Rupert Shuttleworth | fb97fde | 2021-02-11 03:40:05 +0000 | [diff] [blame] | 20 | "android/soong/android" | 
|  | 21 | "android/soong/sh" | 
| Rupert Shuttleworth | fb97fde | 2021-02-11 03:40:05 +0000 | [diff] [blame] | 22 | ) | 
|  | 23 |  | 
|  | 24 | func TestShBinaryLoadStatement(t *testing.T) { | 
|  | 25 | testCases := []struct { | 
|  | 26 | bazelTargets           BazelTargets | 
|  | 27 | expectedLoadStatements string | 
|  | 28 | }{ | 
|  | 29 | { | 
|  | 30 | bazelTargets: BazelTargets{ | 
|  | 31 | BazelTarget{ | 
|  | 32 | name:      "sh_binary_target", | 
|  | 33 | ruleClass: "sh_binary", | 
|  | 34 | // Note: no bzlLoadLocation for native rules | 
|  | 35 | // TODO(ruperts): Could open source the existing, experimental Starlark sh_ rules? | 
|  | 36 | }, | 
|  | 37 | }, | 
|  | 38 | expectedLoadStatements: ``, | 
|  | 39 | }, | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | for _, testCase := range testCases { | 
|  | 43 | actual := testCase.bazelTargets.LoadStatements() | 
|  | 44 | expected := testCase.expectedLoadStatements | 
|  | 45 | if actual != expected { | 
|  | 46 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) | 
|  | 47 | } | 
|  | 48 | } | 
| Rupert Shuttleworth | fb97fde | 2021-02-11 03:40:05 +0000 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 51 | func runShBinaryTestCase(t *testing.T, tc Bp2buildTestCase) { | 
| Liz Kammer | e4982e8 | 2021-05-25 10:39:35 -0400 | [diff] [blame] | 52 | t.Helper() | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 53 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) | 
| Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
|  | 56 | func TestShBinarySimple(t *testing.T) { | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 57 | runShBinaryTestCase(t, Bp2buildTestCase{ | 
|  | 58 | Description:                "sh_binary test", | 
|  | 59 | ModuleTypeUnderTest:        "sh_binary", | 
|  | 60 | ModuleTypeUnderTestFactory: sh.ShBinaryFactory, | 
|  | 61 | Blueprint: `sh_binary { | 
| Rupert Shuttleworth | fb97fde | 2021-02-11 03:40:05 +0000 | [diff] [blame] | 62 | name: "foo", | 
|  | 63 | src: "foo.sh", | 
| Yu Liu | b302455 | 2021-11-23 14:53:41 -0800 | [diff] [blame] | 64 | filename: "foo.exe", | 
|  | 65 | sub_dir: "sub", | 
| Rupert Shuttleworth | fb97fde | 2021-02-11 03:40:05 +0000 | [diff] [blame] | 66 | bazel_module: { bp2build_available: true }, | 
|  | 67 | }`, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 68 | ExpectedBazelTargets: []string{ | 
| Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 69 | MakeBazelTarget("sh_binary", "foo", AttrNameToString{ | 
| Yu Liu | b302455 | 2021-11-23 14:53:41 -0800 | [diff] [blame] | 70 | "srcs":     `["foo.sh"]`, | 
|  | 71 | "filename": `"foo.exe"`, | 
|  | 72 | "sub_dir":  `"sub"`, | 
| Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 73 | })}, | 
| Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 74 | }) | 
| Rupert Shuttleworth | fb97fde | 2021-02-11 03:40:05 +0000 | [diff] [blame] | 75 | } | 
| Liz Kammer | 48c6ead | 2021-12-21 15:36:11 -0500 | [diff] [blame] | 76 |  | 
|  | 77 | func TestShBinaryDefaults(t *testing.T) { | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 78 | runShBinaryTestCase(t, Bp2buildTestCase{ | 
|  | 79 | Description:                "sh_binary test", | 
|  | 80 | ModuleTypeUnderTest:        "sh_binary", | 
|  | 81 | ModuleTypeUnderTestFactory: sh.ShBinaryFactory, | 
|  | 82 | Blueprint: `sh_binary { | 
| Liz Kammer | 48c6ead | 2021-12-21 15:36:11 -0500 | [diff] [blame] | 83 | name: "foo", | 
|  | 84 | src: "foo.sh", | 
|  | 85 | bazel_module: { bp2build_available: true }, | 
|  | 86 | }`, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 87 | ExpectedBazelTargets: []string{ | 
| Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 88 | MakeBazelTarget("sh_binary", "foo", AttrNameToString{ | 
| Liz Kammer | 48c6ead | 2021-12-21 15:36:11 -0500 | [diff] [blame] | 89 | "srcs": `["foo.sh"]`, | 
|  | 90 | })}, | 
|  | 91 | }) | 
|  | 92 | } |