Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1 | // 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "fmt" |
| 20 | "reflect" |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 21 | "strconv" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 22 | "strings" |
| 23 | "testing" |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 24 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | type strsTestCase struct { |
| 29 | in []string |
| 30 | out string |
| 31 | err []error |
| 32 | } |
| 33 | |
| 34 | var 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 Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 75 | { |
| 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 Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | var validateSafePathTestCases = append(commonValidatePathTestCases, []strsTestCase{ |
| 93 | { |
| 94 | in: []string{"$host/../$a"}, |
| 95 | out: "$a", |
| 96 | }, |
| 97 | }...) |
| 98 | |
| 99 | var 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 | |
| 112 | func TestValidateSafePath(t *testing.T) { |
| 113 | for _, testCase := range validateSafePathTestCases { |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 114 | t.Run(strings.Join(testCase.in, ","), func(t *testing.T) { |
| 115 | ctx := &configErrorWrapper{} |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 116 | out, err := validateSafePath(testCase.in...) |
| 117 | if err != nil { |
| 118 | reportPathError(ctx, err) |
| 119 | } |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 120 | check(t, "validateSafePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err) |
| 121 | }) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestValidatePath(t *testing.T) { |
| 126 | for _, testCase := range validatePathTestCases { |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 127 | t.Run(strings.Join(testCase.in, ","), func(t *testing.T) { |
| 128 | ctx := &configErrorWrapper{} |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 129 | out, err := validatePath(testCase.in...) |
| 130 | if err != nil { |
| 131 | reportPathError(ctx, err) |
| 132 | } |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 133 | check(t, "validatePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err) |
| 134 | }) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | func TestOptionalPath(t *testing.T) { |
| 139 | var path OptionalPath |
| 140 | checkInvalidOptionalPath(t, path) |
| 141 | |
| 142 | path = OptionalPathForPath(nil) |
| 143 | checkInvalidOptionalPath(t, path) |
| 144 | } |
| 145 | |
| 146 | func checkInvalidOptionalPath(t *testing.T, path OptionalPath) { |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 147 | t.Helper() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 148 | 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 | |
| 162 | func check(t *testing.T, testType, testString string, |
| 163 | got interface{}, err []error, |
| 164 | expected interface{}, expectedErr []error) { |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 165 | t.Helper() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 166 | |
| 167 | printedTestCase := false |
| 168 | e := func(s string, expected, got interface{}) { |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 169 | t.Helper() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 170 | 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 | |
| 188 | func 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 Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 199 | |
| 200 | type moduleInstallPathContextImpl struct { |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 201 | baseModuleContext |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 202 | |
| 203 | inData bool |
Jaewoong Jung | 0949f31 | 2019-09-11 10:25:18 -0700 | [diff] [blame] | 204 | inTestcases bool |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 205 | inSanitizerDir bool |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 206 | inRamdisk bool |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 207 | inRecovery bool |
Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 208 | inRoot bool |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 209 | forceOS *OsType |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 210 | forceArch *ArchType |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 213 | func (m moduleInstallPathContextImpl) Config() Config { |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 214 | return m.baseModuleContext.config |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | func (moduleInstallPathContextImpl) AddNinjaFileDeps(deps ...string) {} |
| 218 | |
| 219 | func (m moduleInstallPathContextImpl) InstallInData() bool { |
| 220 | return m.inData |
| 221 | } |
| 222 | |
Jaewoong Jung | 0949f31 | 2019-09-11 10:25:18 -0700 | [diff] [blame] | 223 | func (m moduleInstallPathContextImpl) InstallInTestcases() bool { |
| 224 | return m.inTestcases |
| 225 | } |
| 226 | |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 227 | func (m moduleInstallPathContextImpl) InstallInSanitizerDir() bool { |
| 228 | return m.inSanitizerDir |
| 229 | } |
| 230 | |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 231 | func (m moduleInstallPathContextImpl) InstallInRamdisk() bool { |
| 232 | return m.inRamdisk |
| 233 | } |
| 234 | |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 235 | func (m moduleInstallPathContextImpl) InstallInRecovery() bool { |
| 236 | return m.inRecovery |
| 237 | } |
| 238 | |
Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 239 | func (m moduleInstallPathContextImpl) InstallInRoot() bool { |
| 240 | return m.inRoot |
| 241 | } |
| 242 | |
Colin Cross | 607d858 | 2019-07-29 16:44:46 -0700 | [diff] [blame] | 243 | func (m moduleInstallPathContextImpl) InstallBypassMake() bool { |
| 244 | return false |
| 245 | } |
| 246 | |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 247 | func (m moduleInstallPathContextImpl) InstallForceOS() (*OsType, *ArchType) { |
| 248 | return m.forceOS, m.forceArch |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 251 | func pathTestConfig(buildDir string) Config { |
| 252 | return TestConfig(buildDir, nil, "", nil) |
| 253 | } |
| 254 | |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 255 | func TestPathForModuleInstall(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 256 | testConfig := pathTestConfig("") |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 257 | |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 258 | hostTarget := Target{Os: Linux, Arch: Arch{ArchType: X86}} |
| 259 | deviceTarget := Target{Os: Android, Arch: Arch{ArchType: Arm64}} |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 260 | |
| 261 | testCases := []struct { |
| 262 | name string |
| 263 | ctx *moduleInstallPathContextImpl |
| 264 | in []string |
| 265 | out string |
| 266 | }{ |
| 267 | { |
| 268 | name: "host binary", |
| 269 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 270 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 271 | os: hostTarget.Os, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 272 | target: hostTarget, |
| 273 | }, |
| 274 | }, |
| 275 | in: []string{"bin", "my_test"}, |
| 276 | out: "host/linux-x86/bin/my_test", |
| 277 | }, |
| 278 | |
| 279 | { |
| 280 | name: "system binary", |
| 281 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 282 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 283 | os: deviceTarget.Os, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 284 | target: deviceTarget, |
| 285 | }, |
| 286 | }, |
| 287 | in: []string{"bin", "my_test"}, |
| 288 | out: "target/product/test_device/system/bin/my_test", |
| 289 | }, |
| 290 | { |
| 291 | name: "vendor binary", |
| 292 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 293 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 294 | os: deviceTarget.Os, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 295 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 296 | earlyModuleContext: earlyModuleContext{ |
| 297 | kind: socSpecificModule, |
| 298 | }, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 299 | }, |
| 300 | }, |
| 301 | in: []string{"bin", "my_test"}, |
| 302 | out: "target/product/test_device/vendor/bin/my_test", |
| 303 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 304 | { |
| 305 | name: "odm binary", |
| 306 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 307 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 308 | os: deviceTarget.Os, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 309 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 310 | earlyModuleContext: earlyModuleContext{ |
| 311 | kind: deviceSpecificModule, |
| 312 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 313 | }, |
| 314 | }, |
| 315 | in: []string{"bin", "my_test"}, |
| 316 | out: "target/product/test_device/odm/bin/my_test", |
| 317 | }, |
| 318 | { |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 319 | name: "product binary", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 320 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 321 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 322 | os: deviceTarget.Os, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 323 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 324 | earlyModuleContext: earlyModuleContext{ |
| 325 | kind: productSpecificModule, |
| 326 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 327 | }, |
| 328 | }, |
| 329 | in: []string{"bin", "my_test"}, |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 330 | out: "target/product/test_device/product/bin/my_test", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 331 | }, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 332 | { |
Justin Yun | d5f6c82 | 2019-06-25 16:47:17 +0900 | [diff] [blame] | 333 | name: "system_ext binary", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 334 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 335 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 336 | os: deviceTarget.Os, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 337 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 338 | earlyModuleContext: earlyModuleContext{ |
| 339 | kind: systemExtSpecificModule, |
| 340 | }, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 341 | }, |
| 342 | }, |
| 343 | in: []string{"bin", "my_test"}, |
Justin Yun | d5f6c82 | 2019-06-25 16:47:17 +0900 | [diff] [blame] | 344 | out: "target/product/test_device/system_ext/bin/my_test", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 345 | }, |
Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 346 | { |
| 347 | name: "root binary", |
| 348 | ctx: &moduleInstallPathContextImpl{ |
| 349 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 350 | os: deviceTarget.Os, |
Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 351 | target: deviceTarget, |
| 352 | }, |
| 353 | inRoot: true, |
| 354 | }, |
| 355 | in: []string{"my_test"}, |
| 356 | out: "target/product/test_device/root/my_test", |
| 357 | }, |
| 358 | { |
| 359 | name: "recovery binary", |
| 360 | ctx: &moduleInstallPathContextImpl{ |
| 361 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 362 | os: deviceTarget.Os, |
Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 363 | target: deviceTarget, |
| 364 | }, |
| 365 | inRecovery: true, |
| 366 | }, |
| 367 | in: []string{"bin/my_test"}, |
| 368 | out: "target/product/test_device/recovery/root/system/bin/my_test", |
| 369 | }, |
| 370 | { |
| 371 | name: "recovery root binary", |
| 372 | ctx: &moduleInstallPathContextImpl{ |
| 373 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 374 | os: deviceTarget.Os, |
Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 375 | target: deviceTarget, |
| 376 | }, |
| 377 | inRecovery: true, |
| 378 | inRoot: true, |
| 379 | }, |
| 380 | in: []string{"my_test"}, |
| 381 | out: "target/product/test_device/recovery/root/my_test", |
| 382 | }, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 383 | |
| 384 | { |
| 385 | name: "system native test binary", |
| 386 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 387 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 388 | os: deviceTarget.Os, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 389 | target: deviceTarget, |
| 390 | }, |
| 391 | inData: true, |
| 392 | }, |
| 393 | in: []string{"nativetest", "my_test"}, |
| 394 | out: "target/product/test_device/data/nativetest/my_test", |
| 395 | }, |
| 396 | { |
| 397 | name: "vendor native test binary", |
| 398 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 399 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 400 | os: deviceTarget.Os, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 401 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 402 | earlyModuleContext: earlyModuleContext{ |
| 403 | kind: socSpecificModule, |
| 404 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 405 | }, |
| 406 | inData: true, |
| 407 | }, |
| 408 | in: []string{"nativetest", "my_test"}, |
| 409 | out: "target/product/test_device/data/nativetest/my_test", |
| 410 | }, |
| 411 | { |
| 412 | name: "odm native test binary", |
| 413 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 414 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 415 | os: deviceTarget.Os, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 416 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 417 | earlyModuleContext: earlyModuleContext{ |
| 418 | kind: deviceSpecificModule, |
| 419 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 420 | }, |
| 421 | inData: true, |
| 422 | }, |
| 423 | in: []string{"nativetest", "my_test"}, |
| 424 | out: "target/product/test_device/data/nativetest/my_test", |
| 425 | }, |
| 426 | { |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 427 | name: "product native test binary", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 428 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 429 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 430 | os: deviceTarget.Os, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 431 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 432 | earlyModuleContext: earlyModuleContext{ |
| 433 | kind: productSpecificModule, |
| 434 | }, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 435 | }, |
| 436 | inData: true, |
| 437 | }, |
| 438 | in: []string{"nativetest", "my_test"}, |
| 439 | out: "target/product/test_device/data/nativetest/my_test", |
| 440 | }, |
| 441 | |
| 442 | { |
Justin Yun | d5f6c82 | 2019-06-25 16:47:17 +0900 | [diff] [blame] | 443 | name: "system_ext native test binary", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 444 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 445 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 446 | os: deviceTarget.Os, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 447 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 448 | earlyModuleContext: earlyModuleContext{ |
| 449 | kind: systemExtSpecificModule, |
| 450 | }, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 451 | }, |
| 452 | inData: true, |
| 453 | }, |
| 454 | in: []string{"nativetest", "my_test"}, |
| 455 | out: "target/product/test_device/data/nativetest/my_test", |
| 456 | }, |
| 457 | |
| 458 | { |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 459 | name: "sanitized system binary", |
| 460 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 461 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 462 | os: deviceTarget.Os, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 463 | target: deviceTarget, |
| 464 | }, |
| 465 | inSanitizerDir: true, |
| 466 | }, |
| 467 | in: []string{"bin", "my_test"}, |
| 468 | out: "target/product/test_device/data/asan/system/bin/my_test", |
| 469 | }, |
| 470 | { |
| 471 | name: "sanitized vendor binary", |
| 472 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 473 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 474 | os: deviceTarget.Os, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 475 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 476 | earlyModuleContext: earlyModuleContext{ |
| 477 | kind: socSpecificModule, |
| 478 | }, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 479 | }, |
| 480 | inSanitizerDir: true, |
| 481 | }, |
| 482 | in: []string{"bin", "my_test"}, |
| 483 | out: "target/product/test_device/data/asan/vendor/bin/my_test", |
| 484 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 485 | { |
| 486 | name: "sanitized odm binary", |
| 487 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 488 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 489 | os: deviceTarget.Os, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 490 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 491 | earlyModuleContext: earlyModuleContext{ |
| 492 | kind: deviceSpecificModule, |
| 493 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 494 | }, |
| 495 | inSanitizerDir: true, |
| 496 | }, |
| 497 | in: []string{"bin", "my_test"}, |
| 498 | out: "target/product/test_device/data/asan/odm/bin/my_test", |
| 499 | }, |
| 500 | { |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 501 | name: "sanitized product binary", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 502 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 503 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 504 | os: deviceTarget.Os, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 505 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 506 | earlyModuleContext: earlyModuleContext{ |
| 507 | kind: productSpecificModule, |
| 508 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 509 | }, |
| 510 | inSanitizerDir: true, |
| 511 | }, |
| 512 | in: []string{"bin", "my_test"}, |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 513 | out: "target/product/test_device/data/asan/product/bin/my_test", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 514 | }, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 515 | |
| 516 | { |
Justin Yun | d5f6c82 | 2019-06-25 16:47:17 +0900 | [diff] [blame] | 517 | name: "sanitized system_ext binary", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 518 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 519 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 520 | os: deviceTarget.Os, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 521 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 522 | earlyModuleContext: earlyModuleContext{ |
| 523 | kind: systemExtSpecificModule, |
| 524 | }, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 525 | }, |
| 526 | inSanitizerDir: true, |
| 527 | }, |
| 528 | in: []string{"bin", "my_test"}, |
Justin Yun | d5f6c82 | 2019-06-25 16:47:17 +0900 | [diff] [blame] | 529 | out: "target/product/test_device/data/asan/system_ext/bin/my_test", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 530 | }, |
| 531 | |
| 532 | { |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 533 | name: "sanitized system native test binary", |
| 534 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 535 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 536 | os: deviceTarget.Os, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 537 | target: deviceTarget, |
| 538 | }, |
| 539 | inData: true, |
| 540 | inSanitizerDir: true, |
| 541 | }, |
| 542 | in: []string{"nativetest", "my_test"}, |
| 543 | out: "target/product/test_device/data/asan/data/nativetest/my_test", |
| 544 | }, |
| 545 | { |
| 546 | name: "sanitized vendor native test binary", |
| 547 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 548 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 549 | os: deviceTarget.Os, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 550 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 551 | earlyModuleContext: earlyModuleContext{ |
| 552 | kind: socSpecificModule, |
| 553 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 554 | }, |
| 555 | inData: true, |
| 556 | inSanitizerDir: true, |
| 557 | }, |
| 558 | in: []string{"nativetest", "my_test"}, |
| 559 | out: "target/product/test_device/data/asan/data/nativetest/my_test", |
| 560 | }, |
| 561 | { |
| 562 | name: "sanitized odm native test binary", |
| 563 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 564 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 565 | os: deviceTarget.Os, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 566 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 567 | earlyModuleContext: earlyModuleContext{ |
| 568 | kind: deviceSpecificModule, |
| 569 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 570 | }, |
| 571 | inData: true, |
| 572 | inSanitizerDir: true, |
| 573 | }, |
| 574 | in: []string{"nativetest", "my_test"}, |
| 575 | out: "target/product/test_device/data/asan/data/nativetest/my_test", |
| 576 | }, |
| 577 | { |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 578 | name: "sanitized product native test binary", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 579 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 580 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 581 | os: deviceTarget.Os, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 582 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 583 | earlyModuleContext: earlyModuleContext{ |
| 584 | kind: productSpecificModule, |
| 585 | }, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 586 | }, |
| 587 | inData: true, |
| 588 | inSanitizerDir: true, |
| 589 | }, |
| 590 | in: []string{"nativetest", "my_test"}, |
| 591 | out: "target/product/test_device/data/asan/data/nativetest/my_test", |
| 592 | }, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 593 | { |
Justin Yun | d5f6c82 | 2019-06-25 16:47:17 +0900 | [diff] [blame] | 594 | name: "sanitized system_ext native test binary", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 595 | ctx: &moduleInstallPathContextImpl{ |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 596 | baseModuleContext: baseModuleContext{ |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 597 | os: deviceTarget.Os, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 598 | target: deviceTarget, |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 599 | earlyModuleContext: earlyModuleContext{ |
| 600 | kind: systemExtSpecificModule, |
| 601 | }, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 602 | }, |
| 603 | inData: true, |
| 604 | inSanitizerDir: true, |
| 605 | }, |
| 606 | in: []string{"nativetest", "my_test"}, |
| 607 | out: "target/product/test_device/data/asan/data/nativetest/my_test", |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 608 | }, { |
| 609 | name: "device testcases", |
| 610 | ctx: &moduleInstallPathContextImpl{ |
| 611 | baseModuleContext: baseModuleContext{ |
| 612 | os: deviceTarget.Os, |
| 613 | target: deviceTarget, |
| 614 | }, |
| 615 | inTestcases: true, |
| 616 | }, |
| 617 | in: []string{"my_test", "my_test_bin"}, |
| 618 | out: "target/product/test_device/testcases/my_test/my_test_bin", |
| 619 | }, { |
| 620 | name: "host testcases", |
| 621 | ctx: &moduleInstallPathContextImpl{ |
| 622 | baseModuleContext: baseModuleContext{ |
| 623 | os: hostTarget.Os, |
| 624 | target: hostTarget, |
| 625 | }, |
| 626 | inTestcases: true, |
| 627 | }, |
| 628 | in: []string{"my_test", "my_test_bin"}, |
| 629 | out: "host/linux-x86/testcases/my_test/my_test_bin", |
| 630 | }, { |
| 631 | name: "forced host testcases", |
| 632 | ctx: &moduleInstallPathContextImpl{ |
| 633 | baseModuleContext: baseModuleContext{ |
| 634 | os: deviceTarget.Os, |
| 635 | target: deviceTarget, |
| 636 | }, |
| 637 | inTestcases: true, |
| 638 | forceOS: &Linux, |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 639 | forceArch: &X86, |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 640 | }, |
| 641 | in: []string{"my_test", "my_test_bin"}, |
| 642 | out: "host/linux-x86/testcases/my_test/my_test_bin", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 643 | }, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | for _, tc := range testCases { |
| 647 | t.Run(tc.name, func(t *testing.T) { |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 648 | tc.ctx.baseModuleContext.config = testConfig |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 649 | output := PathForModuleInstall(tc.ctx, tc.in...) |
| 650 | if output.basePath.path != tc.out { |
| 651 | t.Errorf("unexpected path:\n got: %q\nwant: %q\n", |
| 652 | output.basePath.path, |
| 653 | tc.out) |
| 654 | } |
| 655 | }) |
| 656 | } |
| 657 | } |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 658 | |
| 659 | func TestDirectorySortedPaths(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 660 | config := TestConfig("out", nil, "", map[string][]byte{ |
| 661 | "Android.bp": nil, |
| 662 | "a.txt": nil, |
| 663 | "a/txt": nil, |
| 664 | "a/b/c": nil, |
| 665 | "a/b/d": nil, |
| 666 | "b": nil, |
| 667 | "b/b.txt": nil, |
| 668 | "a/a.txt": nil, |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 669 | }) |
| 670 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 671 | ctx := PathContextForTesting(config) |
| 672 | |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 673 | makePaths := func() Paths { |
| 674 | return Paths{ |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 675 | PathForSource(ctx, "a.txt"), |
| 676 | PathForSource(ctx, "a/txt"), |
| 677 | PathForSource(ctx, "a/b/c"), |
| 678 | PathForSource(ctx, "a/b/d"), |
| 679 | PathForSource(ctx, "b"), |
| 680 | PathForSource(ctx, "b/b.txt"), |
| 681 | PathForSource(ctx, "a/a.txt"), |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
| 685 | expected := []string{ |
| 686 | "a.txt", |
| 687 | "a/a.txt", |
| 688 | "a/b/c", |
| 689 | "a/b/d", |
| 690 | "a/txt", |
| 691 | "b", |
| 692 | "b/b.txt", |
| 693 | } |
| 694 | |
| 695 | paths := makePaths() |
Colin Cross | a140bb0 | 2018-04-17 10:52:26 -0700 | [diff] [blame] | 696 | reversePaths := ReversePaths(paths) |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 697 | |
| 698 | sortedPaths := PathsToDirectorySortedPaths(paths) |
| 699 | reverseSortedPaths := PathsToDirectorySortedPaths(reversePaths) |
| 700 | |
| 701 | if !reflect.DeepEqual(Paths(sortedPaths).Strings(), expected) { |
| 702 | t.Fatalf("sorted paths:\n %#v\n != \n %#v", paths.Strings(), expected) |
| 703 | } |
| 704 | |
| 705 | if !reflect.DeepEqual(Paths(reverseSortedPaths).Strings(), expected) { |
| 706 | t.Fatalf("sorted reversed paths:\n %#v\n !=\n %#v", reversePaths.Strings(), expected) |
| 707 | } |
| 708 | |
| 709 | expectedA := []string{ |
| 710 | "a/a.txt", |
| 711 | "a/b/c", |
| 712 | "a/b/d", |
| 713 | "a/txt", |
| 714 | } |
| 715 | |
| 716 | inA := sortedPaths.PathsInDirectory("a") |
| 717 | if !reflect.DeepEqual(inA.Strings(), expectedA) { |
| 718 | t.Errorf("FilesInDirectory(a):\n %#v\n != \n %#v", inA.Strings(), expectedA) |
| 719 | } |
| 720 | |
| 721 | expectedA_B := []string{ |
| 722 | "a/b/c", |
| 723 | "a/b/d", |
| 724 | } |
| 725 | |
| 726 | inA_B := sortedPaths.PathsInDirectory("a/b") |
| 727 | if !reflect.DeepEqual(inA_B.Strings(), expectedA_B) { |
| 728 | t.Errorf("FilesInDirectory(a/b):\n %#v\n != \n %#v", inA_B.Strings(), expectedA_B) |
| 729 | } |
| 730 | |
| 731 | expectedB := []string{ |
| 732 | "b/b.txt", |
| 733 | } |
| 734 | |
| 735 | inB := sortedPaths.PathsInDirectory("b") |
| 736 | if !reflect.DeepEqual(inB.Strings(), expectedB) { |
| 737 | t.Errorf("FilesInDirectory(b):\n %#v\n != \n %#v", inA.Strings(), expectedA) |
| 738 | } |
| 739 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 740 | |
| 741 | func TestMaybeRel(t *testing.T) { |
| 742 | testCases := []struct { |
| 743 | name string |
| 744 | base string |
| 745 | target string |
| 746 | out string |
| 747 | isRel bool |
| 748 | }{ |
| 749 | { |
| 750 | name: "normal", |
| 751 | base: "a/b/c", |
| 752 | target: "a/b/c/d", |
| 753 | out: "d", |
| 754 | isRel: true, |
| 755 | }, |
| 756 | { |
| 757 | name: "parent", |
| 758 | base: "a/b/c/d", |
| 759 | target: "a/b/c", |
| 760 | isRel: false, |
| 761 | }, |
| 762 | { |
| 763 | name: "not relative", |
| 764 | base: "a/b", |
| 765 | target: "c/d", |
| 766 | isRel: false, |
| 767 | }, |
| 768 | { |
| 769 | name: "abs1", |
| 770 | base: "/a", |
| 771 | target: "a", |
| 772 | isRel: false, |
| 773 | }, |
| 774 | { |
| 775 | name: "abs2", |
| 776 | base: "a", |
| 777 | target: "/a", |
| 778 | isRel: false, |
| 779 | }, |
| 780 | } |
| 781 | |
| 782 | for _, testCase := range testCases { |
| 783 | t.Run(testCase.name, func(t *testing.T) { |
| 784 | ctx := &configErrorWrapper{} |
| 785 | out, isRel := MaybeRel(ctx, testCase.base, testCase.target) |
| 786 | if len(ctx.errors) > 0 { |
| 787 | t.Errorf("MaybeRel(..., %s, %s) reported unexpected errors %v", |
| 788 | testCase.base, testCase.target, ctx.errors) |
| 789 | } |
| 790 | if isRel != testCase.isRel || out != testCase.out { |
| 791 | t.Errorf("MaybeRel(..., %s, %s) want %v, %v got %v, %v", |
| 792 | testCase.base, testCase.target, testCase.out, testCase.isRel, out, isRel) |
| 793 | } |
| 794 | }) |
| 795 | } |
| 796 | } |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 797 | |
| 798 | func TestPathForSource(t *testing.T) { |
| 799 | testCases := []struct { |
| 800 | name string |
| 801 | buildDir string |
| 802 | src string |
| 803 | err string |
| 804 | }{ |
| 805 | { |
| 806 | name: "normal", |
| 807 | buildDir: "out", |
| 808 | src: "a/b/c", |
| 809 | }, |
| 810 | { |
| 811 | name: "abs", |
| 812 | buildDir: "out", |
| 813 | src: "/a/b/c", |
| 814 | err: "is outside directory", |
| 815 | }, |
| 816 | { |
| 817 | name: "in out dir", |
| 818 | buildDir: "out", |
| 819 | src: "out/a/b/c", |
| 820 | err: "is in output", |
| 821 | }, |
| 822 | } |
| 823 | |
| 824 | funcs := []struct { |
| 825 | name string |
| 826 | f func(ctx PathContext, pathComponents ...string) (SourcePath, error) |
| 827 | }{ |
| 828 | {"pathForSource", pathForSource}, |
| 829 | {"safePathForSource", safePathForSource}, |
| 830 | } |
| 831 | |
| 832 | for _, f := range funcs { |
| 833 | t.Run(f.name, func(t *testing.T) { |
| 834 | for _, test := range testCases { |
| 835 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 836 | testConfig := pathTestConfig(test.buildDir) |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 837 | ctx := &configErrorWrapper{config: testConfig} |
| 838 | _, err := f.f(ctx, test.src) |
| 839 | if len(ctx.errors) > 0 { |
| 840 | t.Fatalf("unexpected errors %v", ctx.errors) |
| 841 | } |
| 842 | if err != nil { |
| 843 | if test.err == "" { |
| 844 | t.Fatalf("unexpected error %q", err.Error()) |
| 845 | } else if !strings.Contains(err.Error(), test.err) { |
| 846 | t.Fatalf("incorrect error, want substring %q got %q", test.err, err.Error()) |
| 847 | } |
| 848 | } else { |
| 849 | if test.err != "" { |
| 850 | t.Fatalf("missing error %q", test.err) |
| 851 | } |
| 852 | } |
| 853 | }) |
| 854 | } |
| 855 | }) |
| 856 | } |
| 857 | } |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 858 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 859 | type pathForModuleSrcTestModule struct { |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 860 | ModuleBase |
| 861 | props struct { |
| 862 | Srcs []string `android:"path"` |
| 863 | Exclude_srcs []string `android:"path"` |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 864 | |
| 865 | Src *string `android:"path"` |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 866 | |
| 867 | Module_handles_missing_deps bool |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 870 | src string |
| 871 | rel string |
| 872 | |
| 873 | srcs []string |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 874 | rels []string |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 875 | |
| 876 | missingDeps []string |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 877 | } |
| 878 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 879 | func pathForModuleSrcTestModuleFactory() Module { |
| 880 | module := &pathForModuleSrcTestModule{} |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 881 | module.AddProperties(&module.props) |
| 882 | InitAndroidModule(module) |
| 883 | return module |
| 884 | } |
| 885 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 886 | func (p *pathForModuleSrcTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 887 | var srcs Paths |
| 888 | if p.props.Module_handles_missing_deps { |
| 889 | srcs, p.missingDeps = PathsAndMissingDepsForModuleSrcExcludes(ctx, p.props.Srcs, p.props.Exclude_srcs) |
| 890 | } else { |
| 891 | srcs = PathsForModuleSrcExcludes(ctx, p.props.Srcs, p.props.Exclude_srcs) |
| 892 | } |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 893 | p.srcs = srcs.Strings() |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 894 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 895 | for _, src := range srcs { |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 896 | p.rels = append(p.rels, src.Rel()) |
| 897 | } |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 898 | |
| 899 | if p.props.Src != nil { |
| 900 | src := PathForModuleSrc(ctx, *p.props.Src) |
| 901 | if src != nil { |
| 902 | p.src = src.String() |
| 903 | p.rel = src.Rel() |
| 904 | } |
| 905 | } |
| 906 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 907 | if !p.props.Module_handles_missing_deps { |
| 908 | p.missingDeps = ctx.GetMissingDependencies() |
| 909 | } |
Colin Cross | 6c4f21f | 2019-06-06 15:41:36 -0700 | [diff] [blame] | 910 | |
| 911 | ctx.Build(pctx, BuildParams{ |
| 912 | Rule: Touch, |
| 913 | Output: PathForModuleOut(ctx, "output"), |
| 914 | }) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 915 | } |
| 916 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 917 | type pathForModuleSrcOutputFileProviderModule struct { |
| 918 | ModuleBase |
| 919 | props struct { |
| 920 | Outs []string |
| 921 | Tagged []string |
| 922 | } |
| 923 | |
| 924 | outs Paths |
| 925 | tagged Paths |
| 926 | } |
| 927 | |
| 928 | func pathForModuleSrcOutputFileProviderModuleFactory() Module { |
| 929 | module := &pathForModuleSrcOutputFileProviderModule{} |
| 930 | module.AddProperties(&module.props) |
| 931 | InitAndroidModule(module) |
| 932 | return module |
| 933 | } |
| 934 | |
| 935 | func (p *pathForModuleSrcOutputFileProviderModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 936 | for _, out := range p.props.Outs { |
| 937 | p.outs = append(p.outs, PathForModuleOut(ctx, out)) |
| 938 | } |
| 939 | |
| 940 | for _, tagged := range p.props.Tagged { |
| 941 | p.tagged = append(p.tagged, PathForModuleOut(ctx, tagged)) |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | func (p *pathForModuleSrcOutputFileProviderModule) OutputFiles(tag string) (Paths, error) { |
| 946 | switch tag { |
| 947 | case "": |
| 948 | return p.outs, nil |
| 949 | case ".tagged": |
| 950 | return p.tagged, nil |
| 951 | default: |
| 952 | return nil, fmt.Errorf("unsupported tag %q", tag) |
| 953 | } |
| 954 | } |
| 955 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 956 | type pathForModuleSrcTestCase struct { |
| 957 | name string |
| 958 | bp string |
| 959 | srcs []string |
| 960 | rels []string |
| 961 | src string |
| 962 | rel string |
| 963 | } |
| 964 | |
| 965 | func testPathForModuleSrc(t *testing.T, buildDir string, tests []pathForModuleSrcTestCase) { |
| 966 | for _, test := range tests { |
| 967 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 968 | ctx := NewTestContext() |
| 969 | |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 970 | ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory) |
| 971 | ctx.RegisterModuleType("output_file_provider", pathForModuleSrcOutputFileProviderModuleFactory) |
| 972 | ctx.RegisterModuleType("filegroup", FileGroupFactory) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 973 | |
| 974 | fgBp := ` |
| 975 | filegroup { |
| 976 | name: "a", |
| 977 | srcs: ["src/a"], |
| 978 | } |
| 979 | ` |
| 980 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 981 | ofpBp := ` |
| 982 | output_file_provider { |
| 983 | name: "b", |
| 984 | outs: ["gen/b"], |
| 985 | tagged: ["gen/c"], |
| 986 | } |
| 987 | ` |
| 988 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 989 | mockFS := map[string][]byte{ |
| 990 | "fg/Android.bp": []byte(fgBp), |
| 991 | "foo/Android.bp": []byte(test.bp), |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 992 | "ofp/Android.bp": []byte(ofpBp), |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 993 | "fg/src/a": nil, |
| 994 | "foo/src/b": nil, |
| 995 | "foo/src/c": nil, |
| 996 | "foo/src/d": nil, |
| 997 | "foo/src/e/e": nil, |
| 998 | "foo/src_special/$": nil, |
| 999 | } |
| 1000 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1001 | config := TestConfig(buildDir, nil, "", mockFS) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1002 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1003 | ctx.Register(config) |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 1004 | _, errs := ctx.ParseFileList(".", []string{"fg/Android.bp", "foo/Android.bp", "ofp/Android.bp"}) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1005 | FailIfErrored(t, errs) |
| 1006 | _, errs = ctx.PrepareBuildActions(config) |
| 1007 | FailIfErrored(t, errs) |
| 1008 | |
| 1009 | m := ctx.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule) |
| 1010 | |
| 1011 | if g, w := m.srcs, test.srcs; !reflect.DeepEqual(g, w) { |
| 1012 | t.Errorf("want srcs %q, got %q", w, g) |
| 1013 | } |
| 1014 | |
| 1015 | if g, w := m.rels, test.rels; !reflect.DeepEqual(g, w) { |
| 1016 | t.Errorf("want rels %q, got %q", w, g) |
| 1017 | } |
| 1018 | |
| 1019 | if g, w := m.src, test.src; g != w { |
| 1020 | t.Errorf("want src %q, got %q", w, g) |
| 1021 | } |
| 1022 | |
| 1023 | if g, w := m.rel, test.rel; g != w { |
| 1024 | t.Errorf("want rel %q, got %q", w, g) |
| 1025 | } |
| 1026 | }) |
| 1027 | } |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1030 | func TestPathsForModuleSrc(t *testing.T) { |
| 1031 | tests := []pathForModuleSrcTestCase{ |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 1032 | { |
| 1033 | name: "path", |
| 1034 | bp: ` |
| 1035 | test { |
| 1036 | name: "foo", |
| 1037 | srcs: ["src/b"], |
| 1038 | }`, |
| 1039 | srcs: []string{"foo/src/b"}, |
| 1040 | rels: []string{"src/b"}, |
| 1041 | }, |
| 1042 | { |
| 1043 | name: "glob", |
| 1044 | bp: ` |
| 1045 | test { |
| 1046 | name: "foo", |
| 1047 | srcs: [ |
| 1048 | "src/*", |
| 1049 | "src/e/*", |
| 1050 | ], |
| 1051 | }`, |
| 1052 | srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"}, |
| 1053 | rels: []string{"src/b", "src/c", "src/d", "src/e/e"}, |
| 1054 | }, |
| 1055 | { |
| 1056 | name: "recursive glob", |
| 1057 | bp: ` |
| 1058 | test { |
| 1059 | name: "foo", |
| 1060 | srcs: ["src/**/*"], |
| 1061 | }`, |
| 1062 | srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"}, |
| 1063 | rels: []string{"src/b", "src/c", "src/d", "src/e/e"}, |
| 1064 | }, |
| 1065 | { |
| 1066 | name: "filegroup", |
| 1067 | bp: ` |
| 1068 | test { |
| 1069 | name: "foo", |
| 1070 | srcs: [":a"], |
| 1071 | }`, |
| 1072 | srcs: []string{"fg/src/a"}, |
| 1073 | rels: []string{"src/a"}, |
| 1074 | }, |
| 1075 | { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 1076 | name: "output file provider", |
| 1077 | bp: ` |
| 1078 | test { |
| 1079 | name: "foo", |
| 1080 | srcs: [":b"], |
| 1081 | }`, |
| 1082 | srcs: []string{buildDir + "/.intermediates/ofp/b/gen/b"}, |
| 1083 | rels: []string{"gen/b"}, |
| 1084 | }, |
| 1085 | { |
| 1086 | name: "output file provider tagged", |
| 1087 | bp: ` |
| 1088 | test { |
| 1089 | name: "foo", |
| 1090 | srcs: [":b{.tagged}"], |
| 1091 | }`, |
| 1092 | srcs: []string{buildDir + "/.intermediates/ofp/b/gen/c"}, |
| 1093 | rels: []string{"gen/c"}, |
| 1094 | }, |
| 1095 | { |
Jooyung Han | 7607dd3 | 2020-07-05 10:23:14 +0900 | [diff] [blame] | 1096 | name: "output file provider with exclude", |
| 1097 | bp: ` |
| 1098 | test { |
| 1099 | name: "foo", |
| 1100 | srcs: [":b", ":c"], |
| 1101 | exclude_srcs: [":c"] |
| 1102 | } |
| 1103 | output_file_provider { |
| 1104 | name: "c", |
| 1105 | outs: ["gen/c"], |
| 1106 | }`, |
| 1107 | srcs: []string{buildDir + "/.intermediates/ofp/b/gen/b"}, |
| 1108 | rels: []string{"gen/b"}, |
| 1109 | }, |
| 1110 | { |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 1111 | name: "special characters glob", |
| 1112 | bp: ` |
| 1113 | test { |
| 1114 | name: "foo", |
| 1115 | srcs: ["src_special/*"], |
| 1116 | }`, |
| 1117 | srcs: []string{"foo/src_special/$"}, |
| 1118 | rels: []string{"src_special/$"}, |
| 1119 | }, |
| 1120 | } |
| 1121 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 1122 | testPathForModuleSrc(t, buildDir, tests) |
| 1123 | } |
| 1124 | |
| 1125 | func TestPathForModuleSrc(t *testing.T) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1126 | tests := []pathForModuleSrcTestCase{ |
| 1127 | { |
| 1128 | name: "path", |
| 1129 | bp: ` |
| 1130 | test { |
| 1131 | name: "foo", |
| 1132 | src: "src/b", |
| 1133 | }`, |
| 1134 | src: "foo/src/b", |
| 1135 | rel: "src/b", |
| 1136 | }, |
| 1137 | { |
| 1138 | name: "glob", |
| 1139 | bp: ` |
| 1140 | test { |
| 1141 | name: "foo", |
| 1142 | src: "src/e/*", |
| 1143 | }`, |
| 1144 | src: "foo/src/e/e", |
| 1145 | rel: "src/e/e", |
| 1146 | }, |
| 1147 | { |
| 1148 | name: "filegroup", |
| 1149 | bp: ` |
| 1150 | test { |
| 1151 | name: "foo", |
| 1152 | src: ":a", |
| 1153 | }`, |
| 1154 | src: "fg/src/a", |
| 1155 | rel: "src/a", |
| 1156 | }, |
| 1157 | { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 1158 | name: "output file provider", |
| 1159 | bp: ` |
| 1160 | test { |
| 1161 | name: "foo", |
| 1162 | src: ":b", |
| 1163 | }`, |
| 1164 | src: buildDir + "/.intermediates/ofp/b/gen/b", |
| 1165 | rel: "gen/b", |
| 1166 | }, |
| 1167 | { |
| 1168 | name: "output file provider tagged", |
| 1169 | bp: ` |
| 1170 | test { |
| 1171 | name: "foo", |
| 1172 | src: ":b{.tagged}", |
| 1173 | }`, |
| 1174 | src: buildDir + "/.intermediates/ofp/b/gen/c", |
| 1175 | rel: "gen/c", |
| 1176 | }, |
| 1177 | { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1178 | name: "special characters glob", |
| 1179 | bp: ` |
| 1180 | test { |
| 1181 | name: "foo", |
| 1182 | src: "src_special/*", |
| 1183 | }`, |
| 1184 | src: "foo/src_special/$", |
| 1185 | rel: "src_special/$", |
| 1186 | }, |
| 1187 | } |
| 1188 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1189 | testPathForModuleSrc(t, buildDir, tests) |
| 1190 | } |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 1191 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1192 | func TestPathsForModuleSrc_AllowMissingDependencies(t *testing.T) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1193 | bp := ` |
| 1194 | test { |
| 1195 | name: "foo", |
| 1196 | srcs: [":a"], |
| 1197 | exclude_srcs: [":b"], |
| 1198 | src: ":c", |
| 1199 | } |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 1200 | |
| 1201 | test { |
| 1202 | name: "bar", |
| 1203 | srcs: [":d"], |
| 1204 | exclude_srcs: [":e"], |
| 1205 | module_handles_missing_deps: true, |
| 1206 | } |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1207 | ` |
| 1208 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1209 | config := TestConfig(buildDir, nil, bp, nil) |
| 1210 | config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1211 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1212 | ctx := NewTestContext() |
| 1213 | ctx.SetAllowMissingDependencies(true) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1214 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1215 | ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory) |
| 1216 | |
| 1217 | ctx.Register(config) |
| 1218 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1219 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 1220 | FailIfErrored(t, errs) |
| 1221 | _, errs = ctx.PrepareBuildActions(config) |
| 1222 | FailIfErrored(t, errs) |
| 1223 | |
| 1224 | foo := ctx.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule) |
| 1225 | |
| 1226 | if g, w := foo.missingDeps, []string{"a", "b", "c"}; !reflect.DeepEqual(g, w) { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 1227 | t.Errorf("want foo missing deps %q, got %q", w, g) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | if g, w := foo.srcs, []string{}; !reflect.DeepEqual(g, w) { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 1231 | t.Errorf("want foo srcs %q, got %q", w, g) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | if g, w := foo.src, ""; g != w { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 1235 | t.Errorf("want foo src %q, got %q", w, g) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1236 | } |
| 1237 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 1238 | bar := ctx.ModuleForTests("bar", "").Module().(*pathForModuleSrcTestModule) |
| 1239 | |
| 1240 | if g, w := bar.missingDeps, []string{"d", "e"}; !reflect.DeepEqual(g, w) { |
| 1241 | t.Errorf("want bar missing deps %q, got %q", w, g) |
| 1242 | } |
| 1243 | |
| 1244 | if g, w := bar.srcs, []string{}; !reflect.DeepEqual(g, w) { |
| 1245 | t.Errorf("want bar srcs %q, got %q", w, g) |
| 1246 | } |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1249 | func ExampleOutputPath_ReplaceExtension() { |
| 1250 | ctx := &configErrorWrapper{ |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1251 | config: TestConfig("out", nil, "", nil), |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1252 | } |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1253 | p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art") |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1254 | p2 := p.ReplaceExtension(ctx, "oat") |
| 1255 | fmt.Println(p, p2) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1256 | fmt.Println(p.Rel(), p2.Rel()) |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1257 | |
| 1258 | // Output: |
| 1259 | // out/system/framework/boot.art out/system/framework/boot.oat |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1260 | // boot.art boot.oat |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1261 | } |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1262 | |
| 1263 | func ExampleOutputPath_FileInSameDir() { |
| 1264 | ctx := &configErrorWrapper{ |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1265 | config: TestConfig("out", nil, "", nil), |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1266 | } |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1267 | p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art") |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1268 | p2 := p.InSameDir(ctx, "oat", "arm", "boot.vdex") |
| 1269 | fmt.Println(p, p2) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1270 | fmt.Println(p.Rel(), p2.Rel()) |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1271 | |
| 1272 | // Output: |
| 1273 | // out/system/framework/boot.art out/system/framework/oat/arm/boot.vdex |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1274 | // boot.art oat/arm/boot.vdex |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1275 | } |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 1276 | |
| 1277 | func BenchmarkFirstUniquePaths(b *testing.B) { |
| 1278 | implementations := []struct { |
| 1279 | name string |
| 1280 | f func(Paths) Paths |
| 1281 | }{ |
| 1282 | { |
| 1283 | name: "list", |
| 1284 | f: firstUniquePathsList, |
| 1285 | }, |
| 1286 | { |
| 1287 | name: "map", |
| 1288 | f: firstUniquePathsMap, |
| 1289 | }, |
| 1290 | } |
| 1291 | const maxSize = 1024 |
| 1292 | uniquePaths := make(Paths, maxSize) |
| 1293 | for i := range uniquePaths { |
| 1294 | uniquePaths[i] = PathForTesting(strconv.Itoa(i)) |
| 1295 | } |
| 1296 | samePath := make(Paths, maxSize) |
| 1297 | for i := range samePath { |
| 1298 | samePath[i] = uniquePaths[0] |
| 1299 | } |
| 1300 | |
| 1301 | f := func(b *testing.B, imp func(Paths) Paths, paths Paths) { |
| 1302 | for i := 0; i < b.N; i++ { |
| 1303 | b.ReportAllocs() |
| 1304 | paths = append(Paths(nil), paths...) |
| 1305 | imp(paths) |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | for n := 1; n <= maxSize; n <<= 1 { |
| 1310 | b.Run(strconv.Itoa(n), func(b *testing.B) { |
| 1311 | for _, implementation := range implementations { |
| 1312 | b.Run(implementation.name, func(b *testing.B) { |
| 1313 | b.Run("same", func(b *testing.B) { |
| 1314 | f(b, implementation.f, samePath[:n]) |
| 1315 | }) |
| 1316 | b.Run("unique", func(b *testing.B) { |
| 1317 | f(b, implementation.f, uniquePaths[:n]) |
| 1318 | }) |
| 1319 | }) |
| 1320 | } |
| 1321 | }) |
| 1322 | } |
| 1323 | } |