Skip to content

Commit 5f64243

Browse files
authored
Merge pull request cli#44 from bchadwic/first-browse-pull
Restructured test file and discussed the structure of browse.go
2 parents 88ec5ad + 7999a45 commit 5f64243

File tree

3 files changed

+95
-141
lines changed

3 files changed

+95
-141
lines changed

pkg/cmd/browse/browse.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ type BrowseOptions struct {
3535
WikiFlag bool
3636
}
3737

38-
func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
39-
38+
func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Command {
4039
opts := &BrowseOptions{
4140
Browser: f.Browser,
4241
HttpClient: f.HttpClient,
@@ -93,6 +92,9 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
9392
opts.SelectorArg = args[0]
9493
}
9594

95+
if runF != nil {
96+
return runF(opts)
97+
}
9698
return runBrowse(opts)
9799
},
98100
}

pkg/cmd/browse/browse_test.go

Lines changed: 90 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,113 @@
11
package browse
22

33
import (
4-
"bytes"
5-
"io/ioutil"
64
"net/http"
75
"testing"
86

97
"github.com/cli/cli/internal/ghrepo"
10-
"github.com/cli/cli/internal/run"
118
"github.com/cli/cli/pkg/cmdutil"
129
"github.com/cli/cli/pkg/httpmock"
1310
"github.com/cli/cli/pkg/iostreams"
14-
"github.com/cli/cli/test"
15-
"github.com/google/shlex"
1611
"github.com/stretchr/testify/assert"
1712
)
1813

19-
func runCommand(rt http.RoundTripper, repo ghrepo.Interface, cli string) (*test.CmdOut, error) {
20-
io, _, stdout, stderr := iostreams.Test()
21-
22-
browser := &cmdutil.TestBrowser{}
23-
factory := &cmdutil.Factory{
24-
IOStreams: io,
25-
Browser: browser,
14+
func TestNewCmdBrowse(t *testing.T) {
15+
f := cmdutil.Factory{}
16+
var opts *BrowseOptions
17+
// pass a stub implementation of `runBrowse` for testing to avoid having real `runBrowse` called
18+
cmd := NewCmdBrowse(&f, func(o *BrowseOptions) error {
19+
opts = o
20+
return nil
21+
})
22+
23+
cmd.SetArgs([]string{"--branch", "main"})
24+
_, err := cmd.ExecuteC()
25+
assert.NoError(t, err)
26+
27+
assert.Equal(t, "main", opts.Branch)
28+
assert.Equal(t, "", opts.SelectorArg)
29+
assert.Equal(t, false, opts.ProjectsFlag)
30+
assert.Equal(t, false, opts.WikiFlag)
31+
assert.Equal(t, false, opts.SettingsFlag)
32+
assert.Equal(t, 1, opts.FlagAmount)
33+
}
2634

27-
HttpClient: func() (*http.Client, error) {
28-
return &http.Client{Transport: rt}, nil
35+
func Test_runBrowse(t *testing.T) {
36+
tests := []struct {
37+
name string
38+
opts BrowseOptions
39+
baseRepo ghrepo.Interface
40+
defaultBranch string
41+
expectedURL string
42+
wantsErr bool
43+
}{
44+
{
45+
name: "no arguments",
46+
opts: BrowseOptions{
47+
SelectorArg: "",
48+
FlagAmount: 0,
49+
},
50+
baseRepo: ghrepo.New("jessica", "cli"),
51+
expectedURL: "https://github.com/jessica/cli",
2952
},
30-
BaseRepo: func() (ghrepo.Interface, error) {
31-
return repo, nil
53+
{
54+
name: "file argument",
55+
opts: BrowseOptions{SelectorArg: "path/to/file.txt"},
56+
baseRepo: ghrepo.New("bchadwic", "cli"),
57+
defaultBranch: "main",
58+
expectedURL: "https://github.com/bchadwic/cli/tree/main/path/to/file.txt",
3259
},
33-
}
34-
35-
cmd := NewCmdBrowse(factory)
36-
37-
argv, err := shlex.Split(cli)
38-
if err != nil {
39-
return nil, err
40-
}
41-
cmd.SetArgs(argv)
42-
43-
cmd.SetIn(&bytes.Buffer{})
44-
cmd.SetOut(ioutil.Discard)
45-
cmd.SetErr(ioutil.Discard)
46-
47-
_, err = cmd.ExecuteC()
48-
return &test.CmdOut{
49-
OutBuf: stdout,
50-
ErrBuf: stderr,
51-
BrowsedURL: browser.BrowsedURL(),
52-
}, err
53-
}
54-
// func TestNewCmdBrowse(t *testing.T) {
55-
56-
// type args struct {
57-
// repo ghrepo.Interface
58-
// cli string
59-
// }
60-
61-
// tests := []struct {
62-
// name string
63-
// args args
64-
// errorExpected bool
65-
// stdoutExpected string
66-
// stderrExpected string
67-
// urlExpected string
68-
// }{
69-
// {
70-
// name: "multiple flag",
71-
// args: args{
72-
// repo: ghrepo.New("jessica", "cli"),
73-
// cli: "--settings --projects",
74-
// },
75-
// errorExpected: true,
76-
// stdoutExpected: "",
77-
// stderrExpected: "these two flags are incompatible, see below for instructions",
78-
// urlExpected: "",
79-
// },
80-
// {
81-
// name: "settings flag",
82-
// args: args{
83-
// repo: ghrepo.New("husrav", "cli"),
84-
// cli: "--settings",
85-
// },
86-
// errorExpected: false,
87-
// stdoutExpected: "now opening https://github.com/husrav/cli/settings in browser . . .\n",
88-
// stderrExpected: "",
89-
// urlExpected: "https://github.com/husrav/cli/settings",
90-
// },
91-
// {
92-
// name: "projects flag",
93-
// args: args{
94-
// repo: ghrepo.New("ben", "cli"),
95-
// cli: "--projects",
96-
// },
97-
// errorExpected: false,
98-
// stdoutExpected: "now opening https://github.com/ben/cli/projects in browser . . .\n",
99-
// stderrExpected: "",
100-
// urlExpected: "https://github.com/ben/cli/projects",
101-
// },
102-
// {
103-
// name: "wiki flag",
104-
// args: args{
105-
// repo: ghrepo.New("thanh", "cli"),
106-
// cli: "--wiki",
107-
// },
108-
// errorExpected: false,
109-
// stdoutExpected: "now opening https://github.com/thanh/cli/wiki in browser . . .\n",
110-
// stderrExpected: "",
111-
// urlExpected: "https://github.com/thanh/cli/wiki",
112-
// },
113-
// }
114-
115-
// for _, tt := range tests {
116-
// t.Run(tt.name, func(t *testing.T) {
117-
// http := &httpmock.Registry{}
118-
// // http.StubRepoInfoResponse(tt.args.repo.RepoHost(), tt.args.repo.RepoName(), "main")
119-
// defer http.Verify(t)
120-
121-
// _, cmdTeardown := run.Stub()
122-
// defer cmdTeardown(t)
123-
124-
// output, err := runCommand(http, tt.args.repo, tt.args.cli)
125-
126-
// if tt.errorExpected {
127-
// assert.Equal(t, err.Error(), tt.stderrExpected)
128-
// } else {
129-
// assert.Equal(t, err, nil)
130-
// }
131-
// assert.Equal(t, tt.stdoutExpected, output.OutBuf.String())
132-
// assert.Equal(t, tt.urlExpected, output.BrowsedURL)
133-
// })
134-
// }
135-
// }
136-
137-
func TestBrowse(t *testing.T) {
138-
tests := []BrowseOptions{
13960
{
140-
141-
BaseRepo func() (ghrepo.Interface, error)
142-
Browser browser
143-
HttpClient func() (*http.Client, error)
144-
IO *iostreams.IOStreams
145-
146-
FlagAmount int
147-
SelectorArg string
148-
149-
Branch string
150-
ProjectsFlag bool
151-
RepoFlag bool
152-
SettingsFlag bool
153-
WikiFlag bool
61+
name: "branch flag",
62+
opts: BrowseOptions{
63+
Branch: "trunk",
64+
FlagAmount: 1,
65+
},
66+
baseRepo: ghrepo.New("bchadwic", "cli"),
67+
expectedURL: "https://github.com/bchadwic/cli/tree/trunk",
68+
},
69+
{
70+
name: "settings flag",
71+
opts: BrowseOptions{
72+
SettingsFlag: true,
73+
FlagAmount: 1,
74+
},
75+
baseRepo: ghrepo.New("bchadwic", "cli"),
76+
expectedURL: "https://github.com/bchadwic/cli/settings",
15477
},
15578
}
156-
for _,tt range tests {
79+
for _, tt := range tests {
80+
t.Run(tt.name, func(t *testing.T) {
81+
io, _, stdout, stderr := iostreams.Test()
82+
browser := cmdutil.TestBrowser{}
83+
84+
reg := httpmock.Registry{}
85+
defer reg.Verify(t)
86+
if tt.defaultBranch != "" {
87+
reg.StubRepoInfoResponse(tt.baseRepo.RepoOwner(), tt.baseRepo.RepoName(), tt.defaultBranch)
88+
}
89+
90+
opts := tt.opts
91+
opts.IO = io
92+
opts.BaseRepo = func() (ghrepo.Interface, error) {
93+
return tt.baseRepo, nil
94+
}
95+
opts.HttpClient = func() (*http.Client, error) {
96+
return &http.Client{Transport: &reg}, nil
97+
}
98+
opts.Browser = &browser
99+
100+
err := runBrowse(&opts)
101+
if tt.wantsErr {
102+
assert.Error(t, err)
103+
} else {
104+
assert.NoError(t, err)
105+
}
157106

107+
assert.Equal(t, "", stdout.String())
108+
assert.Equal(t, "", stderr.String())
109+
browser.Verify(t, tt.expectedURL)
110+
})
158111
}
159112
}
160113

@@ -198,6 +151,5 @@ func TestFileArgParsing(t *testing.T) {
198151
}
199152
assert.Equal(t, tt.fileArg, arr[0])
200153
}
201-
202154
}
203155
}

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))
99+
cmd.AddCommand(browseCmd.NewCmdBrowse(&repoResolvingCmdFactory, nil))
100100
cmd.AddCommand(prCmd.NewCmdPR(&repoResolvingCmdFactory))
101101
cmd.AddCommand(issueCmd.NewCmdIssue(&repoResolvingCmdFactory))
102102
cmd.AddCommand(releaseCmd.NewCmdRelease(&repoResolvingCmdFactory))

0 commit comments

Comments
 (0)