blob: e3cc6133207cd5e657679ac7592abc9304b5ae2e [file] [log] [blame]
Colin Cross41955e82019-05-29 14:40:35 -07001// Copyright 2015 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 android
16
Jooyung Hand48f3c32019-08-23 11:18:57 +090017import (
18 "testing"
19)
Colin Cross41955e82019-05-29 14:40:35 -070020
21func TestSrcIsModule(t *testing.T) {
22 type args struct {
23 s string
24 }
25 tests := []struct {
26 name string
27 args args
28 wantModule string
29 }{
30 {
31 name: "file",
32 args: args{
33 s: "foo",
34 },
35 wantModule: "",
36 },
37 {
38 name: "module",
39 args: args{
40 s: ":foo",
41 },
42 wantModule: "foo",
43 },
44 {
45 name: "tag",
46 args: args{
47 s: ":foo{.bar}",
48 },
49 wantModule: "foo{.bar}",
50 },
51 {
52 name: "extra colon",
53 args: args{
54 s: ":foo:bar",
55 },
56 wantModule: "foo:bar",
57 },
58 }
59 for _, tt := range tests {
60 t.Run(tt.name, func(t *testing.T) {
61 if gotModule := SrcIsModule(tt.args.s); gotModule != tt.wantModule {
62 t.Errorf("SrcIsModule() = %v, want %v", gotModule, tt.wantModule)
63 }
64 })
65 }
66}
67
68func TestSrcIsModuleWithTag(t *testing.T) {
69 type args struct {
70 s string
71 }
72 tests := []struct {
73 name string
74 args args
75 wantModule string
76 wantTag string
77 }{
78 {
79 name: "file",
80 args: args{
81 s: "foo",
82 },
83 wantModule: "",
84 wantTag: "",
85 },
86 {
87 name: "module",
88 args: args{
89 s: ":foo",
90 },
91 wantModule: "foo",
92 wantTag: "",
93 },
94 {
95 name: "tag",
96 args: args{
97 s: ":foo{.bar}",
98 },
99 wantModule: "foo",
100 wantTag: ".bar",
101 },
102 {
103 name: "empty tag",
104 args: args{
105 s: ":foo{}",
106 },
107 wantModule: "foo",
108 wantTag: "",
109 },
110 {
111 name: "extra colon",
112 args: args{
113 s: ":foo:bar",
114 },
115 wantModule: "foo:bar",
116 },
117 {
118 name: "invalid tag",
119 args: args{
120 s: ":foo{.bar",
121 },
122 wantModule: "foo{.bar",
123 },
124 {
125 name: "invalid tag 2",
126 args: args{
127 s: ":foo.bar}",
128 },
129 wantModule: "foo.bar}",
130 },
131 }
132 for _, tt := range tests {
133 t.Run(tt.name, func(t *testing.T) {
134 gotModule, gotTag := SrcIsModuleWithTag(tt.args.s)
135 if gotModule != tt.wantModule {
136 t.Errorf("SrcIsModuleWithTag() gotModule = %v, want %v", gotModule, tt.wantModule)
137 }
138 if gotTag != tt.wantTag {
139 t.Errorf("SrcIsModuleWithTag() gotTag = %v, want %v", gotTag, tt.wantTag)
140 }
141 })
142 }
143}
Jooyung Hand48f3c32019-08-23 11:18:57 +0900144
145type depsModule struct {
146 ModuleBase
147 props struct {
148 Deps []string
149 }
150}
151
152func (m *depsModule) GenerateAndroidBuildActions(ctx ModuleContext) {
153}
154
155func (m *depsModule) DepsMutator(ctx BottomUpMutatorContext) {
156 ctx.AddDependency(ctx.Module(), nil, m.props.Deps...)
157}
158
159func depsModuleFactory() Module {
160 m := &depsModule{}
161 m.AddProperties(&m.props)
162 InitAndroidModule(m)
163 return m
164}
165
166func TestErrorDependsOnDisabledModule(t *testing.T) {
Jooyung Hand48f3c32019-08-23 11:18:57 +0900167 bp := `
168 deps {
169 name: "foo",
170 deps: ["bar"],
171 }
172 deps {
173 name: "bar",
174 enabled: false,
175 }
176 `
177
Colin Cross98be1bb2019-12-13 20:41:13 -0800178 config := TestConfig(buildDir, nil, bp, nil)
Jooyung Hand48f3c32019-08-23 11:18:57 +0900179
Colin Crossae8600b2020-10-29 17:09:13 -0700180 ctx := NewTestContext(config)
181 ctx.RegisterModuleType("deps", depsModuleFactory)
182 ctx.Register()
Jooyung Hand48f3c32019-08-23 11:18:57 +0900183
184 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
185 FailIfErrored(t, errs)
186 _, errs = ctx.PrepareBuildActions(config)
187 FailIfNoMatchingErrors(t, `module "foo": depends on disabled module "bar"`, errs)
188}
Jingwen Chence679d22020-09-23 04:30:02 +0000189
190func TestValidateCorrectBuildParams(t *testing.T) {
191 config := TestConfig(buildDir, nil, "", nil)
192 pathContext := PathContextForTesting(config)
193 bparams := convertBuildParams(BuildParams{
194 // Test with Output
195 Output: PathForOutput(pathContext, "undeclared_symlink"),
196 SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"),
197 })
198
199 err := validateBuildParams(bparams)
200 if err != nil {
201 t.Error(err)
202 }
203
204 bparams = convertBuildParams(BuildParams{
205 // Test with ImplicitOutput
206 ImplicitOutput: PathForOutput(pathContext, "undeclared_symlink"),
207 SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"),
208 })
209
210 err = validateBuildParams(bparams)
211 if err != nil {
212 t.Error(err)
213 }
214}
215
216func TestValidateIncorrectBuildParams(t *testing.T) {
217 config := TestConfig(buildDir, nil, "", nil)
218 pathContext := PathContextForTesting(config)
219 params := BuildParams{
220 Output: PathForOutput(pathContext, "regular_output"),
221 Outputs: PathsForOutput(pathContext, []string{"out1", "out2"}),
222 ImplicitOutput: PathForOutput(pathContext, "implicit_output"),
223 ImplicitOutputs: PathsForOutput(pathContext, []string{"i_out1", "_out2"}),
224 SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"),
225 }
226
227 bparams := convertBuildParams(params)
228 err := validateBuildParams(bparams)
229 if err != nil {
230 FailIfNoMatchingErrors(t, "undeclared_symlink is not a declared output or implicit output", []error{err})
231 } else {
232 t.Errorf("Expected build params to fail validation: %+v", bparams)
233 }
234}
Paul Duffin89968e32020-11-23 18:17:03 +0000235
236func TestDistErrorChecking(t *testing.T) {
237 bp := `
238 deps {
239 name: "foo",
240 dist: {
241 dest: "../invalid-dest",
242 dir: "../invalid-dir",
243 suffix: "invalid/suffix",
244 },
245 dists: [
246 {
247 dest: "../invalid-dest0",
248 dir: "../invalid-dir0",
249 suffix: "invalid/suffix0",
250 },
251 {
252 dest: "../invalid-dest1",
253 dir: "../invalid-dir1",
254 suffix: "invalid/suffix1",
255 },
256 ],
257 }
258 `
259
260 config := TestConfig(buildDir, nil, bp, nil)
261
262 ctx := NewTestContext(config)
263 ctx.RegisterModuleType("deps", depsModuleFactory)
264 ctx.Register()
265
266 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
267 FailIfErrored(t, errs)
268 _, errs = ctx.PrepareBuildActions(config)
269
270 expectedErrs := []string{
271 "\\QAndroid.bp:5:13: module \"foo\": dist.dest: Path is outside directory: ../invalid-dest\\E",
272 "\\QAndroid.bp:6:12: module \"foo\": dist.dir: Path is outside directory: ../invalid-dir\\E",
273 "\\QAndroid.bp:7:15: module \"foo\": dist.suffix: Suffix may not contain a '/' character.\\E",
274 "\\QAndroid.bp:11:15: module \"foo\": dists[0].dest: Path is outside directory: ../invalid-dest0\\E",
275 "\\QAndroid.bp:12:14: module \"foo\": dists[0].dir: Path is outside directory: ../invalid-dir0\\E",
276 "\\QAndroid.bp:13:17: module \"foo\": dists[0].suffix: Suffix may not contain a '/' character.\\E",
277 "\\QAndroid.bp:16:15: module \"foo\": dists[1].dest: Path is outside directory: ../invalid-dest1\\E",
278 "\\QAndroid.bp:17:14: module \"foo\": dists[1].dir: Path is outside directory: ../invalid-dir1\\E",
279 "\\QAndroid.bp:18:17: module \"foo\": dists[1].suffix: Suffix may not contain a '/' character.\\E",
280 }
281 CheckErrorsAgainstExpectations(t, errs, expectedErrs)
282}