Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 1 | // Copyright 2020 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 android |
| 16 | |
Weijia He | 299d62d | 2024-04-18 22:55:10 +0000 | [diff] [blame] | 17 | import ( |
| 18 | "path/filepath" |
| 19 | "strings" |
Yu Liu | 367827f | 2025-02-15 00:18:33 +0000 | [diff] [blame] | 20 | |
| 21 | "github.com/google/blueprint" |
Weijia He | 299d62d | 2024-04-18 22:55:10 +0000 | [diff] [blame] | 22 | ) |
| 23 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 24 | func init() { |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 25 | RegisterParallelSingletonType("testsuites", testSuiteFilesFactory) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | func testSuiteFilesFactory() Singleton { |
| 29 | return &testSuiteFiles{} |
| 30 | } |
| 31 | |
Cole Faust | 725f838 | 2025-03-11 18:31:15 -0700 | [diff] [blame] | 32 | type testSuiteFiles struct{} |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 33 | |
| 34 | type TestSuiteModule interface { |
| 35 | Module |
| 36 | TestSuites() []string |
| 37 | } |
| 38 | |
Yu Liu | 367827f | 2025-02-15 00:18:33 +0000 | [diff] [blame] | 39 | type TestSuiteInfo struct { |
| 40 | TestSuites []string |
| 41 | } |
| 42 | |
| 43 | var TestSuiteInfoProvider = blueprint.NewProvider[TestSuiteInfo]() |
| 44 | |
mrziwang | fb4b996 | 2025-03-18 23:23:44 +0000 | [diff] [blame^] | 45 | type SupportFilesInfo struct { |
| 46 | SupportFiles InstallPaths |
| 47 | } |
| 48 | |
| 49 | var SupportFilesInfoProvider = blueprint.NewProvider[SupportFilesInfo]() |
| 50 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 51 | func (t *testSuiteFiles) GenerateBuildActions(ctx SingletonContext) { |
| 52 | files := make(map[string]map[string]InstallPaths) |
| 53 | |
Yu Liu | 367827f | 2025-02-15 00:18:33 +0000 | [diff] [blame] | 54 | ctx.VisitAllModuleProxies(func(m ModuleProxy) { |
| 55 | if tsm, ok := OtherModuleProvider(ctx, m, TestSuiteInfoProvider); ok { |
| 56 | for _, testSuite := range tsm.TestSuites { |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 57 | if files[testSuite] == nil { |
| 58 | files[testSuite] = make(map[string]InstallPaths) |
| 59 | } |
| 60 | name := ctx.ModuleName(m) |
Yu Liu | d46e5ae | 2024-08-15 18:46:17 +0000 | [diff] [blame] | 61 | files[testSuite][name] = append(files[testSuite][name], |
Yu Liu | 367827f | 2025-02-15 00:18:33 +0000 | [diff] [blame] | 62 | OtherModuleProviderOrDefault(ctx, m, InstallFilesProvider).InstallFiles...) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | }) |
| 66 | |
Cole Faust | 725f838 | 2025-03-11 18:31:15 -0700 | [diff] [blame] | 67 | robolectricZip, robolectrictListZip := buildTestSuite(ctx, "robolectric-tests", files["robolectric-tests"]) |
| 68 | ctx.Phony("robolectric-tests", robolectricZip, robolectrictListZip) |
| 69 | ctx.DistForGoal("robolectric-tests", robolectricZip, robolectrictListZip) |
Makoto Onuki | 4a9869d | 2023-10-20 10:42:47 -0700 | [diff] [blame] | 70 | |
Cole Faust | 725f838 | 2025-03-11 18:31:15 -0700 | [diff] [blame] | 71 | ravenwoodZip, ravenwoodListZip := buildTestSuite(ctx, "ravenwood-tests", files["ravenwood-tests"]) |
| 72 | ctx.Phony("ravenwood-tests", ravenwoodZip, ravenwoodListZip) |
| 73 | ctx.DistForGoal("ravenwood-tests", ravenwoodZip, ravenwoodListZip) |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Cole Faust | 725f838 | 2025-03-11 18:31:15 -0700 | [diff] [blame] | 76 | func buildTestSuite(ctx SingletonContext, suiteName string, files map[string]InstallPaths) (Path, Path) { |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 77 | var installedPaths InstallPaths |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 78 | for _, module := range SortedKeys(files) { |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 79 | installedPaths = append(installedPaths, files[module]...) |
| 80 | } |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 81 | |
Cole Faust | 725f838 | 2025-03-11 18:31:15 -0700 | [diff] [blame] | 82 | outputFile := pathForPackaging(ctx, suiteName+".zip") |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 83 | rule := NewRuleBuilder(pctx, ctx) |
| 84 | rule.Command().BuiltTool("soong_zip"). |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 85 | FlagWithOutput("-o ", outputFile). |
| 86 | FlagWithArg("-P ", "host/testcases"). |
Weijia He | 299d62d | 2024-04-18 22:55:10 +0000 | [diff] [blame] | 87 | FlagWithArg("-C ", pathForTestCases(ctx).String()). |
Julien Desprez | f368073 | 2023-06-27 20:48:04 +0000 | [diff] [blame] | 88 | FlagWithRspFileInputList("-r ", outputFile.ReplaceExtension(ctx, "rsp"), installedPaths.Paths()). |
Weijia He | 299d62d | 2024-04-18 22:55:10 +0000 | [diff] [blame] | 89 | Flag("-sha256") // necessary to save cas_uploader's time |
| 90 | |
Cole Faust | 725f838 | 2025-03-11 18:31:15 -0700 | [diff] [blame] | 91 | testList := buildTestList(ctx, suiteName+"_list", installedPaths) |
| 92 | testListZipOutputFile := pathForPackaging(ctx, suiteName+"_list.zip") |
Weijia He | 299d62d | 2024-04-18 22:55:10 +0000 | [diff] [blame] | 93 | |
| 94 | rule.Command().BuiltTool("soong_zip"). |
| 95 | FlagWithOutput("-o ", testListZipOutputFile). |
| 96 | FlagWithArg("-C ", pathForPackaging(ctx).String()). |
| 97 | FlagWithInput("-f ", testList). |
Cole Faust | 3b703f3 | 2023-10-16 13:30:51 -0700 | [diff] [blame] | 98 | Flag("-sha256") |
Weijia He | 299d62d | 2024-04-18 22:55:10 +0000 | [diff] [blame] | 99 | |
Cole Faust | 725f838 | 2025-03-11 18:31:15 -0700 | [diff] [blame] | 100 | rule.Build(strings.ReplaceAll(suiteName, "-", "_")+"_zip", suiteName+".zip") |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 101 | |
Cole Faust | 725f838 | 2025-03-11 18:31:15 -0700 | [diff] [blame] | 102 | return outputFile, testListZipOutputFile |
Weijia He | 299d62d | 2024-04-18 22:55:10 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | func buildTestList(ctx SingletonContext, listFile string, installedPaths InstallPaths) Path { |
| 106 | buf := &strings.Builder{} |
| 107 | for _, p := range installedPaths { |
| 108 | if p.Ext() != ".config" { |
| 109 | continue |
| 110 | } |
| 111 | pc, err := toTestListPath(p.String(), pathForTestCases(ctx).String(), "host/testcases") |
| 112 | if err != nil { |
| 113 | ctx.Errorf("Failed to convert path: %s, %v", p.String(), err) |
| 114 | continue |
| 115 | } |
| 116 | buf.WriteString(pc) |
| 117 | buf.WriteString("\n") |
| 118 | } |
| 119 | outputFile := pathForPackaging(ctx, listFile) |
| 120 | WriteFileRuleVerbatim(ctx, outputFile, buf.String()) |
Makoto Onuki | 4a9869d | 2023-10-20 10:42:47 -0700 | [diff] [blame] | 121 | return outputFile |
| 122 | } |
Weijia He | 299d62d | 2024-04-18 22:55:10 +0000 | [diff] [blame] | 123 | |
| 124 | func toTestListPath(path, relativeRoot, prefix string) (string, error) { |
| 125 | dest, err := filepath.Rel(relativeRoot, path) |
| 126 | if err != nil { |
| 127 | return "", err |
| 128 | } |
| 129 | return filepath.Join(prefix, dest), nil |
| 130 | } |
| 131 | |
| 132 | func pathForPackaging(ctx PathContext, pathComponents ...string) OutputPath { |
| 133 | pathComponents = append([]string{"packaging"}, pathComponents...) |
| 134 | return PathForOutput(ctx, pathComponents...) |
| 135 | } |
| 136 | |
| 137 | func pathForTestCases(ctx PathContext) InstallPath { |
| 138 | return pathForInstall(ctx, ctx.Config().BuildOS, X86, "testcases") |
| 139 | } |