From 959861361f1d87d4e034f4a06b907a92d053ab24 Mon Sep 17 00:00:00 2001 From: Austin Vazquez <55906459+austinvazquez@users.noreply.github.com> Date: Mon, 7 Nov 2022 10:41:12 -0600 Subject: [PATCH] Remove references to io/ioutil package (#2547) --- example/commitpr/main.go | 3 +-- example/newfilewithappauth/main.go | 4 ++-- github/gen-accessors.go | 3 +-- github/gen-stringify-test.go | 3 +-- github/git_trees_test.go | 8 ++++---- github/github.go | 9 ++++----- github/github_test.go | 26 +++++++++++++------------- github/messages.go | 5 ++--- github/repos_contents_test.go | 10 +++++----- github/repos_pages_test.go | 4 ++-- github/repos_releases_test.go | 6 +++--- github/teams_test.go | 6 +++--- test/integration/repos_test.go | 3 +-- update-urls/main.go | 8 ++++---- 14 files changed, 46 insertions(+), 52 deletions(-) diff --git a/example/commitpr/main.go b/example/commitpr/main.go index 0a86414dd5..1d60625d1c 100644 --- a/example/commitpr/main.go +++ b/example/commitpr/main.go @@ -25,7 +25,6 @@ import ( "errors" "flag" "fmt" - "io/ioutil" "log" "os" "strings" @@ -118,7 +117,7 @@ func getFileContent(fileArg string) (targetName string, b []byte, err error) { targetName = files[1] } - b, err = ioutil.ReadFile(localFile) + b, err = os.ReadFile(localFile) return targetName, b, err } diff --git a/example/newfilewithappauth/main.go b/example/newfilewithappauth/main.go index f6e9c572e5..a448a056f5 100644 --- a/example/newfilewithappauth/main.go +++ b/example/newfilewithappauth/main.go @@ -10,9 +10,9 @@ package main import ( "context" - "io/ioutil" "log" "net/http" + "os" "time" "github.com/bradleyfalzon/ghinstallation/v2" @@ -23,7 +23,7 @@ import ( func main() { const gitHost = "https://git.api.com" - privatePem, err := ioutil.ReadFile("path/to/pem") + privatePem, err := os.ReadFile("path/to/pem") if err != nil { log.Fatalf("failed to read pem: %v", err) } diff --git a/github/gen-accessors.go b/github/gen-accessors.go index d8136d2c5c..b222465f39 100644 --- a/github/gen-accessors.go +++ b/github/gen-accessors.go @@ -21,7 +21,6 @@ import ( "go/format" "go/parser" "go/token" - "io/ioutil" "log" "os" "sort" @@ -190,7 +189,7 @@ func (t *templateData) dump() error { return fmt.Errorf("os.Chmod(%q, 0644): %v", filename, err) } - if err := ioutil.WriteFile(filename, clean, 0444); err != nil { + if err := os.WriteFile(filename, clean, 0444); err != nil { return err } diff --git a/github/gen-stringify-test.go b/github/gen-stringify-test.go index 71783fe578..5a42082b93 100644 --- a/github/gen-stringify-test.go +++ b/github/gen-stringify-test.go @@ -24,7 +24,6 @@ import ( "go/format" "go/parser" "go/token" - "io/ioutil" "log" "os" "strings" @@ -356,7 +355,7 @@ func (t *templateData) dump() error { return fmt.Errorf("os.Chmod(%q, 0644): %v", t.filename, err) } - if err := ioutil.WriteFile(t.filename, clean, 0444); err != nil { + if err := os.WriteFile(t.filename, clean, 0444); err != nil { return err } diff --git a/github/git_trees_test.go b/github/git_trees_test.go index 692272f9df..25e5f2aeec 100644 --- a/github/git_trees_test.go +++ b/github/git_trees_test.go @@ -9,7 +9,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "net/http" "testing" @@ -106,7 +106,7 @@ func TestGitService_CreateTree(t *testing.T) { } mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) { - got, err := ioutil.ReadAll(r.Body) + got, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("unable to read body: %v", err) } @@ -184,7 +184,7 @@ func TestGitService_CreateTree_Content(t *testing.T) { } mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) { - got, err := ioutil.ReadAll(r.Body) + got, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("unable to read body: %v", err) } @@ -264,7 +264,7 @@ func TestGitService_CreateTree_Delete(t *testing.T) { } mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) { - got, err := ioutil.ReadAll(r.Body) + got, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("unable to read body: %v", err) } diff --git a/github/github.go b/github/github.go index 2883c38050..9f8fd53016 100644 --- a/github/github.go +++ b/github/github.go @@ -15,7 +15,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "net/url" "reflect" @@ -710,7 +709,7 @@ func (c *Client) BareDo(ctx context.Context, req *http.Request) (*Response, erro // Issue #1022 aerr, ok := err.(*AcceptedError) if ok { - b, readErr := ioutil.ReadAll(resp.Body) + b, readErr := io.ReadAll(resp.Body) if readErr != nil { return response, readErr } @@ -770,7 +769,7 @@ func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory rat StatusCode: http.StatusForbidden, Request: req, Header: make(http.Header), - Body: ioutil.NopCloser(strings.NewReader("")), + Body: io.NopCloser(strings.NewReader("")), } return &RateLimitError{ Rate: rate, @@ -1032,14 +1031,14 @@ func CheckResponse(r *http.Response) error { } errorResponse := &ErrorResponse{Response: r} - data, err := ioutil.ReadAll(r.Body) + data, err := io.ReadAll(r.Body) if err == nil && data != nil { json.Unmarshal(data, errorResponse) } // Re-populate error response body because GitHub error responses are often // undocumented and inconsistent. // Issue #1136, #540. - r.Body = ioutil.NopCloser(bytes.NewBuffer(data)) + r.Body = io.NopCloser(bytes.NewBuffer(data)) switch { case r.StatusCode == http.StatusUnauthorized && strings.HasPrefix(r.Header.Get(headerOTP), "required"): return (*TwoFactorAuthError)(errorResponse) diff --git a/github/github_test.go b/github/github_test.go index d8b6a74d19..c4ceb89bb5 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -10,7 +10,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "net/url" @@ -70,7 +70,7 @@ func setup() (client *Client, mux *http.ServeMux, serverURL string, teardown fun // directory, and create the file in that directory. It is the caller's // responsibility to remove the directory and its contents when no longer needed. func openTestFile(name, content string) (file *os.File, dir string, err error) { - dir, err = ioutil.TempDir("", "go-github") + dir, err = os.MkdirTemp("", "go-github") if err != nil { return nil, dir, err } @@ -133,7 +133,7 @@ func testURLParseError(t *testing.T, err error) { func testBody(t *testing.T, r *http.Request, want string) { t.Helper() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Errorf("Error reading request body: %v", err) } @@ -502,7 +502,7 @@ func TestNewRequest(t *testing.T) { } // test that body was JSON encoded - body, _ := ioutil.ReadAll(req.Body) + body, _ := io.ReadAll(req.Body) if got, want := string(body), outBody; got != want { t.Errorf("NewRequest(%q) Body is %v, want %v", inBody, got, want) } @@ -617,7 +617,7 @@ func TestNewFormRequest(t *testing.T) { } // test that body was form encoded - body, _ := ioutil.ReadAll(req.Body) + body, _ := io.ReadAll(req.Body) if got, want := string(body), outBody; got != want { t.Errorf("NewFormRequest(%q) Body is %v, want %v", inBody, got, want) } @@ -1398,7 +1398,7 @@ func TestCheckResponse(t *testing.T) { res := &http.Response{ Request: &http.Request{}, StatusCode: http.StatusBadRequest, - Body: ioutil.NopCloser(strings.NewReader(`{"message":"m", + Body: io.NopCloser(strings.NewReader(`{"message":"m", "errors": [{"resource": "r", "field": "f", "code": "c"}], "block": {"reason": "dmca", "created_at": "2016-03-17T15:39:46Z"}}`)), } @@ -1427,7 +1427,7 @@ func TestCheckResponse_RateLimit(t *testing.T) { Request: &http.Request{}, StatusCode: http.StatusForbidden, Header: http.Header{}, - Body: ioutil.NopCloser(strings.NewReader(`{"message":"m", + Body: io.NopCloser(strings.NewReader(`{"message":"m", "documentation_url": "url"}`)), } res.Header.Set(headerRateLimit, "60") @@ -1454,7 +1454,7 @@ func TestCheckResponse_AbuseRateLimit(t *testing.T) { res := &http.Response{ Request: &http.Request{}, StatusCode: http.StatusForbidden, - Body: ioutil.NopCloser(strings.NewReader(`{"message":"m", + Body: io.NopCloser(strings.NewReader(`{"message":"m", "documentation_url": "docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"}`)), } err := CheckResponse(res).(*AbuseRateLimitError) @@ -1847,7 +1847,7 @@ func TestCheckResponse_noBody(t *testing.T) { res := &http.Response{ Request: &http.Request{}, StatusCode: http.StatusBadRequest, - Body: ioutil.NopCloser(strings.NewReader("")), + Body: io.NopCloser(strings.NewReader("")), } err := CheckResponse(res).(*ErrorResponse) @@ -1868,7 +1868,7 @@ func TestCheckResponse_unexpectedErrorStructure(t *testing.T) { res := &http.Response{ Request: &http.Request{}, StatusCode: http.StatusBadRequest, - Body: ioutil.NopCloser(strings.NewReader(httpBody)), + Body: io.NopCloser(strings.NewReader(httpBody)), } err := CheckResponse(res).(*ErrorResponse) @@ -1884,7 +1884,7 @@ func TestCheckResponse_unexpectedErrorStructure(t *testing.T) { if !errors.Is(err, want) { t.Errorf("Error = %#v, want %#v", err, want) } - data, err2 := ioutil.ReadAll(err.Response.Body) + data, err2 := io.ReadAll(err.Response.Body) if err2 != nil { t.Fatalf("failed to read response body: %v", err) } @@ -2373,9 +2373,9 @@ func TestBareDo_returnsOpenBody(t *testing.T) { t.Fatalf("client.BareDo returned error: %v", err) } - got, err := ioutil.ReadAll(resp.Body) + got, err := io.ReadAll(resp.Body) if err != nil { - t.Fatalf("ioutil.ReadAll returned error: %v", err) + t.Fatalf("io.ReadAll returned error: %v", err) } if string(got) != expectedBody { t.Fatalf("Expected %q, got %q", expectedBody, string(got)) diff --git a/github/messages.go b/github/messages.go index e26b13e7b6..ceac6d9bc4 100644 --- a/github/messages.go +++ b/github/messages.go @@ -19,7 +19,6 @@ import ( "fmt" "hash" "io" - "io/ioutil" "mime" "net/http" "net/url" @@ -171,7 +170,7 @@ func ValidatePayloadFromBody(contentType string, readable io.Reader, signature s switch contentType { case "application/json": var err error - if body, err = ioutil.ReadAll(readable); err != nil { + if body, err = io.ReadAll(readable); err != nil { return nil, err } @@ -185,7 +184,7 @@ func ValidatePayloadFromBody(contentType string, readable io.Reader, signature s const payloadFormParam = "payload" var err error - if body, err = ioutil.ReadAll(readable); err != nil { + if body, err = io.ReadAll(readable); err != nil { return nil, err } diff --git a/github/repos_contents_test.go b/github/repos_contents_test.go index 1cfa100df3..f5de5570af 100644 --- a/github/repos_contents_test.go +++ b/github/repos_contents_test.go @@ -9,7 +9,7 @@ import ( "context" "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "testing" @@ -146,7 +146,7 @@ func TestRepositoriesService_DownloadContents_Success(t *testing.T) { t.Errorf("Repositories.DownloadContents returned status code %v, want %v", got, want) } - bytes, err := ioutil.ReadAll(r) + bytes, err := io.ReadAll(r) if err != nil { t.Errorf("Error reading response body: %v", err) } @@ -198,7 +198,7 @@ func TestRepositoriesService_DownloadContents_FailedResponse(t *testing.T) { t.Errorf("Repositories.DownloadContents returned status code %v, want %v", got, want) } - bytes, err := ioutil.ReadAll(r) + bytes, err := io.ReadAll(r) if err != nil { t.Errorf("Error reading response body: %v", err) } @@ -276,7 +276,7 @@ func TestRepositoriesService_DownloadContentsWithMeta_Success(t *testing.T) { t.Errorf("Repositories.DownloadContentsWithMeta returned status code %v, want %v", got, want) } - bytes, err := ioutil.ReadAll(r) + bytes, err := io.ReadAll(r) if err != nil { t.Errorf("Error reading response body: %v", err) } @@ -339,7 +339,7 @@ func TestRepositoriesService_DownloadContentsWithMeta_FailedResponse(t *testing. t.Errorf("Repositories.DownloadContentsWithMeta returned status code %v, want %v", got, want) } - bytes, err := ioutil.ReadAll(r) + bytes, err := io.ReadAll(r) if err != nil { t.Errorf("Error reading response body: %v", err) } diff --git a/github/repos_pages_test.go b/github/repos_pages_test.go index ed17e73520..75d6ccb2bf 100644 --- a/github/repos_pages_test.go +++ b/github/repos_pages_test.go @@ -10,7 +10,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "testing" @@ -118,7 +118,7 @@ func TestRepositoriesService_UpdatePages_NullCNAME(t *testing.T) { } mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { - got, err := ioutil.ReadAll(r.Body) + got, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("unable to read body: %v", err) } diff --git a/github/repos_releases_test.go b/github/repos_releases_test.go index ddd7d44e93..77a20ffe95 100644 --- a/github/repos_releases_test.go +++ b/github/repos_releases_test.go @@ -10,7 +10,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" "strings" @@ -442,7 +442,7 @@ func TestRepositoriesService_DownloadReleaseAsset_Stream(t *testing.T) { t.Errorf("Repositories.DownloadReleaseAsset returned error: %v", err) } want := []byte("Hello World") - content, err := ioutil.ReadAll(reader) + content, err := io.ReadAll(reader) if err != nil { t.Errorf("Repositories.DownloadReleaseAsset returned bad reader: %v", err) } @@ -498,7 +498,7 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirect(t *testing.T) { ctx := context.Background() reader, _, err := client.Repositories.DownloadReleaseAsset(ctx, "o", "r", 1, http.DefaultClient) - content, err := ioutil.ReadAll(reader) + content, err := io.ReadAll(reader) if err != nil { t.Errorf("Repositories.DownloadReleaseAsset returned error: %v", err) } diff --git a/github/teams_test.go b/github/teams_test.go index d267846ecb..6085eac13e 100644 --- a/github/teams_test.go +++ b/github/teams_test.go @@ -10,7 +10,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strings" "testing" @@ -293,7 +293,7 @@ func TestTeamsService_EditTeamByID_RemoveParent(t *testing.T) { mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { v := new(NewTeam) - buf, err := ioutil.ReadAll(r.Body) + buf, err := io.ReadAll(r.Body) if err != nil { t.Errorf("Unable to read body: %v", err) } @@ -377,7 +377,7 @@ func TestTeamsService_EditTeamBySlug_RemoveParent(t *testing.T) { mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { v := new(NewTeam) - buf, err := ioutil.ReadAll(r.Body) + buf, err := io.ReadAll(r.Body) if err != nil { t.Errorf("Unable to read body: %v", err) } diff --git a/test/integration/repos_test.go b/test/integration/repos_test.go index ff39f715df..49ff98f0da 100644 --- a/test/integration/repos_test.go +++ b/test/integration/repos_test.go @@ -11,7 +11,6 @@ package integration import ( "context" "io" - "io/ioutil" "net/http" "testing" @@ -183,7 +182,7 @@ func TestRepositories_DownloadReleaseAsset(t *testing.T) { t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err) } defer func() { _ = rc.Close() }() - _, err = io.Copy(ioutil.Discard, rc) + _, err = io.Copy(io.Discard, rc) if err != nil { t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err) } diff --git a/update-urls/main.go b/update-urls/main.go index e704a5cf66..0a3c53c24d 100644 --- a/update-urls/main.go +++ b/update-urls/main.go @@ -28,7 +28,7 @@ import ( "go/ast" "go/parser" "go/token" - "io/ioutil" + "io" "log" "net/http" "os" @@ -189,10 +189,10 @@ type liveFileRewriter struct { func (lfr *liveFileRewriter) Position(pos token.Pos) token.Position { return lfr.fset.Position(pos) } func (lfr *liveFileRewriter) ReadFile(filename string) ([]byte, error) { - return ioutil.ReadFile(filename) + return os.ReadFile(filename) } func (lfr *liveFileRewriter) WriteFile(filename string, buf []byte, mode os.FileMode) error { - return ioutil.WriteFile(filename, buf, mode) + return os.WriteFile(filename, buf, mode) } func validateRewriteURLs(usedHelpers usedHelpersMap, endpointsByFilename endpointsByFilenameMap, docCache documentCacheReader, fileRewriter FileRewriter) { @@ -599,7 +599,7 @@ func (dc *documentCache) CacheDocFromInternet(urlWithID, filename string, pos to url := baseURL logf("urlWithID: %v ; finalURL: %v ; baseURL: %v, fullURL: %v", urlWithID, finalURL, baseURL, fullURL) - b, err := ioutil.ReadAll(resp.Body) + b, err := io.ReadAll(resp.Body) check("Unable to read body of URL: %v, %v", url, err) check("Unable to close body of URL: %v, %v", url, resp.Body.Close()) dc.apiDocs[url], err = parseWebPageEndpoints(string(b))