testing.go 3.1 KB

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