github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/url/url_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package url
     7  
     8  import (
     9  	"net/url"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestParseGitURLs(t *testing.T) {
    16  	kases := []struct {
    17  		kase     string
    18  		expected *GitURL
    19  	}{
    20  		{
    21  			kase: "git@127.0.0.1:go-gitea/gitea.git",
    22  			expected: &GitURL{
    23  				URL: &url.URL{
    24  					Scheme: "ssh",
    25  					User:   url.User("git"),
    26  					Host:   "127.0.0.1",
    27  					Path:   "go-gitea/gitea.git",
    28  				},
    29  				extraMark: 1,
    30  			},
    31  		},
    32  		{
    33  			kase: "git@[fe80:14fc:cec5:c174:d88%2510]:go-gitea/gitea.git",
    34  			expected: &GitURL{
    35  				URL: &url.URL{
    36  					Scheme: "ssh",
    37  					User:   url.User("git"),
    38  					Host:   "[fe80:14fc:cec5:c174:d88%10]",
    39  					Path:   "go-gitea/gitea.git",
    40  				},
    41  				extraMark: 1,
    42  			},
    43  		},
    44  		{
    45  			kase: "git@[::1]:go-gitea/gitea.git",
    46  			expected: &GitURL{
    47  				URL: &url.URL{
    48  					Scheme: "ssh",
    49  					User:   url.User("git"),
    50  					Host:   "[::1]",
    51  					Path:   "go-gitea/gitea.git",
    52  				},
    53  				extraMark: 1,
    54  			},
    55  		},
    56  		{
    57  			kase: "git@github.com:go-gitea/gitea.git",
    58  			expected: &GitURL{
    59  				URL: &url.URL{
    60  					Scheme: "ssh",
    61  					User:   url.User("git"),
    62  					Host:   "github.com",
    63  					Path:   "go-gitea/gitea.git",
    64  				},
    65  				extraMark: 1,
    66  			},
    67  		},
    68  		{
    69  			kase: "ssh://git@github.com/go-gitea/gitea.git",
    70  			expected: &GitURL{
    71  				URL: &url.URL{
    72  					Scheme: "ssh",
    73  					User:   url.User("git"),
    74  					Host:   "github.com",
    75  					Path:   "/go-gitea/gitea.git",
    76  				},
    77  				extraMark: 0,
    78  			},
    79  		},
    80  		{
    81  			kase: "ssh://git@[::1]/go-gitea/gitea.git",
    82  			expected: &GitURL{
    83  				URL: &url.URL{
    84  					Scheme: "ssh",
    85  					User:   url.User("git"),
    86  					Host:   "[::1]",
    87  					Path:   "/go-gitea/gitea.git",
    88  				},
    89  				extraMark: 0,
    90  			},
    91  		},
    92  		{
    93  			kase: "/repositories/go-gitea/gitea.git",
    94  			expected: &GitURL{
    95  				URL: &url.URL{
    96  					Scheme: "file",
    97  					Path:   "/repositories/go-gitea/gitea.git",
    98  				},
    99  				extraMark: 2,
   100  			},
   101  		},
   102  		{
   103  			kase: "file:///repositories/go-gitea/gitea.git",
   104  			expected: &GitURL{
   105  				URL: &url.URL{
   106  					Scheme: "file",
   107  					Path:   "/repositories/go-gitea/gitea.git",
   108  				},
   109  				extraMark: 0,
   110  			},
   111  		},
   112  		{
   113  			kase: "https://github.com/go-gitea/gitea.git",
   114  			expected: &GitURL{
   115  				URL: &url.URL{
   116  					Scheme: "https",
   117  					Host:   "github.com",
   118  					Path:   "/go-gitea/gitea.git",
   119  				},
   120  				extraMark: 0,
   121  			},
   122  		},
   123  		{
   124  			kase: "https://git:git@github.com/go-gitea/gitea.git",
   125  			expected: &GitURL{
   126  				URL: &url.URL{
   127  					Scheme: "https",
   128  					Host:   "github.com",
   129  					User:   url.UserPassword("git", "git"),
   130  					Path:   "/go-gitea/gitea.git",
   131  				},
   132  				extraMark: 0,
   133  			},
   134  		},
   135  		{
   136  			kase: "https://[fe80:14fc:cec5:c174:d88%2510]:20/go-gitea/gitea.git",
   137  			expected: &GitURL{
   138  				URL: &url.URL{
   139  					Scheme: "https",
   140  					Host:   "[fe80:14fc:cec5:c174:d88%10]:20",
   141  					Path:   "/go-gitea/gitea.git",
   142  				},
   143  				extraMark: 0,
   144  			},
   145  		},
   146  
   147  		{
   148  			kase: "git://github.com/go-gitea/gitea.git",
   149  			expected: &GitURL{
   150  				URL: &url.URL{
   151  					Scheme: "git",
   152  					Host:   "github.com",
   153  					Path:   "/go-gitea/gitea.git",
   154  				},
   155  				extraMark: 0,
   156  			},
   157  		},
   158  	}
   159  
   160  	for _, kase := range kases {
   161  		t.Run(kase.kase, func(t *testing.T) {
   162  			u, err := Parse(kase.kase)
   163  			assert.NoError(t, err)
   164  			assert.EqualValues(t, kase.expected.extraMark, u.extraMark)
   165  			assert.EqualValues(t, *kase.expected, *u)
   166  		})
   167  	}
   168  }