snapshot_etc_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2021 The Android Open Source Project
  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 etc
  15. import (
  16. "android/soong/android"
  17. "testing"
  18. "github.com/google/blueprint"
  19. )
  20. var registerSourceModule = func(ctx android.RegistrationContext) {
  21. ctx.RegisterModuleType("source", newSourceModule)
  22. }
  23. type sourceModuleProperties struct {
  24. Deps []string `android:"path,arch_variant"`
  25. }
  26. type sourceModule struct {
  27. android.ModuleBase
  28. android.OverridableModuleBase
  29. properties sourceModuleProperties
  30. dependsOnSourceModule, dependsOnPrebuiltModule bool
  31. deps android.Paths
  32. src android.Path
  33. }
  34. func newSourceModule() android.Module {
  35. m := &sourceModule{}
  36. m.AddProperties(&m.properties)
  37. android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibFirst)
  38. android.InitOverridableModule(m, nil)
  39. return m
  40. }
  41. func (s *sourceModule) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
  42. // s.properties.Deps are annotated with android:path, so they are
  43. // automatically added to the dependency by pathDeps mutator
  44. }
  45. func (s *sourceModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  46. s.deps = android.PathsForModuleSrc(ctx, s.properties.Deps)
  47. s.src = android.PathForModuleSrc(ctx, "source_file")
  48. }
  49. func (s *sourceModule) Srcs() android.Paths {
  50. return android.Paths{s.src}
  51. }
  52. var prepareForSnapshotEtcTest = android.GroupFixturePreparers(
  53. android.PrepareForTestWithArchMutator,
  54. android.PrepareForTestWithPrebuilts,
  55. PrepareForTestWithPrebuiltEtc,
  56. android.FixtureRegisterWithContext(RegisterSnapshotEtcModule),
  57. android.FixtureRegisterWithContext(registerSourceModule),
  58. android.FixtureMergeMockFs(android.MockFS{
  59. "foo.conf": nil,
  60. "bar.conf": nil,
  61. }),
  62. )
  63. func TestSnapshotWithFilename(t *testing.T) {
  64. var androidBp = `
  65. snapshot_etc {
  66. name: "etc_module",
  67. src: "foo.conf",
  68. filename: "bar.conf",
  69. }
  70. `
  71. result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp)
  72. for _, variant := range result.ModuleVariantsForTests("etc_module") {
  73. module := result.ModuleForTests("etc_module", variant)
  74. s, ok := module.Module().(*SnapshotEtc)
  75. if !ok {
  76. t.Errorf("Expected snapshot_etc module type")
  77. }
  78. if s.outputFilePath.Base() != "bar.conf" {
  79. t.Errorf("Output file path does not match with specified filename")
  80. }
  81. }
  82. }
  83. func TestSnapshotEtcWithOrigin(t *testing.T) {
  84. var androidBp = `
  85. prebuilt_etc {
  86. name: "etc_module",
  87. src: "foo.conf",
  88. }
  89. snapshot_etc {
  90. name: "etc_module",
  91. src: "bar.conf",
  92. }
  93. source {
  94. name: "source",
  95. deps: [":etc_module"],
  96. }
  97. `
  98. result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp)
  99. for _, variant := range result.ModuleVariantsForTests("source") {
  100. source := result.ModuleForTests("source", variant)
  101. result.VisitDirectDeps(source.Module(), func(m blueprint.Module) {
  102. if _, ok := m.(*PrebuiltEtc); !ok {
  103. t.Errorf("Original prebuilt_etc module expected.")
  104. }
  105. })
  106. }
  107. }
  108. func TestSnapshotEtcWithOriginAndPrefer(t *testing.T) {
  109. var androidBp = `
  110. prebuilt_etc {
  111. name: "etc_module",
  112. src: "foo.conf",
  113. }
  114. snapshot_etc {
  115. name: "etc_module",
  116. src: "bar.conf",
  117. prefer: true,
  118. }
  119. source {
  120. name: "source",
  121. deps: [":etc_module"],
  122. }
  123. `
  124. result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp)
  125. for _, variant := range result.ModuleVariantsForTests("source") {
  126. source := result.ModuleForTests("source", variant)
  127. result.VisitDirectDeps(source.Module(), func(m blueprint.Module) {
  128. if _, ok := m.(*SnapshotEtc); !ok {
  129. t.Errorf("Preferred snapshot_etc module expected.")
  130. }
  131. })
  132. }
  133. }
  134. func TestSnapshotEtcWithoutOrigin(t *testing.T) {
  135. var androidBp = `
  136. snapshot_etc {
  137. name: "etc_module",
  138. src: "bar.conf",
  139. }
  140. source {
  141. name: "source",
  142. deps: [":etc_module"],
  143. }
  144. `
  145. result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp)
  146. for _, variant := range result.ModuleVariantsForTests("source") {
  147. source := result.ModuleForTests("source", variant)
  148. result.VisitDirectDeps(source.Module(), func(m blueprint.Module) {
  149. if _, ok := m.(*SnapshotEtc); !ok {
  150. t.Errorf("Only source snapshot_etc module expected.")
  151. }
  152. })
  153. }
  154. }