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