Skip to content

Commit e5b81fb

Browse files
committed
Started working on testing for parseFileArgs
1 parent 1657cf4 commit e5b81fb

File tree

2 files changed

+67
-24
lines changed

2 files changed

+67
-24
lines changed

pkg/cmd/browse/browse.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func addBranch(opts *BrowseOptions, url string) (error, string) {
150150
if opts.SelectorArg == "" {
151151
url += "/tree/" + opts.Branch
152152
} else {
153-
err, arr := parseFileArg(opts)
153+
err, arr := parseFileArg(opts.SelectorArg)
154154
if err != nil {
155155
return err, url
156156
}
@@ -181,7 +181,7 @@ func addArg(opts *BrowseOptions, url string, branchName string) (error, string)
181181
url += "/issues/" + opts.SelectorArg
182182
fmt.Fprintf(opts.IO.Out, "now opening issue/pr in browser . . .\n")
183183
} else {
184-
err, arr := parseFileArg(opts)
184+
err, arr := parseFileArg(opts.SelectorArg)
185185
if err != nil {
186186
return err, url
187187
}
@@ -195,13 +195,13 @@ func addArg(opts *BrowseOptions, url string, branchName string) (error, string)
195195
return nil, url
196196
}
197197

198-
func parseFileArg(opts *BrowseOptions) (error, []string) {
199-
arr := strings.Split(opts.SelectorArg, ":")
198+
func parseFileArg(fileArg string) (error, []string) {
199+
arr := strings.Split(fileArg, ":")
200200
if len(arr) > 2 {
201201
return fmt.Errorf("invalid use of colon\nUse 'gh browse --help' for more information about browse\n"), arr
202202
}
203203
if len(arr) > 1 && !isNumber(arr[1]) {
204-
return fmt.Errorf("nvalid line number after colon\nUse 'gh browse --help' for more information about browse\n"), arr
204+
return fmt.Errorf("invalid line number after colon\nUse 'gh browse --help' for more information about browse\n"), arr
205205
}
206206
return nil, arr
207207
}

pkg/cmd/browse/browse_test.go

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,7 @@ import (
1616
"github.com/stretchr/testify/assert"
1717
)
1818

19-
type args struct {
20-
repo ghrepo.Interface
21-
cli string
22-
}
23-
type testCase struct {
24-
name string
25-
args args
26-
errorExpected bool
27-
stdoutExpected string
28-
stderrExpected string
29-
urlExpected string
30-
}
31-
32-
func runCommand(rt http.RoundTripper, t testCase) (*test.CmdOut, error) {
19+
func runCommand(rt http.RoundTripper, repo ghrepo.Interface, cli string) (*test.CmdOut, error) {
3320
io, _, stdout, stderr := iostreams.Test()
3421

3522
browser := &cmdutil.TestBrowser{}
@@ -41,13 +28,13 @@ func runCommand(rt http.RoundTripper, t testCase) (*test.CmdOut, error) {
4128
return &http.Client{Transport: rt}, nil
4229
},
4330
BaseRepo: func() (ghrepo.Interface, error) {
44-
return t.args.repo, nil
31+
return repo, nil
4532
},
4633
}
4734

4835
cmd := NewCmdBrowse(factory)
4936

50-
argv, err := shlex.Split(t.args.cli)
37+
argv, err := shlex.Split(cli)
5138
if err != nil {
5239
return nil, err
5340
}
@@ -65,7 +52,20 @@ func runCommand(rt http.RoundTripper, t testCase) (*test.CmdOut, error) {
6552
}, err
6653
}
6754
func TestNewCmdBrowse(t *testing.T) {
68-
var tests = []testCase{
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+
}{
6969
{
7070
name: "multiple flag",
7171
args: args{
@@ -74,7 +74,7 @@ func TestNewCmdBrowse(t *testing.T) {
7474
},
7575
errorExpected: true,
7676
stdoutExpected: "",
77-
stderrExpected: "accepts 1 flag, 2 flag(s) were recieved",
77+
stderrExpected: "these two flags are incompatible, see below for instructions",
7878
urlExpected: "",
7979
},
8080
{
@@ -131,7 +131,7 @@ func TestNewCmdBrowse(t *testing.T) {
131131
_, cmdTeardown := run.Stub()
132132
defer cmdTeardown(t)
133133

134-
output, err := runCommand(http, tt)
134+
output, err := runCommand(http, tt.args.repo, tt.args.cli)
135135

136136
if tt.errorExpected {
137137
assert.Equal(t, err.Error(), tt.stderrExpected)
@@ -143,3 +143,46 @@ func TestNewCmdBrowse(t *testing.T) {
143143
})
144144
}
145145
}
146+
func TestFileArgParsing(t *testing.T) {
147+
tests := []struct {
148+
name string
149+
arg string
150+
errorExpected bool
151+
fileArg string
152+
lineArg string
153+
stderrExpected string
154+
}{
155+
{
156+
name: "non line number",
157+
arg: "main.go",
158+
errorExpected: false,
159+
fileArg: "main.go",
160+
},
161+
{
162+
name: "line number",
163+
arg: "main.go:32",
164+
errorExpected: false,
165+
fileArg: "main.go",
166+
lineArg: "32",
167+
},
168+
{
169+
name: "non line number error",
170+
arg: "ma:in.go",
171+
errorExpected: true,
172+
stderrExpected: "invalid line number after colon\nUse 'gh browse --help' for more information about browse\n",
173+
},
174+
}
175+
for _, tt := range tests {
176+
err, arr := parseFileArg(tt.arg)
177+
if tt.errorExpected {
178+
assert.Equal(t, err.Error(), tt.stderrExpected)
179+
} else {
180+
assert.Equal(t, err, nil)
181+
if len(arr) > 1 {
182+
assert.Equal(t, tt.lineArg, arr[1])
183+
}
184+
assert.Equal(t, tt.fileArg, arr[0])
185+
}
186+
187+
}
188+
}

0 commit comments

Comments
 (0)