@@ -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
130131func 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+ }
0 commit comments