@@ -2203,7 +2203,7 @@ func Test_CreateOrUpdateFile(t *testing.T) {
22032203 }
22042204 }
22052205
2206- tests := [] struct {
2206+ type testCase struct {
22072207 name string
22082208 mockedClient * http.Client
22092209 requestArgs map [string ]any
@@ -2213,7 +2213,8 @@ func Test_CreateOrUpdateFile(t *testing.T) {
22132213 expectedErrMsgs []string
22142214 unexpectedErrMsgs []string
22152215 expectedRequestCount int
2216- }{
2216+ }
2217+ tests := []testCase {
22172218 {
22182219 name : "successful file creation" ,
22192220 mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
@@ -2717,6 +2718,40 @@ func Test_CreateOrUpdateFile(t *testing.T) {
27172718 },
27182719 }
27192720
2721+ for _, update := range []bool {false , true } {
2722+ name := "empty file creation"
2723+ args := map [string ]any {"owner" : "owner" , "repo" : "repo" , "path" : "docs/example.md" , "content" : "" , "message" : "Write empty file" , "branch" : "main" }
2724+ body := map [string ]any {"content" : "" , "message" : "Write empty file" , "branch" : "main" }
2725+ handlers := map [string ]http.HandlerFunc {
2726+ "GET /repos/{owner}/{repo}/contents/{path:.*}" : mockResponse (t , http .StatusNotFound , map [string ]any {"message" : "Not Found" }),
2727+ }
2728+ count := 2
2729+ if update {
2730+ name = "clear existing file"
2731+ args ["sha" ] = "abc123def456"
2732+ body ["sha" ] = "abc123def456"
2733+ handlers ["GET /repos/{owner}/{repo}/contents/{path:.*}" ] = mockResponse (t , http .StatusOK , & github.RepositoryContent {SHA : new ("abc123def456 "), Type : new ("file ")})
2734+ handlers [GetReposGitTreesByOwnerByRepoByTree ] = mockPathTree ("100644" )
2735+ count = 4
2736+ }
2737+ handlers ["PUT /repos/{owner}/{repo}/contents/{path:.*}" ] = expectRequestBody (t , body ).andThen (mockResponse (t , http .StatusOK , mockFileResponse ))
2738+ tests = append (tests , testCase {name : name , mockedClient : MockHTTPClientWithHandlers (handlers ), requestArgs : args , expectedContent : mockFileResponse , expectedRequestCount : count })
2739+ }
2740+
2741+ for _, missing := range []bool {false , true } {
2742+ args := map [string ]any {"owner" : "owner" , "repo" : "repo" , "path" : "docs/example.md" , "message" : "Write file" , "branch" : "main" }
2743+ name := "missing content"
2744+ if ! missing {
2745+ name = "null content"
2746+ args ["content" ] = nil
2747+ }
2748+ tests = append (tests , testCase {
2749+ name : name , requestArgs : args ,
2750+ mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {}),
2751+ expectError : true , expectedErrMsg : "missing required parameter: content" ,
2752+ })
2753+ }
2754+
27202755 for _, tc := range tests {
27212756 t .Run (tc .name , func (t * testing.T ) {
27222757 // Setup client with mock
0 commit comments