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