github.com/google/go-github/v65@v65.0.0/github/repos_codeowners_test.go (about) 1 // Copyright 2022 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 "net/http" 12 "testing" 13 14 "github.com/google/go-cmp/cmp" 15 ) 16 17 func TestRepositoriesService_GetCodeownersErrors_noRef(t *testing.T) { 18 client, mux, _, teardown := setup() 19 defer teardown() 20 21 mux.HandleFunc("/repos/o/r/codeowners/errors", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 testHeader(t, r, "Accept", mediaTypeV3) 24 fmt.Fprint(w, `{ 25 "errors": [ 26 { 27 "line": 1, 28 "column": 1, 29 "kind": "Invalid pattern", 30 "source": "***/*.rb @monalisa", 31 "suggestion": "Did you mean **/*.rb?", 32 "message": "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^", 33 "path": ".github/CODEOWNERS" 34 } 35 ] 36 } 37 `) 38 }) 39 40 ctx := context.Background() 41 codeownersErrors, _, err := client.Repositories.GetCodeownersErrors(ctx, "o", "r", nil) 42 if err != nil { 43 t.Errorf("Repositories.GetCodeownersErrors returned error: %v", err) 44 } 45 46 want := &CodeownersErrors{ 47 Errors: []*CodeownersError{ 48 { 49 Line: 1, 50 Column: 1, 51 Kind: "Invalid pattern", 52 Source: "***/*.rb @monalisa", 53 Suggestion: String("Did you mean **/*.rb?"), 54 Message: "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^", 55 Path: ".github/CODEOWNERS", 56 }, 57 }, 58 } 59 if !cmp.Equal(codeownersErrors, want) { 60 t.Errorf("Repositories.GetCodeownersErrors returned %+v, want %+v", codeownersErrors, want) 61 } 62 63 const methodName = "GetCodeownersErrors" 64 testBadOptions(t, methodName, func() (err error) { 65 _, _, err = client.Repositories.GetCodeownersErrors(ctx, "\n", "\n", nil) 66 return err 67 }) 68 69 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 70 got, resp, err := client.Repositories.GetCodeownersErrors(ctx, "o", "r", nil) 71 if got != nil { 72 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 73 } 74 return resp, err 75 }) 76 } 77 78 func TestRepositoriesService_GetCodeownersErrors_specificRef(t *testing.T) { 79 client, mux, _, teardown := setup() 80 defer teardown() 81 82 mux.HandleFunc("/repos/o/r/codeowners/errors", func(w http.ResponseWriter, r *http.Request) { 83 testMethod(t, r, "GET") 84 testHeader(t, r, "Accept", mediaTypeV3) 85 testFormValues(t, r, values{"ref": "mybranch"}) 86 fmt.Fprint(w, `{ 87 "errors": [ 88 { 89 "line": 1, 90 "column": 1, 91 "kind": "Invalid pattern", 92 "source": "***/*.rb @monalisa", 93 "suggestion": "Did you mean **/*.rb?", 94 "message": "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^", 95 "path": ".github/CODEOWNERS" 96 } 97 ] 98 } 99 `) 100 }) 101 102 opts := &GetCodeownersErrorsOptions{Ref: "mybranch"} 103 ctx := context.Background() 104 codeownersErrors, _, err := client.Repositories.GetCodeownersErrors(ctx, "o", "r", opts) 105 if err != nil { 106 t.Errorf("Repositories.GetCodeownersErrors returned error: %v", err) 107 } 108 109 want := &CodeownersErrors{ 110 Errors: []*CodeownersError{ 111 { 112 Line: 1, 113 Column: 1, 114 Kind: "Invalid pattern", 115 Source: "***/*.rb @monalisa", 116 Suggestion: String("Did you mean **/*.rb?"), 117 Message: "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^", 118 Path: ".github/CODEOWNERS", 119 }, 120 }, 121 } 122 if !cmp.Equal(codeownersErrors, want) { 123 t.Errorf("Repositories.GetCodeownersErrors returned %+v, want %+v", codeownersErrors, want) 124 } 125 126 const methodName = "GetCodeownersErrors" 127 testBadOptions(t, methodName, func() (err error) { 128 _, _, err = client.Repositories.GetCodeownersErrors(ctx, "\n", "\n", opts) 129 return err 130 }) 131 132 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 133 got, resp, err := client.Repositories.GetCodeownersErrors(ctx, "o", "r", opts) 134 if got != nil { 135 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 136 } 137 return resp, err 138 }) 139 } 140 141 func TestCodeownersErrors_Marshal(t *testing.T) { 142 testJSONMarshal(t, &CodeownersErrors{}, "{}") 143 144 u := &CodeownersErrors{ 145 Errors: []*CodeownersError{ 146 { 147 Line: 1, 148 Column: 1, 149 Kind: "Invalid pattern", 150 Source: "***/*.rb @monalisa", 151 Suggestion: String("Did you mean **/*.rb?"), 152 Message: "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^", 153 Path: ".github/CODEOWNERS", 154 }, 155 }, 156 } 157 158 want := `{ 159 "errors": [ 160 { 161 "line": 1, 162 "column": 1, 163 "kind": "Invalid pattern", 164 "source": "***/*.rb @monalisa", 165 "suggestion": "Did you mean **/*.rb?", 166 "message": "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^", 167 "path": ".github/CODEOWNERS" 168 } 169 ] 170 } 171 ` 172 testJSONMarshal(t, u, want) 173 }