|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package librarian |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/go-git/go-git/v5" |
| 22 | + gogitConfig "github.com/go-git/go-git/v5/config" |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + "github.com/googleapis/librarian/internal/github" |
| 25 | + "github.com/googleapis/librarian/internal/gitrepo" |
| 26 | +) |
| 27 | + |
| 28 | +func TestGetGitHubRepositoryFromGitRepo(t *testing.T) { |
| 29 | + t.Parallel() |
| 30 | + for _, test := range []struct { |
| 31 | + name string |
| 32 | + remotes map[string][]string |
| 33 | + wantRepo *github.Repository |
| 34 | + wantErr bool |
| 35 | + wantErrSubstr string |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "origin is a GitHub remote", |
| 39 | + remotes: map[string][]string{ |
| 40 | + "origin": {"https://github.com/owner/repo.git"}, |
| 41 | + }, |
| 42 | + wantRepo: &github.Repository{Owner: "owner", Name: "repo"}, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "No remotes", |
| 46 | + remotes: map[string][]string{}, |
| 47 | + wantErr: true, |
| 48 | + wantErrSubstr: "could not find an 'origin' remote", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "origin is not a GitHub remote", |
| 52 | + remotes: map[string][]string{ |
| 53 | + "origin": {"https://gitlab.com/owner/repo.git"}, |
| 54 | + }, |
| 55 | + wantErr: true, |
| 56 | + wantErrSubstr: "is not a GitHub remote", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "upstream is GitHub, but no origin", |
| 60 | + remotes: map[string][]string{ |
| 61 | + "gitlab": {"https://gitlab.com/owner/repo.git"}, |
| 62 | + "upstream": {"https://github.com/gh-owner/gh-repo.git"}, |
| 63 | + }, |
| 64 | + wantErr: true, |
| 65 | + wantErrSubstr: "could not find an 'origin' remote", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "origin and upstream are GitHub remotes, should use origin", |
| 69 | + remotes: map[string][]string{ |
| 70 | + "origin": {"https://github.com/owner/repo.git"}, |
| 71 | + "upstream": {"https://github.com/owner2/repo2.git"}, |
| 72 | + }, |
| 73 | + wantRepo: &github.Repository{Owner: "owner", Name: "repo"}, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "origin is not GitHub, but upstream is", |
| 77 | + remotes: map[string][]string{ |
| 78 | + "origin": {"https://gitlab.com/owner/repo.git"}, |
| 79 | + "upstream": {"https://github.com/gh-owner/gh-repo.git"}, |
| 80 | + }, |
| 81 | + wantErr: true, |
| 82 | + wantErrSubstr: "is not a GitHub remote", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "origin has multiple URLs, first is GitHub", |
| 86 | + remotes: map[string][]string{ |
| 87 | + "origin": {"https://github.com/owner/repo.git", "https://gitlab.com/owner/repo.git"}, |
| 88 | + }, |
| 89 | + wantRepo: &github.Repository{Owner: "owner", Name: "repo"}, |
| 90 | + }, |
| 91 | + } { |
| 92 | + t.Run(test.name, func(t *testing.T) { |
| 93 | + t.Parallel() |
| 94 | + repo := newTestGitRepoWithRemotes(t, test.remotes) |
| 95 | + |
| 96 | + got, err := GetGitHubRepositoryFromGitRepo(repo) |
| 97 | + |
| 98 | + if test.wantErr { |
| 99 | + if err == nil { |
| 100 | + t.Fatalf("FetchGitHubRepoFromRemote() err = nil, want error containing %q", test.wantErrSubstr) |
| 101 | + } |
| 102 | + if !strings.Contains(err.Error(), test.wantErrSubstr) { |
| 103 | + t.Errorf("FetchGitHubRepoFromRemote() err = %v, want error containing %q", err, test.wantErrSubstr) |
| 104 | + } |
| 105 | + } else { |
| 106 | + if err != nil { |
| 107 | + t.Errorf("FetchGitHubRepoFromRemote() err = %v, want nil", err) |
| 108 | + } |
| 109 | + if diff := cmp.Diff(test.wantRepo, got); diff != "" { |
| 110 | + t.Errorf("FetchGitHubRepoFromRemote() repo mismatch (-want +got): %s", diff) |
| 111 | + } |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// newTestGitRepo creates a new git repository in a temporary directory with the given remotes. |
| 118 | +func newTestGitRepoWithRemotes(t *testing.T, remotes map[string][]string) *gitrepo.LocalRepository { |
| 119 | + t.Helper() |
| 120 | + dir := t.TempDir() |
| 121 | + |
| 122 | + r, err := git.PlainInit(dir, false) |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("git.PlainInit failed: %v", err) |
| 125 | + } |
| 126 | + |
| 127 | + for name, urls := range remotes { |
| 128 | + _, err := r.CreateRemote(&gogitConfig.RemoteConfig{ |
| 129 | + Name: name, |
| 130 | + URLs: urls, |
| 131 | + }) |
| 132 | + if err != nil { |
| 133 | + t.Fatalf("CreateRemote failed: %v", err) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + repo, err := gitrepo.NewRepository(&gitrepo.RepositoryOptions{Dir: dir}) |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("gitrepo.NewRepository failed: %v", err) |
| 140 | + } |
| 141 | + return repo |
| 142 | +} |
0 commit comments