blob: f7618f3feaa9e3fe481dce8b13d78f81d87868c5 [file] [log] [blame]
Colin Cross7e129d22024-12-03 14:26:00 -08001// Copyright 2024 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
15package android
16
17import "testing"
18
19type testTransitionMutator struct {
20 split func(ctx BaseModuleContext) []string
21 outgoingTransition func(ctx OutgoingTransitionContext, sourceVariation string) string
22 incomingTransition func(ctx IncomingTransitionContext, incomingVariation string) string
23 mutate func(ctx BottomUpMutatorContext, variation string)
24}
25
26func (t *testTransitionMutator) Split(ctx BaseModuleContext) []string {
27 if t.split != nil {
28 return t.split(ctx)
29 }
30 return []string{""}
31}
32
33func (t *testTransitionMutator) OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string {
34 if t.outgoingTransition != nil {
35 return t.outgoingTransition(ctx, sourceVariation)
36 }
37 return sourceVariation
38}
39
40func (t *testTransitionMutator) IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string {
41 if t.incomingTransition != nil {
42 return t.incomingTransition(ctx, incomingVariation)
43 }
44 return incomingVariation
45}
46
47func (t *testTransitionMutator) Mutate(ctx BottomUpMutatorContext, variation string) {
48 if t.mutate != nil {
49 t.mutate(ctx, variation)
50 }
51}
52
53func TestModuleString(t *testing.T) {
54 bp := `
55 test {
56 name: "foo",
57 }
58 `
59
60 var moduleStrings []string
61
62 GroupFixturePreparers(
63 FixtureRegisterWithContext(func(ctx RegistrationContext) {
64
65 ctx.PreArchMutators(func(ctx RegisterMutatorsContext) {
66 ctx.Transition("pre_arch", &testTransitionMutator{
67 split: func(ctx BaseModuleContext) []string {
68 moduleStrings = append(moduleStrings, ctx.Module().String())
69 return []string{"a", "b"}
70 },
71 })
72 })
73
74 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
75 ctx.Transition("pre_deps", &testTransitionMutator{
76 split: func(ctx BaseModuleContext) []string {
77 moduleStrings = append(moduleStrings, ctx.Module().String())
78 return []string{"c", "d"}
79 },
80 })
81 })
82
83 ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) {
84 ctx.Transition("post_deps", &testTransitionMutator{
85 split: func(ctx BaseModuleContext) []string {
86 moduleStrings = append(moduleStrings, ctx.Module().String())
87 return []string{"e", "f"}
88 },
89 outgoingTransition: func(ctx OutgoingTransitionContext, sourceVariation string) string {
90 return ""
91 },
92 })
93 ctx.BottomUp("rename_bottom_up", func(ctx BottomUpMutatorContext) {
94 moduleStrings = append(moduleStrings, ctx.Module().String())
95 ctx.Rename(ctx.Module().base().Name() + "_renamed1")
96 }).UsesRename()
97 ctx.BottomUp("final", func(ctx BottomUpMutatorContext) {
98 moduleStrings = append(moduleStrings, ctx.Module().String())
99 })
100 })
101
102 ctx.RegisterModuleType("test", mutatorTestModuleFactory)
103 }),
104 FixtureWithRootAndroidBp(bp),
105 ).RunTest(t)
106
107 want := []string{
108 // Initial name.
109 "foo{}",
110
111 // After pre_arch (reversed because rename_top_down is TopDown so it visits in reverse order).
112 "foo{pre_arch:b}",
113 "foo{pre_arch:a}",
114
115 // After pre_deps (reversed because post_deps TransitionMutator.Split is TopDown).
116 "foo{pre_arch:b,pre_deps:d}",
117 "foo{pre_arch:b,pre_deps:c}",
118 "foo{pre_arch:a,pre_deps:d}",
119 "foo{pre_arch:a,pre_deps:c}",
120
121 // After post_deps.
122 "foo{pre_arch:a,pre_deps:c,post_deps:e}",
123 "foo{pre_arch:a,pre_deps:c,post_deps:f}",
124 "foo{pre_arch:a,pre_deps:d,post_deps:e}",
125 "foo{pre_arch:a,pre_deps:d,post_deps:f}",
126 "foo{pre_arch:b,pre_deps:c,post_deps:e}",
127 "foo{pre_arch:b,pre_deps:c,post_deps:f}",
128 "foo{pre_arch:b,pre_deps:d,post_deps:e}",
129 "foo{pre_arch:b,pre_deps:d,post_deps:f}",
130
131 // After rename_bottom_up.
132 "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:e}",
133 "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:f}",
134 "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:e}",
135 "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:f}",
136 "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:e}",
137 "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:f}",
138 "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:e}",
139 "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:f}",
140 }
141
142 AssertDeepEquals(t, "module String() values", want, moduleStrings)
143}
144
145func TestTransitionMutatorInFinalDeps(t *testing.T) {
146 GroupFixturePreparers(
147 FixtureRegisterWithContext(func(ctx RegistrationContext) {
148 ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) {
149 ctx.Transition("vars", &testTransitionMutator{
150 split: func(ctx BaseModuleContext) []string {
151 return []string{"a", "b"}
152 },
153 })
154 })
155
156 ctx.RegisterModuleType("test", mutatorTestModuleFactory)
157 }),
158 FixtureWithRootAndroidBp(`test {name: "foo"}`),
159 ).
160 ExtendWithErrorHandler(FixtureExpectsOneErrorPattern("not allowed in FinalDepsMutators")).
161 RunTest(t)
162}