2021-09-20 18:55:02 +02:00
|
|
|
// Copyright 2021 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package bazel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
)
|
|
|
|
|
2022-03-30 17:50:34 +02:00
|
|
|
// TestModuleInfo implements blueprint.Module interface with sufficient information to mock a subset of
|
2021-09-20 18:55:02 +02:00
|
|
|
// a blueprint ModuleContext
|
2022-03-30 17:50:34 +02:00
|
|
|
type TestModuleInfo struct {
|
|
|
|
ModuleName string
|
|
|
|
Typ string
|
|
|
|
Dir string
|
2021-09-20 18:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns name for testModuleInfo -- required to implement blueprint.Module
|
2022-03-30 17:50:34 +02:00
|
|
|
func (mi TestModuleInfo) Name() string {
|
|
|
|
return mi.ModuleName
|
2021-09-20 18:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateBuildActions unused, but required to implmeent blueprint.Module
|
2022-03-30 17:50:34 +02:00
|
|
|
func (mi TestModuleInfo) GenerateBuildActions(blueprint.ModuleContext) {}
|
2021-09-20 18:55:02 +02:00
|
|
|
|
2022-03-30 17:50:34 +02:00
|
|
|
func (mi TestModuleInfo) equals(other TestModuleInfo) bool {
|
|
|
|
return mi.ModuleName == other.ModuleName && mi.Typ == other.Typ && mi.Dir == other.Dir
|
2021-09-20 18:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure testModuleInfo implements blueprint.Module
|
2022-03-30 17:50:34 +02:00
|
|
|
var _ blueprint.Module = TestModuleInfo{}
|
2021-09-20 18:55:02 +02:00
|
|
|
|
2022-03-30 17:50:34 +02:00
|
|
|
// OtherModuleTestContext is a mock context that implements OtherModuleContext
|
|
|
|
type OtherModuleTestContext struct {
|
|
|
|
Modules []TestModuleInfo
|
2021-09-20 18:55:02 +02:00
|
|
|
errors []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModuleFromName retrieves the testModuleInfo corresponding to name, if it exists
|
2022-03-30 17:50:34 +02:00
|
|
|
func (omc *OtherModuleTestContext) ModuleFromName(name string) (blueprint.Module, bool) {
|
|
|
|
for _, m := range omc.Modules {
|
|
|
|
if m.ModuleName == name {
|
2021-09-20 18:55:02 +02:00
|
|
|
return m, true
|
|
|
|
}
|
|
|
|
}
|
2022-03-30 17:50:34 +02:00
|
|
|
return TestModuleInfo{}, false
|
2021-09-20 18:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testModuleInfo returns the testModuleInfo corresponding to a blueprint.Module if it exists in omc
|
2022-03-30 17:50:34 +02:00
|
|
|
func (omc *OtherModuleTestContext) testModuleInfo(m blueprint.Module) (TestModuleInfo, bool) {
|
|
|
|
mi, ok := m.(TestModuleInfo)
|
2021-09-20 18:55:02 +02:00
|
|
|
if !ok {
|
2022-03-30 17:50:34 +02:00
|
|
|
return TestModuleInfo{}, false
|
2021-09-20 18:55:02 +02:00
|
|
|
}
|
2022-03-30 17:50:34 +02:00
|
|
|
for _, other := range omc.Modules {
|
2021-09-20 18:55:02 +02:00
|
|
|
if other.equals(mi) {
|
|
|
|
return mi, true
|
|
|
|
}
|
|
|
|
}
|
2022-03-30 17:50:34 +02:00
|
|
|
return TestModuleInfo{}, false
|
2021-09-20 18:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// OtherModuleType returns type of m if it exists in omc
|
2022-03-30 17:50:34 +02:00
|
|
|
func (omc *OtherModuleTestContext) OtherModuleType(m blueprint.Module) string {
|
2021-09-20 18:55:02 +02:00
|
|
|
if mi, ok := omc.testModuleInfo(m); ok {
|
2022-03-30 17:50:34 +02:00
|
|
|
return mi.Typ
|
2021-09-20 18:55:02 +02:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// OtherModuleName returns name of m if it exists in omc
|
2022-03-30 17:50:34 +02:00
|
|
|
func (omc *OtherModuleTestContext) OtherModuleName(m blueprint.Module) string {
|
2021-09-20 18:55:02 +02:00
|
|
|
if mi, ok := omc.testModuleInfo(m); ok {
|
2022-03-30 17:50:34 +02:00
|
|
|
return mi.ModuleName
|
2021-09-20 18:55:02 +02:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// OtherModuleDir returns dir of m if it exists in omc
|
2022-03-30 17:50:34 +02:00
|
|
|
func (omc *OtherModuleTestContext) OtherModuleDir(m blueprint.Module) string {
|
2021-09-20 18:55:02 +02:00
|
|
|
if mi, ok := omc.testModuleInfo(m); ok {
|
2022-03-30 17:50:34 +02:00
|
|
|
return mi.Dir
|
2021-09-20 18:55:02 +02:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-03-30 17:50:34 +02:00
|
|
|
func (omc *OtherModuleTestContext) ModuleErrorf(format string, args ...interface{}) {
|
2021-09-20 18:55:02 +02:00
|
|
|
omc.errors = append(omc.errors, fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure otherModuleTestContext implements OtherModuleContext
|
2022-03-30 17:50:34 +02:00
|
|
|
var _ OtherModuleContext = &OtherModuleTestContext{}
|