blob: 9e2b0ca2905dcfc8b8b2b0cc2e939eff788c24e5 [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 },
Paul Duffine6ba0722021-07-12 20:12:12 +010058 {
59 name: "fully qualified",
60 args: args{
61 s: "//foo:bar",
62 },
63 wantModule: "//foo:bar",
64 },
65 {
66 name: "fully qualified with tag",
67 args: args{
68 s: "//foo:bar{.tag}",
69 },
70 wantModule: "//foo:bar{.tag}",
71 },
72 {
73 name: "invalid unqualified name",
74 args: args{
75 s: ":foo/bar",
76 },
77 wantModule: "",
78 },
Colin Cross41955e82019-05-29 14:40:35 -070079 }
80 for _, tt := range tests {
81 t.Run(tt.name, func(t *testing.T) {
82 if gotModule := SrcIsModule(tt.args.s); gotModule != tt.wantModule {
83 t.Errorf("SrcIsModule() = %v, want %v", gotModule, tt.wantModule)
84 }
85 })
86 }
87}
88
89func TestSrcIsModuleWithTag(t *testing.T) {
90 type args struct {
91 s string
92 }
93 tests := []struct {
94 name string
95 args args
96 wantModule string
97 wantTag string
98 }{
99 {
100 name: "file",
101 args: args{
102 s: "foo",
103 },
104 wantModule: "",
105 wantTag: "",
106 },
107 {
108 name: "module",
109 args: args{
110 s: ":foo",
111 },
112 wantModule: "foo",
113 wantTag: "",
114 },
115 {
116 name: "tag",
117 args: args{
118 s: ":foo{.bar}",
119 },
120 wantModule: "foo",
121 wantTag: ".bar",
122 },
123 {
124 name: "empty tag",
125 args: args{
126 s: ":foo{}",
127 },
128 wantModule: "foo",
129 wantTag: "",
130 },
131 {
132 name: "extra colon",
133 args: args{
134 s: ":foo:bar",
135 },
136 wantModule: "foo:bar",
137 },
138 {
139 name: "invalid tag",
140 args: args{
141 s: ":foo{.bar",
142 },
143 wantModule: "foo{.bar",
144 },
145 {
146 name: "invalid tag 2",
147 args: args{
148 s: ":foo.bar}",
149 },
150 wantModule: "foo.bar}",
151 },
Paul Duffine6ba0722021-07-12 20:12:12 +0100152 {
153 name: "fully qualified",
154 args: args{
155 s: "//foo:bar",
156 },
157 wantModule: "//foo:bar",
158 },
159 {
160 name: "fully qualified with tag",
161 args: args{
162 s: "//foo:bar{.tag}",
163 },
164 wantModule: "//foo:bar",
165 wantTag: ".tag",
166 },
167 {
168 name: "invalid unqualified name",
169 args: args{
170 s: ":foo/bar",
171 },
172 wantModule: "",
173 },
174 {
175 name: "invalid unqualified name with tag",
176 args: args{
177 s: ":foo/bar{.tag}",
178 },
179 wantModule: "",
180 },
Colin Cross41955e82019-05-29 14:40:35 -0700181 }
182 for _, tt := range tests {
183 t.Run(tt.name, func(t *testing.T) {
184 gotModule, gotTag := SrcIsModuleWithTag(tt.args.s)
185 if gotModule != tt.wantModule {
186 t.Errorf("SrcIsModuleWithTag() gotModule = %v, want %v", gotModule, tt.wantModule)
187 }
188 if gotTag != tt.wantTag {
189 t.Errorf("SrcIsModuleWithTag() gotTag = %v, want %v", gotTag, tt.wantTag)
190 }
191 })
192 }
193}
Jooyung Hand48f3c32019-08-23 11:18:57 +0900194
195type depsModule struct {
196 ModuleBase
197 props struct {
198 Deps []string
199 }
200}
201
202func (m *depsModule) GenerateAndroidBuildActions(ctx ModuleContext) {
203}
204
205func (m *depsModule) DepsMutator(ctx BottomUpMutatorContext) {
206 ctx.AddDependency(ctx.Module(), nil, m.props.Deps...)
207}
208
209func depsModuleFactory() Module {
210 m := &depsModule{}
211 m.AddProperties(&m.props)
212 InitAndroidModule(m)
213 return m
214}
215
Paul Duffinf62dc9b2021-03-16 19:44:51 +0000216var prepareForModuleTests = FixtureRegisterWithContext(func(ctx RegistrationContext) {
217 ctx.RegisterModuleType("deps", depsModuleFactory)
218})
219
Jooyung Hand48f3c32019-08-23 11:18:57 +0900220func TestErrorDependsOnDisabledModule(t *testing.T) {
Jooyung Hand48f3c32019-08-23 11:18:57 +0900221 bp := `
222 deps {
223 name: "foo",
224 deps: ["bar"],
225 }
226 deps {
227 name: "bar",
228 enabled: false,
229 }
230 `
231
Paul Duffin30ac3e72021-03-20 00:36:14 +0000232 prepareForModuleTests.
Paul Duffinf62dc9b2021-03-16 19:44:51 +0000233 ExtendWithErrorHandler(FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": depends on disabled module "bar"`)).
Paul Duffin30ac3e72021-03-20 00:36:14 +0000234 RunTestWithBp(t, bp)
Jooyung Hand48f3c32019-08-23 11:18:57 +0900235}
Jingwen Chence679d22020-09-23 04:30:02 +0000236
237func TestValidateCorrectBuildParams(t *testing.T) {
Paul Duffinf62dc9b2021-03-16 19:44:51 +0000238 config := TestConfig(t.TempDir(), nil, "", nil)
Jingwen Chence679d22020-09-23 04:30:02 +0000239 pathContext := PathContextForTesting(config)
240 bparams := convertBuildParams(BuildParams{
241 // Test with Output
242 Output: PathForOutput(pathContext, "undeclared_symlink"),
243 SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"),
244 })
245
246 err := validateBuildParams(bparams)
247 if err != nil {
248 t.Error(err)
249 }
250
251 bparams = convertBuildParams(BuildParams{
252 // Test with ImplicitOutput
253 ImplicitOutput: PathForOutput(pathContext, "undeclared_symlink"),
254 SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"),
255 })
256
257 err = validateBuildParams(bparams)
258 if err != nil {
259 t.Error(err)
260 }
261}
262
263func TestValidateIncorrectBuildParams(t *testing.T) {
Paul Duffinf62dc9b2021-03-16 19:44:51 +0000264 config := TestConfig(t.TempDir(), nil, "", nil)
Jingwen Chence679d22020-09-23 04:30:02 +0000265 pathContext := PathContextForTesting(config)
266 params := BuildParams{
267 Output: PathForOutput(pathContext, "regular_output"),
268 Outputs: PathsForOutput(pathContext, []string{"out1", "out2"}),
269 ImplicitOutput: PathForOutput(pathContext, "implicit_output"),
270 ImplicitOutputs: PathsForOutput(pathContext, []string{"i_out1", "_out2"}),
271 SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"),
272 }
273
274 bparams := convertBuildParams(params)
275 err := validateBuildParams(bparams)
276 if err != nil {
277 FailIfNoMatchingErrors(t, "undeclared_symlink is not a declared output or implicit output", []error{err})
278 } else {
279 t.Errorf("Expected build params to fail validation: %+v", bparams)
280 }
281}
Paul Duffin89968e32020-11-23 18:17:03 +0000282
283func TestDistErrorChecking(t *testing.T) {
284 bp := `
285 deps {
286 name: "foo",
287 dist: {
288 dest: "../invalid-dest",
289 dir: "../invalid-dir",
290 suffix: "invalid/suffix",
291 },
292 dists: [
293 {
294 dest: "../invalid-dest0",
295 dir: "../invalid-dir0",
296 suffix: "invalid/suffix0",
297 },
298 {
299 dest: "../invalid-dest1",
300 dir: "../invalid-dir1",
301 suffix: "invalid/suffix1",
302 },
303 ],
304 }
305 `
306
Paul Duffin89968e32020-11-23 18:17:03 +0000307 expectedErrs := []string{
308 "\\QAndroid.bp:5:13: module \"foo\": dist.dest: Path is outside directory: ../invalid-dest\\E",
309 "\\QAndroid.bp:6:12: module \"foo\": dist.dir: Path is outside directory: ../invalid-dir\\E",
310 "\\QAndroid.bp:7:15: module \"foo\": dist.suffix: Suffix may not contain a '/' character.\\E",
311 "\\QAndroid.bp:11:15: module \"foo\": dists[0].dest: Path is outside directory: ../invalid-dest0\\E",
312 "\\QAndroid.bp:12:14: module \"foo\": dists[0].dir: Path is outside directory: ../invalid-dir0\\E",
313 "\\QAndroid.bp:13:17: module \"foo\": dists[0].suffix: Suffix may not contain a '/' character.\\E",
314 "\\QAndroid.bp:16:15: module \"foo\": dists[1].dest: Path is outside directory: ../invalid-dest1\\E",
315 "\\QAndroid.bp:17:14: module \"foo\": dists[1].dir: Path is outside directory: ../invalid-dir1\\E",
316 "\\QAndroid.bp:18:17: module \"foo\": dists[1].suffix: Suffix may not contain a '/' character.\\E",
317 }
Paul Duffinf62dc9b2021-03-16 19:44:51 +0000318
Paul Duffin30ac3e72021-03-20 00:36:14 +0000319 prepareForModuleTests.
Paul Duffinf62dc9b2021-03-16 19:44:51 +0000320 ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(expectedErrs)).
Paul Duffin30ac3e72021-03-20 00:36:14 +0000321 RunTestWithBp(t, bp)
Paul Duffin89968e32020-11-23 18:17:03 +0000322}