blob: 39317ec62d9419a5c329bb036159a43ff7f54d7f [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"
Yu Liu367827f2025-02-15 00:18:33 +000020
21 "github.com/google/blueprint"
Weijia He299d62d2024-04-18 22:55:10 +000022)
23
Colin Cross8eebb132020-01-29 20:07:03 -080024func init() {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000025 RegisterParallelSingletonType("testsuites", testSuiteFilesFactory)
Colin Cross8eebb132020-01-29 20:07:03 -080026}
27
28func testSuiteFilesFactory() Singleton {
29 return &testSuiteFiles{}
30}
31
32type testSuiteFiles struct {
Weijia He299d62d2024-04-18 22:55:10 +000033 robolectric []Path
34 ravenwood []Path
Colin Cross8eebb132020-01-29 20:07:03 -080035}
36
37type TestSuiteModule interface {
38 Module
39 TestSuites() []string
40}
41
Yu Liu367827f2025-02-15 00:18:33 +000042type TestSuiteInfo struct {
43 TestSuites []string
44}
45
46var TestSuiteInfoProvider = blueprint.NewProvider[TestSuiteInfo]()
47
Colin Cross8eebb132020-01-29 20:07:03 -080048func (t *testSuiteFiles) GenerateBuildActions(ctx SingletonContext) {
49 files := make(map[string]map[string]InstallPaths)
50
Yu Liu367827f2025-02-15 00:18:33 +000051 ctx.VisitAllModuleProxies(func(m ModuleProxy) {
52 if tsm, ok := OtherModuleProvider(ctx, m, TestSuiteInfoProvider); ok {
53 for _, testSuite := range tsm.TestSuites {
Colin Cross8eebb132020-01-29 20:07:03 -080054 if files[testSuite] == nil {
55 files[testSuite] = make(map[string]InstallPaths)
56 }
57 name := ctx.ModuleName(m)
Yu Liud46e5ae2024-08-15 18:46:17 +000058 files[testSuite][name] = append(files[testSuite][name],
Yu Liu367827f2025-02-15 00:18:33 +000059 OtherModuleProviderOrDefault(ctx, m, InstallFilesProvider).InstallFiles...)
Colin Cross8eebb132020-01-29 20:07:03 -080060 }
61 }
62 })
63
64 t.robolectric = robolectricTestSuite(ctx, files["robolectric-tests"])
Weijia He299d62d2024-04-18 22:55:10 +000065 ctx.Phony("robolectric-tests", t.robolectric...)
Makoto Onuki4a9869d2023-10-20 10:42:47 -070066
67 t.ravenwood = ravenwoodTestSuite(ctx, files["ravenwood-tests"])
Weijia He299d62d2024-04-18 22:55:10 +000068 ctx.Phony("ravenwood-tests", t.ravenwood...)
Weijia He299d62d2024-04-18 22:55:10 +000069 ctx.DistForGoal("robolectric-tests", t.robolectric...)
70 ctx.DistForGoal("ravenwood-tests", t.ravenwood...)
Colin Cross8eebb132020-01-29 20:07:03 -080071}
72
Weijia He299d62d2024-04-18 22:55:10 +000073func robolectricTestSuite(ctx SingletonContext, files map[string]InstallPaths) []Path {
Colin Cross8eebb132020-01-29 20:07:03 -080074 var installedPaths InstallPaths
Cole Faust18994c72023-02-28 16:02:16 -080075 for _, module := range SortedKeys(files) {
Colin Cross8eebb132020-01-29 20:07:03 -080076 installedPaths = append(installedPaths, files[module]...)
77 }
Colin Cross8eebb132020-01-29 20:07:03 -080078
Weijia He299d62d2024-04-18 22:55:10 +000079 outputFile := pathForPackaging(ctx, "robolectric-tests.zip")
Colin Crossf1a035e2020-11-16 17:32:30 -080080 rule := NewRuleBuilder(pctx, ctx)
81 rule.Command().BuiltTool("soong_zip").
Colin Cross8eebb132020-01-29 20:07:03 -080082 FlagWithOutput("-o ", outputFile).
83 FlagWithArg("-P ", "host/testcases").
Weijia He299d62d2024-04-18 22:55:10 +000084 FlagWithArg("-C ", pathForTestCases(ctx).String()).
Julien Desprezf3680732023-06-27 20:48:04 +000085 FlagWithRspFileInputList("-r ", outputFile.ReplaceExtension(ctx, "rsp"), installedPaths.Paths()).
Weijia He299d62d2024-04-18 22:55:10 +000086 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 Faust3b703f32023-10-16 13:30:51 -070095 Flag("-sha256")
Weijia He299d62d2024-04-18 22:55:10 +000096
Colin Crossf1a035e2020-11-16 17:32:30 -080097 rule.Build("robolectric_tests_zip", "robolectric-tests.zip")
Colin Cross8eebb132020-01-29 20:07:03 -080098
Weijia He299d62d2024-04-18 22:55:10 +000099 return []Path{outputFile, testListZipOutputFile}
Colin Cross8eebb132020-01-29 20:07:03 -0800100}
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700101
Weijia He299d62d2024-04-18 22:55:10 +0000102func ravenwoodTestSuite(ctx SingletonContext, files map[string]InstallPaths) []Path {
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700103 var installedPaths InstallPaths
104 for _, module := range SortedKeys(files) {
105 installedPaths = append(installedPaths, files[module]...)
106 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700107
Weijia He299d62d2024-04-18 22:55:10 +0000108 outputFile := pathForPackaging(ctx, "ravenwood-tests.zip")
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700109 rule := NewRuleBuilder(pctx, ctx)
110 rule.Command().BuiltTool("soong_zip").
111 FlagWithOutput("-o ", outputFile).
112 FlagWithArg("-P ", "host/testcases").
Weijia He299d62d2024-04-18 22:55:10 +0000113 FlagWithArg("-C ", pathForTestCases(ctx).String()).
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700114 FlagWithRspFileInputList("-r ", outputFile.ReplaceExtension(ctx, "rsp"), installedPaths.Paths()).
Weijia He299d62d2024-04-18 22:55:10 +0000115 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 Onuki4a9869d2023-10-20 10:42:47 -0700124 Flag("-sha256")
Weijia He299d62d2024-04-18 22:55:10 +0000125
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700126 rule.Build("ravenwood_tests_zip", "ravenwood-tests.zip")
127
Weijia He299d62d2024-04-18 22:55:10 +0000128 return []Path{outputFile, testListZipOutputFile}
129}
130
131func 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 Onuki4a9869d2023-10-20 10:42:47 -0700147 return outputFile
148}
Weijia He299d62d2024-04-18 22:55:10 +0000149
150func 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
158func pathForPackaging(ctx PathContext, pathComponents ...string) OutputPath {
159 pathComponents = append([]string{"packaging"}, pathComponents...)
160 return PathForOutput(ctx, pathComponents...)
161}
162
163func pathForTestCases(ctx PathContext) InstallPath {
164 return pathForInstall(ctx, ctx.Config().BuildOS, X86, "testcases")
165}