Skip to content

Commit a3f1b43

Browse files
authored
test(tool/cmd/migrate): add test to fetchGoogleapisWithCommit (#4550)
Add test to fetchGoogleapisWithCommit. For #4514 (comment)
1 parent e731135 commit a3f1b43

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

tool/cmd/migrate/java.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func runJavaMigration(ctx context.Context, repoPath string) error {
136136
if commit == "" {
137137
commit = "master"
138138
}
139-
src, err := fetchSourceWithCommit(ctx, commit)
139+
src, err := fetchSourceWithCommit(ctx, githubEndpoints, commit)
140140
if err != nil {
141141
return errFetchSource
142142
}

tool/cmd/migrate/java_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ import (
2222

2323
"github.com/google/go-cmp/cmp"
2424
"github.com/googleapis/librarian/internal/config"
25+
"github.com/googleapis/librarian/internal/fetch"
2526
)
2627

2728
func TestRunJavaMigration(t *testing.T) {
28-
fetchSourceWithCommit = func(ctx context.Context, commitish string) (*config.Source, error) {
29+
fetchSourceWithCommit = func(ctx context.Context, endpoints *fetch.Endpoints, commitish string) (*config.Source, error) {
2930
return &config.Source{
3031
Commit: commitish,
3132
SHA256: "sha123",

tool/cmd/migrate/legacylibrarian.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ var (
4343
"logging": "logadmin",
4444
"pubsub": "v2",
4545
}
46+
47+
githubEndpoints = &fetch.Endpoints{
48+
API: "https://api.github.com",
49+
Download: "https://github.com",
50+
}
4651
)
4752

4853
type goGAPICInfo struct {
@@ -177,20 +182,16 @@ func buildConfigFromLibrarian(ctx context.Context, input *MigrationInput) (*conf
177182
}
178183

179184
func fetchGoogleapis(ctx context.Context) (*config.Source, error) {
180-
return fetchGoogleapisWithCommit(ctx, fetch.DefaultBranchMaster)
185+
return fetchGoogleapisWithCommit(ctx, githubEndpoints, fetch.DefaultBranchMaster)
181186
}
182187

183-
func fetchGoogleapisWithCommit(ctx context.Context, commitish string) (*config.Source, error) {
184-
endpoint := &fetch.Endpoints{
185-
API: "https://api.github.com",
186-
Download: "https://github.com",
187-
}
188+
func fetchGoogleapisWithCommit(ctx context.Context, endpoints *fetch.Endpoints, commitish string) (*config.Source, error) {
188189
repo := &fetch.Repo{
189190
Org: "googleapis",
190191
Repo: "googleapis",
191192
Branch: commitish,
192193
}
193-
commit, sha256, err := fetch.LatestCommitAndChecksum(endpoint, repo)
194+
commit, sha256, err := fetch.LatestCommitAndChecksum(endpoints, repo)
194195
if err != nil {
195196
return nil, err
196197
}

tool/cmd/migrate/legacylibrarian_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ package main
1717
import (
1818
"context"
1919
"errors"
20+
"fmt"
21+
"net/http"
22+
"net/http/httptest"
2023
"os"
24+
"path/filepath"
2125
"strings"
2226
"testing"
2327

2428
"github.com/google/go-cmp/cmp"
2529
"github.com/google/go-cmp/cmp/cmpopts"
2630
"github.com/googleapis/librarian/internal/config"
31+
"github.com/googleapis/librarian/internal/fetch"
2732
"github.com/googleapis/librarian/internal/legacylibrarian/legacyconfig"
2833
)
2934

@@ -935,3 +940,52 @@ func TestToAPIs(t *testing.T) {
935940
t.Errorf("mismatch (-want +got):\n%s", diff)
936941
}
937942
}
943+
944+
func TestFetchGoogleapisWithCommit(t *testing.T) {
945+
const (
946+
wantCommit = "abcd123"
947+
wantSHA = "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" // sha256 of "password"
948+
)
949+
// Mock GitHub server
950+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
951+
if strings.Contains(r.URL.Path, "/commits/") {
952+
w.Write([]byte(wantCommit))
953+
return
954+
}
955+
if strings.Contains(r.URL.Path, ".tar.gz") {
956+
w.Write([]byte("password"))
957+
return
958+
}
959+
w.WriteHeader(http.StatusNotFound)
960+
}))
961+
defer ts.Close()
962+
endpoints := &fetch.Endpoints{
963+
API: ts.URL,
964+
Download: ts.URL,
965+
}
966+
// Mock cache
967+
tmp := t.TempDir()
968+
t.Setenv("LIBRARIAN_CACHE", tmp)
969+
// Pre-populate cache to avoid RepoDir downloading (which ignores our mock download URL)
970+
cachePath := filepath.Join(tmp, fmt.Sprintf("%s@%s", googleapisRepo, wantCommit))
971+
if err := os.MkdirAll(cachePath, 0755); err != nil {
972+
t.Fatal(err)
973+
}
974+
if err := os.WriteFile(filepath.Join(cachePath, "dummy"), []byte("dummy"), 0644); err != nil {
975+
t.Fatal(err)
976+
}
977+
978+
got, err := fetchGoogleapisWithCommit(t.Context(), endpoints, "master")
979+
if err != nil {
980+
t.Fatal(err)
981+
}
982+
983+
want := &config.Source{
984+
Commit: wantCommit,
985+
SHA256: wantSHA,
986+
Dir: cachePath,
987+
}
988+
if diff := cmp.Diff(want, got); diff != "" {
989+
t.Errorf("mismatch (-want +got):\n%s", diff)
990+
}
991+
}

0 commit comments

Comments
 (0)