test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. package python
  15. import (
  16. "github.com/google/blueprint/proptools"
  17. "android/soong/android"
  18. "android/soong/tradefed"
  19. )
  20. // This file contains the module types for building Python test.
  21. func init() {
  22. registerPythonTestComponents(android.InitRegistrationContext)
  23. }
  24. func registerPythonTestComponents(ctx android.RegistrationContext) {
  25. ctx.RegisterModuleType("python_test_host", PythonTestHostFactory)
  26. ctx.RegisterModuleType("python_test", PythonTestFactory)
  27. }
  28. func NewTest(hod android.HostOrDeviceSupported) *PythonTestModule {
  29. return &PythonTestModule{PythonBinaryModule: *NewBinary(hod)}
  30. }
  31. func PythonTestHostFactory() android.Module {
  32. return NewTest(android.HostSupportedNoCross).init()
  33. }
  34. func PythonTestFactory() android.Module {
  35. module := NewTest(android.HostAndDeviceSupported)
  36. module.multilib = android.MultilibBoth
  37. return module.init()
  38. }
  39. type 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. // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
  44. // should be installed with the module.
  45. Test_config_template *string `android:"path,arch_variant"`
  46. // list of files or filegroup modules that provide data that should be installed alongside
  47. // the test
  48. Data []string `android:"path,arch_variant"`
  49. // list of java modules that provide data that should be installed alongside the test.
  50. Java_data []string
  51. // Test options.
  52. Test_options android.CommonTestOptions
  53. }
  54. type PythonTestModule struct {
  55. PythonBinaryModule
  56. testProperties TestProperties
  57. testConfig android.Path
  58. data []android.DataPath
  59. }
  60. func (p *PythonTestModule) init() android.Module {
  61. p.AddProperties(&p.properties, &p.protoProperties)
  62. p.AddProperties(&p.binaryProperties)
  63. p.AddProperties(&p.testProperties)
  64. android.InitAndroidArchModule(p, p.hod, p.multilib)
  65. android.InitDefaultableModule(p)
  66. android.InitBazelModule(p)
  67. if p.hod == android.HostSupportedNoCross && p.testProperties.Test_options.Unit_test == nil {
  68. p.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
  69. }
  70. return p
  71. }
  72. func (p *PythonTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  73. // We inherit from only the library's GenerateAndroidBuildActions, and then
  74. // just use buildBinary() so that the binary is not installed into the location
  75. // it would be for regular binaries.
  76. p.PythonLibraryModule.GenerateAndroidBuildActions(ctx)
  77. p.buildBinary(ctx)
  78. p.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
  79. TestConfigProp: p.testProperties.Test_config,
  80. TestConfigTemplateProp: p.testProperties.Test_config_template,
  81. TestSuites: p.binaryProperties.Test_suites,
  82. AutoGenConfig: p.binaryProperties.Auto_gen_config,
  83. DeviceTemplate: "${PythonBinaryHostTestConfigTemplate}",
  84. HostTemplate: "${PythonBinaryHostTestConfigTemplate}",
  85. })
  86. p.installedDest = ctx.InstallFile(installDir(ctx, "nativetest", "nativetest64", ctx.ModuleName()), p.installSource.Base(), p.installSource)
  87. for _, dataSrcPath := range android.PathsForModuleSrc(ctx, p.testProperties.Data) {
  88. p.data = append(p.data, android.DataPath{SrcPath: dataSrcPath})
  89. }
  90. // Emulate the data property for java_data dependencies.
  91. for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
  92. for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") {
  93. p.data = append(p.data, android.DataPath{SrcPath: javaDataSrcPath})
  94. }
  95. }
  96. }
  97. func (p *PythonTestModule) AndroidMkEntries() []android.AndroidMkEntries {
  98. entriesList := p.PythonBinaryModule.AndroidMkEntries()
  99. if len(entriesList) != 1 {
  100. panic("Expected 1 entry")
  101. }
  102. entries := &entriesList[0]
  103. entries.Class = "NATIVE_TESTS"
  104. entries.ExtraEntries = append(entries.ExtraEntries,
  105. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  106. //entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...)
  107. if p.testConfig != nil {
  108. entries.SetString("LOCAL_FULL_TEST_CONFIG", p.testConfig.String())
  109. }
  110. entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(p.binaryProperties.Auto_gen_config, true))
  111. entries.AddStrings("LOCAL_TEST_DATA", android.AndroidMkDataPaths(p.data)...)
  112. p.testProperties.Test_options.SetAndroidMkEntries(entries)
  113. })
  114. return entriesList
  115. }