blob: f4e85c0cfac4ebcb0af0e2414b4817227f387069 [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 Duffin55e740e2021-03-29 02:06:19 +010098 return android.GroupFixturePreparers(
99 prepareForSdkTest,
100 fs.AddToFixture(),
101 ).RunTestWithBp(t, bp)
Martin Stjernholm7feceb22020-07-11 04:33:29 +0100102}
103
Paul Duffin82d90432019-11-30 09:24:33 +0000104func testSdkError(t *testing.T, pattern, bp string) {
105 t.Helper()
Paul Duffin89648f92021-03-20 00:36:55 +0000106 prepareForSdkTest.
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000107 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
108 RunTestWithBp(t, bp)
Paul Duffin82d90432019-11-30 09:24:33 +0000109}
110
111func ensureListContains(t *testing.T, result []string, expected string) {
112 t.Helper()
113 if !android.InList(expected, result) {
114 t.Errorf("%q is not found in %v", expected, result)
115 }
116}
117
118func pathsToStrings(paths android.Paths) []string {
119 var ret []string
120 for _, p := range paths {
121 ret = append(ret, p.String())
122 }
123 return ret
124}
125
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000126// Analyse the sdk build rules to extract information about what it is doing.
Paul Duffinfe9a9e32021-03-11 17:41:01 +0000127//
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000128// e.g. find the src/dest pairs from each cp command, the various zip files
129// generated, etc.
Paul Duffin36474d32021-03-12 12:19:43 +0000130func getSdkSnapshotBuildInfo(t *testing.T, result *android.TestResult, sdk *sdk) *snapshotBuildInfo {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000131 info := &snapshotBuildInfo{
Paul Duffin36474d32021-03-12 12:19:43 +0000132 t: t,
Paul Duffin981b94b2021-03-11 12:32:12 +0000133 r: result,
Paul Duffind0759072021-02-17 11:23:00 +0000134 androidBpContents: sdk.GetAndroidBpContentsForTests(),
135 androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(),
136 androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(),
Paul Duffin1822a0a2021-03-21 12:56:33 +0000137 snapshotTestCustomizations: map[snapshotTest]*snapshotTestCustomization{},
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000138 }
139
140 buildParams := sdk.BuildParamsForTests()
141 copyRules := &strings.Builder{}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000142 otherCopyRules := &strings.Builder{}
Paul Duffine1ddcc92020-03-03 16:01:26 +0000143 snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000144 for _, bp := range buildParams {
145 switch bp.Rule.String() {
146 case android.Cp.String():
Paul Duffine1ddcc92020-03-03 16:01:26 +0000147 output := bp.Output
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000148 // Get destination relative to the snapshot root
149 dest := output.Rel()
150 src := android.NormalizePathForTesting(bp.Input)
151 // We differentiate between copy rules for the snapshot, and copy rules for the install file.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000152 if strings.HasPrefix(output.String(), snapshotDirPrefix) {
153 // Get source relative to build directory.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000154 _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest)
155 info.snapshotContents = append(info.snapshotContents, dest)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000156 } else {
157 _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest)
Paul Duffine1ddcc92020-03-03 16:01:26 +0000158 }
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000159
160 case repackageZip.String():
161 // Add the destdir to the snapshot contents as that is effectively where
162 // the content of the repackaged zip is copied.
163 dest := bp.Args["destdir"]
164 info.snapshotContents = append(info.snapshotContents, dest)
165
166 case zipFiles.String():
167 // This could be an intermediate zip file and not the actual output zip.
168 // In that case this will be overridden when the rule to merge the zips
169 // is processed.
Paul Duffin9b478b02019-12-10 13:41:51 +0000170 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000171
172 case mergeZips.String():
173 // Copy the current outputZip to the intermediateZip.
174 info.intermediateZip = info.outputZip
Paul Duffin9b478b02019-12-10 13:41:51 +0000175 mergeInput := android.NormalizePathForTesting(bp.Input)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000176 if info.intermediateZip != mergeInput {
Paul Duffin36474d32021-03-12 12:19:43 +0000177 t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead",
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000178 info.intermediateZip, mergeInput)
179 }
180
181 // Override output zip (which was actually the intermediate zip file) with the actual
182 // output zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000183 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000184
185 // Save the zips to be merged into the intermediate zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000186 info.mergeZips = android.NormalizePathsForTesting(bp.Inputs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000187 }
188 }
189
190 info.copyRules = copyRules.String()
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000191 info.otherCopyRules = otherCopyRules.String()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000192
193 return info
194}
195
Paul Duffin1822a0a2021-03-21 12:56:33 +0000196// The enum of different sdk snapshot tests performed by CheckSnapshot.
197type snapshotTest int
198
199const (
200 // The enumeration of the different test configurations.
201 // A test with the snapshot/Android.bp file but without the original Android.bp file.
202 checkSnapshotWithoutSource snapshotTest = iota
203
204 // A test with both the original source and the snapshot, with the source preferred.
205 checkSnapshotWithSourcePreferred
206
207 // A test with both the original source and the snapshot, with the snapshot preferred.
208 checkSnapshotPreferredWithSource
209
210 // The directory into which the snapshot will be 'unpacked'.
211 snapshotSubDir = "snapshot"
212)
213
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000214// Check the snapshot build rules.
215//
216// Takes a list of functions which check different facets of the snapshot build rules.
217// Allows each test to customize what is checked without duplicating lots of code
218// or proliferating check methods of different flavors.
Paul Duffin36474d32021-03-12 12:19:43 +0000219func CheckSnapshot(t *testing.T, result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) {
220 t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000221
Paul Duffin1356d8c2020-02-25 19:26:33 +0000222 // The sdk CommonOS variant is always responsible for generating the snapshot.
223 variant := android.CommonOS.Name
224
Paul Duffin981b94b2021-03-11 12:32:12 +0000225 sdk := result.Module(name, variant).(*sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000226
Paul Duffin36474d32021-03-12 12:19:43 +0000227 snapshotBuildInfo := getSdkSnapshotBuildInfo(t, result, sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000228
229 // Check state of the snapshot build.
230 for _, checker := range checkers {
231 checker(snapshotBuildInfo)
232 }
233
234 // Make sure that the generated zip file is in the correct place.
235 actual := snapshotBuildInfo.outputZip
Paul Duffin593b3c92019-12-05 14:31:48 +0000236 if dir != "" {
237 dir = filepath.Clean(dir) + "/"
238 }
Paul Duffin36474d32021-03-12 12:19:43 +0000239 android.AssertStringEquals(t, "Snapshot zip file in wrong place",
Paul Duffin593b3c92019-12-05 14:31:48 +0000240 fmt.Sprintf(".intermediates/%s%s/%s/%s-current.zip", dir, name, variant, name), actual)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000241
242 // Populate a mock filesystem with the files that would have been copied by
243 // the rules.
Paul Duffinc93c98e2021-03-20 01:32:50 +0000244 fs := android.MockFS{}
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000245 for _, dest := range snapshotBuildInfo.snapshotContents {
Paul Duffinc93c98e2021-03-20 01:32:50 +0000246 fs[filepath.Join(snapshotSubDir, dest)] = nil
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000247 }
Paul Duffinc93c98e2021-03-20 01:32:50 +0000248 fs[filepath.Join(snapshotSubDir, "Android.bp")] = []byte(snapshotBuildInfo.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000249
Paul Duffin1822a0a2021-03-21 12:56:33 +0000250 // The preparers from the original source fixture.
251 sourcePreparers := result.Preparer()
Paul Duffinc93c98e2021-03-20 01:32:50 +0000252
Paul Duffin1822a0a2021-03-21 12:56:33 +0000253 // Preparer to combine the snapshot and the source.
254 snapshotPreparer := android.GroupFixturePreparers(sourcePreparers, fs.AddToFixture())
255
256 var runSnapshotTestWithCheckers = func(t *testing.T, testConfig snapshotTest, extraPreparer android.FixturePreparer) {
Paul Duffin023dba02021-04-22 01:45:29 +0100257 t.Helper()
Paul Duffin1822a0a2021-03-21 12:56:33 +0000258 customization := snapshotBuildInfo.snapshotTestCustomization(testConfig)
Paul Duffina57835e2021-04-19 13:23:06 +0100259 customizedPreparers := android.GroupFixturePreparers(customization.preparers...)
Paul Duffin1822a0a2021-03-21 12:56:33 +0000260
261 // TODO(b/183184375): Set Config.TestAllowNonExistentPaths = false to verify that all the
262 // files the snapshot needs are actually copied into the snapshot.
263
264 // Run the snapshot with the snapshot preparer and the extra preparer, which must come after as
265 // it may need to modify parts of the MockFS populated by the snapshot preparer.
Paul Duffina57835e2021-04-19 13:23:06 +0100266 result := android.GroupFixturePreparers(snapshotPreparer, extraPreparer, customizedPreparers).
Paul Duffin1822a0a2021-03-21 12:56:33 +0000267 ExtendWithErrorHandler(customization.errorHandler).
268 RunTest(t)
269
270 // Perform any additional checks the test need on the result of processing the snapshot.
271 for _, checker := range customization.checkers {
272 checker(t, result)
273 }
274 }
275
Paul Duffinc93c98e2021-03-20 01:32:50 +0000276 t.Run("snapshot without source", func(t *testing.T) {
Paul Duffin1822a0a2021-03-21 12:56:33 +0000277 // Remove the source Android.bp file to make sure it works without.
278 removeSourceAndroidBp := android.FixtureModifyMockFS(func(fs android.MockFS) {
279 delete(fs, "Android.bp")
280 })
Paul Duffinc93c98e2021-03-20 01:32:50 +0000281
Paul Duffin1822a0a2021-03-21 12:56:33 +0000282 runSnapshotTestWithCheckers(t, checkSnapshotWithoutSource, removeSourceAndroidBp)
283 })
Paul Duffinc93c98e2021-03-20 01:32:50 +0000284
Paul Duffin1822a0a2021-03-21 12:56:33 +0000285 t.Run("snapshot with source preferred", func(t *testing.T) {
286 runSnapshotTestWithCheckers(t, checkSnapshotWithSourcePreferred, android.NullFixturePreparer)
287 })
288
289 t.Run("snapshot preferred with source", func(t *testing.T) {
290 // Replace the snapshot/Android.bp file with one where "prefer: false," has been replaced with
291 // "prefer: true,"
292 preferPrebuilts := android.FixtureModifyMockFS(func(fs android.MockFS) {
293 snapshotBpFile := filepath.Join(snapshotSubDir, "Android.bp")
294 unpreferred := string(fs[snapshotBpFile])
295 fs[snapshotBpFile] = []byte(strings.ReplaceAll(unpreferred, "prefer: false,", "prefer: true,"))
296 })
297
298 runSnapshotTestWithCheckers(t, checkSnapshotPreferredWithSource, preferPrebuilts)
Paul Duffinc93c98e2021-03-20 01:32:50 +0000299 })
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000300}
301
302type snapshotBuildInfoChecker func(info *snapshotBuildInfo)
303
304// Check that the snapshot's generated Android.bp is correct.
305//
306// Both the expected and actual string are both trimmed before comparing.
307func checkAndroidBpContents(expected string) snapshotBuildInfoChecker {
308 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000309 info.t.Helper()
310 android.AssertTrimmedStringEquals(info.t, "Android.bp contents do not match", expected, info.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000311 }
312}
313
Paul Duffind0759072021-02-17 11:23:00 +0000314// Check that the snapshot's unversioned generated Android.bp is correct.
315//
316// This func should be used to check the general snapshot generation code.
317//
318// Both the expected and actual string are both trimmed before comparing.
319func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
320 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000321 info.t.Helper()
322 android.AssertTrimmedStringEquals(info.t, "unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000323 }
324}
325
326// Check that the snapshot's versioned generated Android.bp is correct.
327//
328// This func should only be used to check the version specific snapshot generation code,
329// i.e. the encoding of version into module names and the generation of the _snapshot module. The
330// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents()
331// func.
332//
333// Both the expected and actual string are both trimmed before comparing.
334func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
335 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000336 info.t.Helper()
337 android.AssertTrimmedStringEquals(info.t, "versioned Android.bp contents do not match", expected, info.androidVersionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000338 }
339}
340
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000341// Check that the snapshot's copy rules are correct.
342//
343// The copy rules are formatted as <src> -> <dest>, one per line and then compared
344// to the supplied expected string. Both the expected and actual string are trimmed
345// before comparing.
346func checkAllCopyRules(expected string) snapshotBuildInfoChecker {
347 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000348 info.t.Helper()
349 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.copyRules)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000350 }
351}
352
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000353func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker {
354 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000355 info.t.Helper()
356 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.otherCopyRules)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000357 }
358}
359
Paul Duffin3d1248c2020-04-09 00:10:17 +0100360// Check that the specified paths match the list of zips to merge with the intermediate zip.
361func checkMergeZips(expected ...string) snapshotBuildInfoChecker {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000362 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000363 info.t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000364 if info.intermediateZip == "" {
Paul Duffin36474d32021-03-12 12:19:43 +0000365 info.t.Errorf("No intermediate zip file was created")
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000366 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100367
Paul Duffin36474d32021-03-12 12:19:43 +0000368 android.AssertDeepEquals(info.t, "mismatching merge zip files", expected, info.mergeZips)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000369 }
370}
371
Paul Duffin1822a0a2021-03-21 12:56:33 +0000372type resultChecker func(t *testing.T, result *android.TestResult)
373
Paul Duffina57835e2021-04-19 13:23:06 +0100374// snapshotTestPreparer registers a preparer that will be used to customize the specified
375// snapshotTest.
376func snapshotTestPreparer(snapshotTest snapshotTest, preparer android.FixturePreparer) snapshotBuildInfoChecker {
377 return func(info *snapshotBuildInfo) {
378 customization := info.snapshotTestCustomization(snapshotTest)
379 customization.preparers = append(customization.preparers, preparer)
380 }
381}
382
Paul Duffin1822a0a2021-03-21 12:56:33 +0000383// snapshotTestChecker registers a checker that will be run against the result of processing the
384// generated snapshot for the specified snapshotTest.
385func snapshotTestChecker(snapshotTest snapshotTest, checker resultChecker) snapshotBuildInfoChecker {
386 return func(info *snapshotBuildInfo) {
387 customization := info.snapshotTestCustomization(snapshotTest)
388 customization.checkers = append(customization.checkers, checker)
389 }
390}
391
392// snapshotTestErrorHandler registers an error handler to use when processing the snapshot
393// in the specific test case.
394//
395// Generally, the snapshot should work with all the test cases but some do not and just in case
396// there are a lot of issues to resolve, or it will take a lot of time this is a
397// get-out-of-jail-free card that allows progress to be made.
398//
399// deprecated: should only be used as a temporary workaround with an attached to do and bug.
400func snapshotTestErrorHandler(snapshotTest snapshotTest, handler android.FixtureErrorHandler) snapshotBuildInfoChecker {
401 return func(info *snapshotBuildInfo) {
402 customization := info.snapshotTestCustomization(snapshotTest)
403 customization.errorHandler = handler
404 }
405}
406
407// Encapsulates information provided by each test to customize a specific snapshotTest.
408type snapshotTestCustomization struct {
Paul Duffina57835e2021-04-19 13:23:06 +0100409 // Preparers that are used to customize the test fixture before running the test.
410 preparers []android.FixturePreparer
411
Paul Duffin1822a0a2021-03-21 12:56:33 +0000412 // Checkers that are run on the result of processing the preferred snapshot in a specific test
413 // case.
414 checkers []resultChecker
415
416 // Specify an error handler for when processing a specific test case.
417 //
418 // In some cases the generated snapshot cannot be used in a test configuration. Those cases are
419 // invariably bugs that need to be resolved but sometimes that can take a while. This provides a
420 // mechanism to temporarily ignore that error.
421 errorHandler android.FixtureErrorHandler
422}
423
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000424// Encapsulates information about the snapshot build structure in order to insulate tests from
425// knowing too much about internal structures.
426//
427// All source/input paths are relative either the build directory. All dest/output paths are
428// relative to the snapshot root directory.
429type snapshotBuildInfo struct {
Paul Duffin36474d32021-03-12 12:19:43 +0000430 t *testing.T
431
432 // The result from RunTest()
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000433 r *android.TestResult
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000434
435 // The contents of the generated Android.bp file
436 androidBpContents string
437
Paul Duffind0759072021-02-17 11:23:00 +0000438 // The contents of the unversioned Android.bp file
439 androidUnversionedBpContents string
440
441 // The contents of the versioned Android.bp file
442 androidVersionedBpContents string
443
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000444 // The paths, relative to the snapshot root, of all files and directories copied into the
445 // snapshot.
446 snapshotContents []string
447
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000448 // A formatted representation of the src/dest pairs for a snapshot, one pair per line,
449 // of the format src -> dest
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000450 copyRules string
451
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000452 // A formatted representation of the src/dest pairs for files not in a snapshot, one pair
453 // per line, of the format src -> dest
454 otherCopyRules string
455
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000456 // The path to the intermediate zip, which is a zip created from the source files copied
457 // into the snapshot directory and which will be merged with other zips to form the final output.
458 // Is am empty string if there is no intermediate zip because there are no zips to merge in.
459 intermediateZip string
460
461 // The paths to the zips to merge into the output zip, does not include the intermediate
462 // zip.
463 mergeZips []string
464
465 // The final output zip.
466 outputZip string
Paul Duffin1822a0a2021-03-21 12:56:33 +0000467
468 // The test specific customizations for each snapshot test.
469 snapshotTestCustomizations map[snapshotTest]*snapshotTestCustomization
470}
471
472// snapshotTestCustomization gets the test specific customization for the specified snapshotTest.
473//
474// If no customization was created previously then it creates a default customization.
475func (i *snapshotBuildInfo) snapshotTestCustomization(snapshotTest snapshotTest) *snapshotTestCustomization {
476 customization := i.snapshotTestCustomizations[snapshotTest]
477 if customization == nil {
478 customization = &snapshotTestCustomization{
479 errorHandler: android.FixtureExpectsNoErrors,
480 }
481 i.snapshotTestCustomizations[snapshotTest] = customization
482 }
483 return customization
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000484}