path_properties_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2019 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. "testing"
  17. )
  18. type pathDepsMutatorTestModule struct {
  19. ModuleBase
  20. props struct {
  21. Foo string `android:"path"`
  22. Bar []string `android:"path,arch_variant"`
  23. Baz *string `android:"path"`
  24. Qux string
  25. V *struct {
  26. W string `android:"path"`
  27. }
  28. }
  29. // A second property struct with a duplicate property name
  30. props2 struct {
  31. Foo string `android:"path"`
  32. }
  33. // nested slices of struct
  34. props3 struct {
  35. X []struct {
  36. Y []struct {
  37. Z []string `android:"path"`
  38. }
  39. }
  40. }
  41. sourceDeps []string
  42. }
  43. func pathDepsMutatorTestModuleFactory() Module {
  44. module := &pathDepsMutatorTestModule{}
  45. module.AddProperties(&module.props, &module.props2, &module.props3)
  46. InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
  47. return module
  48. }
  49. func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
  50. ctx.VisitDirectDeps(func(dep Module) {
  51. if _, ok := ctx.OtherModuleDependencyTag(dep).(sourceOrOutputDependencyTag); ok {
  52. p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep))
  53. }
  54. })
  55. if p.props.Foo != "" {
  56. // Make sure there is only one dependency on a module listed in a property present in multiple property structs
  57. m := SrcIsModule(p.props.Foo)
  58. if GetModuleFromPathDep(ctx, m, "") == nil {
  59. ctx.ModuleErrorf("GetDirectDepWithTag failed")
  60. }
  61. }
  62. }
  63. func TestPathDepsMutator(t *testing.T) {
  64. tests := []struct {
  65. name string
  66. bp string
  67. deps []string
  68. }{
  69. {
  70. name: "all",
  71. bp: `
  72. test {
  73. name: "foo",
  74. foo: ":a",
  75. bar: [":b"],
  76. baz: ":c{.bar}",
  77. qux: ":d",
  78. x: [
  79. {
  80. y: [
  81. {
  82. z: [":x", ":y"],
  83. },
  84. {
  85. z: [":z"],
  86. },
  87. ],
  88. },
  89. ],
  90. v: {
  91. w: ":w",
  92. },
  93. }`,
  94. deps: []string{"a", "b", "c", "w", "x", "y", "z"},
  95. },
  96. {
  97. name: "arch variant",
  98. bp: `
  99. test {
  100. name: "foo",
  101. arch: {
  102. arm64: {
  103. bar: [":a"],
  104. },
  105. arm: {
  106. bar: [":b"],
  107. },
  108. },
  109. bar: [":c"],
  110. }`,
  111. deps: []string{"c", "a"},
  112. },
  113. }
  114. for _, test := range tests {
  115. t.Run(test.name, func(t *testing.T) {
  116. bp := test.bp + `
  117. filegroup {
  118. name: "a",
  119. }
  120. filegroup {
  121. name: "b",
  122. }
  123. filegroup {
  124. name: "c",
  125. }
  126. filegroup {
  127. name: "d",
  128. }
  129. filegroup {
  130. name: "w",
  131. }
  132. filegroup {
  133. name: "x",
  134. }
  135. filegroup {
  136. name: "y",
  137. }
  138. filegroup {
  139. name: "z",
  140. }
  141. `
  142. result := GroupFixturePreparers(
  143. PrepareForTestWithArchMutator,
  144. PrepareForTestWithFilegroup,
  145. FixtureRegisterWithContext(func(ctx RegistrationContext) {
  146. ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory)
  147. }),
  148. FixtureWithRootAndroidBp(bp),
  149. ).RunTest(t)
  150. m := result.Module("foo", "android_arm64_armv8-a").(*pathDepsMutatorTestModule)
  151. AssertDeepEquals(t, "deps", test.deps, m.sourceDeps)
  152. })
  153. }
  154. }