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