@@ -8,27 +8,121 @@ import (
88 "github.com/cli/cli/pkg/cmdutil"
99 "github.com/cli/cli/pkg/httpmock"
1010 "github.com/cli/cli/pkg/iostreams"
11+ "github.com/google/shlex"
1112 "github.com/stretchr/testify/assert"
1213)
1314
1415func 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- })
16+ tests := []struct {
17+ name string
18+ cli string
19+ factory func (* cmdutil.Factory ) * cmdutil.Factory
20+ wants BrowseOptions
21+ wantsErr bool
22+ }{
23+ {
24+ name : "no arguments" ,
25+ cli : "" ,
26+ wantsErr : false ,
27+ },
28+ {
29+ name : "settings flag" ,
30+ cli : "--settings" ,
31+ wants : BrowseOptions {
32+ SettingsFlag : true ,
33+ },
34+ wantsErr : false ,
35+ },
36+ {
37+ name : "projects flag" ,
38+ cli : "--projects" ,
39+ wants : BrowseOptions {
40+ ProjectsFlag : true ,
41+ },
42+ wantsErr : false ,
43+ },
44+ {
45+ name : "wiki flag" ,
46+ cli : "--wiki" ,
47+ wants : BrowseOptions {
48+ WikiFlag : true ,
49+ },
50+ wantsErr : false ,
51+ },
52+ {
53+ name : "branch flag" ,
54+ cli : "--branch main" ,
55+ wants : BrowseOptions {
56+ Branch : "main" ,
57+ },
58+ wantsErr : false ,
59+ },
60+ {
61+ name : "branch flag without a branch name" ,
62+ cli : "--branch" ,
63+ wantsErr : true ,
64+ },
65+ {
66+ name : "combination: settings projects" ,
67+ cli : "--settings --projects" ,
68+ wants : BrowseOptions {
69+ SettingsFlag : true ,
70+ ProjectsFlag : true ,
71+ },
72+ wantsErr : true ,
73+ },
74+ {
75+ name : "combination: projects wiki" ,
76+ cli : "--projects --wiki" ,
77+ wants : BrowseOptions {
78+ ProjectsFlag : true ,
79+ WikiFlag : true ,
80+ },
81+ wantsErr : true ,
82+ },
83+ {
84+ name : "passed argument" ,
85+ cli : "main.go" ,
86+ wants : BrowseOptions {
87+ SelectorArg : "main.go" ,
88+ },
89+ wantsErr : false ,
90+ },
91+ {
92+ name : "no arguments" ,
93+ cli : "" ,
94+ wantsErr : false ,
95+ },
96+ }
97+ for _ , tt := range tests {
98+ t .Run (tt .name , func (t * testing.T ) {
99+ f := cmdutil.Factory {}
100+ var opts * BrowseOptions
101+ // pass a stub implementation of `runBrowse` for testing to avoid having real `runBrowse` called
102+ cmd := NewCmdBrowse (& f , func (o * BrowseOptions ) error {
103+ opts = o
104+ return nil
105+ })
106+ argv , err := shlex .Split (tt .cli )
107+ assert .NoError (t , err )
108+ cmd .SetArgs (argv )
109+ _ , err = cmd .ExecuteC ()
110+
111+ if tt .wantsErr {
112+ assert .Error (t , err )
113+ return
22114
23- cmd . SetArgs ([] string { "--branch" , "main" })
24- _ , err := cmd . ExecuteC ( )
25- assert . NoError ( t , err )
115+ } else {
116+ assert . NoError ( t , err )
117+ }
26118
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 )
119+ assert .Equal (t , tt .wants .Branch , opts .Branch )
120+ assert .Equal (t , tt .wants .SelectorArg , opts .SelectorArg )
121+ assert .Equal (t , tt .wants .ProjectsFlag , opts .ProjectsFlag )
122+ assert .Equal (t , tt .wants .WikiFlag , opts .WikiFlag )
123+ assert .Equal (t , tt .wants .SettingsFlag , opts .SettingsFlag )
124+ })
125+ }
32126}
33127
34128func Test_runBrowse (t * testing.T ) {
@@ -96,6 +190,33 @@ func Test_runBrowse(t *testing.T) {
96190 baseRepo : ghrepo .New ("bchadwic" , "cli" ),
97191 expectedURL : "https://github.com/bchadwic/cli/issues/217" ,
98192 },
193+ {
194+ name : "file with line argument" ,
195+ opts : BrowseOptions {
196+ SelectorArg : "path/to/file.txt:32" ,
197+ },
198+ baseRepo : ghrepo .New ("bchadwic" , "cli" ),
199+ defaultBranch : "trunk" ,
200+ expectedURL : "https://github.com/bchadwic/cli/tree/trunk/path/to/file.txt#L32" ,
201+ },
202+ {
203+ name : "file with invalid line number" ,
204+ opts : BrowseOptions {
205+ SelectorArg : "path/to/file.txt:32:32" ,
206+ },
207+ baseRepo : ghrepo .New ("bchadwic" , "cli" ),
208+ defaultBranch : "trunk" ,
209+ wantsErr : true ,
210+ },
211+ {
212+ name : "file with line argument" ,
213+ opts : BrowseOptions {
214+ SelectorArg : "path/to/file.txt:32" ,
215+ },
216+ baseRepo : ghrepo .New ("bchadwic" , "cli" ),
217+ defaultBranch : "trunk" ,
218+ expectedURL : "https://github.com/bchadwic/cli/tree/trunk/path/to/file.txt#L32" ,
219+ },
99220 }
100221
101222 for _ , tt := range tests {
0 commit comments