@@ -69,6 +69,13 @@ func Test_GetFileContents(t *testing.T) {
6969 {
7070 name : "successful text content fetch" ,
7171 mockedClient : mock .NewMockedHTTPClient (
72+ mock .WithRequestMatchHandler (
73+ mock .GetReposGitRefByOwnerByRepoByRef ,
74+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
75+ w .WriteHeader (http .StatusOK )
76+ _ , _ = w .Write ([]byte (`{"ref": "refs/heads/main", "object": {"sha": ""}}` ))
77+ }),
78+ ),
7279 mock .WithRequestMatchHandler (
7380 raw .GetRawReposContentsByOwnerByRepoByBranchByPath ,
7481 http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
@@ -93,6 +100,13 @@ func Test_GetFileContents(t *testing.T) {
93100 {
94101 name : "successful file blob content fetch" ,
95102 mockedClient : mock .NewMockedHTTPClient (
103+ mock .WithRequestMatchHandler (
104+ mock .GetReposGitRefByOwnerByRepoByRef ,
105+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
106+ w .WriteHeader (http .StatusOK )
107+ _ , _ = w .Write ([]byte (`{"ref": "refs/heads/main", "object": {"sha": ""}}` ))
108+ }),
109+ ),
96110 mock .WithRequestMatchHandler (
97111 raw .GetRawReposContentsByOwnerByRepoByBranchByPath ,
98112 http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
@@ -117,6 +131,20 @@ func Test_GetFileContents(t *testing.T) {
117131 {
118132 name : "successful directory content fetch" ,
119133 mockedClient : mock .NewMockedHTTPClient (
134+ mock .WithRequestMatchHandler (
135+ mock .GetReposByOwnerByRepo ,
136+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
137+ w .WriteHeader (http .StatusOK )
138+ _ , _ = w .Write ([]byte (`{"name": "repo", "default_branch": "main"}` ))
139+ }),
140+ ),
141+ mock .WithRequestMatchHandler (
142+ mock .GetReposGitRefByOwnerByRepoByRef ,
143+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
144+ w .WriteHeader (http .StatusOK )
145+ _ , _ = w .Write ([]byte (`{"ref": "refs/heads/main", "object": {"sha": ""}}` ))
146+ }),
147+ ),
120148 mock .WithRequestMatchHandler (
121149 mock .GetReposContentsByOwnerByRepoByPath ,
122150 expectQueryParams (t , map [string ]string {}).andThen (
@@ -143,6 +171,13 @@ func Test_GetFileContents(t *testing.T) {
143171 {
144172 name : "content fetch fails" ,
145173 mockedClient : mock .NewMockedHTTPClient (
174+ mock .WithRequestMatchHandler (
175+ mock .GetReposGitRefByOwnerByRepoByRef ,
176+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
177+ w .WriteHeader (http .StatusOK )
178+ _ , _ = w .Write ([]byte (`{"ref": "refs/heads/main", "object": {"sha": ""}}` ))
179+ }),
180+ ),
146181 mock .WithRequestMatchHandler (
147182 mock .GetReposContentsByOwnerByRepoByPath ,
148183 http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
@@ -203,7 +238,7 @@ func Test_GetFileContents(t *testing.T) {
203238 textContent := getTextResult (t , result )
204239 var returnedContents []* github.RepositoryContent
205240 err = json .Unmarshal ([]byte (textContent .Text ), & returnedContents )
206- require .NoError (t , err )
241+ require .NoError (t , err , "Failed to unmarshal directory content result: %v" , textContent . Text )
207242 assert .Len (t , returnedContents , len (expected ))
208243 for i , content := range returnedContents {
209244 assert .Equal (t , * expected [i ].Name , * content .Name )
@@ -2049,3 +2084,76 @@ func Test_GetTag(t *testing.T) {
20492084 })
20502085 }
20512086}
2087+
2088+ func Test_ResolveGitReference (t * testing.T ) {
2089+
2090+ ctx := context .Background ()
2091+ owner := "owner"
2092+ repo := "repo"
2093+ mockedClient := mock .NewMockedHTTPClient (
2094+ mock .WithRequestMatchHandler (
2095+ mock .GetReposByOwnerByRepo ,
2096+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
2097+ w .WriteHeader (http .StatusOK )
2098+ _ , _ = w .Write ([]byte (`{"name": "repo", "default_branch": "main"}` ))
2099+ }),
2100+ ),
2101+ mock .WithRequestMatchHandler (
2102+ mock .GetReposGitRefByOwnerByRepoByRef ,
2103+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
2104+ w .WriteHeader (http .StatusOK )
2105+ _ , _ = w .Write ([]byte (`{"ref": "refs/heads/main", "object": {"sha": "123sha456"}}` ))
2106+ }),
2107+ ),
2108+ )
2109+
2110+ tests := []struct {
2111+ name string
2112+ ref string
2113+ sha string
2114+ expectedOutput * raw.ContentOpts
2115+ }{
2116+ {
2117+ name : "sha takes precedence over ref" ,
2118+ ref : "refs/heads/main" ,
2119+ sha : "123sha456" ,
2120+ expectedOutput : & raw.ContentOpts {
2121+ SHA : "123sha456" ,
2122+ },
2123+ },
2124+ {
2125+ name : "use default branch if ref and sha both empty" ,
2126+ ref : "" ,
2127+ sha : "" ,
2128+ expectedOutput : & raw.ContentOpts {
2129+ Ref : "refs/heads/main" ,
2130+ SHA : "123sha456" ,
2131+ },
2132+ },
2133+ {
2134+ name : "get SHA from ref" ,
2135+ ref : "refs/heads/main" ,
2136+ sha : "" ,
2137+ expectedOutput : & raw.ContentOpts {
2138+ Ref : "refs/heads/main" ,
2139+ SHA : "123sha456" ,
2140+ },
2141+ },
2142+ }
2143+
2144+ for _ , tc := range tests {
2145+ t .Run (tc .name , func (t * testing.T ) {
2146+ // Setup client with mock
2147+ client := github .NewClient (mockedClient )
2148+ opts , err := resolveGitReference (ctx , client , owner , repo , tc .ref , tc .sha )
2149+ require .NoError (t , err )
2150+
2151+ if tc .expectedOutput .SHA != "" {
2152+ assert .Equal (t , tc .expectedOutput .SHA , opts .SHA )
2153+ }
2154+ if tc .expectedOutput .Ref != "" {
2155+ assert .Equal (t , tc .expectedOutput .Ref , opts .Ref )
2156+ }
2157+ })
2158+ }
2159+ }
0 commit comments