X Tutup
package git import ( "os/exec" "reflect" "testing" "github.com/cli/cli/internal/run" "github.com/cli/cli/test" ) func Test_UncommittedChangeCount(t *testing.T) { type c struct { Label string Expected int Output string } cases := []c{ {Label: "no changes", Expected: 0, Output: ""}, {Label: "one change", Expected: 1, Output: " M poem.txt"}, {Label: "untracked file", Expected: 2, Output: " M poem.txt\n?? new.txt"}, } teardown := run.SetPrepareCmd(func(*exec.Cmd) run.Runnable { return &test.OutputStub{} }) defer teardown() for _, v := range cases { _ = run.SetPrepareCmd(func(*exec.Cmd) run.Runnable { return &test.OutputStub{Out: []byte(v.Output)} }) ucc, _ := UncommittedChangeCount() if ucc != v.Expected { t.Errorf("got unexpected ucc value: %d for case %s", ucc, v.Label) } } } func Test_CurrentBranch(t *testing.T) { type c struct { Stub string Expected string } cases := []c{ { Stub: "branch-name\n", Expected: "branch-name", }, { Stub: "refs/heads/branch-name\n", Expected: "branch-name", }, { Stub: "refs/heads/branch\u00A0with\u00A0non\u00A0breaking\u00A0space\n", Expected: "branch\u00A0with\u00A0non\u00A0breaking\u00A0space", }, } for _, v := range cases { cs, teardown := test.InitCmdStubber() cs.Stub(v.Stub) result, err := CurrentBranch() if err != nil { t.Errorf("got unexpected error: %w", err) } if len(cs.Calls) != 1 { t.Errorf("expected 1 git call, saw %d", len(cs.Calls)) } if result != v.Expected { t.Errorf("unexpected branch name: %s instead of %s", result, v.Expected) } teardown() } } func Test_CurrentBranch_detached_head(t *testing.T) { cs, teardown := test.InitCmdStubber() defer teardown() cs.StubError("") _, err := CurrentBranch() if err == nil { t.Errorf("expected an error") } if err != ErrNotOnAnyBranch { t.Errorf("got unexpected error: %s instead of %s", err, ErrNotOnAnyBranch) } if len(cs.Calls) != 1 { t.Errorf("expected 1 git call, saw %d", len(cs.Calls)) } } func Test_CurrentBranch_unexpected_error(t *testing.T) { cs, teardown := test.InitCmdStubber() defer teardown() cs.StubError("lol") expectedError := "lol\nstub: lol" _, err := CurrentBranch() if err == nil { t.Errorf("expected an error") } if err.Error() != expectedError { t.Errorf("got unexpected error: %s instead of %s", err.Error(), expectedError) } if len(cs.Calls) != 1 { t.Errorf("expected 1 git call, saw %d", len(cs.Calls)) } } func TestParseExtraCloneArgs(t *testing.T) { type Wanted struct { args []string dir string } tests := []struct { name string args []string want Wanted }{ { name: "args and target", args: []string{"target_directory", "-o", "upstream", "--depth", "1"}, want: Wanted{ args: []string{"-o", "upstream", "--depth", "1"}, dir: "target_directory", }, }, { name: "only args", args: []string{"-o", "upstream", "--depth", "1"}, want: Wanted{ args: []string{"-o", "upstream", "--depth", "1"}, dir: "", }, }, { name: "only target", args: []string{"target_directory"}, want: Wanted{ args: []string{}, dir: "target_directory", }, }, { name: "no args", args: []string{}, want: Wanted{ args: []string{}, dir: "", }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { args, dir := parseCloneArgs(tt.args) got := Wanted{ args: args, dir: dir, } if !reflect.DeepEqual(got, tt.want) { t.Errorf("got %#v want %#v", got, tt.want) } }) } }
X Tutup