X Tutup
Skip to content

Commit 569d2fd

Browse files
author
bchadwic
committed
Got tests objects to fully work! Currently working on making more test cases
1 parent 3dc66b9 commit 569d2fd

File tree

4 files changed

+108
-112
lines changed

4 files changed

+108
-112
lines changed

.vscode/settings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"search.exclude": {
33
"vendor/**": true
4-
}
4+
},
5+
"vim.easymotion": true,
6+
"vim.handleKeys": {
7+
8+
"<C-d>": true
9+
},
10+
"vim.leader": "Space"
511
}

pkg/cmd/browse/browse.go

Lines changed: 49 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@ type BrowseOptions struct {
3333
BranchFlag bool
3434
}
3535

36-
type exitCode int
37-
38-
const (
39-
exitUrlSuccess exitCode = 0
40-
exitNonUrlSuccess exitCode = 1
41-
exitNotInRepo exitCode = 2
42-
exitTooManyFlags exitCode = 3
43-
exitTooManyArgs exitCode = 4
44-
exitExpectedArg exitCode = 5
45-
exitInvalidCombo exitCode = 6
46-
)
47-
4836
func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
4937

5038
opts := &BrowseOptions{
@@ -108,117 +96,112 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
10896
}
10997

11098
func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) error {
111-
99+
help := "Use 'gh browse --help' for more information about browse\n"
112100
baseRepo, err := opts.BaseRepo()
101+
if err != nil {
102+
return fmt.Errorf("unable to determine base repository: %w\n%s", err, help)
103+
}
104+
113105
httpClient, _ := opts.HttpClient()
114106
apiClient := api.NewClientFromHTTP(httpClient)
115107
branchName, _ := api.RepoDefaultBranch(apiClient, baseRepo)
116108

117-
if !inRepo(err) {
118-
return printExit(exitNotInRepo, cmd, opts, "")
119-
}
120-
121109
if getFlagAmount(cmd) > 1 {
122-
return printExit(exitTooManyFlags, cmd, opts, "")
110+
return fmt.Errorf("accepts 1 flag, %d flag(s) were recieved\n%s", getFlagAmount(cmd), help)
123111
}
124112

125113
repoUrl := ghrepo.GenerateRepoURL(baseRepo, "")
126-
response := exitUrlSuccess
127114

128115
if !hasArg(opts) && hasFlag(cmd) {
129-
response, repoUrl = addFlag(opts, repoUrl)
116+
err, repoUrl = addFlag(opts, repoUrl)
117+
if err != nil {
118+
return err
119+
}
130120
} else if hasArg(opts) && !hasFlag(cmd) {
131-
response, repoUrl = addArg(opts, repoUrl, branchName)
121+
err, repoUrl = addArg(opts, repoUrl, branchName)
122+
if err != nil {
123+
return err
124+
}
132125
} else if hasArg(opts) && hasFlag(cmd) {
133-
response, repoUrl = addCombined(opts, repoUrl, branchName)
126+
err, repoUrl = addCombined(opts, repoUrl, branchName)
127+
if err != nil {
128+
return err
129+
}
134130
}
135131

136-
if response == exitUrlSuccess || response == exitNonUrlSuccess {
137-
opts.Browser.Browse(repoUrl)
132+
if repoUrl == ghrepo.GenerateRepoURL(baseRepo, "") {
133+
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", repoUrl)
138134
}
135+
opts.Browser.Browse(repoUrl)
139136

140-
return printExit(response, cmd, opts, repoUrl)
137+
return nil
141138
}
142139

143-
func addCombined(opts *BrowseOptions, url string, branchName string) (exitCode, string) {
140+
func addCombined(opts *BrowseOptions, url string, branchName string) (error, string) {
141+
help := "Use 'gh browse --help' for more information about browse\n"
144142

145143
if !opts.BranchFlag {
146-
return exitInvalidCombo, ""
144+
return fmt.Errorf("invalid use of flag and argument\n%s", help), ""
147145
}
148146

149147
if opts.AdditionalArg == "" {
150-
return exitUrlSuccess, url + "/tree/" + opts.SelectorArg
148+
url += "/tree/" + opts.SelectorArg
149+
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
150+
return nil, url
151151
}
152152

153153
arr := parseFileArg(opts)
154154
if len(arr) > 1 {
155-
return exitUrlSuccess, url + "/tree/" + opts.AdditionalArg + "/" + arr[0] + "#L" + arr[1]
155+
url += "/tree/" + opts.SelectorArg
156+
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
157+
return nil, url
156158
}
157159

158-
return exitUrlSuccess, url + "/tree/" + opts.AdditionalArg + "/" + arr[0]
160+
url += "/tree/" + opts.SelectorArg
161+
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
162+
return nil, url
159163
}
160164

161-
func addFlag(opts *BrowseOptions, url string) (exitCode, string) {
165+
func addFlag(opts *BrowseOptions, url string) (error, string) {
162166
if opts.ProjectsFlag {
163-
return exitUrlSuccess, url + "/projects"
167+
url += "/projects"
164168
} else if opts.SettingsFlag {
165-
return exitUrlSuccess, url + "/settings"
169+
url += "/settings"
166170
} else if opts.WikiFlag {
167-
return exitUrlSuccess, url + "/wiki"
171+
url += "/wiki"
168172
}
169-
return exitExpectedArg, ""
173+
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
174+
return nil, url
170175
}
171176

172-
func addArg(opts *BrowseOptions, url string, branchName string) (exitCode, string) {
177+
func addArg(opts *BrowseOptions, url string, branchName string) (error, string) {
178+
help := "Use 'gh browse --help' for more information about browse\n"
173179

174180
if opts.AdditionalArg != "" {
175-
return exitTooManyArgs, ""
181+
return fmt.Errorf("accepts 1 arg, 2 arg(s) were recieved\n%s", help), ""
176182
}
177183

178184
if isNumber(opts.SelectorArg) {
179185
url += "/issues/" + opts.SelectorArg
180-
return exitNonUrlSuccess, url
186+
fmt.Fprintf(opts.IO.Out, "now opening issue/pr in browser . . .\n")
187+
return nil, url
181188
}
182189

183190
arr := parseFileArg(opts)
184191
if len(arr) > 1 {
185-
return exitUrlSuccess, url + "/tree/" + branchName + "/" + arr[0] + "#L" + arr[1]
192+
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
193+
return nil, url + "/tree/" + branchName + "/" + arr[0] + "#L" + arr[1]
186194
}
187195

188-
return exitUrlSuccess, url + "/tree/" + branchName + "/" + arr[0]
196+
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
197+
return nil, url + "/tree/" + branchName + "/" + arr[0]
189198
}
190199

191200
func parseFileArg(opts *BrowseOptions) []string {
192201
arr := strings.Split(opts.SelectorArg, ":")
193202
return arr
194203
}
195204

196-
func printExit(exit exitCode, cmd *cobra.Command, opts *BrowseOptions, url string) error {
197-
w := opts.IO.ErrOut
198-
cs := opts.IO.ColorScheme()
199-
help := "Use 'gh browse --help' for more information about browse\n"
200-
201-
switch exit {
202-
case exitUrlSuccess:
203-
//fmt.Fprintf(w, "now opening %s in browser . . .\n", cs.Bold(url))
204-
fmt.Fprintln(w, "Hello World One")
205-
case exitNonUrlSuccess:
206-
//fmt.Fprintf(w, "now opening issue/pr in browser . . .\n")
207-
fmt.Fprintln(w, "Hello World Two")
208-
case exitNotInRepo:
209-
return fmt.Errorf("change directory to a repository to open in browser\n%s", help)
210-
case exitTooManyFlags:
211-
return fmt.Errorf("accepts 1 flag, %d flag(s) were recieved\n%s", getFlagAmount(cmd), help)
212-
case exitTooManyArgs:
213-
return fmt.Errorf("accepts 1 arg, 2 arg(s) were received \n%s", help)
214-
case exitExpectedArg:
215-
return fmt.Errorf("expected argument with this flag %s\n%s", cs.Bold(url), help)
216-
case exitInvalidCombo:
217-
return fmt.Errorf("invalid use of flag and argument\n%s", help)
218-
}
219-
return nil
220-
}
221-
222205
func hasFlag(cmd *cobra.Command) bool {
223206
return getFlagAmount(cmd) > 0
224207
}
@@ -231,10 +214,6 @@ func getFlagAmount(cmd *cobra.Command) int {
231214
return cmd.Flags().NFlag()
232215
}
233216

234-
func inRepo(err error) bool {
235-
return err == nil
236-
}
237-
238217
func isNumber(arg string) bool {
239218
_, err := strconv.Atoi(arg)
240219
if err != nil {

pkg/cmd/browse/browse_test.go

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package browse
22

33
import (
44
"bytes"
5-
//"io/ioutil"
5+
"io/ioutil"
66
"net/http"
77
"testing"
88

9+
"github.com/cli/cli/internal/config"
910
"github.com/cli/cli/internal/ghrepo"
1011
"github.com/cli/cli/internal/run"
1112
"github.com/cli/cli/pkg/cmdutil"
@@ -26,6 +27,7 @@ type testCase struct {
2627
errorExpected bool
2728
stdoutExpected string
2829
stderrExpected string
30+
urlExpected string
2931
}
3032

3133
func runCommand(rt http.RoundTripper, t testCase) (*test.CmdOut, error) {
@@ -39,8 +41,11 @@ func runCommand(rt http.RoundTripper, t testCase) (*test.CmdOut, error) {
3941
HttpClient: func() (*http.Client, error) {
4042
return &http.Client{Transport: rt}, nil
4143
},
44+
Config: func() (config.Config, error) {
45+
return config.NewBlankConfig(), nil
46+
},
4247
BaseRepo: func() (ghrepo.Interface, error) {
43-
return ghrepo.New("OWNER", "REPO"), nil
48+
return t.args.repo, nil
4449
},
4550
}
4651

@@ -53,8 +58,8 @@ func runCommand(rt http.RoundTripper, t testCase) (*test.CmdOut, error) {
5358
cmd.SetArgs(argv)
5459

5560
cmd.SetIn(&bytes.Buffer{})
56-
cmd.SetOut(stdout)
57-
cmd.SetErr(stderr)
61+
cmd.SetOut(ioutil.Discard)
62+
cmd.SetErr(ioutil.Discard)
5863

5964
_, err = cmd.ExecuteC()
6065
return &test.CmdOut{
@@ -64,33 +69,55 @@ func runCommand(rt http.RoundTripper, t testCase) (*test.CmdOut, error) {
6469
}, err
6570
}
6671
func TestNewCmdBrowse(t *testing.T) {
67-
6872
var tests = []testCase{
6973
{
70-
name: "test1",
74+
name: "multiple flag",
7175
args: args{
72-
repo: ghrepo.New("bchadwic", "cli"),
76+
repo: ghrepo.New("jessica", "cli"),
7377
cli: "--settings --projects",
7478
},
7579
errorExpected: true,
7680
stdoutExpected: "",
77-
stderrExpected: "Error: accepts 1 flag, 2 flag(s) were recieved\nUse 'gh browse --help' for more information about browse\n\n",
81+
stderrExpected: "accepts 1 flag, 2 flag(s) were recieved\nUse 'gh browse --help' for more information about browse\n",
82+
urlExpected: "",
83+
},
84+
{
85+
name: "settings flag",
86+
args: args{
87+
repo: ghrepo.New("husrav", "cli"),
88+
cli: "--settings",
89+
},
90+
errorExpected: false,
91+
stdoutExpected: "now opening https://github.com/husrav/cli/settings in browser . . .\n",
92+
stderrExpected: "",
93+
urlExpected: "https://github.com/husrav/cli/settings",
94+
},
95+
{
96+
name: "projects flag",
97+
args: args{
98+
repo: ghrepo.New("ben", "cli"),
99+
cli: "--projects",
100+
},
101+
errorExpected: false,
102+
stdoutExpected: "now opening https://github.com/ben/cli/projects in browser . . .\n",
103+
stderrExpected: "",
104+
urlExpected: "https://github.com/ben/cli/projects",
105+
},
106+
{
107+
name: "wiki flag",
108+
args: args{
109+
repo: ghrepo.New("thanh", "cli"),
110+
cli: "--wiki",
111+
},
112+
errorExpected: false,
113+
stdoutExpected: "now opening https://github.com/thanh/cli/wiki in browser . . .\n",
114+
stderrExpected: "",
115+
urlExpected: "https://github.com/thanh/cli/wiki",
78116
},
79-
// {
80-
// name: "test2",
81-
// args: args{
82-
// repo: ghrepo.New("bchadwic", "cli"),
83-
// cli: "--settings",
84-
// },
85-
// errorExpected: false,
86-
// stdoutExpected: "hello world",
87-
// stderrExpected: "hello world",
88-
// },
89117
}
90118

91119
for _, tt := range tests {
92120
t.Run(tt.name, func(t *testing.T) {
93-
94121
http := &httpmock.Registry{}
95122
defer http.Verify(t)
96123

@@ -100,28 +127,12 @@ func TestNewCmdBrowse(t *testing.T) {
100127
output, err := runCommand(http, tt)
101128

102129
if tt.errorExpected {
103-
assert.Error(t, err)
130+
assert.Equal(t, err.Error(), tt.stderrExpected)
131+
} else {
132+
assert.Equal(t, err, nil)
104133
}
105-
106-
assert.Contains(t, output.OutBuf.String(), tt.stdoutExpected) // success outputs
107-
108-
assert.Contains(t, output.ErrBuf.String(), tt.stderrExpected) // error outputs
109-
134+
assert.Equal(t, output.OutBuf.String(), tt.stdoutExpected)
135+
assert.Equal(t, tt.urlExpected, output.BrowsedURL)
110136
})
111137
}
112138
}
113-
114-
// http := initFakeHTTP()
115-
// defer http.Verify(t)
116-
117-
// _, cmdTeardown := run.Stub()
118-
// defer cmdTeardown(t)
119-
120-
// output, err := runCommand(http, true, "--web -a peter -l bug -l docs -L 10 -s merged -B trunk")
121-
// if err != nil {
122-
// t.Errorf("error running command `pr list` with `--web` flag: %v", err)
123-
// }
124-
125-
// assert.Equal(t, "", output.String())
126-
// assert.Equal(t, "Opening github.com/OWNER/REPO/pulls in your browser.\n", output.Stderr())
127-
// assert.Equal(t, "https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Amerged+assignee%3Apeter+label%3Abug+label%3Adocs+base%3Atrunk", output.BrowsedURL)

pkg/cmd/root/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
9696
repoResolvingCmdFactory := *f
9797
repoResolvingCmdFactory.BaseRepo = resolvedBaseRepo(f)
9898

99-
cmd.AddCommand(browseCmd.NewCmdBrowse(&repoResolvingCmdFactory)) // adds to the commands Commands()
99+
cmd.AddCommand(browseCmd.NewCmdBrowse(&repoResolvingCmdFactory))
100100
cmd.AddCommand(prCmd.NewCmdPR(&repoResolvingCmdFactory))
101101
cmd.AddCommand(issueCmd.NewCmdIssue(&repoResolvingCmdFactory))
102102
cmd.AddCommand(releaseCmd.NewCmdRelease(&repoResolvingCmdFactory))

0 commit comments

Comments
 (0)
X Tutup