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