• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017 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 (
18	"fmt"
19	"strings"
20
21	"github.com/google/blueprint"
22)
23
24func NewTestContext() *TestContext {
25	return &TestContext{
26		Context: blueprint.NewContext(),
27	}
28}
29
30type TestContext struct {
31	*blueprint.Context
32	preArch, preDeps, postDeps []RegisterMutatorFunc
33}
34
35func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
36	ctx.preArch = append(ctx.preArch, f)
37}
38
39func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
40	ctx.preDeps = append(ctx.preDeps, f)
41}
42
43func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
44	ctx.postDeps = append(ctx.postDeps, f)
45}
46
47func (ctx *TestContext) Register() {
48	registerMutators(ctx.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
49
50	ctx.RegisterSingletonType("env", EnvSingleton)
51}
52
53func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
54	var module Module
55	ctx.VisitAllModules(func(m blueprint.Module) {
56		if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
57			module = m.(Module)
58		}
59	})
60
61	if module == nil {
62		panic(fmt.Errorf("failed to find module %q variant %q", name, variant))
63	}
64
65	return TestingModule{module}
66}
67
68type TestingModule struct {
69	module Module
70}
71
72func (m TestingModule) Module() Module {
73	return m.module
74}
75
76func (m TestingModule) Rule(rule string) ModuleBuildParams {
77	for _, p := range m.module.BuildParamsForTests() {
78		if strings.Contains(p.Rule.String(), rule) {
79			return p
80		}
81	}
82	panic(fmt.Errorf("couldn't find rule %q", rule))
83}
84
85func (m TestingModule) Output(file string) ModuleBuildParams {
86	for _, p := range m.module.BuildParamsForTests() {
87		outputs := append(WritablePaths(nil), p.Outputs...)
88		if p.Output != nil {
89			outputs = append(outputs, p.Output)
90		}
91		for _, f := range outputs {
92			if f.Base() == file {
93				return p
94			}
95		}
96	}
97	panic(fmt.Errorf("couldn't find output %q", file))
98}
99