MCPcopy Create free account
hub / github.com/cli/cli / TestClientCurrentBranch

Function TestClientCurrentBranch

git/client_test.go:301–353  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

299}
300
301func TestClientCurrentBranch(t *testing.T) {
302 tests := []struct {
303 name string
304 cmdExitStatus int
305 cmdStdout string
306 cmdStderr string
307 wantCmdArgs string
308 wantErrorMsg string
309 wantBranch string
310 }{
311 {
312 name: "branch name",
313 cmdStdout: "branch-name\n",
314 wantCmdArgs: `path/to/git symbolic-ref --quiet HEAD`,
315 wantBranch: "branch-name",
316 },
317 {
318 name: "ref",
319 cmdStdout: "refs/heads/branch-name\n",
320 wantCmdArgs: `path/to/git symbolic-ref --quiet HEAD`,
321 wantBranch: "branch-name",
322 },
323 {
324 name: "escaped ref",
325 cmdStdout: "refs/heads/branch\u00A0with\u00A0non\u00A0breaking\u00A0space\n",
326 wantCmdArgs: `path/to/git symbolic-ref --quiet HEAD`,
327 wantBranch: "branch\u00A0with\u00A0non\u00A0breaking\u00A0space",
328 },
329 {
330 name: "detached head",
331 cmdExitStatus: 1,
332 wantCmdArgs: `path/to/git symbolic-ref --quiet HEAD`,
333 wantErrorMsg: "failed to run git: not on any branch",
334 },
335 }
336 for _, tt := range tests {
337 t.Run(tt.name, func(t *testing.T) {
338 cmd, cmdCtx := createCommandContext(t, tt.cmdExitStatus, tt.cmdStdout, tt.cmdStderr)
339 client := Client{
340 GitPath: "path/to/git",
341 commandContext: cmdCtx,
342 }
343 branch, err := client.CurrentBranch(context.Background())
344 assert.Equal(t, tt.wantCmdArgs, strings.Join(cmd.Args[3:], " "))
345 if tt.wantErrorMsg == "" {
346 assert.NoError(t, err)
347 } else {
348 assert.EqualError(t, err, tt.wantErrorMsg)
349 }
350 assert.Equal(t, tt.wantBranch, branch)
351 })
352 }
353}
354
355func TestClientShowRefs(t *testing.T) {
356 tests := []struct {

Callers

nothing calls this directly

Calls 5

CurrentBranchMethod · 0.95
createCommandContextFunction · 0.85
EqualMethod · 0.80
JoinMethod · 0.80
RunMethod · 0.65

Tested by

no test coverage detected