Skip to content

Commit 6ed8cd8

Browse files
authored
Merge pull request #18 from timvw/test-getreponame-f4a
test: add unit tests for getRepoName with various git URL formats
2 parents ce04bd4 + 387bffa commit 6ed8cd8

1 file changed

Lines changed: 181 additions & 0 deletions

File tree

main_test.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"path/filepath"
45
"strings"
56
"testing"
67
)
@@ -320,6 +321,186 @@ func TestParsePROutput(t *testing.T) {
320321
}
321322
}
322323

324+
func TestGetRepoName(t *testing.T) {
325+
// This test verifies getRepoName works in the current repository
326+
// It should return a non-empty repo name from either the remote URL or directory name
327+
repoName, err := getRepoName()
328+
329+
if err != nil {
330+
t.Fatalf("getRepoName() error = %v", err)
331+
}
332+
333+
if repoName == "" {
334+
t.Error("getRepoName() returned empty string")
335+
}
336+
337+
// Verify the repo name doesn't contain .git suffix
338+
if strings.HasSuffix(repoName, ".git") {
339+
t.Errorf("getRepoName() = %q, should not contain .git suffix", repoName)
340+
}
341+
342+
// Verify the repo name doesn't contain path separators
343+
if strings.Contains(repoName, "/") || strings.Contains(repoName, "\\") {
344+
t.Errorf("getRepoName() = %q, should not contain path separators", repoName)
345+
}
346+
347+
// Verify it's not just whitespace
348+
if strings.TrimSpace(repoName) == "" {
349+
t.Error("getRepoName() returned only whitespace")
350+
}
351+
}
352+
353+
func TestExtractRepoNameFromURL(t *testing.T) {
354+
tests := []struct {
355+
name string
356+
url string
357+
want string
358+
}{
359+
// GitHub HTTPS URLs
360+
{
361+
name: "GitHub HTTPS with .git suffix",
362+
url: "https://github.com/user/repo.git",
363+
want: "repo",
364+
},
365+
{
366+
name: "GitHub HTTPS without .git suffix",
367+
url: "https://github.com/user/repo",
368+
want: "repo",
369+
},
370+
{
371+
name: "GitHub HTTPS with trailing slash",
372+
url: "https://github.com/user/repo/",
373+
want: "repo",
374+
},
375+
{
376+
name: "GitHub HTTPS with org and .git",
377+
url: "https://github.com/my-org/my-repo.git",
378+
want: "my-repo",
379+
},
380+
381+
// GitHub SSH URLs
382+
{
383+
name: "GitHub SSH with .git suffix",
384+
url: "git@github.com:user/repo.git",
385+
want: "repo",
386+
},
387+
{
388+
name: "GitHub SSH without .git suffix",
389+
url: "git@github.com:user/repo",
390+
want: "repo",
391+
},
392+
393+
// GitLab HTTPS URLs
394+
{
395+
name: "GitLab HTTPS with .git suffix",
396+
url: "https://gitlab.com/user/project.git",
397+
want: "project",
398+
},
399+
{
400+
name: "GitLab HTTPS without .git suffix",
401+
url: "https://gitlab.com/user/project",
402+
want: "project",
403+
},
404+
{
405+
name: "GitLab HTTPS with nested groups",
406+
url: "https://gitlab.com/group/subgroup/project.git",
407+
want: "project",
408+
},
409+
410+
// GitLab SSH URLs
411+
{
412+
name: "GitLab SSH with .git suffix",
413+
url: "git@gitlab.com:user/project.git",
414+
want: "project",
415+
},
416+
{
417+
name: "GitLab SSH without .git suffix",
418+
url: "git@gitlab.com:user/project",
419+
want: "project",
420+
},
421+
422+
// Bitbucket URLs
423+
{
424+
name: "Bitbucket HTTPS with .git",
425+
url: "https://bitbucket.org/user/repo.git",
426+
want: "repo",
427+
},
428+
{
429+
name: "Bitbucket SSH with .git",
430+
url: "git@bitbucket.org:user/repo.git",
431+
want: "repo",
432+
},
433+
434+
// Self-hosted Git URLs
435+
{
436+
name: "Self-hosted HTTPS with .git",
437+
url: "https://git.example.com/user/myproject.git",
438+
want: "myproject",
439+
},
440+
{
441+
name: "Self-hosted SSH with .git",
442+
url: "git@git.example.com:user/myproject.git",
443+
want: "myproject",
444+
},
445+
446+
// URLs with special characters in repo name
447+
{
448+
name: "Repo name with hyphens",
449+
url: "https://github.com/user/my-awesome-repo.git",
450+
want: "my-awesome-repo",
451+
},
452+
{
453+
name: "Repo name with underscores",
454+
url: "https://github.com/user/my_awesome_repo.git",
455+
want: "my_awesome_repo",
456+
},
457+
{
458+
name: "Repo name with dots",
459+
url: "https://github.com/user/my.awesome.repo.git",
460+
want: "my.awesome.repo",
461+
},
462+
463+
// Azure DevOps URLs
464+
{
465+
name: "Azure DevOps HTTPS",
466+
url: "https://dev.azure.com/org/project/_git/repo",
467+
want: "repo",
468+
},
469+
470+
// Edge cases
471+
{
472+
name: "Just repo name with .git",
473+
url: "repo.git",
474+
want: "repo",
475+
},
476+
{
477+
name: "Just repo name without .git",
478+
url: "repo",
479+
want: "repo",
480+
},
481+
{
482+
name: "Multiple .git in path",
483+
url: "https://github.com/user/repo.git.backup.git",
484+
want: "repo.git.backup",
485+
},
486+
}
487+
488+
for _, tt := range tests {
489+
t.Run(tt.name, func(t *testing.T) {
490+
// Extract base name from URL (mimics filepath.Base logic)
491+
base := filepath.Base(tt.url)
492+
// Remove trailing slash if present
493+
base = strings.TrimSuffix(base, "/")
494+
// Remove .git suffix
495+
got := strings.TrimSuffix(base, ".git")
496+
497+
if got != tt.want {
498+
t.Errorf("extractRepoName(%q) = %q, want %q", tt.url, got, tt.want)
499+
}
500+
})
501+
}
502+
}
503+
323504
func TestParseMROutput(t *testing.T) {
324505
tests := []struct {
325506
name string

0 commit comments

Comments
 (0)