Skip to content

Commit 37367b0

Browse files
committed
Add tests
1 parent 7395fac commit 37367b0

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

pkg/github/repositories_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,83 @@ func Test_CreateOrUpdateFile(t *testing.T) {
11211121
expectError: true,
11221122
expectedErrMsg: "failed to create/update file",
11231123
},
1124+
{
1125+
name: "file creation fails with missing sha, then succeeds after fetching sha",
1126+
mockedClient: mock.NewMockedHTTPClient(
1127+
mock.WithRequestMatchHandler(
1128+
mock.PutReposContentsByOwnerByRepoByPath,
1129+
func() http.HandlerFunc {
1130+
callCount := 0
1131+
return func(w http.ResponseWriter, _ *http.Request) {
1132+
callCount++
1133+
if callCount == 1 {
1134+
// First call fails with "sha wasn't supplied" error
1135+
w.WriteHeader(http.StatusUnprocessableEntity)
1136+
_, _ = w.Write([]byte(`{"message": "\"sha\" wasn't supplied"}`))
1137+
} else {
1138+
// Second call succeeds after SHA is retrieved
1139+
w.WriteHeader(http.StatusOK)
1140+
respBytes, _ := json.Marshal(mockFileResponse)
1141+
_, _ = w.Write(respBytes)
1142+
}
1143+
}
1144+
}(),
1145+
),
1146+
mock.WithRequestMatchHandler(
1147+
mock.GetReposContentsByOwnerByRepoByPath,
1148+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1149+
w.WriteHeader(http.StatusOK)
1150+
existingFile := &github.RepositoryContent{
1151+
Name: github.Ptr("example.md"),
1152+
Path: github.Ptr("docs/example.md"),
1153+
SHA: github.Ptr("abc123def456"),
1154+
Type: github.Ptr("file"),
1155+
}
1156+
contentBytes, _ := json.Marshal(existingFile)
1157+
_, _ = w.Write(contentBytes)
1158+
}),
1159+
),
1160+
),
1161+
requestArgs: map[string]interface{}{
1162+
"owner": "owner",
1163+
"repo": "repo",
1164+
"path": "docs/example.md",
1165+
"content": "# Example\n\nThis is an example file.",
1166+
"message": "Add example file",
1167+
"branch": "main",
1168+
},
1169+
expectError: false,
1170+
expectedContent: mockFileResponse,
1171+
},
1172+
{
1173+
name: "file creation fails with missing sha and GetContents also fails",
1174+
mockedClient: mock.NewMockedHTTPClient(
1175+
mock.WithRequestMatchHandler(
1176+
mock.PutReposContentsByOwnerByRepoByPath,
1177+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1178+
w.WriteHeader(http.StatusUnprocessableEntity)
1179+
_, _ = w.Write([]byte(`{"message": "\"sha\" wasn't supplied"}`))
1180+
}),
1181+
),
1182+
mock.WithRequestMatchHandler(
1183+
mock.GetReposContentsByOwnerByRepoByPath,
1184+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1185+
w.WriteHeader(http.StatusNotFound)
1186+
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
1187+
}),
1188+
),
1189+
),
1190+
requestArgs: map[string]interface{}{
1191+
"owner": "owner",
1192+
"repo": "repo",
1193+
"path": "docs/example.md",
1194+
"content": "# Example\n\nThis is an example file.",
1195+
"message": "Add example file",
1196+
"branch": "main",
1197+
},
1198+
expectError: true,
1199+
expectedErrMsg: "failed to get file SHA for update",
1200+
},
11241201
}
11251202

11261203
for _, tc := range tests {

0 commit comments

Comments
 (0)