Skip to content

Commit e6f4497

Browse files
authored
refactor: makes fetch.LatestSha and fetch.Sha256 private and refactor to tests (#3170)
This is a followup to #3143 that: - adds a test for failures in `LatestCommitAndChecksum` - refactor `internal/sidekick/config/update_root_config.go` to use, and simplify it's test. - renames to make fetch.LatestSha and fetch.Sha256 private as they are not used in sidekick anymore. Fix #3068
1 parent 00a83c7 commit e6f4497

File tree

4 files changed

+134
-187
lines changed

4 files changed

+134
-187
lines changed

internal/fetch/fetch.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ func RepoFromTarballLink(githubDownload, tarballLink string) (*Repo, error) {
7272
return repo, nil
7373
}
7474

75-
// Sha256 downloads the content from the given URL and returns its SHA256
75+
// urlSha256 downloads the content from the given URL and returns its SHA256
7676
// checksum as a hex string.
77-
func Sha256(query string) (string, error) {
77+
func urlSha256(query string) (string, error) {
7878
response, err := http.Get(query)
7979
if err != nil {
8080
return "", err
@@ -92,9 +92,9 @@ func Sha256(query string) (string, error) {
9292
return got, nil
9393
}
9494

95-
// LatestSha fetches the latest commit SHA from the GitHub API for the given
95+
// latestSha fetches the latest commit SHA from the GitHub API for the given
9696
// repository URL.
97-
func LatestSha(query string) (string, error) {
97+
func latestSha(query string) (string, error) {
9898
client := &http.Client{}
9999
request, err := http.NewRequest(http.MethodGet, query, nil)
100100
if err != nil {
@@ -120,13 +120,13 @@ func LatestSha(query string) (string, error) {
120120
// commit from the GitHub API for the given repository.
121121
func LatestCommitAndChecksum(endpoints *Endpoints, repo *Repo) (commit, sha256 string, err error) {
122122
apiURL := fmt.Sprintf("%s/repos/%s/%s/commits/%s", endpoints.API, repo.Org, repo.Repo, branch)
123-
commit, err = LatestSha(apiURL)
123+
commit, err = latestSha(apiURL)
124124
if err != nil {
125125
return "", "", err
126126
}
127127

128128
tarballURL := TarballLink(endpoints.Download, repo, commit)
129-
sha256, err = Sha256(tarballURL)
129+
sha256, err = urlSha256(tarballURL)
130130
if err != nil {
131131
return "", "", err
132132
}

internal/fetch/fetch_test.go

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"net/http/httptest"
2727
"os"
2828
"path"
29+
"strings"
2930
"testing"
3031
"time"
3132

@@ -89,7 +90,7 @@ func TestSha256(t *testing.T) {
8990
}))
9091
defer server.Close()
9192

92-
got, err := Sha256(server.URL + tarballPath)
93+
got, err := urlSha256(server.URL + tarballPath)
9394
if err != nil {
9495
t.Fatal(err)
9596
}
@@ -120,7 +121,7 @@ func TestSha256Error(t *testing.T) {
120121
},
121122
} {
122123
t.Run(test.name, func(t *testing.T) {
123-
if _, err := Sha256(test.url); err == nil {
124+
if _, err := urlSha256(test.url); err == nil {
124125
t.Error("expected an error from Sha256()")
125126
}
126127
})
@@ -129,8 +130,8 @@ func TestSha256Error(t *testing.T) {
129130

130131
func TestLatestSha(t *testing.T) {
131132
const (
132-
getLatestShaPath = "/repos/googleapis/googleapis/commits/master"
133-
latestSha = "5d5b1bf126485b0e2c972bac41b376438601e266"
133+
getLatestShaPath = "/repos/googleapis/googleapis/commits/master"
134+
expectedCommitSha = "5d5b1bf126485b0e2c972bac41b376438601e266"
134135
)
135136

136137
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -143,16 +144,16 @@ func TestLatestSha(t *testing.T) {
143144
t.Fatalf("mismatched Accept header for %q, got=%q, want=%s", r.URL.Path, got, want)
144145
}
145146
w.WriteHeader(http.StatusOK)
146-
w.Write([]byte(latestSha))
147+
w.Write([]byte(expectedCommitSha))
147148
}))
148149
defer server.Close()
149150

150-
got, err := LatestSha(server.URL + getLatestShaPath)
151+
got, err := latestSha(server.URL + getLatestShaPath)
151152
if err != nil {
152153
t.Fatal(err)
153154
}
154-
if got != latestSha {
155-
t.Errorf("LatestSha() = %q, want %q", got, latestSha)
155+
if got != expectedCommitSha {
156+
t.Errorf("LatestSha() = %q, want %q", got, expectedCommitSha)
156157
}
157158
}
158159

@@ -178,7 +179,7 @@ func TestLatestShaError(t *testing.T) {
178179
},
179180
} {
180181
t.Run(test.name, func(t *testing.T) {
181-
if _, err := LatestSha(test.url); err == nil {
182+
if _, err := latestSha(test.url); err == nil {
182183
t.Error("expected an error from LatestSha()")
183184
}
184185
})
@@ -619,3 +620,49 @@ func TestDownloadTarballRetry(t *testing.T) {
619620
}
620621
})
621622
}
623+
624+
func TestLatestCommitAndChecksumFailure(t *testing.T) {
625+
const (
626+
commit = "test-commit-sha"
627+
testOrg = "test-org"
628+
testRepo = "test-repo"
629+
)
630+
631+
t.Run("LatestSha fails", func(t *testing.T) {
632+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
633+
// Fail the first call, which is to get the latest SHA
634+
http.Error(w, "failed to get latest sha", http.StatusInternalServerError)
635+
}))
636+
defer server.Close()
637+
638+
endpoints := &Endpoints{API: server.URL, Download: server.URL}
639+
repo := &Repo{Org: testOrg, Repo: testRepo}
640+
641+
_, _, err := LatestCommitAndChecksum(endpoints, repo)
642+
if err == nil {
643+
t.Error("expected an error when LatestSha fails, but got nil")
644+
}
645+
})
646+
647+
t.Run("Sha256 fails", func(t *testing.T) {
648+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
649+
// The first call is for the latest SHA, which should succeed.
650+
if strings.HasSuffix(r.URL.Path, "/commits/master") {
651+
w.WriteHeader(http.StatusOK)
652+
w.Write([]byte(commit))
653+
return
654+
}
655+
// The second call is for the tarball, which should fail.
656+
http.Error(w, "failed to download tarball", http.StatusInternalServerError)
657+
}))
658+
defer server.Close()
659+
660+
endpoints := &Endpoints{API: server.URL, Download: server.URL}
661+
repo := &Repo{Org: testOrg, Repo: testRepo}
662+
663+
_, _, err := LatestCommitAndChecksum(endpoints, repo)
664+
if err == nil {
665+
t.Error("expected an error when Sha256 fails, but got nil")
666+
}
667+
})
668+
}

internal/sidekick/config/update_root_config.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ import (
2222
"github.com/googleapis/librarian/internal/fetch"
2323
)
2424

25+
var latestCommitAndChecksum = fetch.LatestCommitAndChecksum
26+
2527
const (
2628
defaultGitHubAPI = "https://api.github.com"
2729
defaultGitHubDn = "https://github.com"
28-
branch = "master"
2930
defaultRoot = "googleapis"
3031
)
3132

@@ -40,16 +41,7 @@ func UpdateRootConfig(rootConfig *Config, rootName string) error {
4041
return err
4142
}
4243

43-
query := fmt.Sprintf("%s/repos/%s/%s/commits/%s", endpoints.API, repo.Org, repo.Repo, branch)
44-
fmt.Printf("getting latest SHA from %q\n", query)
45-
latestSha, err := fetch.LatestSha(query)
46-
if err != nil {
47-
return err
48-
}
49-
50-
newLink := fetch.TarballLink(endpoints.Download, repo, latestSha)
51-
fmt.Printf("computing SHA256 for %q\n", newLink)
52-
newSha256, err := fetch.Sha256(newLink)
44+
latestSha, newSha256, err := latestCommitAndChecksum(endpoints, repo)
5345
if err != nil {
5446
return err
5547
}

0 commit comments

Comments
 (0)