MCPcopy
hub / github.com/cli/cli / TestClientCurrentBranch

Function TestClientCurrentBranch

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

Source from the content-addressed store, hash-verified

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