@@ -3,15 +3,17 @@ package cmd
33import (
44 "fmt"
55 "os"
6+ "strings"
67 "testing"
78
89 "github.com/go-git/go-git/v5"
910 "github.com/go-git/go-git/v5/config"
11+ "github.com/zhaochunqi/git-open/internal/testhelper"
1012)
1113
1214func Test_getCurrentGitDirectory (t * testing.T ) {
1315 // Use setupTestRepo for setup
14- _ , cleanup := SetupTestRepo (t , "https://github.com/zhaochunqi/git-open.git" , "main" )
16+ _ , cleanup := testhelper . SetupTestRepo (t , "https://github.com/zhaochunqi/git-open.git" , "main" )
1517 defer cleanup ()
1618
1719 tests := []struct {
@@ -118,41 +120,14 @@ func Test_getRemoteURL(t *testing.T) {
118120 want : "" ,
119121 wantErr : true ,
120122 },
121- {
122- name : "empty remote URL list" ,
123- remoteURL : "https://github.com/zhaochunqi/git-open.git" ,
124- setup : func (t * testing.T , repo * git.Repository ) {
125- // Standard setup is fine, we'll use customTest
126- },
127- customTest : func (t * testing.T , repo * git.Repository ) {
128- // Create a mock remote config with empty URLs
129- mockConfig := & config.RemoteConfig {
130- Name : "origin" ,
131- URLs : []string {},
132- }
133123
134- // Test the function with our mock config
135- url , err := getRemoteURLFromConfig (mockConfig )
136- if err == nil {
137- t .Error ("Expected error for empty URLs, got nil" )
138- }
139- if url != "" {
140- t .Errorf ("Expected empty URL, got %s" , url )
141- }
142- if err != nil && err .Error () != "remote URL not found" {
143- t .Errorf ("Expected error message 'remote URL not found', got '%s'" , err .Error ())
144- }
145- },
146- want : "" ,
147- wantErr : true ,
148- },
149124 }
150125
151126 for _ , tt := range tests {
152127 t .Run (tt .name , func (t * testing.T ) {
153128 // If we have a custom test, run it instead of the standard test
154129 if tt .customTest != nil {
155- _ , cleanup := SetupTestRepo (t , tt .remoteURL , "main" )
130+ _ , cleanup := testhelper . SetupTestRepo (t , tt .remoteURL , "main" )
156131 defer cleanup ()
157132
158133 repo , err := getCurrentGitDirectory ()
@@ -170,7 +145,7 @@ func Test_getRemoteURL(t *testing.T) {
170145 }
171146
172147 // Standard test path
173- _ , cleanup := SetupTestRepo (t , tt .remoteURL , "main" )
148+ _ , cleanup := testhelper . SetupTestRepo (t , tt .remoteURL , "main" )
174149 defer cleanup ()
175150
176151 repo , err := getCurrentGitDirectory ()
@@ -231,6 +206,18 @@ func Test_convertToWebURL(t *testing.T) {
231206 want : "" ,
232207 wantErr : true ,
233208 },
209+ {
210+ name : "ssh url with ssh prefix" ,
211+ url : "ssh://git@github.com/user/repo.git" ,
212+ want : "https://github.com/user/repo" ,
213+ wantErr : false ,
214+ },
215+ {
216+ name : "http url without git suffix" ,
217+ url : "http://github.com/user/repo" ,
218+ want : "http://github.com/user/repo" ,
219+ wantErr : false ,
220+ },
234221 }
235222
236223 for _ , tt := range tests {
@@ -243,6 +230,139 @@ func Test_convertToWebURL(t *testing.T) {
243230 }
244231}
245232
233+ func Test_getBranchName (t * testing.T ) {
234+ tests := []struct {
235+ name string
236+ remoteURL string
237+ branchName string
238+ setup func (t * testing.T , repo * git.Repository )
239+ wantErr bool
240+ }{
241+ {
242+ name : "main branch" ,
243+ remoteURL : "https://github.com/zhaochunqi/git-open.git" ,
244+ branchName : "main" ,
245+ wantErr : false ,
246+ },
247+ {
248+ name : "feature branch" ,
249+ remoteURL : "https://github.com/zhaochunqi/git-open.git" ,
250+ branchName : "feature-branch" ,
251+ wantErr : false ,
252+ },
253+
254+ }
255+
256+ for _ , tt := range tests {
257+ t .Run (tt .name , func (t * testing.T ) {
258+ _ , cleanup := testhelper .SetupTestRepo (t , tt .remoteURL , tt .branchName )
259+ defer cleanup ()
260+
261+ repo , err := getCurrentGitDirectory ()
262+ if err != nil {
263+ t .Fatal (err )
264+ }
265+
266+ if tt .setup != nil {
267+ tt .setup (t , repo )
268+ }
269+
270+ got , err := getBranchName (repo )
271+ if (err != nil ) != tt .wantErr {
272+ t .Errorf ("getBranchName() error = %v, wantErr %v" , err , tt .wantErr )
273+ return
274+ }
275+ if ! tt .wantErr && got != tt .branchName {
276+ t .Errorf ("getBranchName() = %v, want %v" , got , tt .branchName )
277+ }
278+ })
279+ }
280+ }
281+
282+ func Test_getHostingService (t * testing.T ) {
283+ tests := []struct {
284+ name string
285+ remoteURL string
286+ want HostingService
287+ }{
288+ {
289+ name : "github" ,
290+ remoteURL : "https://github.com/user/repo.git" ,
291+ want : GitHub ,
292+ },
293+ {
294+ name : "gitlab" ,
295+ remoteURL : "https://gitlab.com/user/repo.git" ,
296+ want : GitLab ,
297+ },
298+ {
299+ name : "bitbucket" ,
300+ remoteURL : "https://bitbucket.org/user/repo.git" ,
301+ want : Bitbucket ,
302+ },
303+ {
304+ name : "unknown service" ,
305+ remoteURL : "https://example.com/user/repo.git" ,
306+ want : Unknown ,
307+ },
308+ }
309+
310+ for _ , tt := range tests {
311+ t .Run (tt .name , func (t * testing.T ) {
312+ if got := getHostingService (tt .remoteURL ); got != tt .want {
313+ t .Errorf ("getHostingService() = %v, want %v" , got , tt .want )
314+ }
315+ })
316+ }
317+ }
318+
319+ func Test_buildBranchURL (t * testing.T ) {
320+ tests := []struct {
321+ name string
322+ baseURL string
323+ branch string
324+ remoteURL string
325+ want string
326+ }{
327+ {
328+ name : "github branch url" ,
329+ baseURL : "https://github.com/user/repo" ,
330+ branch : "feature" ,
331+ remoteURL : "https://github.com/user/repo.git" ,
332+ want : "https://github.com/user/repo/tree/feature" ,
333+ },
334+ {
335+ name : "gitlab branch url" ,
336+ baseURL : "https://gitlab.com/user/repo" ,
337+ branch : "feature" ,
338+ remoteURL : "https://gitlab.com/user/repo.git" ,
339+ want : "https://gitlab.com/user/repo/-/tree/feature" ,
340+ },
341+ {
342+ name : "bitbucket branch url" ,
343+ baseURL : "https://bitbucket.org/user/repo" ,
344+ branch : "feature" ,
345+ remoteURL : "https://bitbucket.org/user/repo.git" ,
346+ want : "https://bitbucket.org/user/repo/src/feature" ,
347+ },
348+ {
349+ name : "unknown service defaults to github style" ,
350+ baseURL : "https://example.com/user/repo" ,
351+ branch : "feature" ,
352+ remoteURL : "https://example.com/user/repo.git" ,
353+ want : "https://example.com/user/repo/tree/feature" ,
354+ },
355+ }
356+
357+ for _ , tt := range tests {
358+ t .Run (tt .name , func (t * testing.T ) {
359+ if got := buildBranchURL (tt .baseURL , tt .branch , tt .remoteURL ); got != tt .want {
360+ t .Errorf ("buildBranchURL() = %v, want %v" , got , tt .want )
361+ }
362+ })
363+ }
364+ }
365+
246366// BenchmarkConvertToWebURL benchmarks the URL conversion function
247367func BenchmarkConvertToWebURL (b * testing.B ) {
248368 urls := []string {
@@ -260,3 +380,63 @@ func BenchmarkConvertToWebURL(b *testing.B) {
260380 })
261381 }
262382}
383+ func Test_getBranchName_Error (t * testing.T ) {
384+ // Save original function
385+ originalGetBranchNameFunc := getBranchNameFunc
386+ defer func () {
387+ getBranchNameFunc = originalGetBranchNameFunc
388+ }()
389+
390+ // Mock getBranchNameFunc to return an error
391+ getBranchNameFunc = func (repo * git.Repository ) (string , error ) {
392+ return "" , fmt .Errorf ("error getting HEAD: mock error" )
393+ }
394+
395+ _ , cleanup := testhelper .SetupTestRepo (t , "https://github.com/zhaochunqi/git-open.git" , "main" )
396+ defer cleanup ()
397+
398+ repo , err := getCurrentGitDirectory ()
399+ if err != nil {
400+ t .Fatal (err )
401+ }
402+
403+ _ , err = getBranchName (repo )
404+ if err == nil {
405+ t .Error ("Expected error from getBranchName, got nil" )
406+ }
407+ if err != nil && ! strings .Contains (err .Error (), "error getting HEAD" ) {
408+ t .Errorf ("Expected error message to contain 'error getting HEAD', got '%s'" , err .Error ())
409+ }
410+ }
411+
412+ func Test_getRemoteURL_EmptyURLs (t * testing.T ) {
413+ // Save original function
414+ originalGetRemoteURLFunc := getRemoteURLFunc
415+ defer func () {
416+ getRemoteURLFunc = originalGetRemoteURLFunc
417+ }()
418+
419+ // Mock getRemoteURLFunc to simulate empty URLs error
420+ getRemoteURLFunc = func (repo * git.Repository ) (string , error ) {
421+ return "" , fmt .Errorf ("remote URL not found" )
422+ }
423+
424+ _ , cleanup := testhelper .SetupTestRepo (t , "https://github.com/zhaochunqi/git-open.git" , "main" )
425+ defer cleanup ()
426+
427+ repo , err := getCurrentGitDirectory ()
428+ if err != nil {
429+ t .Fatal (err )
430+ }
431+
432+ url , err := getRemoteURL (repo )
433+ if err == nil {
434+ t .Error ("Expected error for empty URLs, got nil" )
435+ }
436+ if url != "" {
437+ t .Errorf ("Expected empty URL, got %s" , url )
438+ }
439+ if err != nil && err .Error () != "remote URL not found" {
440+ t .Errorf ("Expected error message 'remote URL not found', got '%s'" , err .Error ())
441+ }
442+ }
0 commit comments