@@ -59,24 +59,84 @@ const (
59
59
60
60
// NewFakeClient creates a new fake client for testing.
61
61
// You can choose to initialize it with a slice of runtime.Object.
62
+ //
63
+ // Deprecated: Please use NewClientBuilder instead.
62
64
func NewFakeClient (initObjs ... runtime.Object ) client.Client {
63
- return NewFakeClientWithScheme ( scheme . Scheme , initObjs ... )
65
+ return NewClientBuilder (). WithRuntimeObjects ( initObjs ... ). Build ( )
64
66
}
65
67
66
68
// NewFakeClientWithScheme creates a new fake client with the given scheme
67
69
// for testing.
68
70
// You can choose to initialize it with a slice of runtime.Object.
71
+ //
72
+ // Deprecated: Please use NewClientBuilder instead.
69
73
func NewFakeClientWithScheme (clientScheme * runtime.Scheme , initObjs ... runtime.Object ) client.Client {
70
- tracker := testing .NewObjectTracker (clientScheme , scheme .Codecs .UniversalDecoder ())
71
- for _ , obj := range initObjs {
72
- err := tracker .Add (obj )
73
- if err != nil {
74
+ return NewClientBuilder ().WithScheme (clientScheme ).WithRuntimeObjects (initObjs ... ).Build ()
75
+ }
76
+
77
+ // NewClientBuilder returns a new builder to create a fake client.
78
+ func NewClientBuilder () * ClientBuilder {
79
+ return & ClientBuilder {}
80
+ }
81
+
82
+ // ClientBuilder builds a fake client.
83
+ type ClientBuilder struct {
84
+ scheme * runtime.Scheme
85
+ initObject []client.Object
86
+ initLists []client.ObjectList
87
+ initRuntimeObjects []runtime.Object
88
+ }
89
+
90
+ // WithScheme sets this builder's internal scheme.
91
+ // If not set, defaults to client-go's global scheme.Scheme.
92
+ func (f * ClientBuilder ) WithScheme (scheme * runtime.Scheme ) * ClientBuilder {
93
+ f .scheme = scheme
94
+ return f
95
+ }
96
+
97
+ // WithObjects can be optionally used to initialize this fake client with client.Object(s).
98
+ func (f * ClientBuilder ) WithObjects (initObjs ... client.Object ) * ClientBuilder {
99
+ f .initObject = append (f .initObject , initObjs ... )
100
+ return f
101
+ }
102
+
103
+ // WithLists can be optionally used to initialize this fake client with client.ObjectList(s).
104
+ func (f * ClientBuilder ) WithLists (initLists ... client.ObjectList ) * ClientBuilder {
105
+ f .initLists = append (f .initLists , initLists ... )
106
+ return f
107
+ }
108
+
109
+ // WithRuntimeObjects can be optionally used to initialize this fake client with runtime.Object(s).
110
+ func (f * ClientBuilder ) WithRuntimeObjects (initRuntimeObjs ... runtime.Object ) * ClientBuilder {
111
+ f .initRuntimeObjects = append (f .initRuntimeObjects , initRuntimeObjs ... )
112
+ return f
113
+ }
114
+
115
+ // Build builds and returns a new fake client.
116
+ func (f * ClientBuilder ) Build () client.Client {
117
+ if f .scheme == nil {
118
+ f .scheme = scheme .Scheme
119
+ }
120
+
121
+ tracker := testing .NewObjectTracker (f .scheme , scheme .Codecs .UniversalDecoder ())
122
+ for _ , obj := range f .initObject {
123
+ if err := tracker .Add (obj ); err != nil {
74
124
panic (fmt .Errorf ("failed to add object %v to fake client: %w" , obj , err ))
75
125
}
76
126
}
127
+ for _ , obj := range f .initLists {
128
+ if err := tracker .Add (obj ); err != nil {
129
+ panic (fmt .Errorf ("failed to add list %v to fake client: %w" , obj , err ))
130
+ }
131
+ }
132
+ for _ , obj := range f .initRuntimeObjects {
133
+ if err := tracker .Add (obj ); err != nil {
134
+ panic (fmt .Errorf ("failed to add runtime object %v to fake client: %w" , obj , err ))
135
+ }
136
+ }
77
137
return & fakeClient {
78
- tracker : versionedTracker {ObjectTracker : tracker , scheme : clientScheme },
79
- scheme : clientScheme ,
138
+ tracker : versionedTracker {ObjectTracker : tracker , scheme : f . scheme },
139
+ scheme : f . scheme ,
80
140
}
81
141
}
82
142
0 commit comments