@@ -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}
6754func 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\n Use '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