Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 1 | // Copyright 2019 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 | |
| 15 | package sdk |
| 16 | |
| 17 | import ( |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 19 | "io/ioutil" |
| 20 | "os" |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 21 | "path/filepath" |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 22 | "reflect" |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 23 | "strings" |
| 24 | "testing" |
| 25 | |
| 26 | "android/soong/android" |
| 27 | "android/soong/apex" |
| 28 | "android/soong/cc" |
| 29 | "android/soong/java" |
| 30 | ) |
| 31 | |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame^] | 32 | func testSdkContext(bp string, fs map[string][]byte, extraOsTypes []android.OsType) (*android.TestContext, android.Config) { |
| 33 | extraOsTypes = append(extraOsTypes, android.Android, android.Windows) |
| 34 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 35 | bp = bp + ` |
| 36 | apex_key { |
| 37 | name: "myapex.key", |
| 38 | public_key: "myapex.avbpubkey", |
| 39 | private_key: "myapex.pem", |
| 40 | } |
| 41 | |
| 42 | android_app_certificate { |
| 43 | name: "myapex.cert", |
| 44 | certificate: "myapex", |
| 45 | } |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame^] | 46 | ` + cc.GatherRequiredDepsForTest(extraOsTypes...) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 47 | |
| 48 | mockFS := map[string][]byte{ |
Martin Stjernholm | cc77601 | 2020-07-07 03:22:21 +0100 | [diff] [blame] | 49 | "build/make/target/product/security": nil, |
| 50 | "apex_manifest.json": nil, |
| 51 | "system/sepolicy/apex/myapex-file_contexts": nil, |
| 52 | "system/sepolicy/apex/myapex2-file_contexts": nil, |
| 53 | "system/sepolicy/apex/mysdkapex-file_contexts": nil, |
| 54 | "myapex.avbpubkey": nil, |
| 55 | "myapex.pem": nil, |
| 56 | "myapex.x509.pem": nil, |
| 57 | "myapex.pk8": nil, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Colin Cross | f28329d | 2020-02-15 11:00:10 -0800 | [diff] [blame] | 60 | cc.GatherRequiredFilesForTest(mockFS) |
| 61 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 62 | for k, v := range fs { |
| 63 | mockFS[k] = v |
| 64 | } |
| 65 | |
| 66 | config := android.TestArchConfig(buildDir, nil, bp, mockFS) |
| 67 | |
Paul Duffin | 08798aa | 2020-02-27 13:12:46 +0000 | [diff] [blame] | 68 | // Add windows as a default disable OS to test behavior when some OS variants |
| 69 | // are disabled. |
| 70 | config.Targets[android.Windows] = []android.Target{ |
| 71 | {android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", ""}, |
| 72 | } |
| 73 | |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame^] | 74 | for _, extraOsType := range extraOsTypes { |
| 75 | switch extraOsType { |
| 76 | case android.LinuxBionic: |
| 77 | config.Targets[android.LinuxBionic] = []android.Target{ |
| 78 | {android.LinuxBionic, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", ""}, |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 83 | ctx := android.NewTestArchContext() |
| 84 | |
Paul Duffin | 8c3fec4 | 2020-03-04 20:15:08 +0000 | [diff] [blame] | 85 | // Enable androidmk support. |
| 86 | // * Register the singleton |
| 87 | // * Configure that we are inside make |
| 88 | // * Add CommonOS to ensure that androidmk processing works. |
| 89 | android.RegisterAndroidMkBuildComponents(ctx) |
| 90 | android.SetInMakeForTests(config) |
| 91 | config.Targets[android.CommonOS] = []android.Target{ |
| 92 | {android.CommonOS, android.Arch{ArchType: android.Common}, android.NativeBridgeDisabled, "", ""}, |
| 93 | } |
| 94 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 95 | // from android package |
Paul Duffin | c132742 | 2020-01-14 12:15:29 +0000 | [diff] [blame] | 96 | android.RegisterPackageBuildComponents(ctx) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 97 | ctx.PreArchMutators(android.RegisterVisibilityRuleChecker) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 98 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 99 | ctx.PreArchMutators(android.RegisterComponentsMutator) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 100 | ctx.PreArchMutators(android.RegisterVisibilityRuleGatherer) |
| 101 | ctx.PostDepsMutators(android.RegisterVisibilityRuleEnforcer) |
| 102 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 103 | // from java package |
Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 104 | java.RegisterJavaBuildComponents(ctx) |
| 105 | java.RegisterAppBuildComponents(ctx) |
Paul Duffin | dd46f71 | 2020-02-10 13:37:10 +0000 | [diff] [blame] | 106 | java.RegisterSdkLibraryBuildComponents(ctx) |
Paul Duffin | 884363e | 2019-12-19 10:21:09 +0000 | [diff] [blame] | 107 | java.RegisterStubsBuildComponents(ctx) |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 108 | java.RegisterSystemModulesBuildComponents(ctx) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 109 | |
| 110 | // from cc package |
Paul Duffin | 77980a8 | 2019-12-19 16:01:36 +0000 | [diff] [blame] | 111 | cc.RegisterRequiredBuildComponentsForTest(ctx) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 112 | |
| 113 | // from apex package |
| 114 | ctx.RegisterModuleType("apex", apex.BundleFactory) |
| 115 | ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory) |
| 116 | ctx.PostDepsMutators(apex.RegisterPostDepsMutators) |
| 117 | |
| 118 | // from this package |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 119 | ctx.RegisterModuleType("sdk", SdkModuleFactory) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 120 | ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 121 | ctx.RegisterModuleType("module_exports", ModuleExportsFactory) |
| 122 | ctx.RegisterModuleType("module_exports_snapshot", ModuleExportsSnapshotsFactory) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 123 | ctx.PreDepsMutators(RegisterPreDepsMutators) |
| 124 | ctx.PostDepsMutators(RegisterPostDepsMutators) |
| 125 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 126 | ctx.Register(config) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 127 | |
| 128 | return ctx, config |
| 129 | } |
| 130 | |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame^] | 131 | func runTests(t *testing.T, ctx *android.TestContext, config android.Config) *testSdkResult { |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 132 | t.Helper() |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 133 | _, errs := ctx.ParseBlueprintsFiles(".") |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 134 | android.FailIfErrored(t, errs) |
| 135 | _, errs = ctx.PrepareBuildActions(config) |
| 136 | android.FailIfErrored(t, errs) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 137 | return &testSdkResult{ |
| 138 | TestHelper: TestHelper{t: t}, |
| 139 | ctx: ctx, |
| 140 | config: config, |
| 141 | } |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame^] | 144 | func testSdkWithFs(t *testing.T, bp string, fs map[string][]byte) *testSdkResult { |
| 145 | t.Helper() |
| 146 | ctx, config := testSdkContext(bp, fs, nil) |
| 147 | return runTests(t, ctx, config) |
| 148 | } |
| 149 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 150 | func testSdkError(t *testing.T, pattern, bp string) { |
| 151 | t.Helper() |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame^] | 152 | ctx, config := testSdkContext(bp, nil, nil) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 153 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 154 | if len(errs) > 0 { |
| 155 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 156 | return |
| 157 | } |
| 158 | _, errs = ctx.PrepareBuildActions(config) |
| 159 | if len(errs) > 0 { |
| 160 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
| 165 | } |
| 166 | |
| 167 | func ensureListContains(t *testing.T, result []string, expected string) { |
| 168 | t.Helper() |
| 169 | if !android.InList(expected, result) { |
| 170 | t.Errorf("%q is not found in %v", expected, result) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func pathsToStrings(paths android.Paths) []string { |
| 175 | var ret []string |
| 176 | for _, p := range paths { |
| 177 | ret = append(ret, p.String()) |
| 178 | } |
| 179 | return ret |
| 180 | } |
| 181 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 182 | // Provides general test support. |
| 183 | type TestHelper struct { |
| 184 | t *testing.T |
| 185 | } |
| 186 | |
| 187 | func (h *TestHelper) AssertStringEquals(message string, expected string, actual string) { |
| 188 | h.t.Helper() |
| 189 | if actual != expected { |
| 190 | h.t.Errorf("%s: expected %s, actual %s", message, expected, actual) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 194 | func (h *TestHelper) AssertErrorMessageEquals(message string, expected string, actual error) { |
| 195 | h.t.Helper() |
| 196 | if actual == nil { |
| 197 | h.t.Errorf("Expected error but was nil") |
| 198 | } else if actual.Error() != expected { |
| 199 | h.t.Errorf("%s: expected %s, actual %s", message, expected, actual.Error()) |
| 200 | } |
| 201 | } |
| 202 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 203 | func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) { |
| 204 | h.t.Helper() |
| 205 | h.AssertStringEquals(message, strings.TrimSpace(expected), strings.TrimSpace(actual)) |
| 206 | } |
| 207 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 208 | func (h *TestHelper) AssertDeepEquals(message string, expected interface{}, actual interface{}) { |
| 209 | h.t.Helper() |
| 210 | if !reflect.DeepEqual(actual, expected) { |
| 211 | h.t.Errorf("%s: expected %#v, actual %#v", message, expected, actual) |
| 212 | } |
| 213 | } |
| 214 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 215 | // Encapsulates result of processing an SDK definition. Provides support for |
| 216 | // checking the state of the build structures. |
| 217 | type testSdkResult struct { |
| 218 | TestHelper |
| 219 | ctx *android.TestContext |
| 220 | config android.Config |
| 221 | } |
| 222 | |
| 223 | // Analyse the sdk build rules to extract information about what it is doing. |
| 224 | |
| 225 | // e.g. find the src/dest pairs from each cp command, the various zip files |
| 226 | // generated, etc. |
| 227 | func (r *testSdkResult) getSdkSnapshotBuildInfo(sdk *sdk) *snapshotBuildInfo { |
Paul Duffin | 1110827 | 2020-05-11 22:59:25 +0100 | [diff] [blame] | 228 | androidBpContents := sdk.GetAndroidBpContentsForTests() |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 229 | |
| 230 | info := &snapshotBuildInfo{ |
| 231 | r: r, |
| 232 | androidBpContents: androidBpContents, |
| 233 | } |
| 234 | |
| 235 | buildParams := sdk.BuildParamsForTests() |
| 236 | copyRules := &strings.Builder{} |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 237 | otherCopyRules := &strings.Builder{} |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 238 | snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/" |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 239 | for _, bp := range buildParams { |
| 240 | switch bp.Rule.String() { |
| 241 | case android.Cp.String(): |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 242 | output := bp.Output |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 243 | // Get destination relative to the snapshot root |
| 244 | dest := output.Rel() |
| 245 | src := android.NormalizePathForTesting(bp.Input) |
| 246 | // We differentiate between copy rules for the snapshot, and copy rules for the install file. |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 247 | if strings.HasPrefix(output.String(), snapshotDirPrefix) { |
| 248 | // Get source relative to build directory. |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 249 | _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest) |
| 250 | info.snapshotContents = append(info.snapshotContents, dest) |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 251 | } else { |
| 252 | _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest) |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 253 | } |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 254 | |
| 255 | case repackageZip.String(): |
| 256 | // Add the destdir to the snapshot contents as that is effectively where |
| 257 | // the content of the repackaged zip is copied. |
| 258 | dest := bp.Args["destdir"] |
| 259 | info.snapshotContents = append(info.snapshotContents, dest) |
| 260 | |
| 261 | case zipFiles.String(): |
| 262 | // This could be an intermediate zip file and not the actual output zip. |
| 263 | // In that case this will be overridden when the rule to merge the zips |
| 264 | // is processed. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 265 | info.outputZip = android.NormalizePathForTesting(bp.Output) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 266 | |
| 267 | case mergeZips.String(): |
| 268 | // Copy the current outputZip to the intermediateZip. |
| 269 | info.intermediateZip = info.outputZip |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 270 | mergeInput := android.NormalizePathForTesting(bp.Input) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 271 | if info.intermediateZip != mergeInput { |
| 272 | r.t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead", |
| 273 | info.intermediateZip, mergeInput) |
| 274 | } |
| 275 | |
| 276 | // Override output zip (which was actually the intermediate zip file) with the actual |
| 277 | // output zip. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 278 | info.outputZip = android.NormalizePathForTesting(bp.Output) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 279 | |
| 280 | // Save the zips to be merged into the intermediate zip. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 281 | info.mergeZips = android.NormalizePathsForTesting(bp.Inputs) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | info.copyRules = copyRules.String() |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 286 | info.otherCopyRules = otherCopyRules.String() |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 287 | |
| 288 | return info |
| 289 | } |
| 290 | |
| 291 | func (r *testSdkResult) Module(name string, variant string) android.Module { |
| 292 | return r.ctx.ModuleForTests(name, variant).Module() |
| 293 | } |
| 294 | |
| 295 | func (r *testSdkResult) ModuleForTests(name string, variant string) android.TestingModule { |
| 296 | return r.ctx.ModuleForTests(name, variant) |
| 297 | } |
| 298 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 299 | // Check the snapshot build rules. |
| 300 | // |
| 301 | // Takes a list of functions which check different facets of the snapshot build rules. |
| 302 | // Allows each test to customize what is checked without duplicating lots of code |
| 303 | // or proliferating check methods of different flavors. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 304 | func (r *testSdkResult) CheckSnapshot(name string, dir string, checkers ...snapshotBuildInfoChecker) { |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 305 | r.t.Helper() |
| 306 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 307 | // The sdk CommonOS variant is always responsible for generating the snapshot. |
| 308 | variant := android.CommonOS.Name |
| 309 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 310 | sdk := r.Module(name, variant).(*sdk) |
| 311 | |
| 312 | snapshotBuildInfo := r.getSdkSnapshotBuildInfo(sdk) |
| 313 | |
| 314 | // Check state of the snapshot build. |
| 315 | for _, checker := range checkers { |
| 316 | checker(snapshotBuildInfo) |
| 317 | } |
| 318 | |
| 319 | // Make sure that the generated zip file is in the correct place. |
| 320 | actual := snapshotBuildInfo.outputZip |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 321 | if dir != "" { |
| 322 | dir = filepath.Clean(dir) + "/" |
| 323 | } |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 324 | r.AssertStringEquals("Snapshot zip file in wrong place", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 325 | fmt.Sprintf(".intermediates/%s%s/%s/%s-current.zip", dir, name, variant, name), actual) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 326 | |
| 327 | // Populate a mock filesystem with the files that would have been copied by |
| 328 | // the rules. |
| 329 | fs := make(map[string][]byte) |
| 330 | for _, dest := range snapshotBuildInfo.snapshotContents { |
| 331 | fs[dest] = nil |
| 332 | } |
| 333 | |
| 334 | // Process the generated bp file to make sure it is valid. |
| 335 | testSdkWithFs(r.t, snapshotBuildInfo.androidBpContents, fs) |
| 336 | } |
| 337 | |
| 338 | type snapshotBuildInfoChecker func(info *snapshotBuildInfo) |
| 339 | |
| 340 | // Check that the snapshot's generated Android.bp is correct. |
| 341 | // |
| 342 | // Both the expected and actual string are both trimmed before comparing. |
| 343 | func checkAndroidBpContents(expected string) snapshotBuildInfoChecker { |
| 344 | return func(info *snapshotBuildInfo) { |
| 345 | info.r.t.Helper() |
| 346 | info.r.AssertTrimmedStringEquals("Android.bp contents do not match", expected, info.androidBpContents) |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // Check that the snapshot's copy rules are correct. |
| 351 | // |
| 352 | // The copy rules are formatted as <src> -> <dest>, one per line and then compared |
| 353 | // to the supplied expected string. Both the expected and actual string are trimmed |
| 354 | // before comparing. |
| 355 | func checkAllCopyRules(expected string) snapshotBuildInfoChecker { |
| 356 | return func(info *snapshotBuildInfo) { |
| 357 | info.r.t.Helper() |
| 358 | info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.copyRules) |
| 359 | } |
| 360 | } |
| 361 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 362 | func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker { |
| 363 | return func(info *snapshotBuildInfo) { |
| 364 | info.r.t.Helper() |
| 365 | info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.otherCopyRules) |
| 366 | } |
| 367 | } |
| 368 | |
Paul Duffin | 3d1248c | 2020-04-09 00:10:17 +0100 | [diff] [blame] | 369 | // Check that the specified paths match the list of zips to merge with the intermediate zip. |
| 370 | func checkMergeZips(expected ...string) snapshotBuildInfoChecker { |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 371 | return func(info *snapshotBuildInfo) { |
| 372 | info.r.t.Helper() |
| 373 | if info.intermediateZip == "" { |
| 374 | info.r.t.Errorf("No intermediate zip file was created") |
| 375 | } |
Paul Duffin | 3d1248c | 2020-04-09 00:10:17 +0100 | [diff] [blame] | 376 | |
| 377 | info.r.AssertDeepEquals("mismatching merge zip files", expected, info.mergeZips) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | // Encapsulates information about the snapshot build structure in order to insulate tests from |
| 382 | // knowing too much about internal structures. |
| 383 | // |
| 384 | // All source/input paths are relative either the build directory. All dest/output paths are |
| 385 | // relative to the snapshot root directory. |
| 386 | type snapshotBuildInfo struct { |
| 387 | r *testSdkResult |
| 388 | |
| 389 | // The contents of the generated Android.bp file |
| 390 | androidBpContents string |
| 391 | |
| 392 | // The paths, relative to the snapshot root, of all files and directories copied into the |
| 393 | // snapshot. |
| 394 | snapshotContents []string |
| 395 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 396 | // A formatted representation of the src/dest pairs for a snapshot, one pair per line, |
| 397 | // of the format src -> dest |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 398 | copyRules string |
| 399 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 400 | // A formatted representation of the src/dest pairs for files not in a snapshot, one pair |
| 401 | // per line, of the format src -> dest |
| 402 | otherCopyRules string |
| 403 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 404 | // The path to the intermediate zip, which is a zip created from the source files copied |
| 405 | // into the snapshot directory and which will be merged with other zips to form the final output. |
| 406 | // Is am empty string if there is no intermediate zip because there are no zips to merge in. |
| 407 | intermediateZip string |
| 408 | |
| 409 | // The paths to the zips to merge into the output zip, does not include the intermediate |
| 410 | // zip. |
| 411 | mergeZips []string |
| 412 | |
| 413 | // The final output zip. |
| 414 | outputZip string |
| 415 | } |
| 416 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 417 | var buildDir string |
| 418 | |
| 419 | func setUp() { |
| 420 | var err error |
| 421 | buildDir, err = ioutil.TempDir("", "soong_sdk_test") |
| 422 | if err != nil { |
| 423 | panic(err) |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | func tearDown() { |
| 428 | _ = os.RemoveAll(buildDir) |
| 429 | } |
| 430 | |
| 431 | func runTestWithBuildDir(m *testing.M) { |
| 432 | run := func() int { |
| 433 | setUp() |
| 434 | defer tearDown() |
| 435 | |
| 436 | return m.Run() |
| 437 | } |
| 438 | |
| 439 | os.Exit(run()) |
| 440 | } |