MCPcopy Index your code
hub / github.com/celer-pkg/celer / TestCurrentUserForGroup

Function TestCurrentUserForGroup

pkgcache/cache_setup_test.go:175–231  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

173}
174
175func TestCurrentUserForGroup(t *testing.T) {
176 tests := []struct {
177 name string
178 sudoUser string
179 user string
180 want string
181 wantErr string
182 }{
183 {
184 name: "prefers SUDO_USER",
185 sudoUser: "alice",
186 user: "root",
187 want: "alice",
188 },
189 {
190 name: "falls back to USER",
191 user: "bob",
192 want: "bob",
193 },
194 {
195 name: "rejects root from SUDO_USER",
196 sudoUser: "root",
197 user: "ignored",
198 wantErr: "cannot determine the invoking non-root user",
199 },
200 {
201 name: "rejects root from USER",
202 user: "root",
203 wantErr: "cannot determine the invoking non-root user",
204 },
205 {
206 name: "rejects empty environment",
207 wantErr: "cannot read current user from environment",
208 },
209 }
210
211 for _, test := range tests {
212 t.Run(test.name, func(t *testing.T) {
213 t.Setenv("SUDO_USER", test.sudoUser)
214 t.Setenv("USER", test.user)
215
216 got, err := currentUserForGroup()
217 if test.wantErr != "" {
218 if err == nil || !strings.Contains(err.Error(), test.wantErr) {
219 t.Fatalf("currentUserForGroup() error = %v, want to contain %q", err, test.wantErr)
220 }
221 return
222 }
223 if err != nil {
224 t.Fatalf("currentUserForGroup() error = %v", err)
225 }
226 if got != test.want {
227 t.Fatalf("currentUserForGroup() = %q, want %q", got, test.want)
228 }
229 })
230 }
231}
232

Callers

nothing calls this directly

Calls 2

currentUserForGroupFunction · 0.85
ErrorMethod · 0.80

Tested by

no test coverage detected