blob: 18744f1da77fc51ad3ab8088f49e25e58479ca0f [file] [log] [blame]
Colin Cross8eebb132020-01-29 20:07:03 -08001// 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
15package android
16
Weijia He299d62d2024-04-18 22:55:10 +000017import (
18 "path/filepath"
19 "strings"
20)
21
Colin Cross8eebb132020-01-29 20:07:03 -080022func init() {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000023 RegisterParallelSingletonType("testsuites", testSuiteFilesFactory)
Colin Cross8eebb132020-01-29 20:07:03 -080024}
25
26func testSuiteFilesFactory() Singleton {
27 return &testSuiteFiles{}
28}
29
30type testSuiteFiles struct {
Weijia He299d62d2024-04-18 22:55:10 +000031 robolectric []Path
32 ravenwood []Path
Colin Cross8eebb132020-01-29 20:07:03 -080033}
34
35type TestSuiteModule interface {
36 Module
37 TestSuites() []string
38}
39
40func (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 Liud46e5ae2024-08-15 18:46:17 +000050 files[testSuite][name] = append(files[testSuite][name],
51 OtherModuleProviderOrDefault(ctx, tsm, InstallFilesProvider).InstallFiles...)
Colin Cross8eebb132020-01-29 20:07:03 -080052 }
53 }
54 })
55
56 t.robolectric = robolectricTestSuite(ctx, files["robolectric-tests"])
Weijia He299d62d2024-04-18 22:55:10 +000057 ctx.Phony("robolectric-tests", t.robolectric...)
Makoto Onuki4a9869d2023-10-20 10:42:47 -070058
59 t.ravenwood = ravenwoodTestSuite(ctx, files["ravenwood-tests"])
Weijia He299d62d2024-04-18 22:55:10 +000060 ctx.Phony("ravenwood-tests", t.ravenwood...)
Weijia He299d62d2024-04-18 22:55:10 +000061 ctx.DistForGoal("robolectric-tests", t.robolectric...)
62 ctx.DistForGoal("ravenwood-tests", t.ravenwood...)
Colin Cross8eebb132020-01-29 20:07:03 -080063}
64
Weijia He299d62d2024-04-18 22:55:10 +000065func robolectricTestSuite(ctx SingletonContext, files map[string]InstallPaths) []Path {
Colin Cross8eebb132020-01-29 20:07:03 -080066 var installedPaths InstallPaths
Cole Faust18994c72023-02-28 16:02:16 -080067 for _, module := range SortedKeys(files) {
Colin Cross8eebb132020-01-29 20:07:03 -080068 installedPaths = append(installedPaths, files[module]...)
69 }
Colin Cross8eebb132020-01-29 20:07:03 -080070
Weijia He299d62d2024-04-18 22:55:10 +000071 outputFile := pathForPackaging(ctx, "robolectric-tests.zip")
Colin Crossf1a035e2020-11-16 17:32:30 -080072 rule := NewRuleBuilder(pctx, ctx)
73 rule.Command().BuiltTool("soong_zip").
Colin Cross8eebb132020-01-29 20:07:03 -080074 FlagWithOutput("-o ", outputFile).
75 FlagWithArg("-P ", "host/testcases").
Weijia He299d62d2024-04-18 22:55:10 +000076 FlagWithArg("-C ", pathForTestCases(ctx).String()).
Julien Desprezf3680732023-06-27 20:48:04 +000077 FlagWithRspFileInputList("-r ", outputFile.ReplaceExtension(ctx, "rsp"), installedPaths.Paths()).
Weijia He299d62d2024-04-18 22:55:10 +000078 Flag("-sha256") // necessary to save cas_uploader's time
79
80 testList := buildTestList(ctx, "robolectric-tests_list", installedPaths)
81 testListZipOutputFile := pathForPackaging(ctx, "robolectric-tests_list.zip")
82
83 rule.Command().BuiltTool("soong_zip").
84 FlagWithOutput("-o ", testListZipOutputFile).
85 FlagWithArg("-C ", pathForPackaging(ctx).String()).
86 FlagWithInput("-f ", testList).
Cole Faust3b703f32023-10-16 13:30:51 -070087 Flag("-sha256")
Weijia He299d62d2024-04-18 22:55:10 +000088
Colin Crossf1a035e2020-11-16 17:32:30 -080089 rule.Build("robolectric_tests_zip", "robolectric-tests.zip")
Colin Cross8eebb132020-01-29 20:07:03 -080090
Weijia He299d62d2024-04-18 22:55:10 +000091 return []Path{outputFile, testListZipOutputFile}
Colin Cross8eebb132020-01-29 20:07:03 -080092}
Makoto Onuki4a9869d2023-10-20 10:42:47 -070093
Weijia He299d62d2024-04-18 22:55:10 +000094func ravenwoodTestSuite(ctx SingletonContext, files map[string]InstallPaths) []Path {
Makoto Onuki4a9869d2023-10-20 10:42:47 -070095 var installedPaths InstallPaths
96 for _, module := range SortedKeys(files) {
97 installedPaths = append(installedPaths, files[module]...)
98 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -070099
Weijia He299d62d2024-04-18 22:55:10 +0000100 outputFile := pathForPackaging(ctx, "ravenwood-tests.zip")
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700101 rule := NewRuleBuilder(pctx, ctx)
102 rule.Command().BuiltTool("soong_zip").
103 FlagWithOutput("-o ", outputFile).
104 FlagWithArg("-P ", "host/testcases").
Weijia He299d62d2024-04-18 22:55:10 +0000105 FlagWithArg("-C ", pathForTestCases(ctx).String()).
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700106 FlagWithRspFileInputList("-r ", outputFile.ReplaceExtension(ctx, "rsp"), installedPaths.Paths()).
Weijia He299d62d2024-04-18 22:55:10 +0000107 Flag("-sha256") // necessary to save cas_uploader's time
108
109 testList := buildTestList(ctx, "ravenwood-tests_list", installedPaths)
110 testListZipOutputFile := pathForPackaging(ctx, "ravenwood-tests_list.zip")
111
112 rule.Command().BuiltTool("soong_zip").
113 FlagWithOutput("-o ", testListZipOutputFile).
114 FlagWithArg("-C ", pathForPackaging(ctx).String()).
115 FlagWithInput("-f ", testList).
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700116 Flag("-sha256")
Weijia He299d62d2024-04-18 22:55:10 +0000117
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700118 rule.Build("ravenwood_tests_zip", "ravenwood-tests.zip")
119
Weijia He299d62d2024-04-18 22:55:10 +0000120 return []Path{outputFile, testListZipOutputFile}
121}
122
123func buildTestList(ctx SingletonContext, listFile string, installedPaths InstallPaths) Path {
124 buf := &strings.Builder{}
125 for _, p := range installedPaths {
126 if p.Ext() != ".config" {
127 continue
128 }
129 pc, err := toTestListPath(p.String(), pathForTestCases(ctx).String(), "host/testcases")
130 if err != nil {
131 ctx.Errorf("Failed to convert path: %s, %v", p.String(), err)
132 continue
133 }
134 buf.WriteString(pc)
135 buf.WriteString("\n")
136 }
137 outputFile := pathForPackaging(ctx, listFile)
138 WriteFileRuleVerbatim(ctx, outputFile, buf.String())
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700139 return outputFile
140}
Weijia He299d62d2024-04-18 22:55:10 +0000141
142func toTestListPath(path, relativeRoot, prefix string) (string, error) {
143 dest, err := filepath.Rel(relativeRoot, path)
144 if err != nil {
145 return "", err
146 }
147 return filepath.Join(prefix, dest), nil
148}
149
150func pathForPackaging(ctx PathContext, pathComponents ...string) OutputPath {
151 pathComponents = append([]string{"packaging"}, pathComponents...)
152 return PathForOutput(ctx, pathComponents...)
153}
154
155func pathForTestCases(ctx PathContext) InstallPath {
156 return pathForInstall(ctx, ctx.Config().BuildOS, X86, "testcases")
157}