blob: ff75f26bb2ab28966f45e066949a5440b10fc512 [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)
Jiyong Park4dc2a1a2020-09-28 17:46:22 +090050 files[testSuite][name] = append(files[testSuite][name], tsm.FilesToInstall()...)
Colin Cross8eebb132020-01-29 20:07:03 -080051 }
52 }
53 })
54
55 t.robolectric = robolectricTestSuite(ctx, files["robolectric-tests"])
Weijia He299d62d2024-04-18 22:55:10 +000056 ctx.Phony("robolectric-tests", t.robolectric...)
Makoto Onuki4a9869d2023-10-20 10:42:47 -070057
58 t.ravenwood = ravenwoodTestSuite(ctx, files["ravenwood-tests"])
Weijia He299d62d2024-04-18 22:55:10 +000059 ctx.Phony("ravenwood-tests", t.ravenwood...)
Colin Cross8eebb132020-01-29 20:07:03 -080060}
61
62func (t *testSuiteFiles) MakeVars(ctx MakeVarsContext) {
Weijia He299d62d2024-04-18 22:55:10 +000063 ctx.DistForGoal("robolectric-tests", t.robolectric...)
64 ctx.DistForGoal("ravenwood-tests", t.ravenwood...)
Colin Cross8eebb132020-01-29 20:07:03 -080065}
66
Weijia He299d62d2024-04-18 22:55:10 +000067func robolectricTestSuite(ctx SingletonContext, files map[string]InstallPaths) []Path {
Colin Cross8eebb132020-01-29 20:07:03 -080068 var installedPaths InstallPaths
Cole Faust18994c72023-02-28 16:02:16 -080069 for _, module := range SortedKeys(files) {
Colin Cross8eebb132020-01-29 20:07:03 -080070 installedPaths = append(installedPaths, files[module]...)
71 }
Colin Cross8eebb132020-01-29 20:07:03 -080072
Weijia He299d62d2024-04-18 22:55:10 +000073 outputFile := pathForPackaging(ctx, "robolectric-tests.zip")
Colin Crossf1a035e2020-11-16 17:32:30 -080074 rule := NewRuleBuilder(pctx, ctx)
75 rule.Command().BuiltTool("soong_zip").
Colin Cross8eebb132020-01-29 20:07:03 -080076 FlagWithOutput("-o ", outputFile).
77 FlagWithArg("-P ", "host/testcases").
Weijia He299d62d2024-04-18 22:55:10 +000078 FlagWithArg("-C ", pathForTestCases(ctx).String()).
Julien Desprezf3680732023-06-27 20:48:04 +000079 FlagWithRspFileInputList("-r ", outputFile.ReplaceExtension(ctx, "rsp"), installedPaths.Paths()).
Weijia He299d62d2024-04-18 22:55:10 +000080 Flag("-sha256") // necessary to save cas_uploader's time
81
82 testList := buildTestList(ctx, "robolectric-tests_list", installedPaths)
83 testListZipOutputFile := pathForPackaging(ctx, "robolectric-tests_list.zip")
84
85 rule.Command().BuiltTool("soong_zip").
86 FlagWithOutput("-o ", testListZipOutputFile).
87 FlagWithArg("-C ", pathForPackaging(ctx).String()).
88 FlagWithInput("-f ", testList).
Cole Faust3b703f32023-10-16 13:30:51 -070089 Flag("-sha256")
Weijia He299d62d2024-04-18 22:55:10 +000090
Colin Crossf1a035e2020-11-16 17:32:30 -080091 rule.Build("robolectric_tests_zip", "robolectric-tests.zip")
Colin Cross8eebb132020-01-29 20:07:03 -080092
Weijia He299d62d2024-04-18 22:55:10 +000093 return []Path{outputFile, testListZipOutputFile}
Colin Cross8eebb132020-01-29 20:07:03 -080094}
Makoto Onuki4a9869d2023-10-20 10:42:47 -070095
Weijia He299d62d2024-04-18 22:55:10 +000096func ravenwoodTestSuite(ctx SingletonContext, files map[string]InstallPaths) []Path {
Makoto Onuki4a9869d2023-10-20 10:42:47 -070097 var installedPaths InstallPaths
98 for _, module := range SortedKeys(files) {
99 installedPaths = append(installedPaths, files[module]...)
100 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700101
Weijia He299d62d2024-04-18 22:55:10 +0000102 outputFile := pathForPackaging(ctx, "ravenwood-tests.zip")
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700103 rule := NewRuleBuilder(pctx, ctx)
104 rule.Command().BuiltTool("soong_zip").
105 FlagWithOutput("-o ", outputFile).
106 FlagWithArg("-P ", "host/testcases").
Weijia He299d62d2024-04-18 22:55:10 +0000107 FlagWithArg("-C ", pathForTestCases(ctx).String()).
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700108 FlagWithRspFileInputList("-r ", outputFile.ReplaceExtension(ctx, "rsp"), installedPaths.Paths()).
Weijia He299d62d2024-04-18 22:55:10 +0000109 Flag("-sha256") // necessary to save cas_uploader's time
110
111 testList := buildTestList(ctx, "ravenwood-tests_list", installedPaths)
112 testListZipOutputFile := pathForPackaging(ctx, "ravenwood-tests_list.zip")
113
114 rule.Command().BuiltTool("soong_zip").
115 FlagWithOutput("-o ", testListZipOutputFile).
116 FlagWithArg("-C ", pathForPackaging(ctx).String()).
117 FlagWithInput("-f ", testList).
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700118 Flag("-sha256")
Weijia He299d62d2024-04-18 22:55:10 +0000119
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700120 rule.Build("ravenwood_tests_zip", "ravenwood-tests.zip")
121
Weijia He299d62d2024-04-18 22:55:10 +0000122 return []Path{outputFile, testListZipOutputFile}
123}
124
125func buildTestList(ctx SingletonContext, listFile string, installedPaths InstallPaths) Path {
126 buf := &strings.Builder{}
127 for _, p := range installedPaths {
128 if p.Ext() != ".config" {
129 continue
130 }
131 pc, err := toTestListPath(p.String(), pathForTestCases(ctx).String(), "host/testcases")
132 if err != nil {
133 ctx.Errorf("Failed to convert path: %s, %v", p.String(), err)
134 continue
135 }
136 buf.WriteString(pc)
137 buf.WriteString("\n")
138 }
139 outputFile := pathForPackaging(ctx, listFile)
140 WriteFileRuleVerbatim(ctx, outputFile, buf.String())
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700141 return outputFile
142}
Weijia He299d62d2024-04-18 22:55:10 +0000143
144func toTestListPath(path, relativeRoot, prefix string) (string, error) {
145 dest, err := filepath.Rel(relativeRoot, path)
146 if err != nil {
147 return "", err
148 }
149 return filepath.Join(prefix, dest), nil
150}
151
152func pathForPackaging(ctx PathContext, pathComponents ...string) OutputPath {
153 pathComponents = append([]string{"packaging"}, pathComponents...)
154 return PathForOutput(ctx, pathComponents...)
155}
156
157func pathForTestCases(ctx PathContext) InstallPath {
158 return pathForInstall(ctx, ctx.Config().BuildOS, X86, "testcases")
159}