singleton_module_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2021 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. "strings"
  18. "testing"
  19. )
  20. type testSingletonModule struct {
  21. SingletonModuleBase
  22. ops []string
  23. }
  24. func (tsm *testSingletonModule) GenerateAndroidBuildActions(ctx ModuleContext) {
  25. tsm.ops = append(tsm.ops, "GenerateAndroidBuildActions")
  26. }
  27. func (tsm *testSingletonModule) GenerateSingletonBuildActions(ctx SingletonContext) {
  28. tsm.ops = append(tsm.ops, "GenerateSingletonBuildActions")
  29. }
  30. func (tsm *testSingletonModule) MakeVars(ctx MakeVarsContext) {
  31. tsm.ops = append(tsm.ops, "MakeVars")
  32. }
  33. func testSingletonModuleFactory() SingletonModule {
  34. tsm := &testSingletonModule{}
  35. InitAndroidSingletonModule(tsm)
  36. return tsm
  37. }
  38. func runSingletonModuleTest(bp string) (*TestContext, []error) {
  39. config := TestConfig(buildDir, nil, bp, nil)
  40. // Enable Kati output to test SingletonModules with MakeVars.
  41. config.katiEnabled = true
  42. ctx := NewTestContext(config)
  43. ctx.RegisterSingletonModuleType("test_singleton_module", testSingletonModuleFactory)
  44. ctx.RegisterSingletonType("makevars", makeVarsSingletonFunc)
  45. ctx.Register()
  46. _, errs := ctx.ParseBlueprintsFiles("Android.bp")
  47. if len(errs) > 0 {
  48. return ctx, errs
  49. }
  50. _, errs = ctx.PrepareBuildActions(config)
  51. return ctx, errs
  52. }
  53. func TestSingletonModule(t *testing.T) {
  54. bp := `
  55. test_singleton_module {
  56. name: "test_singleton_module",
  57. }
  58. `
  59. ctx, errs := runSingletonModuleTest(bp)
  60. if len(errs) > 0 {
  61. t.Fatal(errs)
  62. }
  63. ops := ctx.ModuleForTests("test_singleton_module", "").Module().(*testSingletonModule).ops
  64. wantOps := []string{"GenerateAndroidBuildActions", "GenerateSingletonBuildActions", "MakeVars"}
  65. if !reflect.DeepEqual(ops, wantOps) {
  66. t.Errorf("Expected operations %q, got %q", wantOps, ops)
  67. }
  68. }
  69. func TestDuplicateSingletonModule(t *testing.T) {
  70. bp := `
  71. test_singleton_module {
  72. name: "test_singleton_module",
  73. }
  74. test_singleton_module {
  75. name: "test_singleton_module2",
  76. }
  77. `
  78. _, errs := runSingletonModuleTest(bp)
  79. if len(errs) == 0 {
  80. t.Fatal("expected duplicate SingletonModule error")
  81. }
  82. if len(errs) != 1 || !strings.Contains(errs[0].Error(), `Duplicate SingletonModule "test_singleton_module", previously used in`) {
  83. t.Fatalf("expected duplicate SingletonModule error, got %q", errs)
  84. }
  85. }
  86. func TestUnusedSingletonModule(t *testing.T) {
  87. bp := ``
  88. ctx, errs := runSingletonModuleTest(bp)
  89. if len(errs) > 0 {
  90. t.Fatal(errs)
  91. }
  92. singleton := ctx.SingletonForTests("test_singleton_module").Singleton()
  93. sm := singleton.(*singletonModuleSingletonAdaptor).sm
  94. ops := sm.(*testSingletonModule).ops
  95. if ops != nil {
  96. t.Errorf("Expected no operations, got %q", ops)
  97. }
  98. }
  99. func testVariantSingletonModuleMutator(ctx BottomUpMutatorContext) {
  100. if _, ok := ctx.Module().(*testSingletonModule); ok {
  101. ctx.CreateVariations("a", "b")
  102. }
  103. }
  104. func TestVariantSingletonModule(t *testing.T) {
  105. bp := `
  106. test_singleton_module {
  107. name: "test_singleton_module",
  108. }
  109. `
  110. config := TestConfig(buildDir, nil, bp, nil)
  111. ctx := NewTestContext(config)
  112. ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
  113. ctx.BottomUp("test_singleton_module_mutator", testVariantSingletonModuleMutator)
  114. })
  115. ctx.RegisterSingletonModuleType("test_singleton_module", testSingletonModuleFactory)
  116. ctx.Register()
  117. _, errs := ctx.ParseBlueprintsFiles("Android.bp")
  118. if len(errs) == 0 {
  119. _, errs = ctx.PrepareBuildActions(config)
  120. }
  121. if len(errs) == 0 {
  122. t.Fatal("expected duplicate SingletonModule error")
  123. }
  124. if len(errs) != 1 || !strings.Contains(errs[0].Error(), `GenerateAndroidBuildActions already called for variant`) {
  125. t.Fatalf("expected duplicate SingletonModule error, got %q", errs)
  126. }
  127. }