defaults_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "reflect"
  17. "testing"
  18. "github.com/google/blueprint/proptools"
  19. )
  20. type defaultsTestProperties struct {
  21. Foo []string
  22. }
  23. type defaultsTestModule struct {
  24. ModuleBase
  25. DefaultableModuleBase
  26. properties defaultsTestProperties
  27. }
  28. func (d *defaultsTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
  29. ctx.Build(pctx, BuildParams{
  30. Rule: Touch,
  31. Output: PathForModuleOut(ctx, "out"),
  32. })
  33. }
  34. func defaultsTestModuleFactory() Module {
  35. module := &defaultsTestModule{}
  36. module.AddProperties(&module.properties)
  37. InitAndroidModule(module)
  38. InitDefaultableModule(module)
  39. return module
  40. }
  41. type defaultsTestDefaults struct {
  42. ModuleBase
  43. DefaultsModuleBase
  44. }
  45. func defaultsTestDefaultsFactory() Module {
  46. defaults := &defaultsTestDefaults{}
  47. defaults.AddProperties(&defaultsTestProperties{})
  48. InitDefaultsModule(defaults)
  49. return defaults
  50. }
  51. func TestDefaults(t *testing.T) {
  52. bp := `
  53. defaults {
  54. name: "transitive",
  55. foo: ["transitive"],
  56. }
  57. defaults {
  58. name: "defaults",
  59. defaults: ["transitive"],
  60. foo: ["defaults"],
  61. }
  62. test {
  63. name: "foo",
  64. defaults: ["defaults"],
  65. foo: ["module"],
  66. }
  67. `
  68. config := TestConfig(buildDir, nil, bp, nil)
  69. ctx := NewTestContext()
  70. ctx.RegisterModuleType("test", defaultsTestModuleFactory)
  71. ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory)
  72. ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
  73. ctx.Register(config)
  74. _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
  75. FailIfErrored(t, errs)
  76. _, errs = ctx.PrepareBuildActions(config)
  77. FailIfErrored(t, errs)
  78. foo := ctx.ModuleForTests("foo", "").Module().(*defaultsTestModule)
  79. if g, w := foo.properties.Foo, []string{"transitive", "defaults", "module"}; !reflect.DeepEqual(g, w) {
  80. t.Errorf("expected foo %q, got %q", w, g)
  81. }
  82. }
  83. func TestDefaultsAllowMissingDependencies(t *testing.T) {
  84. bp := `
  85. defaults {
  86. name: "defaults",
  87. defaults: ["missing"],
  88. foo: ["defaults"],
  89. }
  90. test {
  91. name: "missing_defaults",
  92. defaults: ["missing"],
  93. foo: ["module"],
  94. }
  95. test {
  96. name: "missing_transitive_defaults",
  97. defaults: ["defaults"],
  98. foo: ["module"],
  99. }
  100. `
  101. config := TestConfig(buildDir, nil, bp, nil)
  102. config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
  103. ctx := NewTestContext()
  104. ctx.SetAllowMissingDependencies(true)
  105. ctx.RegisterModuleType("test", defaultsTestModuleFactory)
  106. ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory)
  107. ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
  108. ctx.Register(config)
  109. _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
  110. FailIfErrored(t, errs)
  111. _, errs = ctx.PrepareBuildActions(config)
  112. FailIfErrored(t, errs)
  113. missingDefaults := ctx.ModuleForTests("missing_defaults", "").Output("out")
  114. missingTransitiveDefaults := ctx.ModuleForTests("missing_transitive_defaults", "").Output("out")
  115. if missingDefaults.Rule != ErrorRule {
  116. t.Errorf("expected missing_defaults rule to be ErrorRule, got %#v", missingDefaults.Rule)
  117. }
  118. if g, w := missingDefaults.Args["error"], "module missing_defaults missing dependencies: missing\n"; g != w {
  119. t.Errorf("want error %q, got %q", w, g)
  120. }
  121. // TODO: missing transitive defaults is currently not handled
  122. _ = missingTransitiveDefaults
  123. }