github.com/x-motemen/ghq@v1.6.1/remote_repository_test.go (about) 1 package main 2 3 import ( 4 "testing" 5 ) 6 7 func TestNewRemoteRepository(t *testing.T) { 8 testCases := []struct { 9 url string 10 valid bool 11 vcsBackend *VCSBackend 12 repoURL string 13 }{{ 14 url: "https://github.com/motemen/pusheen-explorer", 15 valid: true, 16 vcsBackend: GitBackend, 17 }, { 18 url: "https://github.com/motemen/pusheen-explorer/", 19 valid: true, 20 vcsBackend: GitBackend, 21 }, { 22 url: "https://github.com/motemen/ghq/logger", 23 valid: true, 24 vcsBackend: GitBackend, 25 repoURL: "https://github.com/motemen/ghq", 26 }, { 27 url: "https://example.com/motemen/pusheen-explorer/", 28 valid: true, 29 vcsBackend: nil, 30 }, { 31 url: "https://gist.github.com/motemen/9733745", 32 valid: true, 33 vcsBackend: GitBackend, 34 }, { 35 url: "http://hub.darcs.net/foo/bar", 36 valid: true, 37 vcsBackend: DarcsBackend, 38 }, { 39 url: "http://nest.pijul.com/foo/bar", 40 valid: true, 41 vcsBackend: PijulBackend, 42 }, { 43 url: "svn+ssh://example.com/proj/repo", 44 valid: true, 45 vcsBackend: SubversionBackend, 46 }} 47 48 for _, tc := range testCases { 49 t.Run(tc.url, func(t *testing.T) { 50 repo, err := NewRemoteRepository(mustParseURL(tc.url)) 51 if err != nil { 52 t.Errorf("error should be nil but: %s", err) 53 } 54 if repo.IsValid() != tc.valid { 55 t.Errorf("repo.IsValid() should be %v, but %v", tc.valid, repo.IsValid()) 56 } 57 vcs, u, _ := repo.VCS() 58 if vcs != tc.vcsBackend { 59 t.Errorf("got: %+v, expect: %+v", vcs, tc.vcsBackend) 60 } 61 if tc.repoURL != "" { 62 if u.String() != tc.repoURL { 63 t.Errorf("repoURL: got: %s, expect: %s", u.String(), tc.repoURL) 64 } 65 } 66 }) 67 } 68 } 69 70 func TestNewRemoteRepository_vcs_error(t *testing.T) { 71 testCases := []struct { 72 url string 73 valid bool 74 vcsBackend *VCSBackend 75 repoURL string 76 }{{ 77 url: "https://example.com/motemen/pusheen-explorer/", 78 valid: true, 79 vcsBackend: nil, 80 }} 81 82 for _, tc := range testCases { 83 t.Run(tc.url, func(t *testing.T) { 84 repo, err := NewRemoteRepository(mustParseURL(tc.url)) 85 if err != nil { 86 t.Errorf("error should be nil but: %s", err) 87 } 88 if repo.IsValid() != tc.valid { 89 t.Errorf("repo.IsValid() should be %v, but %v", tc.valid, repo.IsValid()) 90 } 91 vcs, u, err := repo.VCS() 92 if err == nil { 93 t.Fatalf("error should be nil but: %s", err) 94 } 95 if vcs != tc.vcsBackend { 96 t.Errorf("got: %+v, expect: %+v", vcs, tc.vcsBackend) 97 } 98 if u != nil { 99 t.Errorf("u should be nil: %s", u.String()) 100 } 101 }) 102 } 103 } 104 105 func TestNewRemoteRepository_error(t *testing.T) { 106 testCases := []struct { 107 url string 108 }{{ 109 url: "https://github.com/blog/github", 110 }} 111 112 for _, tc := range testCases { 113 t.Run(tc.url, func(t *testing.T) { 114 repo, err := NewRemoteRepository(mustParseURL(tc.url)) 115 if err == nil { 116 t.Errorf("error should be nil but: %s", err) 117 } 118 if repo != nil { 119 t.Errorf("repo should be nil: %v", repo) 120 } 121 }) 122 } 123 }