blob: 5ff5f99e26953cb92e093b5bc6f5970352a6fb81 [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
24 "github.com/google/blueprint/pathtools"
Colin Cross8a497952019-03-05 22:25:09 -080025 "github.com/google/blueprint/proptools"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070026)
27
28type strsTestCase struct {
29 in []string
30 out string
31 err []error
32}
33
34var commonValidatePathTestCases = []strsTestCase{
35 {
36 in: []string{""},
37 out: "",
38 },
39 {
40 in: []string{"a/b"},
41 out: "a/b",
42 },
43 {
44 in: []string{"a/b", "c"},
45 out: "a/b/c",
46 },
47 {
48 in: []string{"a/.."},
49 out: ".",
50 },
51 {
52 in: []string{"."},
53 out: ".",
54 },
55 {
56 in: []string{".."},
57 out: "",
58 err: []error{errors.New("Path is outside directory: ..")},
59 },
60 {
61 in: []string{"../a"},
62 out: "",
63 err: []error{errors.New("Path is outside directory: ../a")},
64 },
65 {
66 in: []string{"b/../../a"},
67 out: "",
68 err: []error{errors.New("Path is outside directory: ../a")},
69 },
70 {
71 in: []string{"/a"},
72 out: "",
73 err: []error{errors.New("Path is outside directory: /a")},
74 },
Dan Willemsen80a7c2a2015-12-21 14:57:11 -080075 {
76 in: []string{"a", "../b"},
77 out: "",
78 err: []error{errors.New("Path is outside directory: ../b")},
79 },
80 {
81 in: []string{"a", "b/../../c"},
82 out: "",
83 err: []error{errors.New("Path is outside directory: ../c")},
84 },
85 {
86 in: []string{"a", "./.."},
87 out: "",
88 err: []error{errors.New("Path is outside directory: ..")},
89 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -070090}
91
92var validateSafePathTestCases = append(commonValidatePathTestCases, []strsTestCase{
93 {
94 in: []string{"$host/../$a"},
95 out: "$a",
96 },
97}...)
98
99var validatePathTestCases = append(commonValidatePathTestCases, []strsTestCase{
100 {
101 in: []string{"$host/../$a"},
102 out: "",
103 err: []error{errors.New("Path contains invalid character($): $host/../$a")},
104 },
105 {
106 in: []string{"$host/.."},
107 out: "",
108 err: []error{errors.New("Path contains invalid character($): $host/..")},
109 },
110}...)
111
112func TestValidateSafePath(t *testing.T) {
113 for _, testCase := range validateSafePathTestCases {
Colin Crossdc75ae72018-02-22 13:48:13 -0800114 t.Run(strings.Join(testCase.in, ","), func(t *testing.T) {
115 ctx := &configErrorWrapper{}
Colin Cross1ccfcc32018-02-22 13:54:26 -0800116 out, err := validateSafePath(testCase.in...)
117 if err != nil {
118 reportPathError(ctx, err)
119 }
Colin Crossdc75ae72018-02-22 13:48:13 -0800120 check(t, "validateSafePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err)
121 })
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700122 }
123}
124
125func TestValidatePath(t *testing.T) {
126 for _, testCase := range validatePathTestCases {
Colin Crossdc75ae72018-02-22 13:48:13 -0800127 t.Run(strings.Join(testCase.in, ","), func(t *testing.T) {
128 ctx := &configErrorWrapper{}
Colin Cross1ccfcc32018-02-22 13:54:26 -0800129 out, err := validatePath(testCase.in...)
130 if err != nil {
131 reportPathError(ctx, err)
132 }
Colin Crossdc75ae72018-02-22 13:48:13 -0800133 check(t, "validatePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err)
134 })
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700135 }
136}
137
138func TestOptionalPath(t *testing.T) {
139 var path OptionalPath
140 checkInvalidOptionalPath(t, path)
141
142 path = OptionalPathForPath(nil)
143 checkInvalidOptionalPath(t, path)
144}
145
146func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
Colin Crossdc75ae72018-02-22 13:48:13 -0800147 t.Helper()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700148 if path.Valid() {
149 t.Errorf("Uninitialized OptionalPath should not be valid")
150 }
151 if path.String() != "" {
152 t.Errorf("Uninitialized OptionalPath String() should return \"\", not %q", path.String())
153 }
154 defer func() {
155 if r := recover(); r == nil {
156 t.Errorf("Expected a panic when calling Path() on an uninitialized OptionalPath")
157 }
158 }()
159 path.Path()
160}
161
162func check(t *testing.T, testType, testString string,
163 got interface{}, err []error,
164 expected interface{}, expectedErr []error) {
Colin Crossdc75ae72018-02-22 13:48:13 -0800165 t.Helper()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700166
167 printedTestCase := false
168 e := func(s string, expected, got interface{}) {
Colin Crossdc75ae72018-02-22 13:48:13 -0800169 t.Helper()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700170 if !printedTestCase {
171 t.Errorf("test case %s: %s", testType, testString)
172 printedTestCase = true
173 }
174 t.Errorf("incorrect %s", s)
175 t.Errorf(" expected: %s", p(expected))
176 t.Errorf(" got: %s", p(got))
177 }
178
179 if !reflect.DeepEqual(expectedErr, err) {
180 e("errors:", expectedErr, err)
181 }
182
183 if !reflect.DeepEqual(expected, got) {
184 e("output:", expected, got)
185 }
186}
187
188func p(in interface{}) string {
189 if v, ok := in.([]interface{}); ok {
190 s := make([]string, len(v))
191 for i := range v {
192 s[i] = fmt.Sprintf("%#v", v[i])
193 }
194 return "[" + strings.Join(s, ", ") + "]"
195 } else {
196 return fmt.Sprintf("%#v", in)
197 }
198}
Dan Willemsen00269f22017-07-06 16:59:48 -0700199
200type moduleInstallPathContextImpl struct {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700201 baseModuleContext
Dan Willemsen00269f22017-07-06 16:59:48 -0700202
203 inData bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700204 inTestcases bool
Dan Willemsen00269f22017-07-06 16:59:48 -0700205 inSanitizerDir bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900206 inRecovery bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700207 inRoot bool
Dan Willemsen00269f22017-07-06 16:59:48 -0700208}
209
210func (moduleInstallPathContextImpl) Fs() pathtools.FileSystem {
211 return pathtools.MockFs(nil)
212}
213
Colin Crossaabf6792017-11-29 00:27:14 -0800214func (m moduleInstallPathContextImpl) Config() Config {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700215 return m.baseModuleContext.config
Dan Willemsen00269f22017-07-06 16:59:48 -0700216}
217
218func (moduleInstallPathContextImpl) AddNinjaFileDeps(deps ...string) {}
219
220func (m moduleInstallPathContextImpl) InstallInData() bool {
221 return m.inData
222}
223
Jaewoong Jung0949f312019-09-11 10:25:18 -0700224func (m moduleInstallPathContextImpl) InstallInTestcases() bool {
225 return m.inTestcases
226}
227
Dan Willemsen00269f22017-07-06 16:59:48 -0700228func (m moduleInstallPathContextImpl) InstallInSanitizerDir() bool {
229 return m.inSanitizerDir
230}
231
Jiyong Parkf9332f12018-02-01 00:54:12 +0900232func (m moduleInstallPathContextImpl) InstallInRecovery() bool {
233 return m.inRecovery
234}
235
Colin Cross90ba5f42019-10-02 11:10:58 -0700236func (m moduleInstallPathContextImpl) InstallInRoot() bool {
237 return m.inRoot
238}
239
Colin Cross607d8582019-07-29 16:44:46 -0700240func (m moduleInstallPathContextImpl) InstallBypassMake() bool {
241 return false
242}
243
Colin Cross98be1bb2019-12-13 20:41:13 -0800244func pathTestConfig(buildDir string) Config {
245 return TestConfig(buildDir, nil, "", nil)
246}
247
Dan Willemsen00269f22017-07-06 16:59:48 -0700248func TestPathForModuleInstall(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800249 testConfig := pathTestConfig("")
Dan Willemsen00269f22017-07-06 16:59:48 -0700250
251 hostTarget := Target{Os: Linux}
252 deviceTarget := Target{Os: Android}
253
254 testCases := []struct {
255 name string
256 ctx *moduleInstallPathContextImpl
257 in []string
258 out string
259 }{
260 {
261 name: "host binary",
262 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700263 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800264 os: hostTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700265 target: hostTarget,
266 },
267 },
268 in: []string{"bin", "my_test"},
269 out: "host/linux-x86/bin/my_test",
270 },
271
272 {
273 name: "system binary",
274 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700275 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800276 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700277 target: deviceTarget,
278 },
279 },
280 in: []string{"bin", "my_test"},
281 out: "target/product/test_device/system/bin/my_test",
282 },
283 {
284 name: "vendor binary",
285 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700286 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800287 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700288 target: deviceTarget,
Jiyong Park2db76922017-11-08 16:03:48 +0900289 kind: socSpecificModule,
Dan Willemsen00269f22017-07-06 16:59:48 -0700290 },
291 },
292 in: []string{"bin", "my_test"},
293 out: "target/product/test_device/vendor/bin/my_test",
294 },
Jiyong Park2db76922017-11-08 16:03:48 +0900295 {
296 name: "odm binary",
297 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700298 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800299 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900300 target: deviceTarget,
301 kind: deviceSpecificModule,
302 },
303 },
304 in: []string{"bin", "my_test"},
305 out: "target/product/test_device/odm/bin/my_test",
306 },
307 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900308 name: "product binary",
Jiyong Park2db76922017-11-08 16:03:48 +0900309 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700310 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800311 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900312 target: deviceTarget,
313 kind: productSpecificModule,
314 },
315 },
316 in: []string{"bin", "my_test"},
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900317 out: "target/product/test_device/product/bin/my_test",
Jiyong Park2db76922017-11-08 16:03:48 +0900318 },
Dario Frenifd05a742018-05-29 13:28:54 +0100319 {
Justin Yund5f6c822019-06-25 16:47:17 +0900320 name: "system_ext binary",
Dario Frenifd05a742018-05-29 13:28:54 +0100321 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700322 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800323 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100324 target: deviceTarget,
Justin Yund5f6c822019-06-25 16:47:17 +0900325 kind: systemExtSpecificModule,
Dario Frenifd05a742018-05-29 13:28:54 +0100326 },
327 },
328 in: []string{"bin", "my_test"},
Justin Yund5f6c822019-06-25 16:47:17 +0900329 out: "target/product/test_device/system_ext/bin/my_test",
Dario Frenifd05a742018-05-29 13:28:54 +0100330 },
Colin Cross90ba5f42019-10-02 11:10:58 -0700331 {
332 name: "root binary",
333 ctx: &moduleInstallPathContextImpl{
334 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800335 os: deviceTarget.Os,
Colin Cross90ba5f42019-10-02 11:10:58 -0700336 target: deviceTarget,
337 },
338 inRoot: true,
339 },
340 in: []string{"my_test"},
341 out: "target/product/test_device/root/my_test",
342 },
343 {
344 name: "recovery binary",
345 ctx: &moduleInstallPathContextImpl{
346 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800347 os: deviceTarget.Os,
Colin Cross90ba5f42019-10-02 11:10:58 -0700348 target: deviceTarget,
349 },
350 inRecovery: true,
351 },
352 in: []string{"bin/my_test"},
353 out: "target/product/test_device/recovery/root/system/bin/my_test",
354 },
355 {
356 name: "recovery root binary",
357 ctx: &moduleInstallPathContextImpl{
358 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800359 os: deviceTarget.Os,
Colin Cross90ba5f42019-10-02 11:10:58 -0700360 target: deviceTarget,
361 },
362 inRecovery: true,
363 inRoot: true,
364 },
365 in: []string{"my_test"},
366 out: "target/product/test_device/recovery/root/my_test",
367 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700368
369 {
370 name: "system native test binary",
371 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700372 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800373 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700374 target: deviceTarget,
375 },
376 inData: true,
377 },
378 in: []string{"nativetest", "my_test"},
379 out: "target/product/test_device/data/nativetest/my_test",
380 },
381 {
382 name: "vendor native test binary",
383 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700384 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800385 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700386 target: deviceTarget,
Jiyong Park2db76922017-11-08 16:03:48 +0900387 kind: socSpecificModule,
388 },
389 inData: true,
390 },
391 in: []string{"nativetest", "my_test"},
392 out: "target/product/test_device/data/nativetest/my_test",
393 },
394 {
395 name: "odm native test binary",
396 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700397 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800398 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900399 target: deviceTarget,
400 kind: deviceSpecificModule,
401 },
402 inData: true,
403 },
404 in: []string{"nativetest", "my_test"},
405 out: "target/product/test_device/data/nativetest/my_test",
406 },
407 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900408 name: "product native test binary",
Jiyong Park2db76922017-11-08 16:03:48 +0900409 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700410 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800411 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900412 target: deviceTarget,
413 kind: productSpecificModule,
Dan Willemsen00269f22017-07-06 16:59:48 -0700414 },
415 inData: true,
416 },
417 in: []string{"nativetest", "my_test"},
418 out: "target/product/test_device/data/nativetest/my_test",
419 },
420
421 {
Justin Yund5f6c822019-06-25 16:47:17 +0900422 name: "system_ext native test binary",
Dario Frenifd05a742018-05-29 13:28:54 +0100423 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700424 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800425 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100426 target: deviceTarget,
Justin Yund5f6c822019-06-25 16:47:17 +0900427 kind: systemExtSpecificModule,
Dario Frenifd05a742018-05-29 13:28:54 +0100428 },
429 inData: true,
430 },
431 in: []string{"nativetest", "my_test"},
432 out: "target/product/test_device/data/nativetest/my_test",
433 },
434
435 {
Dan Willemsen00269f22017-07-06 16:59:48 -0700436 name: "sanitized system binary",
437 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700438 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800439 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700440 target: deviceTarget,
441 },
442 inSanitizerDir: true,
443 },
444 in: []string{"bin", "my_test"},
445 out: "target/product/test_device/data/asan/system/bin/my_test",
446 },
447 {
448 name: "sanitized vendor binary",
449 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700450 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800451 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700452 target: deviceTarget,
Jiyong Park2db76922017-11-08 16:03:48 +0900453 kind: socSpecificModule,
Dan Willemsen00269f22017-07-06 16:59:48 -0700454 },
455 inSanitizerDir: true,
456 },
457 in: []string{"bin", "my_test"},
458 out: "target/product/test_device/data/asan/vendor/bin/my_test",
459 },
Jiyong Park2db76922017-11-08 16:03:48 +0900460 {
461 name: "sanitized odm binary",
462 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700463 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800464 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900465 target: deviceTarget,
466 kind: deviceSpecificModule,
467 },
468 inSanitizerDir: true,
469 },
470 in: []string{"bin", "my_test"},
471 out: "target/product/test_device/data/asan/odm/bin/my_test",
472 },
473 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900474 name: "sanitized product binary",
Jiyong Park2db76922017-11-08 16:03:48 +0900475 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,
479 kind: productSpecificModule,
480 },
481 inSanitizerDir: true,
482 },
483 in: []string{"bin", "my_test"},
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900484 out: "target/product/test_device/data/asan/product/bin/my_test",
Jiyong Park2db76922017-11-08 16:03:48 +0900485 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700486
487 {
Justin Yund5f6c822019-06-25 16:47:17 +0900488 name: "sanitized system_ext binary",
Dario Frenifd05a742018-05-29 13:28:54 +0100489 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700490 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800491 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100492 target: deviceTarget,
Justin Yund5f6c822019-06-25 16:47:17 +0900493 kind: systemExtSpecificModule,
Dario Frenifd05a742018-05-29 13:28:54 +0100494 },
495 inSanitizerDir: true,
496 },
497 in: []string{"bin", "my_test"},
Justin Yund5f6c822019-06-25 16:47:17 +0900498 out: "target/product/test_device/data/asan/system_ext/bin/my_test",
Dario Frenifd05a742018-05-29 13:28:54 +0100499 },
500
501 {
Dan Willemsen00269f22017-07-06 16:59:48 -0700502 name: "sanitized system native test binary",
503 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700504 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800505 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700506 target: deviceTarget,
507 },
508 inData: true,
509 inSanitizerDir: true,
510 },
511 in: []string{"nativetest", "my_test"},
512 out: "target/product/test_device/data/asan/data/nativetest/my_test",
513 },
514 {
515 name: "sanitized vendor native test binary",
516 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700517 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800518 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700519 target: deviceTarget,
Jiyong Park2db76922017-11-08 16:03:48 +0900520 kind: socSpecificModule,
521 },
522 inData: true,
523 inSanitizerDir: true,
524 },
525 in: []string{"nativetest", "my_test"},
526 out: "target/product/test_device/data/asan/data/nativetest/my_test",
527 },
528 {
529 name: "sanitized odm native test binary",
530 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700531 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800532 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900533 target: deviceTarget,
534 kind: deviceSpecificModule,
535 },
536 inData: true,
537 inSanitizerDir: true,
538 },
539 in: []string{"nativetest", "my_test"},
540 out: "target/product/test_device/data/asan/data/nativetest/my_test",
541 },
542 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900543 name: "sanitized product native test binary",
Jiyong Park2db76922017-11-08 16:03:48 +0900544 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700545 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800546 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900547 target: deviceTarget,
548 kind: productSpecificModule,
Dan Willemsen00269f22017-07-06 16:59:48 -0700549 },
550 inData: true,
551 inSanitizerDir: true,
552 },
553 in: []string{"nativetest", "my_test"},
554 out: "target/product/test_device/data/asan/data/nativetest/my_test",
555 },
Dario Frenifd05a742018-05-29 13:28:54 +0100556 {
Justin Yund5f6c822019-06-25 16:47:17 +0900557 name: "sanitized system_ext native test binary",
Dario Frenifd05a742018-05-29 13:28:54 +0100558 ctx: &moduleInstallPathContextImpl{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700559 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800560 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100561 target: deviceTarget,
Justin Yund5f6c822019-06-25 16:47:17 +0900562 kind: systemExtSpecificModule,
Dario Frenifd05a742018-05-29 13:28:54 +0100563 },
564 inData: true,
565 inSanitizerDir: true,
566 },
567 in: []string{"nativetest", "my_test"},
568 out: "target/product/test_device/data/asan/data/nativetest/my_test",
569 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700570 }
571
572 for _, tc := range testCases {
573 t.Run(tc.name, func(t *testing.T) {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700574 tc.ctx.baseModuleContext.config = testConfig
Dan Willemsen00269f22017-07-06 16:59:48 -0700575 output := PathForModuleInstall(tc.ctx, tc.in...)
576 if output.basePath.path != tc.out {
577 t.Errorf("unexpected path:\n got: %q\nwant: %q\n",
578 output.basePath.path,
579 tc.out)
580 }
581 })
582 }
583}
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700584
585func TestDirectorySortedPaths(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800586 config := TestConfig("out", nil, "", map[string][]byte{
587 "Android.bp": nil,
588 "a.txt": nil,
589 "a/txt": nil,
590 "a/b/c": nil,
591 "a/b/d": nil,
592 "b": nil,
593 "b/b.txt": nil,
594 "a/a.txt": nil,
Colin Cross07e51612019-03-05 12:46:40 -0800595 })
596
Colin Cross98be1bb2019-12-13 20:41:13 -0800597 ctx := PathContextForTesting(config)
598
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700599 makePaths := func() Paths {
600 return Paths{
Colin Cross07e51612019-03-05 12:46:40 -0800601 PathForSource(ctx, "a.txt"),
602 PathForSource(ctx, "a/txt"),
603 PathForSource(ctx, "a/b/c"),
604 PathForSource(ctx, "a/b/d"),
605 PathForSource(ctx, "b"),
606 PathForSource(ctx, "b/b.txt"),
607 PathForSource(ctx, "a/a.txt"),
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700608 }
609 }
610
611 expected := []string{
612 "a.txt",
613 "a/a.txt",
614 "a/b/c",
615 "a/b/d",
616 "a/txt",
617 "b",
618 "b/b.txt",
619 }
620
621 paths := makePaths()
Colin Crossa140bb02018-04-17 10:52:26 -0700622 reversePaths := ReversePaths(paths)
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700623
624 sortedPaths := PathsToDirectorySortedPaths(paths)
625 reverseSortedPaths := PathsToDirectorySortedPaths(reversePaths)
626
627 if !reflect.DeepEqual(Paths(sortedPaths).Strings(), expected) {
628 t.Fatalf("sorted paths:\n %#v\n != \n %#v", paths.Strings(), expected)
629 }
630
631 if !reflect.DeepEqual(Paths(reverseSortedPaths).Strings(), expected) {
632 t.Fatalf("sorted reversed paths:\n %#v\n !=\n %#v", reversePaths.Strings(), expected)
633 }
634
635 expectedA := []string{
636 "a/a.txt",
637 "a/b/c",
638 "a/b/d",
639 "a/txt",
640 }
641
642 inA := sortedPaths.PathsInDirectory("a")
643 if !reflect.DeepEqual(inA.Strings(), expectedA) {
644 t.Errorf("FilesInDirectory(a):\n %#v\n != \n %#v", inA.Strings(), expectedA)
645 }
646
647 expectedA_B := []string{
648 "a/b/c",
649 "a/b/d",
650 }
651
652 inA_B := sortedPaths.PathsInDirectory("a/b")
653 if !reflect.DeepEqual(inA_B.Strings(), expectedA_B) {
654 t.Errorf("FilesInDirectory(a/b):\n %#v\n != \n %#v", inA_B.Strings(), expectedA_B)
655 }
656
657 expectedB := []string{
658 "b/b.txt",
659 }
660
661 inB := sortedPaths.PathsInDirectory("b")
662 if !reflect.DeepEqual(inB.Strings(), expectedB) {
663 t.Errorf("FilesInDirectory(b):\n %#v\n != \n %#v", inA.Strings(), expectedA)
664 }
665}
Colin Cross43f08db2018-11-12 10:13:39 -0800666
667func TestMaybeRel(t *testing.T) {
668 testCases := []struct {
669 name string
670 base string
671 target string
672 out string
673 isRel bool
674 }{
675 {
676 name: "normal",
677 base: "a/b/c",
678 target: "a/b/c/d",
679 out: "d",
680 isRel: true,
681 },
682 {
683 name: "parent",
684 base: "a/b/c/d",
685 target: "a/b/c",
686 isRel: false,
687 },
688 {
689 name: "not relative",
690 base: "a/b",
691 target: "c/d",
692 isRel: false,
693 },
694 {
695 name: "abs1",
696 base: "/a",
697 target: "a",
698 isRel: false,
699 },
700 {
701 name: "abs2",
702 base: "a",
703 target: "/a",
704 isRel: false,
705 },
706 }
707
708 for _, testCase := range testCases {
709 t.Run(testCase.name, func(t *testing.T) {
710 ctx := &configErrorWrapper{}
711 out, isRel := MaybeRel(ctx, testCase.base, testCase.target)
712 if len(ctx.errors) > 0 {
713 t.Errorf("MaybeRel(..., %s, %s) reported unexpected errors %v",
714 testCase.base, testCase.target, ctx.errors)
715 }
716 if isRel != testCase.isRel || out != testCase.out {
717 t.Errorf("MaybeRel(..., %s, %s) want %v, %v got %v, %v",
718 testCase.base, testCase.target, testCase.out, testCase.isRel, out, isRel)
719 }
720 })
721 }
722}
Colin Cross7b3dcc32019-01-24 13:14:39 -0800723
724func TestPathForSource(t *testing.T) {
725 testCases := []struct {
726 name string
727 buildDir string
728 src string
729 err string
730 }{
731 {
732 name: "normal",
733 buildDir: "out",
734 src: "a/b/c",
735 },
736 {
737 name: "abs",
738 buildDir: "out",
739 src: "/a/b/c",
740 err: "is outside directory",
741 },
742 {
743 name: "in out dir",
744 buildDir: "out",
745 src: "out/a/b/c",
746 err: "is in output",
747 },
748 }
749
750 funcs := []struct {
751 name string
752 f func(ctx PathContext, pathComponents ...string) (SourcePath, error)
753 }{
754 {"pathForSource", pathForSource},
755 {"safePathForSource", safePathForSource},
756 }
757
758 for _, f := range funcs {
759 t.Run(f.name, func(t *testing.T) {
760 for _, test := range testCases {
761 t.Run(test.name, func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800762 testConfig := pathTestConfig(test.buildDir)
Colin Cross7b3dcc32019-01-24 13:14:39 -0800763 ctx := &configErrorWrapper{config: testConfig}
764 _, err := f.f(ctx, test.src)
765 if len(ctx.errors) > 0 {
766 t.Fatalf("unexpected errors %v", ctx.errors)
767 }
768 if err != nil {
769 if test.err == "" {
770 t.Fatalf("unexpected error %q", err.Error())
771 } else if !strings.Contains(err.Error(), test.err) {
772 t.Fatalf("incorrect error, want substring %q got %q", test.err, err.Error())
773 }
774 } else {
775 if test.err != "" {
776 t.Fatalf("missing error %q", test.err)
777 }
778 }
779 })
780 }
781 })
782 }
783}
Colin Cross8854a5a2019-02-11 14:14:16 -0800784
Colin Cross8a497952019-03-05 22:25:09 -0800785type pathForModuleSrcTestModule struct {
Colin Cross937664a2019-03-06 10:17:32 -0800786 ModuleBase
787 props struct {
788 Srcs []string `android:"path"`
789 Exclude_srcs []string `android:"path"`
Colin Cross8a497952019-03-05 22:25:09 -0800790
791 Src *string `android:"path"`
Colin Crossba71a3f2019-03-18 12:12:48 -0700792
793 Module_handles_missing_deps bool
Colin Cross937664a2019-03-06 10:17:32 -0800794 }
795
Colin Cross8a497952019-03-05 22:25:09 -0800796 src string
797 rel string
798
799 srcs []string
Colin Cross937664a2019-03-06 10:17:32 -0800800 rels []string
Colin Cross8a497952019-03-05 22:25:09 -0800801
802 missingDeps []string
Colin Cross937664a2019-03-06 10:17:32 -0800803}
804
Colin Cross8a497952019-03-05 22:25:09 -0800805func pathForModuleSrcTestModuleFactory() Module {
806 module := &pathForModuleSrcTestModule{}
Colin Cross937664a2019-03-06 10:17:32 -0800807 module.AddProperties(&module.props)
808 InitAndroidModule(module)
809 return module
810}
811
Colin Cross8a497952019-03-05 22:25:09 -0800812func (p *pathForModuleSrcTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Crossba71a3f2019-03-18 12:12:48 -0700813 var srcs Paths
814 if p.props.Module_handles_missing_deps {
815 srcs, p.missingDeps = PathsAndMissingDepsForModuleSrcExcludes(ctx, p.props.Srcs, p.props.Exclude_srcs)
816 } else {
817 srcs = PathsForModuleSrcExcludes(ctx, p.props.Srcs, p.props.Exclude_srcs)
818 }
Colin Cross8a497952019-03-05 22:25:09 -0800819 p.srcs = srcs.Strings()
Colin Cross937664a2019-03-06 10:17:32 -0800820
Colin Cross8a497952019-03-05 22:25:09 -0800821 for _, src := range srcs {
Colin Cross937664a2019-03-06 10:17:32 -0800822 p.rels = append(p.rels, src.Rel())
823 }
Colin Cross8a497952019-03-05 22:25:09 -0800824
825 if p.props.Src != nil {
826 src := PathForModuleSrc(ctx, *p.props.Src)
827 if src != nil {
828 p.src = src.String()
829 p.rel = src.Rel()
830 }
831 }
832
Colin Crossba71a3f2019-03-18 12:12:48 -0700833 if !p.props.Module_handles_missing_deps {
834 p.missingDeps = ctx.GetMissingDependencies()
835 }
Colin Cross6c4f21f2019-06-06 15:41:36 -0700836
837 ctx.Build(pctx, BuildParams{
838 Rule: Touch,
839 Output: PathForModuleOut(ctx, "output"),
840 })
Colin Cross8a497952019-03-05 22:25:09 -0800841}
842
Colin Cross41955e82019-05-29 14:40:35 -0700843type pathForModuleSrcOutputFileProviderModule struct {
844 ModuleBase
845 props struct {
846 Outs []string
847 Tagged []string
848 }
849
850 outs Paths
851 tagged Paths
852}
853
854func pathForModuleSrcOutputFileProviderModuleFactory() Module {
855 module := &pathForModuleSrcOutputFileProviderModule{}
856 module.AddProperties(&module.props)
857 InitAndroidModule(module)
858 return module
859}
860
861func (p *pathForModuleSrcOutputFileProviderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
862 for _, out := range p.props.Outs {
863 p.outs = append(p.outs, PathForModuleOut(ctx, out))
864 }
865
866 for _, tagged := range p.props.Tagged {
867 p.tagged = append(p.tagged, PathForModuleOut(ctx, tagged))
868 }
869}
870
871func (p *pathForModuleSrcOutputFileProviderModule) OutputFiles(tag string) (Paths, error) {
872 switch tag {
873 case "":
874 return p.outs, nil
875 case ".tagged":
876 return p.tagged, nil
877 default:
878 return nil, fmt.Errorf("unsupported tag %q", tag)
879 }
880}
881
Colin Cross8a497952019-03-05 22:25:09 -0800882type pathForModuleSrcTestCase struct {
883 name string
884 bp string
885 srcs []string
886 rels []string
887 src string
888 rel string
889}
890
891func testPathForModuleSrc(t *testing.T, buildDir string, tests []pathForModuleSrcTestCase) {
892 for _, test := range tests {
893 t.Run(test.name, func(t *testing.T) {
Colin Cross8a497952019-03-05 22:25:09 -0800894 ctx := NewTestContext()
895
Colin Cross4b49b762019-11-22 15:25:03 -0800896 ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory)
897 ctx.RegisterModuleType("output_file_provider", pathForModuleSrcOutputFileProviderModuleFactory)
898 ctx.RegisterModuleType("filegroup", FileGroupFactory)
Colin Cross8a497952019-03-05 22:25:09 -0800899
900 fgBp := `
901 filegroup {
902 name: "a",
903 srcs: ["src/a"],
904 }
905 `
906
Colin Cross41955e82019-05-29 14:40:35 -0700907 ofpBp := `
908 output_file_provider {
909 name: "b",
910 outs: ["gen/b"],
911 tagged: ["gen/c"],
912 }
913 `
914
Colin Cross8a497952019-03-05 22:25:09 -0800915 mockFS := map[string][]byte{
916 "fg/Android.bp": []byte(fgBp),
917 "foo/Android.bp": []byte(test.bp),
Colin Cross41955e82019-05-29 14:40:35 -0700918 "ofp/Android.bp": []byte(ofpBp),
Colin Cross8a497952019-03-05 22:25:09 -0800919 "fg/src/a": nil,
920 "foo/src/b": nil,
921 "foo/src/c": nil,
922 "foo/src/d": nil,
923 "foo/src/e/e": nil,
924 "foo/src_special/$": nil,
925 }
926
Colin Cross98be1bb2019-12-13 20:41:13 -0800927 config := TestConfig(buildDir, nil, "", mockFS)
Colin Cross8a497952019-03-05 22:25:09 -0800928
Colin Cross98be1bb2019-12-13 20:41:13 -0800929 ctx.Register(config)
Colin Cross41955e82019-05-29 14:40:35 -0700930 _, errs := ctx.ParseFileList(".", []string{"fg/Android.bp", "foo/Android.bp", "ofp/Android.bp"})
Colin Cross8a497952019-03-05 22:25:09 -0800931 FailIfErrored(t, errs)
932 _, errs = ctx.PrepareBuildActions(config)
933 FailIfErrored(t, errs)
934
935 m := ctx.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule)
936
937 if g, w := m.srcs, test.srcs; !reflect.DeepEqual(g, w) {
938 t.Errorf("want srcs %q, got %q", w, g)
939 }
940
941 if g, w := m.rels, test.rels; !reflect.DeepEqual(g, w) {
942 t.Errorf("want rels %q, got %q", w, g)
943 }
944
945 if g, w := m.src, test.src; g != w {
946 t.Errorf("want src %q, got %q", w, g)
947 }
948
949 if g, w := m.rel, test.rel; g != w {
950 t.Errorf("want rel %q, got %q", w, g)
951 }
952 })
953 }
Colin Cross937664a2019-03-06 10:17:32 -0800954}
955
Colin Cross8a497952019-03-05 22:25:09 -0800956func TestPathsForModuleSrc(t *testing.T) {
957 tests := []pathForModuleSrcTestCase{
Colin Cross937664a2019-03-06 10:17:32 -0800958 {
959 name: "path",
960 bp: `
961 test {
962 name: "foo",
963 srcs: ["src/b"],
964 }`,
965 srcs: []string{"foo/src/b"},
966 rels: []string{"src/b"},
967 },
968 {
969 name: "glob",
970 bp: `
971 test {
972 name: "foo",
973 srcs: [
974 "src/*",
975 "src/e/*",
976 ],
977 }`,
978 srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"},
979 rels: []string{"src/b", "src/c", "src/d", "src/e/e"},
980 },
981 {
982 name: "recursive glob",
983 bp: `
984 test {
985 name: "foo",
986 srcs: ["src/**/*"],
987 }`,
988 srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"},
989 rels: []string{"src/b", "src/c", "src/d", "src/e/e"},
990 },
991 {
992 name: "filegroup",
993 bp: `
994 test {
995 name: "foo",
996 srcs: [":a"],
997 }`,
998 srcs: []string{"fg/src/a"},
999 rels: []string{"src/a"},
1000 },
1001 {
Colin Cross41955e82019-05-29 14:40:35 -07001002 name: "output file provider",
1003 bp: `
1004 test {
1005 name: "foo",
1006 srcs: [":b"],
1007 }`,
1008 srcs: []string{buildDir + "/.intermediates/ofp/b/gen/b"},
1009 rels: []string{"gen/b"},
1010 },
1011 {
1012 name: "output file provider tagged",
1013 bp: `
1014 test {
1015 name: "foo",
1016 srcs: [":b{.tagged}"],
1017 }`,
1018 srcs: []string{buildDir + "/.intermediates/ofp/b/gen/c"},
1019 rels: []string{"gen/c"},
1020 },
1021 {
Colin Cross937664a2019-03-06 10:17:32 -08001022 name: "special characters glob",
1023 bp: `
1024 test {
1025 name: "foo",
1026 srcs: ["src_special/*"],
1027 }`,
1028 srcs: []string{"foo/src_special/$"},
1029 rels: []string{"src_special/$"},
1030 },
1031 }
1032
Colin Cross41955e82019-05-29 14:40:35 -07001033 testPathForModuleSrc(t, buildDir, tests)
1034}
1035
1036func TestPathForModuleSrc(t *testing.T) {
Colin Cross8a497952019-03-05 22:25:09 -08001037 tests := []pathForModuleSrcTestCase{
1038 {
1039 name: "path",
1040 bp: `
1041 test {
1042 name: "foo",
1043 src: "src/b",
1044 }`,
1045 src: "foo/src/b",
1046 rel: "src/b",
1047 },
1048 {
1049 name: "glob",
1050 bp: `
1051 test {
1052 name: "foo",
1053 src: "src/e/*",
1054 }`,
1055 src: "foo/src/e/e",
1056 rel: "src/e/e",
1057 },
1058 {
1059 name: "filegroup",
1060 bp: `
1061 test {
1062 name: "foo",
1063 src: ":a",
1064 }`,
1065 src: "fg/src/a",
1066 rel: "src/a",
1067 },
1068 {
Colin Cross41955e82019-05-29 14:40:35 -07001069 name: "output file provider",
1070 bp: `
1071 test {
1072 name: "foo",
1073 src: ":b",
1074 }`,
1075 src: buildDir + "/.intermediates/ofp/b/gen/b",
1076 rel: "gen/b",
1077 },
1078 {
1079 name: "output file provider tagged",
1080 bp: `
1081 test {
1082 name: "foo",
1083 src: ":b{.tagged}",
1084 }`,
1085 src: buildDir + "/.intermediates/ofp/b/gen/c",
1086 rel: "gen/c",
1087 },
1088 {
Colin Cross8a497952019-03-05 22:25:09 -08001089 name: "special characters glob",
1090 bp: `
1091 test {
1092 name: "foo",
1093 src: "src_special/*",
1094 }`,
1095 src: "foo/src_special/$",
1096 rel: "src_special/$",
1097 },
1098 }
1099
Colin Cross8a497952019-03-05 22:25:09 -08001100 testPathForModuleSrc(t, buildDir, tests)
1101}
Colin Cross937664a2019-03-06 10:17:32 -08001102
Colin Cross8a497952019-03-05 22:25:09 -08001103func TestPathsForModuleSrc_AllowMissingDependencies(t *testing.T) {
Colin Cross8a497952019-03-05 22:25:09 -08001104 bp := `
1105 test {
1106 name: "foo",
1107 srcs: [":a"],
1108 exclude_srcs: [":b"],
1109 src: ":c",
1110 }
Colin Crossba71a3f2019-03-18 12:12:48 -07001111
1112 test {
1113 name: "bar",
1114 srcs: [":d"],
1115 exclude_srcs: [":e"],
1116 module_handles_missing_deps: true,
1117 }
Colin Cross8a497952019-03-05 22:25:09 -08001118 `
1119
Colin Cross98be1bb2019-12-13 20:41:13 -08001120 config := TestConfig(buildDir, nil, bp, nil)
1121 config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
Colin Cross8a497952019-03-05 22:25:09 -08001122
Colin Cross98be1bb2019-12-13 20:41:13 -08001123 ctx := NewTestContext()
1124 ctx.SetAllowMissingDependencies(true)
Colin Cross8a497952019-03-05 22:25:09 -08001125
Colin Cross98be1bb2019-12-13 20:41:13 -08001126 ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory)
1127
1128 ctx.Register(config)
1129
Colin Cross8a497952019-03-05 22:25:09 -08001130 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
1131 FailIfErrored(t, errs)
1132 _, errs = ctx.PrepareBuildActions(config)
1133 FailIfErrored(t, errs)
1134
1135 foo := ctx.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule)
1136
1137 if g, w := foo.missingDeps, []string{"a", "b", "c"}; !reflect.DeepEqual(g, w) {
Colin Crossba71a3f2019-03-18 12:12:48 -07001138 t.Errorf("want foo missing deps %q, got %q", w, g)
Colin Cross8a497952019-03-05 22:25:09 -08001139 }
1140
1141 if g, w := foo.srcs, []string{}; !reflect.DeepEqual(g, w) {
Colin Crossba71a3f2019-03-18 12:12:48 -07001142 t.Errorf("want foo srcs %q, got %q", w, g)
Colin Cross8a497952019-03-05 22:25:09 -08001143 }
1144
1145 if g, w := foo.src, ""; g != w {
Colin Crossba71a3f2019-03-18 12:12:48 -07001146 t.Errorf("want foo src %q, got %q", w, g)
Colin Cross8a497952019-03-05 22:25:09 -08001147 }
1148
Colin Crossba71a3f2019-03-18 12:12:48 -07001149 bar := ctx.ModuleForTests("bar", "").Module().(*pathForModuleSrcTestModule)
1150
1151 if g, w := bar.missingDeps, []string{"d", "e"}; !reflect.DeepEqual(g, w) {
1152 t.Errorf("want bar missing deps %q, got %q", w, g)
1153 }
1154
1155 if g, w := bar.srcs, []string{}; !reflect.DeepEqual(g, w) {
1156 t.Errorf("want bar srcs %q, got %q", w, g)
1157 }
Colin Cross937664a2019-03-06 10:17:32 -08001158}
1159
Colin Cross8854a5a2019-02-11 14:14:16 -08001160func ExampleOutputPath_ReplaceExtension() {
1161 ctx := &configErrorWrapper{
Colin Cross98be1bb2019-12-13 20:41:13 -08001162 config: TestConfig("out", nil, "", nil),
Colin Cross8854a5a2019-02-11 14:14:16 -08001163 }
Colin Cross2cdd5df2019-02-25 10:25:24 -08001164 p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
Colin Cross8854a5a2019-02-11 14:14:16 -08001165 p2 := p.ReplaceExtension(ctx, "oat")
1166 fmt.Println(p, p2)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001167 fmt.Println(p.Rel(), p2.Rel())
Colin Cross8854a5a2019-02-11 14:14:16 -08001168
1169 // Output:
1170 // out/system/framework/boot.art out/system/framework/boot.oat
Colin Cross2cdd5df2019-02-25 10:25:24 -08001171 // boot.art boot.oat
Colin Cross8854a5a2019-02-11 14:14:16 -08001172}
Colin Cross40e33732019-02-15 11:08:35 -08001173
1174func ExampleOutputPath_FileInSameDir() {
1175 ctx := &configErrorWrapper{
Colin Cross98be1bb2019-12-13 20:41:13 -08001176 config: TestConfig("out", nil, "", nil),
Colin Cross40e33732019-02-15 11:08:35 -08001177 }
Colin Cross2cdd5df2019-02-25 10:25:24 -08001178 p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
Colin Cross40e33732019-02-15 11:08:35 -08001179 p2 := p.InSameDir(ctx, "oat", "arm", "boot.vdex")
1180 fmt.Println(p, p2)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001181 fmt.Println(p.Rel(), p2.Rel())
Colin Cross40e33732019-02-15 11:08:35 -08001182
1183 // Output:
1184 // out/system/framework/boot.art out/system/framework/oat/arm/boot.vdex
Colin Cross2cdd5df2019-02-25 10:25:24 -08001185 // boot.art oat/arm/boot.vdex
Colin Cross40e33732019-02-15 11:08:35 -08001186}