mutator_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Copyright 2015 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 android
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. "github.com/google/blueprint"
  21. )
  22. type mutatorTestModule struct {
  23. ModuleBase
  24. props struct {
  25. Deps_missing_deps []string
  26. Mutator_missing_deps []string
  27. }
  28. missingDeps []string
  29. }
  30. func mutatorTestModuleFactory() Module {
  31. module := &mutatorTestModule{}
  32. module.AddProperties(&module.props)
  33. InitAndroidModule(module)
  34. return module
  35. }
  36. func (m *mutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
  37. ctx.Build(pctx, BuildParams{
  38. Rule: Touch,
  39. Output: PathForModuleOut(ctx, "output"),
  40. })
  41. m.missingDeps = ctx.GetMissingDependencies()
  42. }
  43. func (m *mutatorTestModule) DepsMutator(ctx BottomUpMutatorContext) {
  44. ctx.AddDependency(ctx.Module(), nil, m.props.Deps_missing_deps...)
  45. }
  46. func addMissingDependenciesMutator(ctx TopDownMutatorContext) {
  47. ctx.AddMissingDependencies(ctx.Module().(*mutatorTestModule).props.Mutator_missing_deps)
  48. }
  49. func TestMutatorAddMissingDependencies(t *testing.T) {
  50. bp := `
  51. test {
  52. name: "foo",
  53. deps_missing_deps: ["regular_missing_dep"],
  54. mutator_missing_deps: ["added_missing_dep"],
  55. }
  56. `
  57. result := GroupFixturePreparers(
  58. PrepareForTestWithAllowMissingDependencies,
  59. FixtureRegisterWithContext(func(ctx RegistrationContext) {
  60. ctx.RegisterModuleType("test", mutatorTestModuleFactory)
  61. ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
  62. ctx.TopDown("add_missing_dependencies", addMissingDependenciesMutator)
  63. })
  64. }),
  65. FixtureWithRootAndroidBp(bp),
  66. ).RunTest(t)
  67. foo := result.ModuleForTests("foo", "").Module().(*mutatorTestModule)
  68. AssertDeepEquals(t, "foo missing deps", []string{"added_missing_dep", "regular_missing_dep"}, foo.missingDeps)
  69. }
  70. func TestModuleString(t *testing.T) {
  71. bp := `
  72. test {
  73. name: "foo",
  74. }
  75. `
  76. var moduleStrings []string
  77. GroupFixturePreparers(
  78. FixtureRegisterWithContext(func(ctx RegistrationContext) {
  79. ctx.PreArchMutators(func(ctx RegisterMutatorsContext) {
  80. ctx.BottomUp("pre_arch", func(ctx BottomUpMutatorContext) {
  81. moduleStrings = append(moduleStrings, ctx.Module().String())
  82. ctx.CreateVariations("a", "b")
  83. })
  84. ctx.TopDown("rename_top_down", func(ctx TopDownMutatorContext) {
  85. moduleStrings = append(moduleStrings, ctx.Module().String())
  86. ctx.Rename(ctx.Module().base().Name() + "_renamed1")
  87. })
  88. })
  89. ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
  90. ctx.BottomUp("pre_deps", func(ctx BottomUpMutatorContext) {
  91. moduleStrings = append(moduleStrings, ctx.Module().String())
  92. ctx.CreateVariations("c", "d")
  93. })
  94. })
  95. ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) {
  96. ctx.BottomUp("post_deps", func(ctx BottomUpMutatorContext) {
  97. moduleStrings = append(moduleStrings, ctx.Module().String())
  98. ctx.CreateLocalVariations("e", "f")
  99. })
  100. ctx.BottomUp("rename_bottom_up", func(ctx BottomUpMutatorContext) {
  101. moduleStrings = append(moduleStrings, ctx.Module().String())
  102. ctx.Rename(ctx.Module().base().Name() + "_renamed2")
  103. })
  104. ctx.BottomUp("final", func(ctx BottomUpMutatorContext) {
  105. moduleStrings = append(moduleStrings, ctx.Module().String())
  106. })
  107. })
  108. ctx.RegisterModuleType("test", mutatorTestModuleFactory)
  109. }),
  110. FixtureWithRootAndroidBp(bp),
  111. ).RunTest(t)
  112. want := []string{
  113. // Initial name.
  114. "foo{}",
  115. // After pre_arch (reversed because rename_top_down is TopDown so it visits in reverse order).
  116. "foo{pre_arch:b}",
  117. "foo{pre_arch:a}",
  118. // After rename_top_down.
  119. "foo_renamed1{pre_arch:a}",
  120. "foo_renamed1{pre_arch:b}",
  121. // After pre_deps.
  122. "foo_renamed1{pre_arch:a,pre_deps:c}",
  123. "foo_renamed1{pre_arch:a,pre_deps:d}",
  124. "foo_renamed1{pre_arch:b,pre_deps:c}",
  125. "foo_renamed1{pre_arch:b,pre_deps:d}",
  126. // After post_deps.
  127. "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:e}",
  128. "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:f}",
  129. "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:e}",
  130. "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:f}",
  131. "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:e}",
  132. "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:f}",
  133. "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:e}",
  134. "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:f}",
  135. // After rename_bottom_up.
  136. "foo_renamed2{pre_arch:a,pre_deps:c,post_deps:e}",
  137. "foo_renamed2{pre_arch:a,pre_deps:c,post_deps:f}",
  138. "foo_renamed2{pre_arch:a,pre_deps:d,post_deps:e}",
  139. "foo_renamed2{pre_arch:a,pre_deps:d,post_deps:f}",
  140. "foo_renamed2{pre_arch:b,pre_deps:c,post_deps:e}",
  141. "foo_renamed2{pre_arch:b,pre_deps:c,post_deps:f}",
  142. "foo_renamed2{pre_arch:b,pre_deps:d,post_deps:e}",
  143. "foo_renamed2{pre_arch:b,pre_deps:d,post_deps:f}",
  144. }
  145. AssertDeepEquals(t, "module String() values", want, moduleStrings)
  146. }
  147. func TestFinalDepsPhase(t *testing.T) {
  148. bp := `
  149. test {
  150. name: "common_dep_1",
  151. }
  152. test {
  153. name: "common_dep_2",
  154. }
  155. test {
  156. name: "foo",
  157. }
  158. `
  159. finalGot := map[string]int{}
  160. GroupFixturePreparers(
  161. FixtureRegisterWithContext(func(ctx RegistrationContext) {
  162. dep1Tag := struct {
  163. blueprint.BaseDependencyTag
  164. }{}
  165. dep2Tag := struct {
  166. blueprint.BaseDependencyTag
  167. }{}
  168. ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) {
  169. ctx.BottomUp("far_deps_1", func(ctx BottomUpMutatorContext) {
  170. if !strings.HasPrefix(ctx.ModuleName(), "common_dep") {
  171. ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep1Tag, "common_dep_1")
  172. }
  173. })
  174. ctx.BottomUp("variant", func(ctx BottomUpMutatorContext) {
  175. ctx.CreateLocalVariations("a", "b")
  176. })
  177. })
  178. ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) {
  179. ctx.BottomUp("far_deps_2", func(ctx BottomUpMutatorContext) {
  180. if !strings.HasPrefix(ctx.ModuleName(), "common_dep") {
  181. ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep2Tag, "common_dep_2")
  182. }
  183. })
  184. ctx.BottomUp("final", func(ctx BottomUpMutatorContext) {
  185. finalGot[ctx.Module().String()] += 1
  186. ctx.VisitDirectDeps(func(mod Module) {
  187. finalGot[fmt.Sprintf("%s -> %s", ctx.Module().String(), mod)] += 1
  188. })
  189. })
  190. })
  191. ctx.RegisterModuleType("test", mutatorTestModuleFactory)
  192. }),
  193. FixtureWithRootAndroidBp(bp),
  194. ).RunTest(t)
  195. finalWant := map[string]int{
  196. "common_dep_1{variant:a}": 1,
  197. "common_dep_1{variant:b}": 1,
  198. "common_dep_2{variant:a}": 1,
  199. "common_dep_2{variant:b}": 1,
  200. "foo{variant:a}": 1,
  201. "foo{variant:a} -> common_dep_1{variant:a}": 1,
  202. "foo{variant:a} -> common_dep_2{variant:a}": 1,
  203. "foo{variant:b}": 1,
  204. "foo{variant:b} -> common_dep_1{variant:b}": 1,
  205. "foo{variant:b} -> common_dep_2{variant:a}": 1,
  206. }
  207. AssertDeepEquals(t, "final", finalWant, finalGot)
  208. }
  209. func TestNoCreateVariationsInFinalDeps(t *testing.T) {
  210. checkErr := func() {
  211. if err := recover(); err == nil || !strings.Contains(fmt.Sprintf("%s", err), "not allowed in FinalDepsMutators") {
  212. panic("Expected FinalDepsMutators consistency check to fail")
  213. }
  214. }
  215. GroupFixturePreparers(
  216. FixtureRegisterWithContext(func(ctx RegistrationContext) {
  217. ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) {
  218. ctx.BottomUp("vars", func(ctx BottomUpMutatorContext) {
  219. defer checkErr()
  220. ctx.CreateVariations("a", "b")
  221. })
  222. ctx.BottomUp("local_vars", func(ctx BottomUpMutatorContext) {
  223. defer checkErr()
  224. ctx.CreateLocalVariations("a", "b")
  225. })
  226. })
  227. ctx.RegisterModuleType("test", mutatorTestModuleFactory)
  228. }),
  229. FixtureWithRootAndroidBp(`test {name: "foo"}`),
  230. ).RunTest(t)
  231. }
  232. func TestConvertApexAvailableToTags(t *testing.T) {
  233. input := []string{
  234. "com.android.adbd",
  235. "//apex_available:platform",
  236. }
  237. actual := ConvertApexAvailableToTags(input)
  238. expected := []string{
  239. "apex_available=com.android.adbd",
  240. "apex_available=//apex_available:platform",
  241. }
  242. if !reflect.DeepEqual(actual, expected) {
  243. t.Errorf("Expected: %v, actual: %v", expected, actual)
  244. }
  245. if ConvertApexAvailableToTags(nil) != nil {
  246. t.Errorf("Expected providing nil to return nil")
  247. }
  248. }