-
Notifications
You must be signed in to change notification settings - Fork 408
/
helpers.go
128 lines (100 loc) · 3.13 KB
/
helpers.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Copyright 2019 The Kubernetes Authors.
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 testutils defines utilities for using loader.Packages
// in tests.
//
// The main utility is LoadFakeRoots, which allows writing of fake
// modules to the local filesystem, then loading those from within
// a Ginkgo test. If not using Ginkgo, you can reproduce similar
// logic with loader.LoadWithConfig and go/packages/packagestest.
package testutils
import (
"testing"
"github.com/onsi/ginkgo"
pkgstest "golang.org/x/tools/go/packages/packagestest"
"sigs.k8s.io/controller-tools/pkg/loader"
)
// fakeT wraps Ginkgo's fake testing.T with a real testing.T
// to get the few missing methods.
// NOTE: Go 1.17 doesn't allow ambiguous embedded funcs anymore.
// We have to proxy ginkgo.GinkgoTInterface instead of testing.T
// because we have to embed testing.T's private() func.
type fakeT struct {
g ginkgo.GinkgoTInterface
*testing.T
}
func (t fakeT) Cleanup(f func()) {
t.g.Cleanup(f)
}
func (t fakeT) Error(args ...interface{}) {
t.g.Error(args)
}
func (t fakeT) Errorf(format string, args ...interface{}) {
t.g.Errorf(format, args)
}
func (t fakeT) Fail() {
t.g.Fail()
}
func (t fakeT) FailNow() {
t.g.FailNow()
}
func (t fakeT) Failed() bool {
return t.g.Failed()
}
func (t fakeT) Fatal(args ...interface{}) {
t.g.Fatal(args)
}
func (t fakeT) Fatalf(format string, args ...interface{}) {
t.g.Fatalf(format, args)
}
func (t fakeT) Helper() {
t.g.Helper()
}
func (t fakeT) Log(args ...interface{}) {
t.g.Log(args)
}
func (t fakeT) Logf(format string, args ...interface{}) {
t.g.Logf(format, args)
}
func (t fakeT) Name() string {
return t.g.Name()
}
func (t fakeT) Setenv(key, value string) {
t.g.Setenv(key, value)
}
func (t fakeT) Skip(args ...interface{}) {
t.g.Skip(args)
}
func (t fakeT) SkipNow() {
t.g.SkipNow()
}
func (t fakeT) Skipf(format string, args ...interface{}) {
t.g.Skipf(format, args)
}
func (t fakeT) Skipped() bool {
return t.g.Skipped()
}
func (t fakeT) TempDir() string {
return t.g.TempDir()
}
// LoadFakeRoots loads the given "root" packages by fake module,
// transitively loading and all imports as well.
//
// Loaded packages will have type size, imports, and exports file information
// populated. Additional information, like ASTs and type-checking information,
// can be accessed via methods on individual packages.
func LoadFakeRoots(exporter pkgstest.Exporter, modules []pkgstest.Module, roots ...string) ([]*loader.Package, *pkgstest.Exported, error) {
exported := pkgstest.Export(fakeT{ginkgo.GinkgoT(), &testing.T{}}, exporter, modules)
pkgs, err := loader.LoadRootsWithConfig(exported.Config, roots...)
return pkgs, exported, err
}