blob: 44970f7561c6026f2aaec6dba4bbed8625caf1b8 [file] [log] [blame]
Paul Duffin82d90432019-11-30 09:24:33 +00001// 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
15package sdk
16
17import (
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000018 "fmt"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000019 "path/filepath"
Paul Duffin82d90432019-11-30 09:24:33 +000020 "strings"
21 "testing"
22
23 "android/soong/android"
24 "android/soong/apex"
25 "android/soong/cc"
Paul Duffind6ceb862021-03-04 23:02:31 +000026 "android/soong/genrule"
Paul Duffin82d90432019-11-30 09:24:33 +000027 "android/soong/java"
28)
29
Paul Duffincf3ee2f2021-03-15 13:08:51 +000030// Prepare for running an sdk test with an apex.
31var prepareForSdkTestWithApex = android.GroupFixturePreparers(
Paul Duffin4a2a29c2021-03-09 22:27:13 +000032 apex.PrepareForTestWithApexBuildComponents,
Paul Duffin4a2a29c2021-03-09 22:27:13 +000033 android.FixtureAddTextFile("sdk/tests/Android.bp", `
Colin Cross98be1bb2019-12-13 20:41:13 -080034 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 Duffin4a2a29c2021-03-09 22:27:13 +000044 `),
Colin Cross98be1bb2019-12-13 20:41:13 -080045
Paul Duffin4a2a29c2021-03-09 22:27:13 +000046 android.FixtureMergeMockFs(map[string][]byte{
Martin Stjernholmcc776012020-07-07 03:22:21 +010047 "apex_manifest.json": nil,
48 "system/sepolicy/apex/myapex-file_contexts": nil,
49 "system/sepolicy/apex/myapex2-file_contexts": nil,
50 "system/sepolicy/apex/mysdkapex-file_contexts": nil,
Paul Duffindb462dd2021-03-21 22:01:55 +000051 "sdk/tests/myapex.avbpubkey": nil,
52 "sdk/tests/myapex.pem": nil,
53 "sdk/tests/myapex.x509.pem": nil,
54 "sdk/tests/myapex.pk8": nil,
Paul Duffin4a2a29c2021-03-09 22:27:13 +000055 }),
Paul Duffincf3ee2f2021-03-15 13:08:51 +000056)
57
58// Legacy preparer used for running tests within the sdk package.
59//
60// This includes everything that was needed to run any test in the sdk package prior to the
61// introduction of the test fixtures. Tests that are being converted to use fixtures directly
62// rather than through the testSdkError() and testSdkWithFs() methods should avoid using this and
63// instead should use the various preparers directly using android.GroupFixturePreparers(...) to
64// group them when necessary.
65//
66// deprecated
67var prepareForSdkTest = android.GroupFixturePreparers(
68 cc.PrepareForTestWithCcDefaultModules,
69 genrule.PrepareForTestWithGenRuleBuildComponents,
70 java.PrepareForTestWithJavaBuildComponents,
71 PrepareForTestWithSdkBuildComponents,
72
73 prepareForSdkTestWithApex,
Colin Cross98be1bb2019-12-13 20:41:13 -080074
Paul Duffin4a2a29c2021-03-09 22:27:13 +000075 cc.PrepareForTestOnWindows,
76 android.FixtureModifyConfig(func(config android.Config) {
77 // Add windows as a default disable OS to test behavior when some OS variants
78 // are disabled.
79 config.Targets[android.Windows] = []android.Target{
80 {android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", true},
Martin Stjernholm7feceb22020-07-11 04:33:29 +010081 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000082 }),
Paul Duffindb462dd2021-03-21 22:01:55 +000083
84 // Make sure that every test provides all the source files.
85 android.PrepareForTestDisallowNonExistentPaths,
86 android.MockFS{
87 "Test.java": nil,
88 }.AddToFixture(),
Paul Duffin4a2a29c2021-03-09 22:27:13 +000089)
Martin Stjernholm7feceb22020-07-11 04:33:29 +010090
Paul Duffin4a2a29c2021-03-09 22:27:13 +000091var PrepareForTestWithSdkBuildComponents = android.GroupFixturePreparers(
92 android.FixtureRegisterWithContext(registerModuleExportsBuildComponents),
93 android.FixtureRegisterWithContext(registerSdkBuildComponents),
94)
Paul Duffin82d90432019-11-30 09:24:33 +000095
Paul Duffin4a2a29c2021-03-09 22:27:13 +000096func testSdkWithFs(t *testing.T, bp string, fs android.MockFS) *android.TestResult {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000097 t.Helper()
Paul Duffin89648f92021-03-20 00:36:55 +000098 return prepareForSdkTest.RunTest(t, fs.AddToFixture(), android.FixtureWithRootAndroidBp(bp))
Martin Stjernholm7feceb22020-07-11 04:33:29 +010099}
100
Paul Duffin82d90432019-11-30 09:24:33 +0000101func testSdkError(t *testing.T, pattern, bp string) {
102 t.Helper()
Paul Duffin89648f92021-03-20 00:36:55 +0000103 prepareForSdkTest.
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000104 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
105 RunTestWithBp(t, bp)
Paul Duffin82d90432019-11-30 09:24:33 +0000106}
107
108func ensureListContains(t *testing.T, result []string, expected string) {
109 t.Helper()
110 if !android.InList(expected, result) {
111 t.Errorf("%q is not found in %v", expected, result)
112 }
113}
114
115func pathsToStrings(paths android.Paths) []string {
116 var ret []string
117 for _, p := range paths {
118 ret = append(ret, p.String())
119 }
120 return ret
121}
122
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000123// Analyse the sdk build rules to extract information about what it is doing.
Paul Duffinfe9a9e32021-03-11 17:41:01 +0000124//
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000125// e.g. find the src/dest pairs from each cp command, the various zip files
126// generated, etc.
Paul Duffin36474d32021-03-12 12:19:43 +0000127func getSdkSnapshotBuildInfo(t *testing.T, result *android.TestResult, sdk *sdk) *snapshotBuildInfo {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000128 info := &snapshotBuildInfo{
Paul Duffin36474d32021-03-12 12:19:43 +0000129 t: t,
Paul Duffin981b94b2021-03-11 12:32:12 +0000130 r: result,
Paul Duffind0759072021-02-17 11:23:00 +0000131 androidBpContents: sdk.GetAndroidBpContentsForTests(),
132 androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(),
133 androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(),
Paul Duffin1822a0a2021-03-21 12:56:33 +0000134 snapshotTestCustomizations: map[snapshotTest]*snapshotTestCustomization{},
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000135 }
136
137 buildParams := sdk.BuildParamsForTests()
138 copyRules := &strings.Builder{}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000139 otherCopyRules := &strings.Builder{}
Paul Duffine1ddcc92020-03-03 16:01:26 +0000140 snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000141 for _, bp := range buildParams {
142 switch bp.Rule.String() {
143 case android.Cp.String():
Paul Duffine1ddcc92020-03-03 16:01:26 +0000144 output := bp.Output
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000145 // Get destination relative to the snapshot root
146 dest := output.Rel()
147 src := android.NormalizePathForTesting(bp.Input)
148 // We differentiate between copy rules for the snapshot, and copy rules for the install file.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000149 if strings.HasPrefix(output.String(), snapshotDirPrefix) {
150 // Get source relative to build directory.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000151 _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest)
152 info.snapshotContents = append(info.snapshotContents, dest)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000153 } else {
154 _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest)
Paul Duffine1ddcc92020-03-03 16:01:26 +0000155 }
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000156
157 case repackageZip.String():
158 // Add the destdir to the snapshot contents as that is effectively where
159 // the content of the repackaged zip is copied.
160 dest := bp.Args["destdir"]
161 info.snapshotContents = append(info.snapshotContents, dest)
162
163 case zipFiles.String():
164 // This could be an intermediate zip file and not the actual output zip.
165 // In that case this will be overridden when the rule to merge the zips
166 // is processed.
Paul Duffin9b478b02019-12-10 13:41:51 +0000167 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000168
169 case mergeZips.String():
170 // Copy the current outputZip to the intermediateZip.
171 info.intermediateZip = info.outputZip
Paul Duffin9b478b02019-12-10 13:41:51 +0000172 mergeInput := android.NormalizePathForTesting(bp.Input)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000173 if info.intermediateZip != mergeInput {
Paul Duffin36474d32021-03-12 12:19:43 +0000174 t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead",
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000175 info.intermediateZip, mergeInput)
176 }
177
178 // Override output zip (which was actually the intermediate zip file) with the actual
179 // output zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000180 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000181
182 // Save the zips to be merged into the intermediate zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000183 info.mergeZips = android.NormalizePathsForTesting(bp.Inputs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000184 }
185 }
186
187 info.copyRules = copyRules.String()
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000188 info.otherCopyRules = otherCopyRules.String()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000189
190 return info
191}
192
Paul Duffin1822a0a2021-03-21 12:56:33 +0000193// The enum of different sdk snapshot tests performed by CheckSnapshot.
194type snapshotTest int
195
196const (
197 // The enumeration of the different test configurations.
198 // A test with the snapshot/Android.bp file but without the original Android.bp file.
199 checkSnapshotWithoutSource snapshotTest = iota
200
201 // A test with both the original source and the snapshot, with the source preferred.
202 checkSnapshotWithSourcePreferred
203
204 // A test with both the original source and the snapshot, with the snapshot preferred.
205 checkSnapshotPreferredWithSource
206
207 // The directory into which the snapshot will be 'unpacked'.
208 snapshotSubDir = "snapshot"
209)
210
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000211// Check the snapshot build rules.
212//
213// Takes a list of functions which check different facets of the snapshot build rules.
214// Allows each test to customize what is checked without duplicating lots of code
215// or proliferating check methods of different flavors.
Paul Duffin36474d32021-03-12 12:19:43 +0000216func CheckSnapshot(t *testing.T, result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) {
217 t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000218
Paul Duffin1356d8c2020-02-25 19:26:33 +0000219 // The sdk CommonOS variant is always responsible for generating the snapshot.
220 variant := android.CommonOS.Name
221
Paul Duffin981b94b2021-03-11 12:32:12 +0000222 sdk := result.Module(name, variant).(*sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000223
Paul Duffin36474d32021-03-12 12:19:43 +0000224 snapshotBuildInfo := getSdkSnapshotBuildInfo(t, result, sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000225
226 // Check state of the snapshot build.
227 for _, checker := range checkers {
228 checker(snapshotBuildInfo)
229 }
230
231 // Make sure that the generated zip file is in the correct place.
232 actual := snapshotBuildInfo.outputZip
Paul Duffin593b3c92019-12-05 14:31:48 +0000233 if dir != "" {
234 dir = filepath.Clean(dir) + "/"
235 }
Paul Duffin36474d32021-03-12 12:19:43 +0000236 android.AssertStringEquals(t, "Snapshot zip file in wrong place",
Paul Duffin593b3c92019-12-05 14:31:48 +0000237 fmt.Sprintf(".intermediates/%s%s/%s/%s-current.zip", dir, name, variant, name), actual)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000238
239 // Populate a mock filesystem with the files that would have been copied by
240 // the rules.
Paul Duffinc93c98e2021-03-20 01:32:50 +0000241 fs := android.MockFS{}
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000242 for _, dest := range snapshotBuildInfo.snapshotContents {
Paul Duffinc93c98e2021-03-20 01:32:50 +0000243 fs[filepath.Join(snapshotSubDir, dest)] = nil
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000244 }
Paul Duffinc93c98e2021-03-20 01:32:50 +0000245 fs[filepath.Join(snapshotSubDir, "Android.bp")] = []byte(snapshotBuildInfo.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000246
Paul Duffin1822a0a2021-03-21 12:56:33 +0000247 // The preparers from the original source fixture.
248 sourcePreparers := result.Preparer()
Paul Duffinc93c98e2021-03-20 01:32:50 +0000249
Paul Duffin1822a0a2021-03-21 12:56:33 +0000250 // Preparer to combine the snapshot and the source.
251 snapshotPreparer := android.GroupFixturePreparers(sourcePreparers, fs.AddToFixture())
252
253 var runSnapshotTestWithCheckers = func(t *testing.T, testConfig snapshotTest, extraPreparer android.FixturePreparer) {
254 customization := snapshotBuildInfo.snapshotTestCustomization(testConfig)
255
256 // TODO(b/183184375): Set Config.TestAllowNonExistentPaths = false to verify that all the
257 // files the snapshot needs are actually copied into the snapshot.
258
259 // Run the snapshot with the snapshot preparer and the extra preparer, which must come after as
260 // it may need to modify parts of the MockFS populated by the snapshot preparer.
261 result := android.GroupFixturePreparers(snapshotPreparer, extraPreparer).
262 ExtendWithErrorHandler(customization.errorHandler).
263 RunTest(t)
264
265 // Perform any additional checks the test need on the result of processing the snapshot.
266 for _, checker := range customization.checkers {
267 checker(t, result)
268 }
269 }
270
Paul Duffinc93c98e2021-03-20 01:32:50 +0000271 t.Run("snapshot without source", func(t *testing.T) {
Paul Duffin1822a0a2021-03-21 12:56:33 +0000272 // Remove the source Android.bp file to make sure it works without.
273 removeSourceAndroidBp := android.FixtureModifyMockFS(func(fs android.MockFS) {
274 delete(fs, "Android.bp")
275 })
Paul Duffinc93c98e2021-03-20 01:32:50 +0000276
Paul Duffin1822a0a2021-03-21 12:56:33 +0000277 runSnapshotTestWithCheckers(t, checkSnapshotWithoutSource, removeSourceAndroidBp)
278 })
Paul Duffinc93c98e2021-03-20 01:32:50 +0000279
Paul Duffin1822a0a2021-03-21 12:56:33 +0000280 t.Run("snapshot with source preferred", func(t *testing.T) {
281 runSnapshotTestWithCheckers(t, checkSnapshotWithSourcePreferred, android.NullFixturePreparer)
282 })
283
284 t.Run("snapshot preferred with source", func(t *testing.T) {
285 // Replace the snapshot/Android.bp file with one where "prefer: false," has been replaced with
286 // "prefer: true,"
287 preferPrebuilts := android.FixtureModifyMockFS(func(fs android.MockFS) {
288 snapshotBpFile := filepath.Join(snapshotSubDir, "Android.bp")
289 unpreferred := string(fs[snapshotBpFile])
290 fs[snapshotBpFile] = []byte(strings.ReplaceAll(unpreferred, "prefer: false,", "prefer: true,"))
291 })
292
293 runSnapshotTestWithCheckers(t, checkSnapshotPreferredWithSource, preferPrebuilts)
Paul Duffinc93c98e2021-03-20 01:32:50 +0000294 })
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000295}
296
297type snapshotBuildInfoChecker func(info *snapshotBuildInfo)
298
299// Check that the snapshot's generated Android.bp is correct.
300//
301// Both the expected and actual string are both trimmed before comparing.
302func checkAndroidBpContents(expected string) snapshotBuildInfoChecker {
303 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000304 info.t.Helper()
305 android.AssertTrimmedStringEquals(info.t, "Android.bp contents do not match", expected, info.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000306 }
307}
308
Paul Duffind0759072021-02-17 11:23:00 +0000309// Check that the snapshot's unversioned generated Android.bp is correct.
310//
311// This func should be used to check the general snapshot generation code.
312//
313// Both the expected and actual string are both trimmed before comparing.
314func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
315 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000316 info.t.Helper()
317 android.AssertTrimmedStringEquals(info.t, "unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000318 }
319}
320
321// Check that the snapshot's versioned generated Android.bp is correct.
322//
323// This func should only be used to check the version specific snapshot generation code,
324// i.e. the encoding of version into module names and the generation of the _snapshot module. The
325// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents()
326// func.
327//
328// Both the expected and actual string are both trimmed before comparing.
329func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
330 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000331 info.t.Helper()
332 android.AssertTrimmedStringEquals(info.t, "versioned Android.bp contents do not match", expected, info.androidVersionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000333 }
334}
335
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000336// Check that the snapshot's copy rules are correct.
337//
338// The copy rules are formatted as <src> -> <dest>, one per line and then compared
339// to the supplied expected string. Both the expected and actual string are trimmed
340// before comparing.
341func checkAllCopyRules(expected string) snapshotBuildInfoChecker {
342 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000343 info.t.Helper()
344 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.copyRules)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000345 }
346}
347
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000348func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker {
349 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000350 info.t.Helper()
351 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.otherCopyRules)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000352 }
353}
354
Paul Duffin3d1248c2020-04-09 00:10:17 +0100355// Check that the specified paths match the list of zips to merge with the intermediate zip.
356func checkMergeZips(expected ...string) snapshotBuildInfoChecker {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000357 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000358 info.t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000359 if info.intermediateZip == "" {
Paul Duffin36474d32021-03-12 12:19:43 +0000360 info.t.Errorf("No intermediate zip file was created")
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000361 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100362
Paul Duffin36474d32021-03-12 12:19:43 +0000363 android.AssertDeepEquals(info.t, "mismatching merge zip files", expected, info.mergeZips)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000364 }
365}
366
Paul Duffin1822a0a2021-03-21 12:56:33 +0000367type resultChecker func(t *testing.T, result *android.TestResult)
368
369// snapshotTestChecker registers a checker that will be run against the result of processing the
370// generated snapshot for the specified snapshotTest.
371func snapshotTestChecker(snapshotTest snapshotTest, checker resultChecker) snapshotBuildInfoChecker {
372 return func(info *snapshotBuildInfo) {
373 customization := info.snapshotTestCustomization(snapshotTest)
374 customization.checkers = append(customization.checkers, checker)
375 }
376}
377
378// snapshotTestErrorHandler registers an error handler to use when processing the snapshot
379// in the specific test case.
380//
381// Generally, the snapshot should work with all the test cases but some do not and just in case
382// there are a lot of issues to resolve, or it will take a lot of time this is a
383// get-out-of-jail-free card that allows progress to be made.
384//
385// deprecated: should only be used as a temporary workaround with an attached to do and bug.
386func snapshotTestErrorHandler(snapshotTest snapshotTest, handler android.FixtureErrorHandler) snapshotBuildInfoChecker {
387 return func(info *snapshotBuildInfo) {
388 customization := info.snapshotTestCustomization(snapshotTest)
389 customization.errorHandler = handler
390 }
391}
392
393// Encapsulates information provided by each test to customize a specific snapshotTest.
394type snapshotTestCustomization struct {
395 // Checkers that are run on the result of processing the preferred snapshot in a specific test
396 // case.
397 checkers []resultChecker
398
399 // Specify an error handler for when processing a specific test case.
400 //
401 // In some cases the generated snapshot cannot be used in a test configuration. Those cases are
402 // invariably bugs that need to be resolved but sometimes that can take a while. This provides a
403 // mechanism to temporarily ignore that error.
404 errorHandler android.FixtureErrorHandler
405}
406
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000407// Encapsulates information about the snapshot build structure in order to insulate tests from
408// knowing too much about internal structures.
409//
410// All source/input paths are relative either the build directory. All dest/output paths are
411// relative to the snapshot root directory.
412type snapshotBuildInfo struct {
Paul Duffin36474d32021-03-12 12:19:43 +0000413 t *testing.T
414
415 // The result from RunTest()
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000416 r *android.TestResult
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000417
418 // The contents of the generated Android.bp file
419 androidBpContents string
420
Paul Duffind0759072021-02-17 11:23:00 +0000421 // The contents of the unversioned Android.bp file
422 androidUnversionedBpContents string
423
424 // The contents of the versioned Android.bp file
425 androidVersionedBpContents string
426
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000427 // The paths, relative to the snapshot root, of all files and directories copied into the
428 // snapshot.
429 snapshotContents []string
430
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000431 // A formatted representation of the src/dest pairs for a snapshot, one pair per line,
432 // of the format src -> dest
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000433 copyRules string
434
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000435 // A formatted representation of the src/dest pairs for files not in a snapshot, one pair
436 // per line, of the format src -> dest
437 otherCopyRules string
438
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000439 // The path to the intermediate zip, which is a zip created from the source files copied
440 // into the snapshot directory and which will be merged with other zips to form the final output.
441 // Is am empty string if there is no intermediate zip because there are no zips to merge in.
442 intermediateZip string
443
444 // The paths to the zips to merge into the output zip, does not include the intermediate
445 // zip.
446 mergeZips []string
447
448 // The final output zip.
449 outputZip string
Paul Duffin1822a0a2021-03-21 12:56:33 +0000450
451 // The test specific customizations for each snapshot test.
452 snapshotTestCustomizations map[snapshotTest]*snapshotTestCustomization
453}
454
455// snapshotTestCustomization gets the test specific customization for the specified snapshotTest.
456//
457// If no customization was created previously then it creates a default customization.
458func (i *snapshotBuildInfo) snapshotTestCustomization(snapshotTest snapshotTest) *snapshotTestCustomization {
459 customization := i.snapshotTestCustomizations[snapshotTest]
460 if customization == nil {
461 customization = &snapshotTestCustomization{
462 errorHandler: android.FixtureExpectsNoErrors,
463 }
464 i.snapshotTestCustomizations[snapshotTest] = customization
465 }
466 return customization
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000467}