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