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 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 ModuleName 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.ModuleName 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.ModuleName == other.ModuleName && 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.ModuleName == 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.ModuleName 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{} 106