|
| 1 | +package shared |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/cli/go-gh/v2/pkg/api" |
| 9 | + |
| 10 | + "github.com/cli/cli/v2/internal/ghrepo" |
| 11 | + "github.com/cli/cli/v2/pkg/httpmock" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestFetchRefSHA(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + tagName string |
| 20 | + responseStatus int |
| 21 | + responseBody string |
| 22 | + responseMessage string |
| 23 | + expectedSHA string |
| 24 | + errorMessage string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "match (200)", |
| 28 | + tagName: "v1.2.3", |
| 29 | + responseStatus: 200, |
| 30 | + responseBody: `{"object": {"sha": "1234567890abcdef1234567890abcdef12345678"}}`, |
| 31 | + expectedSHA: "1234567890abcdef1234567890abcdef12345678", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "non-match (404)", |
| 35 | + tagName: "v1.2.3", |
| 36 | + responseStatus: 404, |
| 37 | + responseMessage: `Not found`, |
| 38 | + errorMessage: "release not found", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "server error (500)", |
| 42 | + tagName: "v1.2.3", |
| 43 | + responseStatus: 500, |
| 44 | + responseMessage: `arbitrary error"`, |
| 45 | + errorMessage: "HTTP 500: arbitrary error\" (https://api.github.com/repos/owner/repo/git/ref/tags/v1.2.3)", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "malformed JSON with 200", |
| 49 | + tagName: "v1.2.3", |
| 50 | + responseStatus: 200, |
| 51 | + responseBody: `{"object": {"sha":`, |
| 52 | + errorMessage: "failed to parse ref response: unexpected EOF", |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + fakeHTTP := &httpmock.Registry{} |
| 59 | + defer fakeHTTP.Verify(t) |
| 60 | + |
| 61 | + repo, err := ghrepo.FromFullName("owner/repo") |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + path := "repos/owner/repo/git/ref/tags/" + tt.tagName |
| 65 | + if tt.responseStatus == 404 || tt.responseStatus == 500 { |
| 66 | + fakeHTTP.Register( |
| 67 | + httpmock.REST("GET", path), |
| 68 | + httpmock.JSONErrorResponse(tt.responseStatus, api.HTTPError{ |
| 69 | + StatusCode: tt.responseStatus, |
| 70 | + Message: tt.responseMessage, |
| 71 | + }), |
| 72 | + ) |
| 73 | + } else { |
| 74 | + fakeHTTP.Register( |
| 75 | + httpmock.REST("GET", path), |
| 76 | + httpmock.StatusStringResponse(tt.responseStatus, tt.responseBody), |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + httpClient := &http.Client{Transport: fakeHTTP} |
| 81 | + ctx := context.Background() |
| 82 | + |
| 83 | + sha, err := FetchRefSHA(ctx, httpClient, repo, tt.tagName) |
| 84 | + |
| 85 | + if tt.errorMessage != "" { |
| 86 | + assert.Contains(t, err.Error(), tt.errorMessage) |
| 87 | + assert.Empty(t, sha) |
| 88 | + } else { |
| 89 | + require.NoError(t, err) |
| 90 | + assert.Equal(t, tt.expectedSHA, sha) |
| 91 | + } |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
0 commit comments