blob: 9a43b61d77a45e434674cba5a2214bed01657016 [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
Sam Delmericocc518432022-03-30 15:50:34 +000023// TestModuleInfo implements blueprint.Module interface with sufficient information to mock a subset of
Liz Kammer57e2e7a2021-09-20 12:55:02 -040024// a blueprint ModuleContext
Sam Delmericocc518432022-03-30 15:50:34 +000025type TestModuleInfo struct {
26 ModuleName string
27 Typ string
28 Dir string
Liz Kammer57e2e7a2021-09-20 12:55:02 -040029}
30
31// Name returns name for testModuleInfo -- required to implement blueprint.Module
Sam Delmericocc518432022-03-30 15:50:34 +000032func (mi TestModuleInfo) Name() string {
33 return mi.ModuleName
Liz Kammer57e2e7a2021-09-20 12:55:02 -040034}
35
36// GenerateBuildActions unused, but required to implmeent blueprint.Module
Sam Delmericocc518432022-03-30 15:50:34 +000037func (mi TestModuleInfo) GenerateBuildActions(blueprint.ModuleContext) {}
Liz Kammer57e2e7a2021-09-20 12:55:02 -040038
Sam Delmericocc518432022-03-30 15:50:34 +000039func (mi TestModuleInfo) equals(other TestModuleInfo) bool {
40 return mi.ModuleName == other.ModuleName && mi.Typ == other.Typ && mi.Dir == other.Dir
Liz Kammer57e2e7a2021-09-20 12:55:02 -040041}
42
43// ensure testModuleInfo implements blueprint.Module
Sam Delmericocc518432022-03-30 15:50:34 +000044var _ blueprint.Module = TestModuleInfo{}
Liz Kammer57e2e7a2021-09-20 12:55:02 -040045
Sam Delmericocc518432022-03-30 15:50:34 +000046// OtherModuleTestContext is a mock context that implements OtherModuleContext
47type OtherModuleTestContext struct {
48 Modules []TestModuleInfo
Liz Kammer57e2e7a2021-09-20 12:55:02 -040049 errors []string
50}
51
52// ModuleFromName retrieves the testModuleInfo corresponding to name, if it exists
Sam Delmericocc518432022-03-30 15:50:34 +000053func (omc *OtherModuleTestContext) ModuleFromName(name string) (blueprint.Module, bool) {
54 for _, m := range omc.Modules {
55 if m.ModuleName == name {
Liz Kammer57e2e7a2021-09-20 12:55:02 -040056 return m, true
57 }
58 }
Sam Delmericocc518432022-03-30 15:50:34 +000059 return TestModuleInfo{}, false
Liz Kammer57e2e7a2021-09-20 12:55:02 -040060}
61
62// testModuleInfo returns the testModuleInfo corresponding to a blueprint.Module if it exists in omc
Sam Delmericocc518432022-03-30 15:50:34 +000063func (omc *OtherModuleTestContext) testModuleInfo(m blueprint.Module) (TestModuleInfo, bool) {
64 mi, ok := m.(TestModuleInfo)
Liz Kammer57e2e7a2021-09-20 12:55:02 -040065 if !ok {
Sam Delmericocc518432022-03-30 15:50:34 +000066 return TestModuleInfo{}, false
Liz Kammer57e2e7a2021-09-20 12:55:02 -040067 }
Sam Delmericocc518432022-03-30 15:50:34 +000068 for _, other := range omc.Modules {
Liz Kammer57e2e7a2021-09-20 12:55:02 -040069 if other.equals(mi) {
70 return mi, true
71 }
72 }
Sam Delmericocc518432022-03-30 15:50:34 +000073 return TestModuleInfo{}, false
Liz Kammer57e2e7a2021-09-20 12:55:02 -040074}
75
76// OtherModuleType returns type of m if it exists in omc
Sam Delmericocc518432022-03-30 15:50:34 +000077func (omc *OtherModuleTestContext) OtherModuleType(m blueprint.Module) string {
Liz Kammer57e2e7a2021-09-20 12:55:02 -040078 if mi, ok := omc.testModuleInfo(m); ok {
Sam Delmericocc518432022-03-30 15:50:34 +000079 return mi.Typ
Liz Kammer57e2e7a2021-09-20 12:55:02 -040080 }
81 return ""
82}
83
84// OtherModuleName returns name of m if it exists in omc
Sam Delmericocc518432022-03-30 15:50:34 +000085func (omc *OtherModuleTestContext) OtherModuleName(m blueprint.Module) string {
Liz Kammer57e2e7a2021-09-20 12:55:02 -040086 if mi, ok := omc.testModuleInfo(m); ok {
Sam Delmericocc518432022-03-30 15:50:34 +000087 return mi.ModuleName
Liz Kammer57e2e7a2021-09-20 12:55:02 -040088 }
89 return ""
90}
91
92// OtherModuleDir returns dir of m if it exists in omc
Sam Delmericocc518432022-03-30 15:50:34 +000093func (omc *OtherModuleTestContext) OtherModuleDir(m blueprint.Module) string {
Liz Kammer57e2e7a2021-09-20 12:55:02 -040094 if mi, ok := omc.testModuleInfo(m); ok {
Sam Delmericocc518432022-03-30 15:50:34 +000095 return mi.Dir
Liz Kammer57e2e7a2021-09-20 12:55:02 -040096 }
97 return ""
98}
99
Sam Delmericocc518432022-03-30 15:50:34 +0000100func (omc *OtherModuleTestContext) ModuleErrorf(format string, args ...interface{}) {
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400101 omc.errors = append(omc.errors, fmt.Sprintf(format, args...))
102}
103
104// Ensure otherModuleTestContext implements OtherModuleContext
Sam Delmericocc518432022-03-30 15:50:34 +0000105var _ OtherModuleContext = &OtherModuleTestContext{}