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