github.com/zaquestion/lab@v0.25.1/cmd/util_test.go (about) 1 package cmd 2 3 import ( 4 "os" 5 "os/exec" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 gitlab "github.com/xanzy/go-gitlab" 11 ) 12 13 func Test_textToMarkdown(t *testing.T) { 14 basestring := "This string should have two spaces at the end." 15 teststring := basestring + "\n" 16 newteststring := textToMarkdown(teststring) 17 assert.Equal(t, basestring+" \n", newteststring) 18 } 19 20 func Test_getCurrentBranchMR(t *testing.T) { 21 repo := copyTestRepo(t) 22 23 // make sure the branch does not exist 24 cmd := exec.Command("git", "branch", "-D", "mrtest") 25 cmd.Dir = repo 26 cmd.CombinedOutput() 27 28 cmd = exec.Command(labBinaryPath, "mr", "checkout", "1") 29 cmd.Dir = repo 30 b, err := cmd.CombinedOutput() 31 if err != nil { 32 t.Log(string(b)) 33 t.Fatal(err) 34 } 35 36 curDir, err := os.Getwd() 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 err = os.Chdir(repo) 42 if err != nil { 43 t.Log(string(b)) 44 t.Fatal(err) 45 } 46 mrNum := getCurrentBranchMR("zaquestion/test") 47 err = os.Chdir(curDir) 48 if err != nil { 49 t.Log(string(b)) 50 t.Fatal(err) 51 } 52 53 assert.Equal(t, 1, mrNum) 54 } 55 56 func Test_parseArgsStringAndID(t *testing.T) { 57 tests := []struct { 58 Name string 59 Args []string 60 ExpectedString string 61 ExpectedInt int64 62 ExpectedErr string 63 }{ 64 { 65 Name: "No Args", 66 Args: nil, 67 ExpectedString: "", 68 ExpectedInt: 0, 69 ExpectedErr: "", 70 }, 71 { 72 Name: "1 arg remote", 73 Args: []string{"origin"}, 74 ExpectedString: "origin", 75 ExpectedInt: 0, 76 ExpectedErr: "", 77 }, 78 { 79 Name: "1 arg non remote", 80 Args: []string{"foo"}, 81 ExpectedString: "foo", 82 ExpectedInt: 0, 83 ExpectedErr: "", 84 }, 85 { 86 Name: "1 arg page", 87 Args: []string{"100"}, 88 ExpectedString: "", 89 ExpectedInt: 100, 90 ExpectedErr: "", 91 }, 92 { 93 Name: "1 arg invalid page", 94 Args: []string{"asdf100"}, 95 ExpectedString: "asdf100", 96 ExpectedInt: 0, 97 ExpectedErr: "", 98 }, 99 { 100 Name: "2 arg str page", 101 Args: []string{"origin", "100"}, 102 ExpectedString: "origin", 103 ExpectedInt: 100, 104 ExpectedErr: "", 105 }, 106 { 107 Name: "2 arg valid str valid page", 108 Args: []string{"foo", "100"}, 109 ExpectedString: "foo", 110 ExpectedInt: 100, 111 ExpectedErr: "", 112 }, 113 { 114 Name: "2 arg valid str invalid page", 115 Args: []string{"foo", "asdf100"}, 116 ExpectedString: "foo", 117 ExpectedInt: 0, 118 ExpectedErr: "strconv.ParseInt: parsing \"asdf100\": invalid syntax", 119 }, 120 } 121 for _, test := range tests { 122 t.Run(test.Name, func(t *testing.T) { 123 test := test 124 t.Parallel() 125 s, i, err := parseArgsStringAndID(test.Args) 126 if err != nil { 127 assert.EqualError(t, err, test.ExpectedErr) 128 } 129 assert.Equal(t, test.ExpectedString, s) 130 assert.Equal(t, test.ExpectedInt, i) 131 }) 132 } 133 } 134 135 func Test_parseArgsRemoteAndID(t *testing.T) { 136 tests := []struct { 137 Name string 138 Args []string 139 ExpectedString string 140 ExpectedInt int64 141 ExpectedErr string 142 }{ 143 { 144 Name: "No Args", 145 Args: nil, 146 ExpectedString: "zaquestion/test", 147 ExpectedInt: 0, 148 ExpectedErr: "", 149 }, 150 { 151 Name: "1 arg remote", 152 Args: []string{"lab-testing"}, 153 ExpectedString: "lab-testing/test", 154 ExpectedInt: 0, 155 ExpectedErr: "", 156 }, 157 { 158 Name: "1 arg non remote", 159 Args: []string{"foo"}, 160 ExpectedString: "", 161 ExpectedInt: 0, 162 ExpectedErr: "foo is not a valid remote or number", 163 }, 164 { 165 Name: "1 arg page", 166 Args: []string{"100"}, 167 ExpectedString: "zaquestion/test", 168 ExpectedInt: 100, 169 ExpectedErr: "", 170 }, 171 { 172 Name: "1 arg invalid page", 173 Args: []string{"asdf100"}, 174 ExpectedString: "", 175 ExpectedInt: 0, 176 ExpectedErr: "asdf100 is not a valid remote or number", 177 }, 178 { 179 Name: "2 arg remote page", 180 Args: []string{"origin", "100"}, 181 ExpectedString: "zaquestion/test", 182 ExpectedInt: 100, 183 ExpectedErr: "", 184 }, 185 { 186 Name: "2 arg invalid remote valid page", 187 Args: []string{"foo", "100"}, 188 ExpectedString: "", 189 ExpectedInt: 0, 190 ExpectedErr: "foo is not a valid remote", 191 }, 192 { 193 Name: "2 arg invalid remote invalid page", 194 Args: []string{"foo", "asdf100"}, 195 ExpectedString: "", 196 ExpectedInt: 0, 197 ExpectedErr: "strconv.ParseInt: parsing \"asdf100\": invalid syntax", 198 }, 199 } 200 for _, test := range tests { 201 t.Run(test.Name, func(t *testing.T) { 202 test := test 203 t.Parallel() 204 s, i, err := parseArgsRemoteAndID(test.Args) 205 if err != nil { 206 assert.EqualError(t, err, test.ExpectedErr) 207 } 208 assert.Equal(t, test.ExpectedString, s) 209 assert.Equal(t, test.ExpectedInt, i) 210 }) 211 } 212 } 213 214 func Test_parseArgsRemoteAndProject(t *testing.T) { 215 tests := []struct { 216 Name string 217 Args []string 218 ExpectedRemote string 219 ExpectedString string 220 ExpectedErr string 221 }{ 222 { 223 Name: "No Args", 224 Args: nil, 225 ExpectedRemote: "zaquestion/test", 226 ExpectedString: "", 227 ExpectedErr: "", 228 }, 229 { 230 Name: "1 arg remote", 231 Args: []string{"lab-testing"}, 232 ExpectedRemote: "lab-testing/test", 233 ExpectedString: "", 234 ExpectedErr: "", 235 }, 236 { 237 Name: "1 arg non remote", 238 Args: []string{"foo123"}, 239 ExpectedRemote: "zaquestion/test", 240 ExpectedString: "foo123", 241 ExpectedErr: "", 242 }, 243 { 244 Name: "1 arg page", 245 Args: []string{"100"}, 246 ExpectedRemote: "zaquestion/test", 247 ExpectedString: "100", 248 ExpectedErr: "", 249 }, 250 { 251 Name: "2 arg remote and string", 252 Args: []string{"origin", "foo123"}, 253 ExpectedRemote: "zaquestion/test", 254 ExpectedString: "foo123", 255 ExpectedErr: "", 256 }, 257 { 258 Name: "2 arg invalid remote and string", 259 Args: []string{"foo", "string123"}, 260 ExpectedRemote: "", 261 ExpectedString: "", 262 ExpectedErr: "foo is not a valid remote", 263 }, 264 } 265 for _, test := range tests { 266 t.Run(test.Name, func(t *testing.T) { 267 test := test 268 t.Parallel() 269 r, s, err := parseArgsRemoteAndProject(test.Args) 270 if err != nil { 271 assert.EqualError(t, err, test.ExpectedErr) 272 } 273 assert.Equal(t, test.ExpectedRemote, r) 274 assert.Equal(t, test.ExpectedString, s) 275 }) 276 } 277 } 278 279 func Test_labURLToRepo(t *testing.T) { 280 HTTPURL := "https://test" 281 SSHURL := "ssh://test" 282 project := gitlab.Project{ 283 HTTPURLToRepo: HTTPURL, 284 SSHURLToRepo: SSHURL, 285 } 286 287 urlToRepo := labURLToRepo(&project) 288 assert.Equal(t, urlToRepo, SSHURL) 289 290 useHTTP = true 291 urlToRepo = labURLToRepo(&project) 292 assert.Equal(t, urlToRepo, HTTPURL) 293 } 294 295 func Test_determineSourceRemote(t *testing.T) { 296 tests := []struct { 297 desc string 298 branch string 299 expected string 300 }{ 301 { 302 desc: "branch.<name>.remote", 303 branch: "mrtest", 304 expected: "lab-testing", 305 }, 306 { 307 desc: "branch.<name>.pushRemote", 308 branch: "mrtest-pushRemote", 309 expected: "lab-testing", 310 }, 311 { 312 desc: "pushDefault without pushRemote set", 313 branch: "mrtest", 314 expected: "garbageurl", 315 }, 316 { 317 desc: "pushDefault with pushRemote set", 318 branch: "mrtest-pushRemote", 319 expected: "lab-testing", 320 }, 321 } 322 323 // The function being tested here depends on being in the test 324 // directory, where 'git config --local' can retrieve the correct 325 // info from 326 repo := copyTestRepo(t) 327 oldWd, err := os.Getwd() 328 if err != nil { 329 t.Log(err) 330 } 331 os.Chdir(repo) 332 333 var remoteModified bool 334 for _, test := range tests { 335 test := test 336 if strings.Contains(test.desc, "pushDefault") && !remoteModified { 337 git := exec.Command("git", "config", "--local", "remote.pushDefault", "garbageurl") 338 git.Dir = repo 339 b, err := git.CombinedOutput() 340 if err != nil { 341 t.Log(string(b)) 342 t.Fatal(err) 343 } 344 remoteModified = true 345 } 346 347 t.Run(test.desc, func(t *testing.T) { 348 sourceRemote, err := determineSourceRemote(test.branch) 349 if err != nil { 350 t.Fatal(err) 351 } 352 assert.Equal(t, test.expected, sourceRemote) 353 }) 354 } 355 // Remove the added option to avoid messing with other tests 356 git := exec.Command("git", "config", "--local", "--unset", "remote.pushDefault") 357 git.Dir = repo 358 b, err := git.CombinedOutput() 359 if err != nil { 360 t.Log(string(b)) 361 t.Fatal(err) 362 } 363 // And move back to the workdir we were before the test 364 os.Chdir(oldWd) 365 } 366 367 func Test_matchTerms(t *testing.T) { 368 tests := []struct { 369 desc string 370 search []string 371 existent []string 372 expected []string 373 expectedErr string 374 }{ 375 { 376 desc: "no match", 377 search: []string{"asd", "zxc"}, 378 existent: []string{"dsa", "cxz"}, 379 expected: []string{""}, 380 expectedErr: "'asd' not found", 381 }, 382 { 383 desc: "full match", 384 search: []string{"asd", "zxc"}, 385 existent: []string{"asd", "zxc"}, 386 expected: []string{"asd", "zxc"}, 387 expectedErr: "", 388 }, 389 { 390 desc: "substring match", 391 search: []string{"as", "zx"}, 392 existent: []string{"as", "asd", "zxc"}, 393 expected: []string{"as", "zxc"}, 394 expectedErr: "", 395 }, 396 { 397 desc: "ambiguous terms", 398 search: []string{"as", "zx"}, 399 existent: []string{"asd", "asf", "zxc", "zxv"}, 400 expected: []string{""}, 401 expectedErr: "'as' has no exact match and is ambiguous", 402 }, 403 } 404 405 t.Parallel() 406 407 for _, test := range tests { 408 t.Run(test.desc, func(t *testing.T) { 409 matches, err := matchTerms(test.search, test.existent) 410 if test.expected[0] != "" { 411 assert.Equal(t, test.expected, matches) 412 } else { 413 assert.Nil(t, matches) 414 } 415 416 if test.expectedErr != "" { 417 assert.EqualError(t, err, test.expectedErr) 418 } else { 419 assert.Nil(t, err) 420 } 421 }) 422 } 423 } 424 425 func Test_same(t *testing.T) { 426 t.Parallel() 427 assert.True(t, same([]string{}, []string{})) 428 assert.True(t, same([]string{"a"}, []string{"a"})) 429 assert.True(t, same([]string{"a", "b"}, []string{"a", "b"})) 430 assert.True(t, same([]string{"a", "b"}, []string{"b", "a"})) 431 assert.True(t, same([]string{"b", "a"}, []string{"a", "b"})) 432 433 assert.False(t, same([]string{"a"}, []string{})) 434 assert.False(t, same([]string{"a"}, []string{"c"})) 435 assert.False(t, same([]string{}, []string{"c"})) 436 assert.False(t, same([]string{"a", "b"}, []string{"a", "c"})) 437 assert.False(t, same([]string{"a", "b"}, []string{"a"})) 438 assert.False(t, same([]string{"a", "b"}, []string{"c"})) 439 } 440 441 func Test_union(t *testing.T) { 442 t.Parallel() 443 s := union([]string{"a", "b"}, []string{"c"}) 444 assert.Equal(t, 3, len(s)) 445 assert.True(t, same(s, []string{"a", "b", "c"})) 446 } 447 448 func Test_difference(t *testing.T) { 449 t.Parallel() 450 s := difference([]string{"a", "b"}, []string{"c"}) 451 assert.Equal(t, 2, len(s)) 452 assert.True(t, same(s, []string{"a", "b"})) 453 454 s = difference([]string{"a", "b"}, []string{"a", "c"}) 455 assert.Equal(t, 1, len(s)) 456 assert.True(t, same(s, []string{"b"})) 457 }