aboutsummaryrefslogtreecommitdiff
path: root/bazel/testing.go
blob: 9a43b61d77a45e434674cba5a2214bed01657016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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"
)

// TestModuleInfo implements blueprint.Module interface with sufficient information to mock a subset of
// a blueprint ModuleContext
type TestModuleInfo struct {
	ModuleName string
	Typ        string
	Dir        string
}

// Name returns name for testModuleInfo -- required to implement blueprint.Module
func (mi TestModuleInfo) Name() string {
	return mi.ModuleName
}

// GenerateBuildActions unused, but required to implmeent blueprint.Module
func (mi TestModuleInfo) GenerateBuildActions(blueprint.ModuleContext) {}

func (mi TestModuleInfo) equals(other TestModuleInfo) bool {
	return mi.ModuleName == other.ModuleName && mi.Typ == other.Typ && mi.Dir == other.Dir
}

// ensure testModuleInfo implements blueprint.Module
var _ blueprint.Module = TestModuleInfo{}

// OtherModuleTestContext is a mock context that implements OtherModuleContext
type OtherModuleTestContext struct {
	Modules []TestModuleInfo
	errors  []string
}

// ModuleFromName retrieves the testModuleInfo corresponding to name, if it exists
func (omc *OtherModuleTestContext) ModuleFromName(name string) (blueprint.Module, bool) {
	for _, m := range omc.Modules {
		if m.ModuleName == name {
			return m, true
		}
	}
	return TestModuleInfo{}, false
}

// testModuleInfo returns the testModuleInfo corresponding to a blueprint.Module if it exists in omc
func (omc *OtherModuleTestContext) testModuleInfo(m blueprint.Module) (TestModuleInfo, bool) {
	mi, ok := m.(TestModuleInfo)
	if !ok {
		return TestModuleInfo{}, false
	}
	for _, other := range omc.Modules {
		if other.equals(mi) {
			return mi, true
		}
	}
	return TestModuleInfo{}, false
}

// OtherModuleType returns type of m if it exists in omc
func (omc *OtherModuleTestContext) OtherModuleType(m blueprint.Module) string {
	if mi, ok := omc.testModuleInfo(m); ok {
		return mi.Typ
	}
	return ""
}

// OtherModuleName returns name of m if it exists in omc
func (omc *OtherModuleTestContext) OtherModuleName(m blueprint.Module) string {
	if mi, ok := omc.testModuleInfo(m); ok {
		return mi.ModuleName
	}
	return ""
}

// OtherModuleDir returns dir of m if it exists in omc
func (omc *OtherModuleTestContext) OtherModuleDir(m blueprint.Module) string {
	if mi, ok := omc.testModuleInfo(m); ok {
		return mi.Dir
	}
	return ""
}

func (omc *OtherModuleTestContext) ModuleErrorf(format string, args ...interface{}) {
	omc.errors = append(omc.errors, fmt.Sprintf(format, args...))
}

// Ensure otherModuleTestContext implements OtherModuleContext
var _ OtherModuleContext = &OtherModuleTestContext{}