github.com/Benchkram/bob@v0.0.0-20220321080157-7c8f3876e225/bob/repo_test.go (about) 1 package bob 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 giturls "github.com/whilp/git-urls" 9 ) 10 11 func TestRepoName(t *testing.T) { 12 13 tests := []struct { 14 input string // url 15 result string 16 }{ 17 { 18 input: "git@ssh.dev.azure.com:v3/first-level/seconlevel/my.repo.name", 19 result: "my.repo.name", 20 }, 21 { 22 input: "git@github.com:bob/bob.git", 23 result: "bob", 24 }, 25 } 26 27 for _, test := range tests { 28 url, err := giturls.Parse(test.input) 29 assert.Nil(t, err, test.input) 30 assert.Equal(t, test.result, RepoName(url), test.input) 31 } 32 } 33 34 func TestParseAzure(t *testing.T) { 35 type input struct { 36 rawurl string 37 } 38 type result struct { 39 https string 40 ssh string 41 } 42 tests := []struct { 43 name string 44 input input 45 result result 46 }{ 47 { 48 name: "azure-devops-https", 49 input: input{ 50 rawurl: "https://xxx@dev.azure.com/xxx/Yyy/_git/zzz.zzz.zzz", 51 }, 52 result: result{ 53 https: "https://xxx@dev.azure.com/xxx/Yyy/_git/zzz.zzz.zzz", 54 ssh: "git@ssh.dev.azure.com:v3/xxx/Yyy/zzz.zzz.zzz", 55 }, 56 }, 57 { 58 name: "azure-devops-ssh", 59 input: input{ 60 rawurl: "git@ssh.dev.azure.com:v3/xxx/Yyy/zzz.zzz.zzz", 61 }, 62 result: result{ 63 https: "https://xxx@dev.azure.com/xxx/Yyy/_git/zzz.zzz.zzz", 64 ssh: "git@ssh.dev.azure.com:v3/xxx/Yyy/zzz.zzz.zzz", 65 }, 66 }, 67 } 68 69 for _, test := range tests { 70 fmt.Println(test.name) 71 72 repo, err := ParseAzure(test.input.rawurl) 73 assert.Nil(t, err, test.name) 74 75 assert.Equal(t, test.result.https, repo.HTTPS.String()) 76 assert.Equal(t, test.result.ssh, repo.SSH.String()) 77 } 78 } 79 80 func TestParseGeneral(t *testing.T) { 81 type input struct { 82 rawurl string 83 } 84 type result struct { 85 https string 86 ssh string 87 } 88 tests := []struct { 89 name string 90 input input 91 result result 92 }{ 93 { 94 name: "github-https", 95 input: input{ 96 rawurl: "https://github.com/Benchkram/bob.git", 97 }, 98 result: result{ 99 https: "https://github.com/Benchkram/bob.git", 100 ssh: "git@github.com:Benchkram/bob.git", 101 }, 102 }, 103 { 104 name: "github-ssh", 105 input: input{ 106 rawurl: "git@github.com:Benchkram/bob.git", 107 }, 108 result: result{ 109 https: "https://github.com/Benchkram/bob.git", 110 ssh: "git@github.com:Benchkram/bob.git", 111 }, 112 }, 113 { 114 name: "subdomain-https", 115 input: input{ 116 rawurl: "https://subdomain.gitlab.de/project/repo.git", 117 }, 118 result: result{ 119 https: "https://subdomain.gitlab.de/project/repo.git", 120 ssh: "git@subdomain.gitlab.de:project/repo.git", 121 }, 122 }, 123 { 124 name: "subdomain-ssh", 125 input: input{ 126 rawurl: "git@subdomain.gitlab.de:project/repo.git", 127 }, 128 result: result{ 129 https: "https://subdomain.gitlab.de/project/repo.git", 130 ssh: "git@subdomain.gitlab.de:project/repo.git", 131 }, 132 }, 133 } 134 135 for _, test := range tests { 136 fmt.Println(test.name) 137 138 repo, err := ParseGeneral(test.input.rawurl) 139 assert.Nil(t, err, test.name) 140 141 assert.Equal(t, test.result.https, repo.HTTPS.String()) 142 assert.Equal(t, test.result.ssh, repo.SSH.String()) 143 } 144 } 145 146 // func TestRepoURL(t *testing.T) { 147 // // Gathering of http + ssh urls 148 // // 149 // // Github: 150 // // https://github.com/Benchkram/bob.git 151 // // git@github.com:Benchkram/bob.git 152 // // Azure: 153 // // https://xxx@dev.azure.com/xxx/Yyy/_git/zzz.zzz.zzz 154 // // git@ssh.dev.azure.com:v3/xxx/Yyy/zzz.zzz.zzz 155 // // Gitlab: 156 // // https://gitlab.domain.de/xxx/zzz-zzz-zzz.git 157 // // git@gitlab.domain.de:xxx/zzz-zzz-zzz.git 158 // // Bitbucket: 159 // // https://equanox@bitbucket.org/equanox/build-server.git 160 // // git@bitbucket.org:equanox/build-server.git 161 162 // type input struct { 163 // https string 164 // ssh string 165 // } 166 // tests := []struct { 167 // name string 168 // input input 169 // result string 170 // }{ 171 // { 172 // name: "github", 173 // input: input{ 174 // https: "https://github.com/Benchkram/bob.git", 175 // ssh: "git@github.com:Benchkram/bob.git", 176 // }, 177 // result: "", 178 // }, 179 // { 180 // name: "azure-devops", 181 // input: input{ 182 // https: "https://xxx@dev.azure.com/xxx/Yyy/_git/zzz.zzz.zzz", 183 // ssh: "git@ssh.dev.azure.com:v3/xxx/Yyy/zzz.zzz.zzz", 184 // }, 185 // result: "", 186 // }, 187 // // { 188 // // name: "gitlab", 189 // // input: input{ 190 // // https: "https://gitlab.domain.de/xxx/zzz-zzz-zzz.git", 191 // // ssh: "git@gitlab.domain.de:xxx/zzz-zzz-zzz.git", 192 // // }, 193 // // result: "", 194 // // }, 195 // // { 196 // // name: "bitbucket", 197 // // input: input{ 198 // // https: "https://equanox@bitbucket.org/equanox/build-server.git", 199 // // ssh: "git@bitbucket.org:equanox/build-server.git", 200 // // }, 201 // // result: "", 202 // // }, 203 // } 204 205 // for _, test := range tests { 206 // httpsURL, err := giturls.Parse(test.input.https) 207 // assert.Nil(t, err, test.input) 208 // fmt.Println(httpsURL.String()) 209 // sshURL, err := giturls.Parse(test.input.ssh) 210 // assert.Nil(t, err, test.input) 211 // fmt.Println(sshURL.String()) 212 213 // } 214 // }