blob: 2aa373c3fd01903dc6867991d5af56b3fc597eb8 [file] [log] [blame]
Rupert Shuttleworthfb97fde2021-02-11 03:40:05 +00001// 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
15package bp2build
16
17import (
18 "android/soong/android"
19 "android/soong/sh"
20 "strings"
21 "testing"
22)
23
24func 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 }
49
50}
51
52func TestShBinaryBp2Build(t *testing.T) {
53 testCases := []struct {
54 description string
55 moduleTypeUnderTest string
56 moduleTypeUnderTestFactory android.ModuleFactory
57 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
58 preArchMutators []android.RegisterMutatorFunc
59 depsMutators []android.RegisterMutatorFunc
60 bp string
61 expectedBazelTargets []string
62 filesystem map[string]string
63 dir string
64 }{
65 {
66 description: "sh_binary test",
67 moduleTypeUnderTest: "sh_binary",
68 moduleTypeUnderTestFactory: sh.ShBinaryFactory,
69 moduleTypeUnderTestBp2BuildMutator: sh.ShBinaryBp2Build,
70 bp: `sh_binary {
71 name: "foo",
72 src: "foo.sh",
73 bazel_module: { bp2build_available: true },
74}`,
75 expectedBazelTargets: []string{`sh_binary(
76 name = "foo",
77 srcs = [
78 "foo.sh",
79 ],
80)`},
81 },
82 }
83
84 dir := "."
85 for _, testCase := range testCases {
86 filesystem := make(map[string][]byte)
87 toParse := []string{
88 "Android.bp",
89 }
90 for f, content := range testCase.filesystem {
91 if strings.HasSuffix(f, "Android.bp") {
92 toParse = append(toParse, f)
93 }
94 filesystem[f] = []byte(content)
95 }
96 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
97 ctx := android.NewTestContext(config)
98 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
99 for _, m := range testCase.depsMutators {
100 ctx.DepsBp2BuildMutators(m)
101 }
102 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
103 ctx.RegisterForBazelConversion()
104
105 _, errs := ctx.ParseFileList(dir, toParse)
106 if Errored(t, testCase.description, errs) {
107 continue
108 }
109 _, errs = ctx.ResolveDependencies(config)
110 if Errored(t, testCase.description, errs) {
111 continue
112 }
113
114 checkDir := dir
115 if testCase.dir != "" {
116 checkDir = testCase.dir
117 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500118 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500119 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Rupert Shuttleworthfb97fde2021-02-11 03:40:05 +0000120 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
121 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
122 } else {
123 for i, target := range bazelTargets {
124 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
125 t.Errorf(
126 "%s: Expected generated Bazel target to be '%s', got '%s'",
127 testCase.description,
128 w,
129 g,
130 )
131 }
132 }
133 }
134 }
135}