module_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. "testing"
  17. )
  18. func TestSrcIsModule(t *testing.T) {
  19. type args struct {
  20. s string
  21. }
  22. tests := []struct {
  23. name string
  24. args args
  25. wantModule string
  26. }{
  27. {
  28. name: "file",
  29. args: args{
  30. s: "foo",
  31. },
  32. wantModule: "",
  33. },
  34. {
  35. name: "module",
  36. args: args{
  37. s: ":foo",
  38. },
  39. wantModule: "foo",
  40. },
  41. {
  42. name: "tag",
  43. args: args{
  44. s: ":foo{.bar}",
  45. },
  46. wantModule: "foo{.bar}",
  47. },
  48. {
  49. name: "extra colon",
  50. args: args{
  51. s: ":foo:bar",
  52. },
  53. wantModule: "foo:bar",
  54. },
  55. }
  56. for _, tt := range tests {
  57. t.Run(tt.name, func(t *testing.T) {
  58. if gotModule := SrcIsModule(tt.args.s); gotModule != tt.wantModule {
  59. t.Errorf("SrcIsModule() = %v, want %v", gotModule, tt.wantModule)
  60. }
  61. })
  62. }
  63. }
  64. func TestSrcIsModuleWithTag(t *testing.T) {
  65. type args struct {
  66. s string
  67. }
  68. tests := []struct {
  69. name string
  70. args args
  71. wantModule string
  72. wantTag string
  73. }{
  74. {
  75. name: "file",
  76. args: args{
  77. s: "foo",
  78. },
  79. wantModule: "",
  80. wantTag: "",
  81. },
  82. {
  83. name: "module",
  84. args: args{
  85. s: ":foo",
  86. },
  87. wantModule: "foo",
  88. wantTag: "",
  89. },
  90. {
  91. name: "tag",
  92. args: args{
  93. s: ":foo{.bar}",
  94. },
  95. wantModule: "foo",
  96. wantTag: ".bar",
  97. },
  98. {
  99. name: "empty tag",
  100. args: args{
  101. s: ":foo{}",
  102. },
  103. wantModule: "foo",
  104. wantTag: "",
  105. },
  106. {
  107. name: "extra colon",
  108. args: args{
  109. s: ":foo:bar",
  110. },
  111. wantModule: "foo:bar",
  112. },
  113. {
  114. name: "invalid tag",
  115. args: args{
  116. s: ":foo{.bar",
  117. },
  118. wantModule: "foo{.bar",
  119. },
  120. {
  121. name: "invalid tag 2",
  122. args: args{
  123. s: ":foo.bar}",
  124. },
  125. wantModule: "foo.bar}",
  126. },
  127. }
  128. for _, tt := range tests {
  129. t.Run(tt.name, func(t *testing.T) {
  130. gotModule, gotTag := SrcIsModuleWithTag(tt.args.s)
  131. if gotModule != tt.wantModule {
  132. t.Errorf("SrcIsModuleWithTag() gotModule = %v, want %v", gotModule, tt.wantModule)
  133. }
  134. if gotTag != tt.wantTag {
  135. t.Errorf("SrcIsModuleWithTag() gotTag = %v, want %v", gotTag, tt.wantTag)
  136. }
  137. })
  138. }
  139. }
  140. type depsModule struct {
  141. ModuleBase
  142. props struct {
  143. Deps []string
  144. }
  145. }
  146. func (m *depsModule) GenerateAndroidBuildActions(ctx ModuleContext) {
  147. }
  148. func (m *depsModule) DepsMutator(ctx BottomUpMutatorContext) {
  149. ctx.AddDependency(ctx.Module(), nil, m.props.Deps...)
  150. }
  151. func depsModuleFactory() Module {
  152. m := &depsModule{}
  153. m.AddProperties(&m.props)
  154. InitAndroidModule(m)
  155. return m
  156. }
  157. func TestErrorDependsOnDisabledModule(t *testing.T) {
  158. ctx := NewTestContext()
  159. ctx.RegisterModuleType("deps", depsModuleFactory)
  160. bp := `
  161. deps {
  162. name: "foo",
  163. deps: ["bar"],
  164. }
  165. deps {
  166. name: "bar",
  167. enabled: false,
  168. }
  169. `
  170. config := TestConfig(buildDir, nil, bp, nil)
  171. ctx.Register(config)
  172. _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
  173. FailIfErrored(t, errs)
  174. _, errs = ctx.PrepareBuildActions(config)
  175. FailIfNoMatchingErrors(t, `module "foo": depends on disabled module "bar"`, errs)
  176. }