github.com/traefik/lobicornis/v3@v3.3.1/pkg/repository/clone_test.go (about) 1 package repository 2 3 import ( 4 "context" 5 "os" 6 "strings" 7 "testing" 8 9 "github.com/google/go-github/v58/github" 10 "github.com/ldez/go-git-cmd-wrapper/v2/git" 11 "github.com/ldez/go-git-cmd-wrapper/v2/remote" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 "github.com/traefik/lobicornis/v3/pkg/conf" 15 ) 16 17 func TestClone_PullRequestForUpdate(t *testing.T) { 18 testCases := []struct { 19 name string 20 sameRepo bool 21 expectedRemoteName string 22 expectedOriginURL string 23 expectedUpstreamURL string 24 }{ 25 { 26 name: "PR with 2 different repositories", 27 expectedRemoteName: "upstream", 28 expectedOriginURL: "https://github.com/ldez/traefik.git", 29 expectedUpstreamURL: "https://github.com/traefik/traefik.git", 30 }, 31 { 32 name: "PR from main repository", 33 sameRepo: true, 34 expectedRemoteName: "origin", 35 expectedOriginURL: "https://github.com/traefik/traefik.git", 36 expectedUpstreamURL: "https://github.com/traefik/traefik.git", 37 }, 38 } 39 40 gitConfig := conf.Git{ 41 UserName: "hubert", 42 Email: "hubert@foo.com", 43 SSH: false, 44 } 45 46 clone := newClone(gitConfig, "") 47 48 for _, test := range testCases { 49 t.Run(test.name, func(t *testing.T) { 50 dir, err := os.MkdirTemp("", "myrmica-lobicornis") 51 require.NoError(t, err) 52 53 t.Cleanup(func() { _ = os.RemoveAll(dir) }) 54 55 err = os.Chdir(dir) 56 require.NoError(t, err) 57 58 tempDir, err := os.Getwd() 59 require.NoError(t, err) 60 61 t.Log(tempDir) 62 63 pr := createFakePR(test.sameRepo) 64 65 remoteName, err := clone.PullRequestForUpdate(context.Background(), pr) 66 require.NoError(t, err) 67 68 assert.Equal(t, test.expectedRemoteName, remoteName) 69 70 localOriginURL, err := git.Remote(remote.GetURL("origin")) 71 require.NoError(t, err) 72 73 assert.Equal(t, test.expectedOriginURL, strings.TrimSpace(localOriginURL)) 74 75 localUpstreamURL, err := git.Remote(remote.GetURL(test.expectedRemoteName)) 76 require.NoError(t, err) 77 78 assert.Equal(t, test.expectedUpstreamURL, strings.TrimSpace(localUpstreamURL)) 79 }) 80 } 81 } 82 83 func TestClone_PullRequestForMerge(t *testing.T) { 84 testCases := []struct { 85 name string 86 sameRepo bool 87 expectedRemoteName string 88 expectedOriginURL string 89 expectedUpstreamURL string 90 }{ 91 { 92 name: "PR with 2 different repositories", 93 expectedRemoteName: "upstream", 94 expectedOriginURL: "https://github.com/traefik/traefik.git", 95 expectedUpstreamURL: "https://github.com/ldez/traefik.git", 96 }, 97 { 98 name: "PR from main repository", 99 sameRepo: true, 100 expectedRemoteName: "origin", 101 expectedOriginURL: "https://github.com/traefik/traefik.git", 102 expectedUpstreamURL: "https://github.com/traefik/traefik.git", 103 }, 104 } 105 106 gitConfig := conf.Git{ 107 UserName: "hubert", 108 Email: "hubert@foo.com", 109 SSH: false, 110 } 111 112 clone := newClone(gitConfig, "") 113 114 for _, test := range testCases { 115 t.Run(test.name, func(t *testing.T) { 116 dir, err := os.MkdirTemp("", "myrmica-lobicornis") 117 require.NoError(t, err) 118 119 t.Cleanup(func() { _ = os.RemoveAll(dir) }) 120 121 err = os.Chdir(dir) 122 require.NoError(t, err) 123 124 tempDir, err := os.Getwd() 125 require.NoError(t, err) 126 127 t.Log(tempDir) 128 129 pr := createFakePR(test.sameRepo) 130 131 remoteName, err := clone.PullRequestForMerge(context.Background(), pr) 132 require.NoError(t, err) 133 134 assert.Equal(t, test.expectedRemoteName, remoteName) 135 136 localOriginURL, err := git.Remote(remote.GetURL("origin")) 137 require.NoError(t, err) 138 139 assert.Equal(t, test.expectedOriginURL, strings.TrimSpace(localOriginURL)) 140 141 localUpstreamURL, err := git.Remote(remote.GetURL(test.expectedRemoteName)) 142 require.NoError(t, err) 143 144 assert.Equal(t, test.expectedUpstreamURL, strings.TrimSpace(localUpstreamURL)) 145 }) 146 } 147 } 148 149 func Test_makeRepositoryURL(t *testing.T) { 150 testCases := []struct { 151 name string 152 url string 153 ssh bool 154 token string 155 expectedURL string 156 }{ 157 { 158 name: "HTTPS", 159 url: "git://github.com/traefik/traefik.git", 160 expectedURL: "https://github.com/traefik/traefik.git", 161 }, 162 { 163 name: "HTTPS with token", 164 url: "git://github.com/traefik/traefik.git", 165 token: "token", 166 expectedURL: "https://token@github.com/traefik/traefik.git", 167 }, 168 { 169 name: "SSH", 170 url: "git://github.com/traefik/traefik.git", 171 ssh: true, 172 expectedURL: "git@github.com:traefik/traefik.git", 173 }, 174 { 175 name: "SSH with token", 176 url: "git://github.com/traefik/traefik.git", 177 ssh: true, 178 token: "token", 179 expectedURL: "git@github.com:traefik/traefik.git", 180 }, 181 } 182 183 for _, test := range testCases { 184 test := test 185 t.Run(test.name, func(t *testing.T) { 186 t.Parallel() 187 url := makeRepositoryURL(test.url, test.ssh, test.token) 188 189 if url != test.expectedURL { 190 t.Errorf("Got %s, want %s.", url, test.expectedURL) 191 } 192 }) 193 } 194 } 195 196 func createFakePR(sameRepo bool) *github.PullRequest { 197 pr := &github.PullRequest{ 198 Number: github.Int(666), 199 } 200 pr.Base = &github.PullRequestBranch{ 201 Repo: &github.Repository{ 202 GitURL: github.String("git://github.com/traefik/traefik.git"), 203 }, 204 Ref: github.String("master"), 205 } 206 207 if sameRepo { 208 pr.Head = &github.PullRequestBranch{ 209 Repo: &github.Repository{ 210 GitURL: github.String("git://github.com/traefik/traefik.git"), 211 }, 212 Ref: github.String("v1.3"), 213 } 214 } else { 215 pr.Head = &github.PullRequestBranch{ 216 Repo: &github.Repository{ 217 GitURL: github.String("git://github.com/ldez/traefik.git"), 218 }, 219 Ref: github.String("v1.3"), 220 } 221 } 222 223 return pr 224 }