1// Copyright 2017 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 python
16
17import (
18	"android/soong/android"
19	"android/soong/tradefed"
20)
21
22// This file contains the module types for building Python test.
23
24func init() {
25	registerPythonTestComponents(android.InitRegistrationContext)
26}
27
28func registerPythonTestComponents(ctx android.RegistrationContext) {
29	ctx.RegisterModuleType("python_test_host", PythonTestHostFactory)
30	ctx.RegisterModuleType("python_test", PythonTestFactory)
31}
32
33// Test option struct.
34type TestOptions struct {
35	// If the test is a hostside(no device required) unittest that shall be run during presubmit check.
36	Unit_test *bool
37}
38
39type TestProperties struct {
40	// the name of the test configuration (for example "AndroidTest.xml") that should be
41	// installed with the module.
42	Test_config *string `android:"path,arch_variant"`
43
44	// the name of the test configuration template (for example "AndroidTestTemplate.xml") that
45	// should be installed with the module.
46	Test_config_template *string `android:"path,arch_variant"`
47
48	// list of files or filegroup modules that provide data that should be installed alongside
49	// the test
50	Data []string `android:"path,arch_variant"`
51
52	// list of java modules that provide data that should be installed alongside the test.
53	Java_data []string
54
55	// Test options.
56	Test_options TestOptions
57}
58
59type testDecorator struct {
60	*binaryDecorator
61
62	testProperties TestProperties
63
64	testConfig android.Path
65
66	data []android.DataPath
67}
68
69func (test *testDecorator) bootstrapperProps() []interface{} {
70	return append(test.binaryDecorator.bootstrapperProps(), &test.testProperties)
71}
72
73func (test *testDecorator) install(ctx android.ModuleContext, file android.Path) {
74	test.testConfig = tradefed.AutoGenPythonBinaryHostTestConfig(ctx, test.testProperties.Test_config,
75		test.testProperties.Test_config_template, test.binaryDecorator.binaryProperties.Test_suites,
76		test.binaryDecorator.binaryProperties.Auto_gen_config)
77
78	test.binaryDecorator.pythonInstaller.dir = "nativetest"
79	test.binaryDecorator.pythonInstaller.dir64 = "nativetest64"
80
81	test.binaryDecorator.pythonInstaller.relative = ctx.ModuleName()
82
83	test.binaryDecorator.pythonInstaller.install(ctx, file)
84
85	dataSrcPaths := android.PathsForModuleSrc(ctx, test.testProperties.Data)
86
87	for _, dataSrcPath := range dataSrcPaths {
88		test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
89	}
90
91	// Emulate the data property for java_data dependencies.
92	for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
93		for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") {
94			test.data = append(test.data, android.DataPath{SrcPath: javaDataSrcPath})
95		}
96	}
97}
98
99func NewTest(hod android.HostOrDeviceSupported) *Module {
100	module, binary := NewBinary(hod)
101
102	binary.pythonInstaller = NewPythonInstaller("nativetest", "nativetest64")
103
104	test := &testDecorator{binaryDecorator: binary}
105
106	module.bootstrapper = test
107	module.installer = test
108
109	return module
110}
111
112func PythonTestHostFactory() android.Module {
113	module := NewTest(android.HostSupportedNoCross)
114
115	return module.init()
116}
117
118func PythonTestFactory() android.Module {
119	module := NewTest(android.HostAndDeviceSupported)
120	module.multilib = android.MultilibBoth
121
122	return module.init()
123}
124