blob: 23c8350842d45e0f764317439d4baa937250791c [file] [log] [blame]
Liz Kammer57e2e7a2021-09-20 12:55:02 -04001// 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
15package bazel
16
17import (
18 "fmt"
19
20 "github.com/google/blueprint"
21)
22
23// testModuleInfo implements blueprint.Module interface with sufficient information to mock a subset of
24// a blueprint ModuleContext
25type testModuleInfo struct {
26 name string
27 typ string
28 dir string
29}
30
31// Name returns name for testModuleInfo -- required to implement blueprint.Module
32func (mi testModuleInfo) Name() string {
33 return mi.name
34}
35
36// GenerateBuildActions unused, but required to implmeent blueprint.Module
37func (mi testModuleInfo) GenerateBuildActions(blueprint.ModuleContext) {}
38
39func (mi testModuleInfo) equals(other testModuleInfo) bool {
40 return mi.name == other.name && mi.typ == other.typ && mi.dir == other.dir
41}
42
43// ensure testModuleInfo implements blueprint.Module
44var _ blueprint.Module = testModuleInfo{}
45
46// otherModuleTestContext is a mock context that implements OtherModuleContext
47type otherModuleTestContext struct {
48 modules []testModuleInfo
49 errors []string
50}
51
52// ModuleFromName retrieves the testModuleInfo corresponding to name, if it exists
53func (omc *otherModuleTestContext) ModuleFromName(name string) (blueprint.Module, bool) {
54 for _, m := range omc.modules {
55 if m.name == name {
56 return m, true
57 }
58 }
59 return testModuleInfo{}, false
60}
61
62// testModuleInfo returns the testModuleInfo corresponding to a blueprint.Module if it exists in omc
63func (omc *otherModuleTestContext) testModuleInfo(m blueprint.Module) (testModuleInfo, bool) {
64 mi, ok := m.(testModuleInfo)
65 if !ok {
66 return testModuleInfo{}, false
67 }
68 for _, other := range omc.modules {
69 if other.equals(mi) {
70 return mi, true
71 }
72 }
73 return testModuleInfo{}, false
74}
75
76// OtherModuleType returns type of m if it exists in omc
77func (omc *otherModuleTestContext) OtherModuleType(m blueprint.Module) string {
78 if mi, ok := omc.testModuleInfo(m); ok {
79 return mi.typ
80 }
81 return ""
82}
83
84// OtherModuleName returns name of m if it exists in omc
85func (omc *otherModuleTestContext) OtherModuleName(m blueprint.Module) string {
86 if mi, ok := omc.testModuleInfo(m); ok {
87 return mi.name
88 }
89 return ""
90}
91
92// OtherModuleDir returns dir of m if it exists in omc
93func (omc *otherModuleTestContext) OtherModuleDir(m blueprint.Module) string {
94 if mi, ok := omc.testModuleInfo(m); ok {
95 return mi.dir
96 }
97 return ""
98}
99
100func (omc *otherModuleTestContext) ModuleErrorf(format string, args ...interface{}) {
101 omc.errors = append(omc.errors, fmt.Sprintf(format, args...))
102}
103
104// Ensure otherModuleTestContext implements OtherModuleContext
105var _ OtherModuleContext = &otherModuleTestContext{}