github.com/emmahsax/go-git-helper@v0.0.8-0.20240519163017-907b9de0fa52/cmd/newBranch/newBranch_test.go (about) 1 package newBranch 2 3 import ( 4 "testing" 5 6 "github.com/emmahsax/go-git-helper/internal/commandline" 7 ) 8 9 type MockExecutor struct { 10 Args []string 11 Command string 12 Debug bool 13 Output []byte 14 } 15 16 func (me *MockExecutor) Exec(execType string, command string, args ...string) ([]byte, error) { 17 me.Command = command 18 me.Args = args 19 return me.Output, nil 20 } 21 22 func Test_determineBranch(t *testing.T) { 23 tests := []struct { 24 args []string 25 branch string 26 }{ 27 {args: []string{}, branch: "hello-something-or-other"}, 28 {args: []string{"hello-world"}, branch: ""}, 29 } 30 31 originalAskOpenEndedQuestion := commandline.AskOpenEndedQuestion 32 t.Cleanup(func() { 33 commandline.AskOpenEndedQuestion = originalAskOpenEndedQuestion 34 }) 35 36 for _, test := range tests { 37 commandline.AskOpenEndedQuestion = func(question string, secret bool) string { 38 return test.branch 39 } 40 41 o := determineBranch(test.args) 42 43 if o == test.branch { 44 continue 45 } 46 47 if len(test.args) > 0 && o == test.args[0] { 48 continue 49 } 50 51 t.Errorf("branch should be %s, but was %s", test.branch, o) 52 } 53 } 54 55 func Test_askForBranch(t *testing.T) { 56 tests := []struct { 57 branch string 58 }{ 59 {branch: "hello-world"}, 60 } 61 62 originalAskOpenEndedQuestion := commandline.AskOpenEndedQuestion 63 t.Cleanup(func() { 64 commandline.AskOpenEndedQuestion = originalAskOpenEndedQuestion 65 }) 66 67 for _, test := range tests { 68 commandline.AskOpenEndedQuestion = func(question string, secret bool) string { 69 return test.branch 70 } 71 72 o := askForBranch() 73 74 if o != test.branch { 75 t.Errorf("branch should be %s, but was %s", "hello-world", o) 76 } 77 } 78 } 79 80 func Test_execute(t *testing.T) { 81 tests := []struct { 82 expectedArgs []string 83 }{ 84 {expectedArgs: []string{"push", "--set-upstream", "origin", "hello-world"}}, 85 } 86 87 for _, test := range tests { 88 executor := &MockExecutor{Debug: true} 89 nb := newNewBranch("hello-world", true, executor) 90 nb.execute() 91 92 if executor.Command != "git" { 93 t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command) 94 } 95 96 if len(executor.Args) != len(test.expectedArgs) { 97 t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args) 98 } 99 100 for i, v := range executor.Args { 101 if v != test.expectedArgs[i] { 102 t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args) 103 } 104 } 105 } 106 }