Skip to content

Commit 9598613

Browse files
Remove references to io/ioutil package (#2547)
1 parent 331265b commit 9598613

File tree

14 files changed

+46
-52
lines changed

14 files changed

+46
-52
lines changed

example/commitpr/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"errors"
2626
"flag"
2727
"fmt"
28-
"io/ioutil"
2928
"log"
3029
"os"
3130
"strings"
@@ -118,7 +117,7 @@ func getFileContent(fileArg string) (targetName string, b []byte, err error) {
118117
targetName = files[1]
119118
}
120119

121-
b, err = ioutil.ReadFile(localFile)
120+
b, err = os.ReadFile(localFile)
122121
return targetName, b, err
123122
}
124123

example/newfilewithappauth/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ package main
1010

1111
import (
1212
"context"
13-
"io/ioutil"
1413
"log"
1514
"net/http"
15+
"os"
1616
"time"
1717

1818
"github.com/bradleyfalzon/ghinstallation/v2"
@@ -23,7 +23,7 @@ import (
2323
func main() {
2424
const gitHost = "https://git.api.com"
2525

26-
privatePem, err := ioutil.ReadFile("path/to/pem")
26+
privatePem, err := os.ReadFile("path/to/pem")
2727
if err != nil {
2828
log.Fatalf("failed to read pem: %v", err)
2929
}

github/gen-accessors.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"go/format"
2222
"go/parser"
2323
"go/token"
24-
"io/ioutil"
2524
"log"
2625
"os"
2726
"sort"
@@ -190,7 +189,7 @@ func (t *templateData) dump() error {
190189
return fmt.Errorf("os.Chmod(%q, 0644): %v", filename, err)
191190
}
192191

193-
if err := ioutil.WriteFile(filename, clean, 0444); err != nil {
192+
if err := os.WriteFile(filename, clean, 0444); err != nil {
194193
return err
195194
}
196195

github/gen-stringify-test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"go/format"
2525
"go/parser"
2626
"go/token"
27-
"io/ioutil"
2827
"log"
2928
"os"
3029
"strings"
@@ -356,7 +355,7 @@ func (t *templateData) dump() error {
356355
return fmt.Errorf("os.Chmod(%q, 0644): %v", t.filename, err)
357356
}
358357

359-
if err := ioutil.WriteFile(t.filename, clean, 0444); err != nil {
358+
if err := os.WriteFile(t.filename, clean, 0444); err != nil {
360359
return err
361360
}
362361

github/git_trees_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"bytes"
1010
"context"
1111
"fmt"
12-
"io/ioutil"
12+
"io"
1313
"net/http"
1414
"testing"
1515

@@ -106,7 +106,7 @@ func TestGitService_CreateTree(t *testing.T) {
106106
}
107107

108108
mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) {
109-
got, err := ioutil.ReadAll(r.Body)
109+
got, err := io.ReadAll(r.Body)
110110
if err != nil {
111111
t.Fatalf("unable to read body: %v", err)
112112
}
@@ -184,7 +184,7 @@ func TestGitService_CreateTree_Content(t *testing.T) {
184184
}
185185

186186
mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) {
187-
got, err := ioutil.ReadAll(r.Body)
187+
got, err := io.ReadAll(r.Body)
188188
if err != nil {
189189
t.Fatalf("unable to read body: %v", err)
190190
}
@@ -264,7 +264,7 @@ func TestGitService_CreateTree_Delete(t *testing.T) {
264264
}
265265

266266
mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) {
267-
got, err := ioutil.ReadAll(r.Body)
267+
got, err := io.ReadAll(r.Body)
268268
if err != nil {
269269
t.Fatalf("unable to read body: %v", err)
270270
}

github/github.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"errors"
1616
"fmt"
1717
"io"
18-
"io/ioutil"
1918
"net/http"
2019
"net/url"
2120
"reflect"
@@ -710,7 +709,7 @@ func (c *Client) BareDo(ctx context.Context, req *http.Request) (*Response, erro
710709
// Issue #1022
711710
aerr, ok := err.(*AcceptedError)
712711
if ok {
713-
b, readErr := ioutil.ReadAll(resp.Body)
712+
b, readErr := io.ReadAll(resp.Body)
714713
if readErr != nil {
715714
return response, readErr
716715
}
@@ -770,7 +769,7 @@ func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory rat
770769
StatusCode: http.StatusForbidden,
771770
Request: req,
772771
Header: make(http.Header),
773-
Body: ioutil.NopCloser(strings.NewReader("")),
772+
Body: io.NopCloser(strings.NewReader("")),
774773
}
775774
return &RateLimitError{
776775
Rate: rate,
@@ -1032,14 +1031,14 @@ func CheckResponse(r *http.Response) error {
10321031
}
10331032

10341033
errorResponse := &ErrorResponse{Response: r}
1035-
data, err := ioutil.ReadAll(r.Body)
1034+
data, err := io.ReadAll(r.Body)
10361035
if err == nil && data != nil {
10371036
json.Unmarshal(data, errorResponse)
10381037
}
10391038
// Re-populate error response body because GitHub error responses are often
10401039
// undocumented and inconsistent.
10411040
// Issue #1136, #540.
1042-
r.Body = ioutil.NopCloser(bytes.NewBuffer(data))
1041+
r.Body = io.NopCloser(bytes.NewBuffer(data))
10431042
switch {
10441043
case r.StatusCode == http.StatusUnauthorized && strings.HasPrefix(r.Header.Get(headerOTP), "required"):
10451044
return (*TwoFactorAuthError)(errorResponse)

github/github_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"encoding/json"
1111
"errors"
1212
"fmt"
13-
"io/ioutil"
13+
"io"
1414
"net/http"
1515
"net/http/httptest"
1616
"net/url"
@@ -70,7 +70,7 @@ func setup() (client *Client, mux *http.ServeMux, serverURL string, teardown fun
7070
// directory, and create the file in that directory. It is the caller's
7171
// responsibility to remove the directory and its contents when no longer needed.
7272
func openTestFile(name, content string) (file *os.File, dir string, err error) {
73-
dir, err = ioutil.TempDir("", "go-github")
73+
dir, err = os.MkdirTemp("", "go-github")
7474
if err != nil {
7575
return nil, dir, err
7676
}
@@ -133,7 +133,7 @@ func testURLParseError(t *testing.T, err error) {
133133

134134
func testBody(t *testing.T, r *http.Request, want string) {
135135
t.Helper()
136-
b, err := ioutil.ReadAll(r.Body)
136+
b, err := io.ReadAll(r.Body)
137137
if err != nil {
138138
t.Errorf("Error reading request body: %v", err)
139139
}
@@ -502,7 +502,7 @@ func TestNewRequest(t *testing.T) {
502502
}
503503

504504
// test that body was JSON encoded
505-
body, _ := ioutil.ReadAll(req.Body)
505+
body, _ := io.ReadAll(req.Body)
506506
if got, want := string(body), outBody; got != want {
507507
t.Errorf("NewRequest(%q) Body is %v, want %v", inBody, got, want)
508508
}
@@ -617,7 +617,7 @@ func TestNewFormRequest(t *testing.T) {
617617
}
618618

619619
// test that body was form encoded
620-
body, _ := ioutil.ReadAll(req.Body)
620+
body, _ := io.ReadAll(req.Body)
621621
if got, want := string(body), outBody; got != want {
622622
t.Errorf("NewFormRequest(%q) Body is %v, want %v", inBody, got, want)
623623
}
@@ -1398,7 +1398,7 @@ func TestCheckResponse(t *testing.T) {
13981398
res := &http.Response{
13991399
Request: &http.Request{},
14001400
StatusCode: http.StatusBadRequest,
1401-
Body: ioutil.NopCloser(strings.NewReader(`{"message":"m",
1401+
Body: io.NopCloser(strings.NewReader(`{"message":"m",
14021402
"errors": [{"resource": "r", "field": "f", "code": "c"}],
14031403
"block": {"reason": "dmca", "created_at": "2016-03-17T15:39:46Z"}}`)),
14041404
}
@@ -1427,7 +1427,7 @@ func TestCheckResponse_RateLimit(t *testing.T) {
14271427
Request: &http.Request{},
14281428
StatusCode: http.StatusForbidden,
14291429
Header: http.Header{},
1430-
Body: ioutil.NopCloser(strings.NewReader(`{"message":"m",
1430+
Body: io.NopCloser(strings.NewReader(`{"message":"m",
14311431
"documentation_url": "url"}`)),
14321432
}
14331433
res.Header.Set(headerRateLimit, "60")
@@ -1454,7 +1454,7 @@ func TestCheckResponse_AbuseRateLimit(t *testing.T) {
14541454
res := &http.Response{
14551455
Request: &http.Request{},
14561456
StatusCode: http.StatusForbidden,
1457-
Body: ioutil.NopCloser(strings.NewReader(`{"message":"m",
1457+
Body: io.NopCloser(strings.NewReader(`{"message":"m",
14581458
"documentation_url": "docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"}`)),
14591459
}
14601460
err := CheckResponse(res).(*AbuseRateLimitError)
@@ -1847,7 +1847,7 @@ func TestCheckResponse_noBody(t *testing.T) {
18471847
res := &http.Response{
18481848
Request: &http.Request{},
18491849
StatusCode: http.StatusBadRequest,
1850-
Body: ioutil.NopCloser(strings.NewReader("")),
1850+
Body: io.NopCloser(strings.NewReader("")),
18511851
}
18521852
err := CheckResponse(res).(*ErrorResponse)
18531853

@@ -1868,7 +1868,7 @@ func TestCheckResponse_unexpectedErrorStructure(t *testing.T) {
18681868
res := &http.Response{
18691869
Request: &http.Request{},
18701870
StatusCode: http.StatusBadRequest,
1871-
Body: ioutil.NopCloser(strings.NewReader(httpBody)),
1871+
Body: io.NopCloser(strings.NewReader(httpBody)),
18721872
}
18731873
err := CheckResponse(res).(*ErrorResponse)
18741874

@@ -1884,7 +1884,7 @@ func TestCheckResponse_unexpectedErrorStructure(t *testing.T) {
18841884
if !errors.Is(err, want) {
18851885
t.Errorf("Error = %#v, want %#v", err, want)
18861886
}
1887-
data, err2 := ioutil.ReadAll(err.Response.Body)
1887+
data, err2 := io.ReadAll(err.Response.Body)
18881888
if err2 != nil {
18891889
t.Fatalf("failed to read response body: %v", err)
18901890
}
@@ -2373,9 +2373,9 @@ func TestBareDo_returnsOpenBody(t *testing.T) {
23732373
t.Fatalf("client.BareDo returned error: %v", err)
23742374
}
23752375

2376-
got, err := ioutil.ReadAll(resp.Body)
2376+
got, err := io.ReadAll(resp.Body)
23772377
if err != nil {
2378-
t.Fatalf("ioutil.ReadAll returned error: %v", err)
2378+
t.Fatalf("io.ReadAll returned error: %v", err)
23792379
}
23802380
if string(got) != expectedBody {
23812381
t.Fatalf("Expected %q, got %q", expectedBody, string(got))

github/messages.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"fmt"
2020
"hash"
2121
"io"
22-
"io/ioutil"
2322
"mime"
2423
"net/http"
2524
"net/url"
@@ -171,7 +170,7 @@ func ValidatePayloadFromBody(contentType string, readable io.Reader, signature s
171170
switch contentType {
172171
case "application/json":
173172
var err error
174-
if body, err = ioutil.ReadAll(readable); err != nil {
173+
if body, err = io.ReadAll(readable); err != nil {
175174
return nil, err
176175
}
177176

@@ -185,7 +184,7 @@ func ValidatePayloadFromBody(contentType string, readable io.Reader, signature s
185184
const payloadFormParam = "payload"
186185

187186
var err error
188-
if body, err = ioutil.ReadAll(readable); err != nil {
187+
if body, err = io.ReadAll(readable); err != nil {
189188
return nil, err
190189
}
191190

github/repos_contents_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"context"
1010
"errors"
1111
"fmt"
12-
"io/ioutil"
12+
"io"
1313
"net/http"
1414
"net/url"
1515
"testing"
@@ -146,7 +146,7 @@ func TestRepositoriesService_DownloadContents_Success(t *testing.T) {
146146
t.Errorf("Repositories.DownloadContents returned status code %v, want %v", got, want)
147147
}
148148

149-
bytes, err := ioutil.ReadAll(r)
149+
bytes, err := io.ReadAll(r)
150150
if err != nil {
151151
t.Errorf("Error reading response body: %v", err)
152152
}
@@ -198,7 +198,7 @@ func TestRepositoriesService_DownloadContents_FailedResponse(t *testing.T) {
198198
t.Errorf("Repositories.DownloadContents returned status code %v, want %v", got, want)
199199
}
200200

201-
bytes, err := ioutil.ReadAll(r)
201+
bytes, err := io.ReadAll(r)
202202
if err != nil {
203203
t.Errorf("Error reading response body: %v", err)
204204
}
@@ -276,7 +276,7 @@ func TestRepositoriesService_DownloadContentsWithMeta_Success(t *testing.T) {
276276
t.Errorf("Repositories.DownloadContentsWithMeta returned status code %v, want %v", got, want)
277277
}
278278

279-
bytes, err := ioutil.ReadAll(r)
279+
bytes, err := io.ReadAll(r)
280280
if err != nil {
281281
t.Errorf("Error reading response body: %v", err)
282282
}
@@ -339,7 +339,7 @@ func TestRepositoriesService_DownloadContentsWithMeta_FailedResponse(t *testing.
339339
t.Errorf("Repositories.DownloadContentsWithMeta returned status code %v, want %v", got, want)
340340
}
341341

342-
bytes, err := ioutil.ReadAll(r)
342+
bytes, err := io.ReadAll(r)
343343
if err != nil {
344344
t.Errorf("Error reading response body: %v", err)
345345
}

github/repos_pages_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"context"
1111
"encoding/json"
1212
"fmt"
13-
"io/ioutil"
13+
"io"
1414
"net/http"
1515
"testing"
1616

@@ -118,7 +118,7 @@ func TestRepositoriesService_UpdatePages_NullCNAME(t *testing.T) {
118118
}
119119

120120
mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) {
121-
got, err := ioutil.ReadAll(r.Body)
121+
got, err := io.ReadAll(r.Body)
122122
if err != nil {
123123
t.Fatalf("unable to read body: %v", err)
124124
}

0 commit comments

Comments
 (0)