blob: 46e3e1fa6457d6f3cce26cc7ab47e2892d9d2ec6 [file] [log] [blame]
Dan Willemsen34cc69e2015-09-23 15:26:20 -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
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Dan Willemsen34cc69e2015-09-23 15:26:20 -070016
17import (
18 "errors"
19 "fmt"
20 "reflect"
21 "strings"
22 "testing"
Dan Willemsen00269f22017-07-06 16:59:48 -070023
Colin Cross8a497952019-03-05 22:25:09 -080024 "github.com/google/blueprint/proptools"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070025)
26
27type strsTestCase struct {
28 in []string
29 out string
30 err []error
31}
32
33var commonValidatePathTestCases = []strsTestCase{
34 {
35 in: []string{""},
36 out: "",
37 },
38 {
39 in: []string{"a/b"},
40 out: "a/b",
41 },
42 {
43 in: []string{"a/b", "c"},
44 out: "a/b/c",
45 },
46 {
47 in: []string{"a/.."},
48 out: ".",
49 },
50 {
51 in: []string{"."},
52 out: ".",
53 },
54 {
55 in: []string{".."},
56 out: "",
57 err: []error{errors.New("Path is outside directory: ..")},
58 },
59 {
60 in: []string{"../a"},
61 out: "",
62 err: []error{errors.New("Path is outside directory: ../a")},
63 },
64 {
65 in: []string{"b/../../a"},
66 out: "",
67 err: []error{errors.New("Path is outside directory: ../a")},
68 },
69 {
70 in: []string{"/a"},
71 out: "",
72 err: []error{errors.New("Path is outside directory: /a")},
73 },
Dan Willemsen80a7c2a2015-12-21 14:57:11 -080074 {
75 in: []string{"a", "../b"},
76 out: "",
77 err: []error{errors.New("Path is outside directory: ../b")},
78 },
79 {
80 in: []string{"a", "b/../../c"},
81 out: "",
82 err: []error{errors.New("Path is outside directory: ../c")},
83 },
84 {
85 in: []string{"a", "./.."},
86 out: "",
87 err: []error{errors.New("Path is outside directory: ..")},
88 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089}
90
91var validateSafePathTestCases = append(commonValidatePathTestCases, []strsTestCase{
92 {
93 in: []string{"$host/../$a"},
94 out: "$a",
95 },
96}...)
97
98var validatePathTestCases = append(commonValidatePathTestCases, []strsTestCase{
99 {
100 in: []string{"$host/../$a"},
101 out: "",
102 err: []error{errors.New("Path contains invalid character($): $host/../$a")},
103 },
104 {
105 in: []string{"$host/.."},
106 out: "",
107 err: []error{errors.New("Path contains invalid character($): $host/..")},
108 },
109}...)
110
111func TestValidateSafePath(t *testing.T) {
112 for _, testCase := range validateSafePathTestCases {
Colin Crossdc75ae72018-02-22 13:48:13 -0800113 t.Run(strings.Join(testCase.in, ","), func(t *testing.T) {
114 ctx := &configErrorWrapper{}
Colin Cross1ccfcc32018-02-22 13:54:26 -0800115 out, err := validateSafePath(testCase.in...)
116 if err != nil {
117 reportPathError(ctx, err)
118 }
Colin Crossdc75ae72018-02-22 13:48:13 -0800119 check(t, "validateSafePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err)
120 })
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700121 }
122}
123
124func TestValidatePath(t *testing.T) {
125 for _, testCase := range validatePathTestCases {
Colin Crossdc75ae72018-02-22 13:48:13 -0800126 t.Run(strings.Join(testCase.in, ","), func(t *testing.T) {
127 ctx := &configErrorWrapper{}
Colin Cross1ccfcc32018-02-22 13:54:26 -0800128 out, err := validatePath(testCase.in...)
129 if err != nil {
130 reportPathError(ctx, err)
131 }
Colin Crossdc75ae72018-02-22 13:48:13 -0800132 check(t, "validatePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err)
133 })
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700134 }
135}
136
137func TestOptionalPath(t *testing.T) {
138 var path OptionalPath
139 checkInvalidOptionalPath(t, path)
140
141 path = OptionalPathForPath(nil)
142 checkInvalidOptionalPath(t, path)
143}
144
145func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
Colin Crossdc75ae72018-02-22 13:48:13 -0800146 t.Helper()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700147 if path.Valid() {
148 t.Errorf("Uninitialized OptionalPath should not be valid")
149 }
150 if path.String() != "" {
151 t.Errorf("Uninitialized OptionalPath String() should return \"\", not %q", path.String())
152 }
153 defer func() {
154 if r := recover(); r == nil {
155 t.Errorf("Expected a panic when calling Path() on an uninitialized OptionalPath")
156 }
157 }()
158 path.Path()
159}
160
161func check(t *testing.T, testType, testString string,
162 got interface{}, err []error,
163 expected interface{}, expectedErr []error) {
Colin Crossdc75ae72018-02-22 13:48:13 -0800164 t.Helper()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700165
166 printedTestCase := false
167 e := func(s string, expected, got interface{}) {
Colin Crossdc75ae72018-02-22 13:48:13 -0800168 t.Helper()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700169 if !printedTestCase {
170 t.Errorf("test case %s: %s", testType, testString)
171 printedTestCase = true
172 }
173 t.Errorf("incorrect %s", s)
174 t.Errorf(" expected: %s", p(expected))
175 t.Errorf(" got: %s", p(got))
176 }
177
178 if !reflect.DeepEqual(expectedErr, err) {
179 e("errors:", expectedErr, err)
180 }
181
182 if !reflect.DeepEqual(expected, got) {
183 e("output:", expected, got)
184 }
185}
186
187func p(in interface{}) string {
188 if v, ok := in.([]interface{}); ok {
189 s := make([]string, len(v))
190 for i := range v {
191 s[i] = fmt.Sprintf("%#v", v[i])
192 }
193 return "[" + strings.Join(s, ", ") + "]"
194 } else {
195 return fmt.Sprintf("%#v", in)
196 }
197}
Dan Willemsen00269f22017-07-06 16:59:48 -0700198
199type moduleInstallPathContextImpl struct {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700200 baseModuleContext
Dan Willemsen00269f22017-07-06 16:59:48 -0700201
202 inData bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700203 inTestcases bool
Dan Willemsen00269f22017-07-06 16:59:48 -0700204 inSanitizerDir bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900205 inRecovery bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700206 inRoot bool
Dan Willemsen00269f22017-07-06 16:59:48 -0700207}
208
Colin Crossaabf6792017-11-29 00:27:14 -0800209func (m moduleInstallPathContextImpl) Config() Config {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700210 return m.baseModuleContext.config
Dan Willemsen00269f22017-07-06 16:59:48 -0700211}
212
213func (moduleInstallPathContextImpl) AddNinjaFileDeps(deps ...string) {}
214
215func (m moduleInstallPathContextImpl) InstallInData() bool {
216 return m.inData
217}
218
Jaewoong Jung0949f312019-09-11 10:25:18 -0700219func (m moduleInstallPathContextImpl) InstallInTestcases() bool {
220 return m.inTestcases
221}
222
Dan Willemsen00269f22017-07-06 16:59:48 -0700223func (m moduleInstallPathContextImpl) InstallInSanitizerDir() bool {
224 return m.inSanitizerDir
225}
226
Jiyong Parkf9332f12018-02-01 00:54:12 +0900227func (m moduleInstallPathContextImpl) InstallInRecovery() bool {
228 return m.inRecovery
229}
230
Colin Cross90ba5f42019-10-02 11:10:58 -0700231func (m moduleInstallPathContextImpl) InstallInRoot() bool {
232 return m.inRoot
233}
234
Colin Cross607d8582019-07-29 16:44:46 -0700235func (m moduleInstallPathContextImpl) InstallBypassMake() bool {
236 return false
237}
238
Colin Cross98be1bb2019-12-13 20:41:13 -0800239func pathTestConfig(buildDir string) Config {
240 return TestConfig(buildDir, nil, "", nil)
241}
242
Dan Willemsen00269f22017-07-06 16:59:48 -0700243func TestPathForModuleInstall(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800244 testConfig := pathTestConfig("")
Dan Willemsen00269f22017-07-06 16:59:48 -0700245
246 hostTarget := Target{Os: Linux}
247 deviceTarget := Target{Os: Android}
248
249 testCases := []struct {
250 name string
251 ctx *moduleInstallPathContextImpl
252 in []string
253 out string
254 }{
255 {
256 name: "host binary",
257 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700258 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800259 os: hostTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700260 target: hostTarget,
261 },
262 },
263 in: []string{"bin", "my_test"},
264 out: "host/linux-x86/bin/my_test",
265 },
266
267 {
268 name: "system binary",
269 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700270 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800271 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700272 target: deviceTarget,
273 },
274 },
275 in: []string{"bin", "my_test"},
276 out: "target/product/test_device/system/bin/my_test",
277 },
278 {
279 name: "vendor binary",
280 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700281 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800282 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700283 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800284 earlyModuleContext: earlyModuleContext{
285 kind: socSpecificModule,
286 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700287 },
288 },
289 in: []string{"bin", "my_test"},
290 out: "target/product/test_device/vendor/bin/my_test",
291 },
Jiyong Park2db76922017-11-08 16:03:48 +0900292 {
293 name: "odm binary",
294 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700295 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800296 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900297 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800298 earlyModuleContext: earlyModuleContext{
299 kind: deviceSpecificModule,
300 },
Jiyong Park2db76922017-11-08 16:03:48 +0900301 },
302 },
303 in: []string{"bin", "my_test"},
304 out: "target/product/test_device/odm/bin/my_test",
305 },
306 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900307 name: "product binary",
Jiyong Park2db76922017-11-08 16:03:48 +0900308 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700309 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800310 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900311 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800312 earlyModuleContext: earlyModuleContext{
313 kind: productSpecificModule,
314 },
Jiyong Park2db76922017-11-08 16:03:48 +0900315 },
316 },
317 in: []string{"bin", "my_test"},
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900318 out: "target/product/test_device/product/bin/my_test",
Jiyong Park2db76922017-11-08 16:03:48 +0900319 },
Dario Frenifd05a742018-05-29 13:28:54 +0100320 {
Justin Yund5f6c822019-06-25 16:47:17 +0900321 name: "system_ext binary",
Dario Frenifd05a742018-05-29 13:28:54 +0100322 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700323 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800324 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100325 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800326 earlyModuleContext: earlyModuleContext{
327 kind: systemExtSpecificModule,
328 },
Dario Frenifd05a742018-05-29 13:28:54 +0100329 },
330 },
331 in: []string{"bin", "my_test"},
Justin Yund5f6c822019-06-25 16:47:17 +0900332 out: "target/product/test_device/system_ext/bin/my_test",
Dario Frenifd05a742018-05-29 13:28:54 +0100333 },
Colin Cross90ba5f42019-10-02 11:10:58 -0700334 {
335 name: "root binary",
336 ctx: &moduleInstallPathContextImpl{
337 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800338 os: deviceTarget.Os,
Colin Cross90ba5f42019-10-02 11:10:58 -0700339 target: deviceTarget,
340 },
341 inRoot: true,
342 },
343 in: []string{"my_test"},
344 out: "target/product/test_device/root/my_test",
345 },
346 {
347 name: "recovery binary",
348 ctx: &moduleInstallPathContextImpl{
349 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800350 os: deviceTarget.Os,
Colin Cross90ba5f42019-10-02 11:10:58 -0700351 target: deviceTarget,
352 },
353 inRecovery: true,
354 },
355 in: []string{"bin/my_test"},
356 out: "target/product/test_device/recovery/root/system/bin/my_test",
357 },
358 {
359 name: "recovery root binary",
360 ctx: &moduleInstallPathContextImpl{
361 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800362 os: deviceTarget.Os,
Colin Cross90ba5f42019-10-02 11:10:58 -0700363 target: deviceTarget,
364 },
365 inRecovery: true,
366 inRoot: true,
367 },
368 in: []string{"my_test"},
369 out: "target/product/test_device/recovery/root/my_test",
370 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700371
372 {
373 name: "system native test binary",
374 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700375 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800376 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700377 target: deviceTarget,
378 },
379 inData: true,
380 },
381 in: []string{"nativetest", "my_test"},
382 out: "target/product/test_device/data/nativetest/my_test",
383 },
384 {
385 name: "vendor native test binary",
386 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700387 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800388 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700389 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800390 earlyModuleContext: earlyModuleContext{
391 kind: socSpecificModule,
392 },
Jiyong Park2db76922017-11-08 16:03:48 +0900393 },
394 inData: true,
395 },
396 in: []string{"nativetest", "my_test"},
397 out: "target/product/test_device/data/nativetest/my_test",
398 },
399 {
400 name: "odm native test binary",
401 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700402 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800403 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900404 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800405 earlyModuleContext: earlyModuleContext{
406 kind: deviceSpecificModule,
407 },
Jiyong Park2db76922017-11-08 16:03:48 +0900408 },
409 inData: true,
410 },
411 in: []string{"nativetest", "my_test"},
412 out: "target/product/test_device/data/nativetest/my_test",
413 },
414 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900415 name: "product native test binary",
Jiyong Park2db76922017-11-08 16:03:48 +0900416 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700417 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800418 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900419 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800420 earlyModuleContext: earlyModuleContext{
421 kind: productSpecificModule,
422 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700423 },
424 inData: true,
425 },
426 in: []string{"nativetest", "my_test"},
427 out: "target/product/test_device/data/nativetest/my_test",
428 },
429
430 {
Justin Yund5f6c822019-06-25 16:47:17 +0900431 name: "system_ext native test binary",
Dario Frenifd05a742018-05-29 13:28:54 +0100432 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700433 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800434 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100435 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800436 earlyModuleContext: earlyModuleContext{
437 kind: systemExtSpecificModule,
438 },
Dario Frenifd05a742018-05-29 13:28:54 +0100439 },
440 inData: true,
441 },
442 in: []string{"nativetest", "my_test"},
443 out: "target/product/test_device/data/nativetest/my_test",
444 },
445
446 {
Dan Willemsen00269f22017-07-06 16:59:48 -0700447 name: "sanitized system binary",
448 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700449 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800450 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700451 target: deviceTarget,
452 },
453 inSanitizerDir: true,
454 },
455 in: []string{"bin", "my_test"},
456 out: "target/product/test_device/data/asan/system/bin/my_test",
457 },
458 {
459 name: "sanitized vendor binary",
460 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700461 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800462 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700463 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800464 earlyModuleContext: earlyModuleContext{
465 kind: socSpecificModule,
466 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700467 },
468 inSanitizerDir: true,
469 },
470 in: []string{"bin", "my_test"},
471 out: "target/product/test_device/data/asan/vendor/bin/my_test",
472 },
Jiyong Park2db76922017-11-08 16:03:48 +0900473 {
474 name: "sanitized odm binary",
475 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700476 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800477 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900478 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800479 earlyModuleContext: earlyModuleContext{
480 kind: deviceSpecificModule,
481 },
Jiyong Park2db76922017-11-08 16:03:48 +0900482 },
483 inSanitizerDir: true,
484 },
485 in: []string{"bin", "my_test"},
486 out: "target/product/test_device/data/asan/odm/bin/my_test",
487 },
488 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900489 name: "sanitized product binary",
Jiyong Park2db76922017-11-08 16:03:48 +0900490 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700491 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800492 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900493 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800494 earlyModuleContext: earlyModuleContext{
495 kind: productSpecificModule,
496 },
Jiyong Park2db76922017-11-08 16:03:48 +0900497 },
498 inSanitizerDir: true,
499 },
500 in: []string{"bin", "my_test"},
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900501 out: "target/product/test_device/data/asan/product/bin/my_test",
Jiyong Park2db76922017-11-08 16:03:48 +0900502 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700503
504 {
Justin Yund5f6c822019-06-25 16:47:17 +0900505 name: "sanitized system_ext binary",
Dario Frenifd05a742018-05-29 13:28:54 +0100506 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700507 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800508 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100509 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800510 earlyModuleContext: earlyModuleContext{
511 kind: systemExtSpecificModule,
512 },
Dario Frenifd05a742018-05-29 13:28:54 +0100513 },
514 inSanitizerDir: true,
515 },
516 in: []string{"bin", "my_test"},
Justin Yund5f6c822019-06-25 16:47:17 +0900517 out: "target/product/test_device/data/asan/system_ext/bin/my_test",
Dario Frenifd05a742018-05-29 13:28:54 +0100518 },
519
520 {
Dan Willemsen00269f22017-07-06 16:59:48 -0700521 name: "sanitized system native test binary",
522 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700523 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800524 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700525 target: deviceTarget,
526 },
527 inData: true,
528 inSanitizerDir: true,
529 },
530 in: []string{"nativetest", "my_test"},
531 out: "target/product/test_device/data/asan/data/nativetest/my_test",
532 },
533 {
534 name: "sanitized vendor native test binary",
535 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700536 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800537 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700538 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800539 earlyModuleContext: earlyModuleContext{
540 kind: socSpecificModule,
541 },
Jiyong Park2db76922017-11-08 16:03:48 +0900542 },
543 inData: true,
544 inSanitizerDir: true,
545 },
546 in: []string{"nativetest", "my_test"},
547 out: "target/product/test_device/data/asan/data/nativetest/my_test",
548 },
549 {
550 name: "sanitized odm native test binary",
551 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700552 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800553 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900554 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800555 earlyModuleContext: earlyModuleContext{
556 kind: deviceSpecificModule,
557 },
Jiyong Park2db76922017-11-08 16:03:48 +0900558 },
559 inData: true,
560 inSanitizerDir: true,
561 },
562 in: []string{"nativetest", "my_test"},
563 out: "target/product/test_device/data/asan/data/nativetest/my_test",
564 },
565 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900566 name: "sanitized product native test binary",
Jiyong Park2db76922017-11-08 16:03:48 +0900567 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700568 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800569 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900570 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800571 earlyModuleContext: earlyModuleContext{
572 kind: productSpecificModule,
573 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700574 },
575 inData: true,
576 inSanitizerDir: true,
577 },
578 in: []string{"nativetest", "my_test"},
579 out: "target/product/test_device/data/asan/data/nativetest/my_test",
580 },
Dario Frenifd05a742018-05-29 13:28:54 +0100581 {
Justin Yund5f6c822019-06-25 16:47:17 +0900582 name: "sanitized system_ext native test binary",
Dario Frenifd05a742018-05-29 13:28:54 +0100583 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700584 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800585 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100586 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800587 earlyModuleContext: earlyModuleContext{
588 kind: systemExtSpecificModule,
589 },
Dario Frenifd05a742018-05-29 13:28:54 +0100590 },
591 inData: true,
592 inSanitizerDir: true,
593 },
594 in: []string{"nativetest", "my_test"},
595 out: "target/product/test_device/data/asan/data/nativetest/my_test",
596 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700597 }
598
599 for _, tc := range testCases {
600 t.Run(tc.name, func(t *testing.T) {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700601 tc.ctx.baseModuleContext.config = testConfig
Dan Willemsen00269f22017-07-06 16:59:48 -0700602 output := PathForModuleInstall(tc.ctx, tc.in...)
603 if output.basePath.path != tc.out {
604 t.Errorf("unexpected path:\n got: %q\nwant: %q\n",
605 output.basePath.path,
606 tc.out)
607 }
608 })
609 }
610}
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700611
612func TestDirectorySortedPaths(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800613 config := TestConfig("out", nil, "", map[string][]byte{
614 "Android.bp": nil,
615 "a.txt": nil,
616 "a/txt": nil,
617 "a/b/c": nil,
618 "a/b/d": nil,
619 "b": nil,
620 "b/b.txt": nil,
621 "a/a.txt": nil,
Colin Cross07e51612019-03-05 12:46:40 -0800622 })
623
Colin Cross98be1bb2019-12-13 20:41:13 -0800624 ctx := PathContextForTesting(config)
625
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700626 makePaths := func() Paths {
627 return Paths{
Colin Cross07e51612019-03-05 12:46:40 -0800628 PathForSource(ctx, "a.txt"),
629 PathForSource(ctx, "a/txt"),
630 PathForSource(ctx, "a/b/c"),
631 PathForSource(ctx, "a/b/d"),
632 PathForSource(ctx, "b"),
633 PathForSource(ctx, "b/b.txt"),
634 PathForSource(ctx, "a/a.txt"),
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700635 }
636 }
637
638 expected := []string{
639 "a.txt",
640 "a/a.txt",
641 "a/b/c",
642 "a/b/d",
643 "a/txt",
644 "b",
645 "b/b.txt",
646 }
647
648 paths := makePaths()
Colin Crossa140bb02018-04-17 10:52:26 -0700649 reversePaths := ReversePaths(paths)
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700650
651 sortedPaths := PathsToDirectorySortedPaths(paths)
652 reverseSortedPaths := PathsToDirectorySortedPaths(reversePaths)
653
654 if !reflect.DeepEqual(Paths(sortedPaths).Strings(), expected) {
655 t.Fatalf("sorted paths:\n %#v\n != \n %#v", paths.Strings(), expected)
656 }
657
658 if !reflect.DeepEqual(Paths(reverseSortedPaths).Strings(), expected) {
659 t.Fatalf("sorted reversed paths:\n %#v\n !=\n %#v", reversePaths.Strings(), expected)
660 }
661
662 expectedA := []string{
663 "a/a.txt",
664 "a/b/c",
665 "a/b/d",
666 "a/txt",
667 }
668
669 inA := sortedPaths.PathsInDirectory("a")
670 if !reflect.DeepEqual(inA.Strings(), expectedA) {
671 t.Errorf("FilesInDirectory(a):\n %#v\n != \n %#v", inA.Strings(), expectedA)
672 }
673
674 expectedA_B := []string{
675 "a/b/c",
676 "a/b/d",
677 }
678
679 inA_B := sortedPaths.PathsInDirectory("a/b")
680 if !reflect.DeepEqual(inA_B.Strings(), expectedA_B) {
681 t.Errorf("FilesInDirectory(a/b):\n %#v\n != \n %#v", inA_B.Strings(), expectedA_B)
682 }
683
684 expectedB := []string{
685 "b/b.txt",
686 }
687
688 inB := sortedPaths.PathsInDirectory("b")
689 if !reflect.DeepEqual(inB.Strings(), expectedB) {
690 t.Errorf("FilesInDirectory(b):\n %#v\n != \n %#v", inA.Strings(), expectedA)
691 }
692}
Colin Cross43f08db2018-11-12 10:13:39 -0800693
694func TestMaybeRel(t *testing.T) {
695 testCases := []struct {
696 name string
697 base string
698 target string
699 out string
700 isRel bool
701 }{
702 {
703 name: "normal",
704 base: "a/b/c",
705 target: "a/b/c/d",
706 out: "d",
707 isRel: true,
708 },
709 {
710 name: "parent",
711 base: "a/b/c/d",
712 target: "a/b/c",
713 isRel: false,
714 },
715 {
716 name: "not relative",
717 base: "a/b",
718 target: "c/d",
719 isRel: false,
720 },
721 {
722 name: "abs1",
723 base: "/a",
724 target: "a",
725 isRel: false,
726 },
727 {
728 name: "abs2",
729 base: "a",
730 target: "/a",
731 isRel: false,
732 },
733 }
734
735 for _, testCase := range testCases {
736 t.Run(testCase.name, func(t *testing.T) {
737 ctx := &configErrorWrapper{}
738 out, isRel := MaybeRel(ctx, testCase.base, testCase.target)
739 if len(ctx.errors) > 0 {
740 t.Errorf("MaybeRel(..., %s, %s) reported unexpected errors %v",
741 testCase.base, testCase.target, ctx.errors)
742 }
743 if isRel != testCase.isRel || out != testCase.out {
744 t.Errorf("MaybeRel(..., %s, %s) want %v, %v got %v, %v",
745 testCase.base, testCase.target, testCase.out, testCase.isRel, out, isRel)
746 }
747 })
748 }
749}
Colin Cross7b3dcc32019-01-24 13:14:39 -0800750
751func TestPathForSource(t *testing.T) {
752 testCases := []struct {
753 name string
754 buildDir string
755 src string
756 err string
757 }{
758 {
759 name: "normal",
760 buildDir: "out",
761 src: "a/b/c",
762 },
763 {
764 name: "abs",
765 buildDir: "out",
766 src: "/a/b/c",
767 err: "is outside directory",
768 },
769 {
770 name: "in out dir",
771 buildDir: "out",
772 src: "out/a/b/c",
773 err: "is in output",
774 },
775 }
776
777 funcs := []struct {
778 name string
779 f func(ctx PathContext, pathComponents ...string) (SourcePath, error)
780 }{
781 {"pathForSource", pathForSource},
782 {"safePathForSource", safePathForSource},
783 }
784
785 for _, f := range funcs {
786 t.Run(f.name, func(t *testing.T) {
787 for _, test := range testCases {
788 t.Run(test.name, func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800789 testConfig := pathTestConfig(test.buildDir)
Colin Cross7b3dcc32019-01-24 13:14:39 -0800790 ctx := &configErrorWrapper{config: testConfig}
791 _, err := f.f(ctx, test.src)
792 if len(ctx.errors) > 0 {
793 t.Fatalf("unexpected errors %v", ctx.errors)
794 }
795 if err != nil {
796 if test.err == "" {
797 t.Fatalf("unexpected error %q", err.Error())
798 } else if !strings.Contains(err.Error(), test.err) {
799 t.Fatalf("incorrect error, want substring %q got %q", test.err, err.Error())
800 }
801 } else {
802 if test.err != "" {
803 t.Fatalf("missing error %q", test.err)
804 }
805 }
806 })
807 }
808 })
809 }
810}
Colin Cross8854a5a2019-02-11 14:14:16 -0800811
Colin Cross8a497952019-03-05 22:25:09 -0800812type pathForModuleSrcTestModule struct {
Colin Cross937664a2019-03-06 10:17:32 -0800813 ModuleBase
814 props struct {
815 Srcs []string `android:"path"`
816 Exclude_srcs []string `android:"path"`
Colin Cross8a497952019-03-05 22:25:09 -0800817
818 Src *string `android:"path"`
Colin Crossba71a3f2019-03-18 12:12:48 -0700819
820 Module_handles_missing_deps bool
Colin Cross937664a2019-03-06 10:17:32 -0800821 }
822
Colin Cross8a497952019-03-05 22:25:09 -0800823 src string
824 rel string
825
826 srcs []string
Colin Cross937664a2019-03-06 10:17:32 -0800827 rels []string
Colin Cross8a497952019-03-05 22:25:09 -0800828
829 missingDeps []string
Colin Cross937664a2019-03-06 10:17:32 -0800830}
831
Colin Cross8a497952019-03-05 22:25:09 -0800832func pathForModuleSrcTestModuleFactory() Module {
833 module := &pathForModuleSrcTestModule{}
Colin Cross937664a2019-03-06 10:17:32 -0800834 module.AddProperties(&module.props)
835 InitAndroidModule(module)
836 return module
837}
838
Colin Cross8a497952019-03-05 22:25:09 -0800839func (p *pathForModuleSrcTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Crossba71a3f2019-03-18 12:12:48 -0700840 var srcs Paths
841 if p.props.Module_handles_missing_deps {
842 srcs, p.missingDeps = PathsAndMissingDepsForModuleSrcExcludes(ctx, p.props.Srcs, p.props.Exclude_srcs)
843 } else {
844 srcs = PathsForModuleSrcExcludes(ctx, p.props.Srcs, p.props.Exclude_srcs)
845 }
Colin Cross8a497952019-03-05 22:25:09 -0800846 p.srcs = srcs.Strings()
Colin Cross937664a2019-03-06 10:17:32 -0800847
Colin Cross8a497952019-03-05 22:25:09 -0800848 for _, src := range srcs {
Colin Cross937664a2019-03-06 10:17:32 -0800849 p.rels = append(p.rels, src.Rel())
850 }
Colin Cross8a497952019-03-05 22:25:09 -0800851
852 if p.props.Src != nil {
853 src := PathForModuleSrc(ctx, *p.props.Src)
854 if src != nil {
855 p.src = src.String()
856 p.rel = src.Rel()
857 }
858 }
859
Colin Crossba71a3f2019-03-18 12:12:48 -0700860 if !p.props.Module_handles_missing_deps {
861 p.missingDeps = ctx.GetMissingDependencies()
862 }
Colin Cross6c4f21f2019-06-06 15:41:36 -0700863
864 ctx.Build(pctx, BuildParams{
865 Rule: Touch,
866 Output: PathForModuleOut(ctx, "output"),
867 })
Colin Cross8a497952019-03-05 22:25:09 -0800868}
869
Colin Cross41955e82019-05-29 14:40:35 -0700870type pathForModuleSrcOutputFileProviderModule struct {
871 ModuleBase
872 props struct {
873 Outs []string
874 Tagged []string
875 }
876
877 outs Paths
878 tagged Paths
879}
880
881func pathForModuleSrcOutputFileProviderModuleFactory() Module {
882 module := &pathForModuleSrcOutputFileProviderModule{}
883 module.AddProperties(&module.props)
884 InitAndroidModule(module)
885 return module
886}
887
888func (p *pathForModuleSrcOutputFileProviderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
889 for _, out := range p.props.Outs {
890 p.outs = append(p.outs, PathForModuleOut(ctx, out))
891 }
892
893 for _, tagged := range p.props.Tagged {
894 p.tagged = append(p.tagged, PathForModuleOut(ctx, tagged))
895 }
896}
897
898func (p *pathForModuleSrcOutputFileProviderModule) OutputFiles(tag string) (Paths, error) {
899 switch tag {
900 case "":
901 return p.outs, nil
902 case ".tagged":
903 return p.tagged, nil
904 default:
905 return nil, fmt.Errorf("unsupported tag %q", tag)
906 }
907}
908
Colin Cross8a497952019-03-05 22:25:09 -0800909type pathForModuleSrcTestCase struct {
910 name string
911 bp string
912 srcs []string
913 rels []string
914 src string
915 rel string
916}
917
918func testPathForModuleSrc(t *testing.T, buildDir string, tests []pathForModuleSrcTestCase) {
919 for _, test := range tests {
920 t.Run(test.name, func(t *testing.T) {
Colin Cross8a497952019-03-05 22:25:09 -0800921 ctx := NewTestContext()
922
Colin Cross4b49b762019-11-22 15:25:03 -0800923 ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory)
924 ctx.RegisterModuleType("output_file_provider", pathForModuleSrcOutputFileProviderModuleFactory)
925 ctx.RegisterModuleType("filegroup", FileGroupFactory)
Colin Cross8a497952019-03-05 22:25:09 -0800926
927 fgBp := `
928 filegroup {
929 name: "a",
930 srcs: ["src/a"],
931 }
932 `
933
Colin Cross41955e82019-05-29 14:40:35 -0700934 ofpBp := `
935 output_file_provider {
936 name: "b",
937 outs: ["gen/b"],
938 tagged: ["gen/c"],
939 }
940 `
941
Colin Cross8a497952019-03-05 22:25:09 -0800942 mockFS := map[string][]byte{
943 "fg/Android.bp": []byte(fgBp),
944 "foo/Android.bp": []byte(test.bp),
Colin Cross41955e82019-05-29 14:40:35 -0700945 "ofp/Android.bp": []byte(ofpBp),
Colin Cross8a497952019-03-05 22:25:09 -0800946 "fg/src/a": nil,
947 "foo/src/b": nil,
948 "foo/src/c": nil,
949 "foo/src/d": nil,
950 "foo/src/e/e": nil,
951 "foo/src_special/$": nil,
952 }
953
Colin Cross98be1bb2019-12-13 20:41:13 -0800954 config := TestConfig(buildDir, nil, "", mockFS)
Colin Cross8a497952019-03-05 22:25:09 -0800955
Colin Cross98be1bb2019-12-13 20:41:13 -0800956 ctx.Register(config)
Colin Cross41955e82019-05-29 14:40:35 -0700957 _, errs := ctx.ParseFileList(".", []string{"fg/Android.bp", "foo/Android.bp", "ofp/Android.bp"})
Colin Cross8a497952019-03-05 22:25:09 -0800958 FailIfErrored(t, errs)
959 _, errs = ctx.PrepareBuildActions(config)
960 FailIfErrored(t, errs)
961
962 m := ctx.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule)
963
964 if g, w := m.srcs, test.srcs; !reflect.DeepEqual(g, w) {
965 t.Errorf("want srcs %q, got %q", w, g)
966 }
967
968 if g, w := m.rels, test.rels; !reflect.DeepEqual(g, w) {
969 t.Errorf("want rels %q, got %q", w, g)
970 }
971
972 if g, w := m.src, test.src; g != w {
973 t.Errorf("want src %q, got %q", w, g)
974 }
975
976 if g, w := m.rel, test.rel; g != w {
977 t.Errorf("want rel %q, got %q", w, g)
978 }
979 })
980 }
Colin Cross937664a2019-03-06 10:17:32 -0800981}
982
Colin Cross8a497952019-03-05 22:25:09 -0800983func TestPathsForModuleSrc(t *testing.T) {
984 tests := []pathForModuleSrcTestCase{
Colin Cross937664a2019-03-06 10:17:32 -0800985 {
986 name: "path",
987 bp: `
988 test {
989 name: "foo",
990 srcs: ["src/b"],
991 }`,
992 srcs: []string{"foo/src/b"},
993 rels: []string{"src/b"},
994 },
995 {
996 name: "glob",
997 bp: `
998 test {
999 name: "foo",
1000 srcs: [
1001 "src/*",
1002 "src/e/*",
1003 ],
1004 }`,
1005 srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"},
1006 rels: []string{"src/b", "src/c", "src/d", "src/e/e"},
1007 },
1008 {
1009 name: "recursive glob",
1010 bp: `
1011 test {
1012 name: "foo",
1013 srcs: ["src/**/*"],
1014 }`,
1015 srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"},
1016 rels: []string{"src/b", "src/c", "src/d", "src/e/e"},
1017 },
1018 {
1019 name: "filegroup",
1020 bp: `
1021 test {
1022 name: "foo",
1023 srcs: [":a"],
1024 }`,
1025 srcs: []string{"fg/src/a"},
1026 rels: []string{"src/a"},
1027 },
1028 {
Colin Cross41955e82019-05-29 14:40:35 -07001029 name: "output file provider",
1030 bp: `
1031 test {
1032 name: "foo",
1033 srcs: [":b"],
1034 }`,
1035 srcs: []string{buildDir + "/.intermediates/ofp/b/gen/b"},
1036 rels: []string{"gen/b"},
1037 },
1038 {
1039 name: "output file provider tagged",
1040 bp: `
1041 test {
1042 name: "foo",
1043 srcs: [":b{.tagged}"],
1044 }`,
1045 srcs: []string{buildDir + "/.intermediates/ofp/b/gen/c"},
1046 rels: []string{"gen/c"},
1047 },
1048 {
Colin Cross937664a2019-03-06 10:17:32 -08001049 name: "special characters glob",
1050 bp: `
1051 test {
1052 name: "foo",
1053 srcs: ["src_special/*"],
1054 }`,
1055 srcs: []string{"foo/src_special/$"},
1056 rels: []string{"src_special/$"},
1057 },
1058 }
1059
Colin Cross41955e82019-05-29 14:40:35 -07001060 testPathForModuleSrc(t, buildDir, tests)
1061}
1062
1063func TestPathForModuleSrc(t *testing.T) {
Colin Cross8a497952019-03-05 22:25:09 -08001064 tests := []pathForModuleSrcTestCase{
1065 {
1066 name: "path",
1067 bp: `
1068 test {
1069 name: "foo",
1070 src: "src/b",
1071 }`,
1072 src: "foo/src/b",
1073 rel: "src/b",
1074 },
1075 {
1076 name: "glob",
1077 bp: `
1078 test {
1079 name: "foo",
1080 src: "src/e/*",
1081 }`,
1082 src: "foo/src/e/e",
1083 rel: "src/e/e",
1084 },
1085 {
1086 name: "filegroup",
1087 bp: `
1088 test {
1089 name: "foo",
1090 src: ":a",
1091 }`,
1092 src: "fg/src/a",
1093 rel: "src/a",
1094 },
1095 {
Colin Cross41955e82019-05-29 14:40:35 -07001096 name: "output file provider",
1097 bp: `
1098 test {
1099 name: "foo",
1100 src: ":b",
1101 }`,
1102 src: buildDir + "/.intermediates/ofp/b/gen/b",
1103 rel: "gen/b",
1104 },
1105 {
1106 name: "output file provider tagged",
1107 bp: `
1108 test {
1109 name: "foo",
1110 src: ":b{.tagged}",
1111 }`,
1112 src: buildDir + "/.intermediates/ofp/b/gen/c",
1113 rel: "gen/c",
1114 },
1115 {
Colin Cross8a497952019-03-05 22:25:09 -08001116 name: "special characters glob",
1117 bp: `
1118 test {
1119 name: "foo",
1120 src: "src_special/*",
1121 }`,
1122 src: "foo/src_special/$",
1123 rel: "src_special/$",
1124 },
1125 }
1126
Colin Cross8a497952019-03-05 22:25:09 -08001127 testPathForModuleSrc(t, buildDir, tests)
1128}
Colin Cross937664a2019-03-06 10:17:32 -08001129
Colin Cross8a497952019-03-05 22:25:09 -08001130func TestPathsForModuleSrc_AllowMissingDependencies(t *testing.T) {
Colin Cross8a497952019-03-05 22:25:09 -08001131 bp := `
1132 test {
1133 name: "foo",
1134 srcs: [":a"],
1135 exclude_srcs: [":b"],
1136 src: ":c",
1137 }
Colin Crossba71a3f2019-03-18 12:12:48 -07001138
1139 test {
1140 name: "bar",
1141 srcs: [":d"],
1142 exclude_srcs: [":e"],
1143 module_handles_missing_deps: true,
1144 }
Colin Cross8a497952019-03-05 22:25:09 -08001145 `
1146
Colin Cross98be1bb2019-12-13 20:41:13 -08001147 config := TestConfig(buildDir, nil, bp, nil)
1148 config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
Colin Cross8a497952019-03-05 22:25:09 -08001149
Colin Cross98be1bb2019-12-13 20:41:13 -08001150 ctx := NewTestContext()
1151 ctx.SetAllowMissingDependencies(true)
Colin Cross8a497952019-03-05 22:25:09 -08001152
Colin Cross98be1bb2019-12-13 20:41:13 -08001153 ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory)
1154
1155 ctx.Register(config)
1156
Colin Cross8a497952019-03-05 22:25:09 -08001157 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
1158 FailIfErrored(t, errs)
1159 _, errs = ctx.PrepareBuildActions(config)
1160 FailIfErrored(t, errs)
1161
1162 foo := ctx.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule)
1163
1164 if g, w := foo.missingDeps, []string{"a", "b", "c"}; !reflect.DeepEqual(g, w) {
Colin Crossba71a3f2019-03-18 12:12:48 -07001165 t.Errorf("want foo missing deps %q, got %q", w, g)
Colin Cross8a497952019-03-05 22:25:09 -08001166 }
1167
1168 if g, w := foo.srcs, []string{}; !reflect.DeepEqual(g, w) {
Colin Crossba71a3f2019-03-18 12:12:48 -07001169 t.Errorf("want foo srcs %q, got %q", w, g)
Colin Cross8a497952019-03-05 22:25:09 -08001170 }
1171
1172 if g, w := foo.src, ""; g != w {
Colin Crossba71a3f2019-03-18 12:12:48 -07001173 t.Errorf("want foo src %q, got %q", w, g)
Colin Cross8a497952019-03-05 22:25:09 -08001174 }
1175
Colin Crossba71a3f2019-03-18 12:12:48 -07001176 bar := ctx.ModuleForTests("bar", "").Module().(*pathForModuleSrcTestModule)
1177
1178 if g, w := bar.missingDeps, []string{"d", "e"}; !reflect.DeepEqual(g, w) {
1179 t.Errorf("want bar missing deps %q, got %q", w, g)
1180 }
1181
1182 if g, w := bar.srcs, []string{}; !reflect.DeepEqual(g, w) {
1183 t.Errorf("want bar srcs %q, got %q", w, g)
1184 }
Colin Cross937664a2019-03-06 10:17:32 -08001185}
1186
Colin Cross8854a5a2019-02-11 14:14:16 -08001187func ExampleOutputPath_ReplaceExtension() {
1188 ctx := &configErrorWrapper{
Colin Cross98be1bb2019-12-13 20:41:13 -08001189 config: TestConfig("out", nil, "", nil),
Colin Cross8854a5a2019-02-11 14:14:16 -08001190 }
Colin Cross2cdd5df2019-02-25 10:25:24 -08001191 p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
Colin Cross8854a5a2019-02-11 14:14:16 -08001192 p2 := p.ReplaceExtension(ctx, "oat")
1193 fmt.Println(p, p2)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001194 fmt.Println(p.Rel(), p2.Rel())
Colin Cross8854a5a2019-02-11 14:14:16 -08001195
1196 // Output:
1197 // out/system/framework/boot.art out/system/framework/boot.oat
Colin Cross2cdd5df2019-02-25 10:25:24 -08001198 // boot.art boot.oat
Colin Cross8854a5a2019-02-11 14:14:16 -08001199}
Colin Cross40e33732019-02-15 11:08:35 -08001200
1201func ExampleOutputPath_FileInSameDir() {
1202 ctx := &configErrorWrapper{
Colin Cross98be1bb2019-12-13 20:41:13 -08001203 config: TestConfig("out", nil, "", nil),
Colin Cross40e33732019-02-15 11:08:35 -08001204 }
Colin Cross2cdd5df2019-02-25 10:25:24 -08001205 p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
Colin Cross40e33732019-02-15 11:08:35 -08001206 p2 := p.InSameDir(ctx, "oat", "arm", "boot.vdex")
1207 fmt.Println(p, p2)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001208 fmt.Println(p.Rel(), p2.Rel())
Colin Cross40e33732019-02-15 11:08:35 -08001209
1210 // Output:
1211 // out/system/framework/boot.art out/system/framework/oat/arm/boot.vdex
Colin Cross2cdd5df2019-02-25 10:25:24 -08001212 // boot.art oat/arm/boot.vdex
Colin Cross40e33732019-02-15 11:08:35 -08001213}