From 795c377e1450ad8e88deaaa5365ff1cc90c9f922 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 16 Mar 2017 16:50:10 -0700 Subject: Use a minimal set of mutators, module types, and singletons for tests Calling android.NewContext() in tests results in a context that contains all the mutators, module types, and singletons, which causes unexpected interactions in unit tests. Create an empty context instead, and add in only the necessary mutators, module types, and singletons. Bug: 36366816 Test: soong tests Change-Id: Ic61262c37e3436b3ad4ccaca18b737021c304be6 --- android/mutator.go | 82 ++++++++++++++++++++++++++++++++++++++--------------- android/register.go | 19 ++----------- 2 files changed, 61 insertions(+), 40 deletions(-) (limited to 'android') diff --git a/android/mutator.go b/android/mutator.go index 34202807..940b0ffb 100644 --- a/android/mutator.go +++ b/android/mutator.go @@ -14,7 +14,11 @@ package android -import "github.com/google/blueprint" +import ( + "sync" + + "github.com/google/blueprint" +) // Mutator phases: // Pre-arch @@ -23,36 +27,68 @@ import "github.com/google/blueprint" // Deps // PostDeps -func registerMutators() { - ctx := registerMutatorsContext{} +var registerMutatorsOnce sync.Once +var registeredMutators []*mutator - register := func(funcs []RegisterMutatorFunc) { - for _, f := range funcs { - f(ctx) +func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) { + for _, t := range mutators { + var handle blueprint.MutatorHandle + if t.bottomUpMutator != nil { + handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator) + } else if t.topDownMutator != nil { + handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator) + } + if t.parallel { + handle.Parallel() } } +} + +func registerMutators(ctx *blueprint.Context) { - ctx.TopDown("load_hooks", loadHookMutator).Parallel() - ctx.BottomUp("prebuilts", prebuiltMutator).Parallel() - ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel() - ctx.TopDown("defaults", defaultsMutator).Parallel() + registerMutatorsOnce.Do(func() { + ctx := ®isterMutatorsContext{} + + register := func(funcs []RegisterMutatorFunc) { + for _, f := range funcs { + f(ctx) + } + } - register(preArch) + ctx.TopDown("load_hooks", loadHookMutator).Parallel() + ctx.BottomUp("prebuilts", prebuiltMutator).Parallel() + ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel() + ctx.TopDown("defaults", defaultsMutator).Parallel() - ctx.BottomUp("arch", archMutator).Parallel() - ctx.TopDown("arch_hooks", archHookMutator).Parallel() + register(preArch) - register(preDeps) + ctx.BottomUp("arch", archMutator).Parallel() + ctx.TopDown("arch_hooks", archHookMutator).Parallel() - ctx.BottomUp("deps", depsMutator).Parallel() + register(preDeps) - ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel() - ctx.BottomUp("prebuilt_replace", PrebuiltReplaceMutator).Parallel() + ctx.BottomUp("deps", depsMutator).Parallel() - register(postDeps) + ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel() + ctx.BottomUp("prebuilt_replace", PrebuiltReplaceMutator).Parallel() + + register(postDeps) + + registeredMutators = ctx.mutators + }) + + registerMutatorsToContext(ctx, registeredMutators) } -type registerMutatorsContext struct{} +func RegisterTestMutators(ctx *blueprint.Context) { + mutators := registerMutatorsContext{} + mutators.BottomUp("deps", depsMutator).Parallel() + registerMutatorsToContext(ctx, mutators.mutators) +} + +type registerMutatorsContext struct { + mutators []*mutator +} type RegisterMutatorsContext interface { TopDown(name string, m AndroidTopDownMutator) MutatorHandle @@ -99,7 +135,7 @@ type androidBottomUpMutatorContext struct { androidBaseContextImpl } -func (registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle { +func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle { f := func(ctx blueprint.BottomUpMutatorContext) { if a, ok := ctx.Module().(Module); ok { actx := &androidBottomUpMutatorContext{ @@ -110,11 +146,11 @@ func (registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) M } } mutator := &mutator{name: name, bottomUpMutator: f} - mutators = append(mutators, mutator) + x.mutators = append(x.mutators, mutator) return mutator } -func (registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle { +func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle { f := func(ctx blueprint.TopDownMutatorContext) { if a, ok := ctx.Module().(Module); ok { actx := &androidTopDownMutatorContext{ @@ -125,7 +161,7 @@ func (registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) Mut } } mutator := &mutator{name: name, topDownMutator: f} - mutators = append(mutators, mutator) + x.mutators = append(x.mutators, mutator) return mutator } diff --git a/android/register.go b/android/register.go index d6b290db..93966646 100644 --- a/android/register.go +++ b/android/register.go @@ -15,8 +15,6 @@ package android import ( - "sync" - "github.com/google/blueprint" ) @@ -51,8 +49,6 @@ func RegisterSingletonType(name string, factory blueprint.SingletonFactory) { singletons = append(singletons, singleton{name, factory}) } -var registerMutatorsOnce sync.Once - func NewContext() *blueprint.Context { ctx := blueprint.NewContext() @@ -64,19 +60,8 @@ func NewContext() *blueprint.Context { ctx.RegisterSingletonType(t.name, t.factory) } - registerMutatorsOnce.Do(registerMutators) - - for _, t := range mutators { - var handle blueprint.MutatorHandle - if t.bottomUpMutator != nil { - handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator) - } else if t.topDownMutator != nil { - handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator) - } - if t.parallel { - handle.Parallel() - } - } + registerMutators(ctx) + ctx.RegisterSingletonType("env", EnvSingleton) return ctx -- cgit v1.2.3