Skip to content

Commit 09e1563

Browse files
williammartinSamMorrowDrums
authored andcommitted
Test path params for tag tools
1 parent 866a791 commit 09e1563

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

pkg/github/helper_test.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import (
1010
"github.com/stretchr/testify/require"
1111
)
1212

13+
// expectPath is a helper function to create a partial mock that expects a
14+
// request with the given path, with the ability to chain a response handler.
15+
func expectPath(t *testing.T, expectedPath string) *partialMock {
16+
return &partialMock{
17+
t: t,
18+
expectedPath: expectedPath,
19+
}
20+
}
21+
1322
// expectQueryParams is a helper function to create a partial mock that expects a
1423
// request with the given query parameters, with the ability to chain a response handler.
1524
func expectQueryParams(t *testing.T, expectedQueryParams map[string]string) *partialMock {
@@ -29,20 +38,18 @@ func expectRequestBody(t *testing.T, expectedRequestBody any) *partialMock {
2938
}
3039

3140
type partialMock struct {
32-
t *testing.T
41+
t *testing.T
42+
43+
expectedPath string
3344
expectedQueryParams map[string]string
3445
expectedRequestBody any
3546
}
3647

3748
func (p *partialMock) andThen(responseHandler http.HandlerFunc) http.HandlerFunc {
3849
p.t.Helper()
3950
return func(w http.ResponseWriter, r *http.Request) {
40-
if p.expectedRequestBody != nil {
41-
var unmarshaledRequestBody any
42-
err := json.NewDecoder(r.Body).Decode(&unmarshaledRequestBody)
43-
require.NoError(p.t, err)
44-
45-
require.Equal(p.t, p.expectedRequestBody, unmarshaledRequestBody)
51+
if p.expectedPath != "" {
52+
require.Equal(p.t, p.expectedPath, r.URL.Path)
4653
}
4754

4855
if p.expectedQueryParams != nil {
@@ -52,6 +59,14 @@ func (p *partialMock) andThen(responseHandler http.HandlerFunc) http.HandlerFunc
5259
}
5360
}
5461

62+
if p.expectedRequestBody != nil {
63+
var unmarshaledRequestBody any
64+
err := json.NewDecoder(r.Body).Decode(&unmarshaledRequestBody)
65+
require.NoError(p.t, err)
66+
67+
require.Equal(p.t, p.expectedRequestBody, unmarshaledRequestBody)
68+
}
69+
5570
responseHandler(w, r)
5671
}
5772
}

pkg/github/repositories_test.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ func Test_ListTags(t *testing.T) {
15451545
{
15461546
Name: github.Ptr("v1.0.0"),
15471547
Commit: &github.Commit{
1548-
SHA: github.Ptr("abc123"),
1548+
SHA: github.Ptr("v1.0.0-tag-sha"),
15491549
URL: github.Ptr("https://api.github.com/repos/owner/repo/commits/abc123"),
15501550
},
15511551
ZipballURL: github.Ptr("https://github.com/owner/repo/zipball/v1.0.0"),
@@ -1554,7 +1554,7 @@ func Test_ListTags(t *testing.T) {
15541554
{
15551555
Name: github.Ptr("v0.9.0"),
15561556
Commit: &github.Commit{
1557-
SHA: github.Ptr("def456"),
1557+
SHA: github.Ptr("v0.9.0-tag-sha"),
15581558
URL: github.Ptr("https://api.github.com/repos/owner/repo/commits/def456"),
15591559
},
15601560
ZipballURL: github.Ptr("https://github.com/owner/repo/zipball/v0.9.0"),
@@ -1573,9 +1573,14 @@ func Test_ListTags(t *testing.T) {
15731573
{
15741574
name: "successful tags list",
15751575
mockedClient: mock.NewMockedHTTPClient(
1576-
mock.WithRequestMatch(
1576+
mock.WithRequestMatchHandler(
15771577
mock.GetReposTagsByOwnerByRepo,
1578-
mockTags,
1578+
expectPath(
1579+
t,
1580+
"/repos/owner/repo/tags",
1581+
).andThen(
1582+
mockResponse(t, http.StatusOK, mockTags),
1583+
),
15791584
),
15801585
),
15811586
requestArgs: map[string]interface{}{
@@ -1659,12 +1664,12 @@ func Test_GetTag(t *testing.T) {
16591664
mockTagRef := &github.Reference{
16601665
Ref: github.Ptr("refs/tags/v1.0.0"),
16611666
Object: &github.GitObject{
1662-
SHA: github.Ptr("tag123"),
1667+
SHA: github.Ptr("v1.0.0-tag-sha"),
16631668
},
16641669
}
16651670

16661671
mockTagObj := &github.Tag{
1667-
SHA: github.Ptr("tag123"),
1672+
SHA: github.Ptr("v1.0.0-tag-sha"),
16681673
Tag: github.Ptr("v1.0.0"),
16691674
Message: github.Ptr("Release v1.0.0"),
16701675
Object: &github.GitObject{
@@ -1684,13 +1689,23 @@ func Test_GetTag(t *testing.T) {
16841689
{
16851690
name: "successful tag retrieval",
16861691
mockedClient: mock.NewMockedHTTPClient(
1687-
mock.WithRequestMatch(
1692+
mock.WithRequestMatchHandler(
16881693
mock.GetReposGitRefByOwnerByRepoByRef,
1689-
mockTagRef,
1694+
expectPath(
1695+
t,
1696+
"/repos/owner/repo/git/ref/tags/v1.0.0",
1697+
).andThen(
1698+
mockResponse(t, http.StatusOK, mockTagRef),
1699+
),
16901700
),
1691-
mock.WithRequestMatch(
1701+
mock.WithRequestMatchHandler(
16921702
mock.GetReposGitTagsByOwnerByRepoByTagSha,
1693-
mockTagObj,
1703+
expectPath(
1704+
t,
1705+
"/repos/owner/repo/git/tags/v1.0.0-tag-sha",
1706+
).andThen(
1707+
mockResponse(t, http.StatusOK, mockTagObj),
1708+
),
16941709
),
16951710
),
16961711
requestArgs: map[string]interface{}{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy