Skip to content

Commit 798e03c

Browse files
author
bchadwic
committed
working on testing browse.go
1 parent 8fa2457 commit 798e03c

File tree

2 files changed

+72
-13
lines changed

2 files changed

+72
-13
lines changed

pkg/cmd/browse/browse.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
5555
}
5656

5757
cmd := &cobra.Command{
58-
Long: "Work with GitHub in the browser", // displays when you are on the help page of this command
59-
Short: "Open GitHub in the browser", // displays in the gh root help
60-
Use: "browse", // necessary!!! This is the cmd that gets passed on the prompt
61-
Args: cobra.RangeArgs(0, 2), // make sure only one arg at most is passed
62-
58+
Long: "Work with GitHub in the browser", // displays when you are on the help page of this command
59+
Short: "Open GitHub in the browser", // displays in the gh root help
60+
Use: "browse {<number> | <file> | <branch>}", // necessary!!! This is the cmd that gets passed on the prompt
61+
Args: cobra.RangeArgs(0, 2), // make sure only one arg at most is passed
6362
Example: heredoc.Doc(`
6463
$ gh browse
6564
#=> Opens repository in browser
@@ -73,7 +72,6 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
7372
$ gh browse src/fileName:312 --branch main
7473
#=> Opens src/fileName at line 312 in main branch
7574
`),
76-
7775
Annotations: map[string]string{
7876
"IsCore": "true",
7977
"help:arguments": heredoc.Doc(`
@@ -82,7 +80,6 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
8280
- by file or branch name, e.g. "main.java" or "trunk".
8381
`),
8482
},
85-
8683
Run: func(cmd *cobra.Command, args []string) {
8784

8885
if len(args) > 1 {
@@ -104,25 +101,25 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
104101
return cmd
105102
}
106103

107-
func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) {
104+
func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) (exitCode, string) {
108105

109106
baseRepo, err := opts.BaseRepo()
110107
httpClient, _ := opts.HttpClient()
111108
apiClient := api.NewClientFromHTTP(httpClient)
112109
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
113-
response := exitSuccess
114110

115111
if !inRepo(err) { // must be in a repo to execute
116112
printExit(exitNotInRepo, cmd, opts, "")
117-
return
113+
return exitNotInRepo, ""
118114
}
119115

120116
if getFlagAmount(cmd) > 1 { // command can't have more than one flag
121117
printExit(exitTooManyFlags, cmd, opts, "")
122-
return
118+
return exitTooManyFlags, ""
123119
}
124120

125121
repoUrl := ghrepo.GenerateRepoURL(baseRepo, "")
122+
response := exitSuccess
126123

127124
if !hasArg(opts) && hasFlag(cmd) {
128125
response, repoUrl = addFlag(opts, repoUrl)
@@ -137,7 +134,7 @@ func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) {
137134
}
138135

139136
printExit(response, cmd, opts, repoUrl) // print success
140-
137+
return response, repoUrl
141138
}
142139

143140
func addCombined(opts *BrowseOptions, url string, branchName string) (exitCode, string) {

pkg/cmd/browse/browse_test.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,74 @@
11
package browse
22

33
import (
4+
"bytes"
5+
"io/ioutil"
6+
"net/http"
47
"testing"
8+
9+
"github.com/cli/cli/internal/config"
10+
"github.com/cli/cli/internal/ghrepo"
11+
"github.com/cli/cli/pkg/cmdutil"
12+
"github.com/cli/cli/pkg/iostreams"
13+
"github.com/cli/cli/test"
14+
"github.com/google/shlex"
515
)
616

717
func TestNewCmdBrowse(t *testing.T) {
818
// TODO test the use of the api using "gh browse"
919
// instead of opening multiple browsers for each test,
1020
// we can test the http code sent back after calling a site
1121

12-
}
22+
}
23+
24+
func runCommand(isTTY bool, cli string) (*test.CmdOut, error) {
25+
io, _, stdout, stderr := iostreams.Test()
26+
io.SetStdoutTTY(isTTY)
27+
io.SetStdinTTY(isTTY)
28+
io.SetStderrTTY(isTTY)
29+
30+
factory := &cmdutil.Factory{
31+
IOStreams: io,
32+
HttpClient: func() (*http.Client, error) {
33+
return &http.Client{Transport: rt}, nil
34+
},
35+
Config: func() (config.Config, error) {
36+
return config.NewBlankConfig(), nil
37+
},
38+
BaseRepo: func() (ghrepo.Interface, error) {
39+
return ghrepo.New("OWNER", "REPO"), nil
40+
},
41+
}
42+
43+
cmd := NewCmdBrowse(factory)
44+
45+
argv, err := shlex.Split(cli)
46+
if err != nil {
47+
return nil, err
48+
}
49+
cmd.SetArgs(argv)
50+
51+
cmd.SetIn(&bytes.Buffer{})
52+
cmd.SetOut(ioutil.Discard)
53+
cmd.SetErr(ioutil.Discard)
54+
55+
_, err = cmd.ExecuteC()
56+
return &test.CmdOut{
57+
OutBuf: stdout,
58+
ErrBuf: stderr,
59+
}, err
60+
}
61+
62+
func TestBrowseOpen(t *testing.T) {
63+
runCommand(true, "")
64+
}
65+
66+
func Test_browseList(t *testing.T) {
67+
type args struct {
68+
repo ghrepo.Interface
69+
cli string
70+
expectedOutputs string
71+
}
72+
tests := []struct {
73+
}{}
74+
}

0 commit comments

Comments
 (0)