88
99 "github.com/go-git/go-git/v5"
1010 "github.com/spf13/cobra"
11- "path/filepath"
1211 "github.com/zhaochunqi/git-open/internal/testhelper"
12+ "path/filepath"
1313)
1414
1515func Test_rootCmd (t * testing.T ) {
@@ -46,7 +46,7 @@ func Test_rootCmd(t *testing.T) {
4646 cmd := & cobra.Command {}
4747 buf := new (bytes.Buffer )
4848 cmd .SetOut (buf )
49-
49+
5050 // Set flags
5151 cmd .Flags ().Bool ("plain" , false , "" )
5252 if err := cmd .Flags ().Set ("plain" , "true" ); err != nil {
@@ -69,33 +69,33 @@ func Test_rootCmd(t *testing.T) {
6969
7070func Test_Execute (t * testing.T ) {
7171 tests := []struct {
72- name string
73- args []string
74- wantError bool
72+ name string
73+ args []string
74+ wantError bool
7575 expectedURL string // New field to store the expected URL
7676 }{
7777 {
78- name : "normal execution - github main branch" ,
79- wantError : false ,
78+ name : "normal execution - github main branch" ,
79+ wantError : false ,
8080 expectedURL : "https://github.com/zhaochunqi/git-open" ,
8181 },
8282 {
83- name : "normal execution - github feature branch" ,
84- wantError : false ,
83+ name : "normal execution - github feature branch" ,
84+ wantError : false ,
8585 expectedURL : "https://github.com/zhaochunqi/git-open/tree/feature-branch" ,
8686 },
8787 {
88- name : "normal execution - gitlab feature branch" ,
89- wantError : false ,
88+ name : "normal execution - gitlab feature branch" ,
89+ wantError : false ,
9090 expectedURL : "https://gitlab.com/zhaochunqi/git-open/-/tree/feature-branch" ,
9191 },
9292 {
93- name : "normal execution - bitbucket feature branch" ,
94- wantError : false ,
93+ name : "normal execution - bitbucket feature branch" ,
94+ wantError : false ,
9595 expectedURL : "https://bitbucket.org/zhaochunqi/git-open/src/feature-branch" ,
9696 },
9797 {
98- name : "no git repo" ,
98+ name : "no git repo" ,
9999 wantError : true ,
100100 },
101101 }
@@ -105,7 +105,7 @@ func Test_Execute(t *testing.T) {
105105 // Save and restore os.Args
106106 oldArgs := os .Args
107107 defer func () { os .Args = oldArgs }()
108-
108+
109109 if tt .args != nil {
110110 os .Args = tt .args
111111 }
@@ -168,11 +168,11 @@ func Test_initConfig(t *testing.T) {
168168 wantErr bool
169169 }{
170170 {
171- name : "normal config" ,
171+ name : "normal config" ,
172172 wantErr : false ,
173173 },
174174 {
175- name : "with config file" ,
175+ name : "with config file" ,
176176 wantErr : false ,
177177 },
178178 }
@@ -189,18 +189,18 @@ func Test_initConfig(t *testing.T) {
189189 })
190190
191191 // Create temporary home directory
192- tmpHome , err := os .MkdirTemp ("" , "home" )
193- if err != nil {
194- t .Fatal (err )
195- }
196- t .Cleanup (func () {
197- os .RemoveAll (tmpHome )
198- })
192+ tmpHome , err := os .MkdirTemp ("" , "home" )
193+ if err != nil {
194+ t .Fatal (err )
195+ }
196+ t .Cleanup (func () {
197+ os .RemoveAll (tmpHome )
198+ })
199199
200200 // Set temporary home directory
201- if err := os .Setenv ("HOME" , tmpHome ); err != nil {
202- t .Fatal (err )
203- }
201+ if err := os .Setenv ("HOME" , tmpHome ); err != nil {
202+ t .Fatal (err )
203+ }
204204 case "with config file" :
205205 // Save original home directory
206206 origHome := os .Getenv ("HOME" )
@@ -209,30 +209,30 @@ func Test_initConfig(t *testing.T) {
209209 })
210210
211211 // Create temporary home directory
212- tmpHome , err := os .MkdirTemp ("" , "home" )
213- if err != nil {
214- t .Fatal (err )
215- }
216- t .Cleanup (func () {
217- os .RemoveAll (tmpHome )
218- })
212+ tmpHome , err := os .MkdirTemp ("" , "home" )
213+ if err != nil {
214+ t .Fatal (err )
215+ }
216+ t .Cleanup (func () {
217+ os .RemoveAll (tmpHome )
218+ })
219219
220220 // Create .git-open directory
221- configDir := filepath .Join (tmpHome , ".git-open" )
222- if err := os .MkdirAll (configDir , 0755 ); err != nil {
223- t .Fatal (err )
224- }
221+ configDir := filepath .Join (tmpHome , ".git-open" )
222+ if err := os .MkdirAll (configDir , 0755 ); err != nil {
223+ t .Fatal (err )
224+ }
225225
226226 // Create config file
227- configFile := filepath .Join (configDir , "config.yaml" )
228- if err := os .WriteFile (configFile , []byte ("browser: firefox" ), 0644 ); err != nil {
229- t .Fatal (err )
230- }
227+ configFile := filepath .Join (configDir , "config.yaml" )
228+ if err := os .WriteFile (configFile , []byte ("browser: firefox" ), 0644 ); err != nil {
229+ t .Fatal (err )
230+ }
231231
232232 // Set temporary home directory
233- if err := os .Setenv ("HOME" , tmpHome ); err != nil {
234- t .Fatal (err )
235- }
233+ if err := os .Setenv ("HOME" , tmpHome ); err != nil {
234+ t .Fatal (err )
235+ }
236236 }
237237
238238 initConfig ()
@@ -245,7 +245,7 @@ func Test_rootCmd_ErrorHandling(t *testing.T) {
245245 originalGetRemoteURLFunc := getRemoteURLFunc
246246 originalGetBranchNameFunc := getBranchNameFunc
247247 originalOpenURLInBrowser := OpenURLInBrowser
248-
248+
249249 defer func () {
250250 getCurrentGitDirectoryFunc = originalGetCurrentGitDirectoryFunc
251251 getRemoteURLFunc = originalGetRemoteURLFunc
@@ -272,7 +272,7 @@ func Test_rootCmd_ErrorHandling(t *testing.T) {
272272 setup : func () {
273273 _ , cleanup := testhelper .SetupTestRepo (t , "https://github.com/test/repo.git" , "main" )
274274 t .Cleanup (cleanup )
275-
275+
276276 getRemoteURLFunc = func (repo * git.Repository ) (string , error ) {
277277 return "" , errors .New ("remote URL error" )
278278 }
@@ -284,7 +284,7 @@ func Test_rootCmd_ErrorHandling(t *testing.T) {
284284 setup : func () {
285285 _ , cleanup := testhelper .SetupTestRepo (t , "https://github.com/test/repo.git" , "main" )
286286 t .Cleanup (cleanup )
287-
287+
288288 getBranchNameFunc = func (repo * git.Repository ) (string , error ) {
289289 return "" , errors .New ("branch name error" )
290290 }
@@ -296,7 +296,7 @@ func Test_rootCmd_ErrorHandling(t *testing.T) {
296296 setup : func () {
297297 _ , cleanup := testhelper .SetupTestRepo (t , "https://github.com/test/repo.git" , "main" )
298298 t .Cleanup (cleanup )
299-
299+
300300 OpenURLInBrowser = func (url string ) error {
301301 return errors .New ("browser error" )
302302 }
@@ -312,18 +312,18 @@ func Test_rootCmd_ErrorHandling(t *testing.T) {
312312 getRemoteURLFunc = originalGetRemoteURLFunc
313313 getBranchNameFunc = originalGetBranchNameFunc
314314 OpenURLInBrowser = originalOpenURLInBrowser
315-
315+
316316 tt .setup ()
317-
317+
318318 // Create a buffer to capture stderr
319319 buf := new (bytes.Buffer )
320320 cmd := & cobra.Command {}
321321 cmd .SetErr (buf )
322-
322+
323323 // Run the root command function
324324 runE := rootCmd .RunE
325325 err := runE (cmd , []string {})
326-
326+
327327 if (err != nil ) != tt .wantErr {
328328 t .Errorf ("rootCmd.RunE() error = %v, wantErr %v" , err , tt .wantErr )
329329 }
@@ -355,7 +355,7 @@ func Test_initConfig_ErrorCases(t *testing.T) {
355355 t .Cleanup (func () {
356356 os .Remove (tmpFile .Name ())
357357 })
358-
358+
359359 cfgFile = tmpFile .Name ()
360360 },
361361 },
@@ -381,7 +381,7 @@ func Test_initConfig_ErrorCases(t *testing.T) {
381381 if err := os .Setenv ("HOME" , tmpHome ); err != nil {
382382 t .Fatal (err )
383383 }
384-
384+
385385 cfgFile = ""
386386 },
387387 },
@@ -393,4 +393,4 @@ func Test_initConfig_ErrorCases(t *testing.T) {
393393 initConfig ()
394394 })
395395 }
396- }
396+ }
0 commit comments