Skip to content

Commit

Permalink
chore: io/ioutil deprecated (#8018)
Browse files Browse the repository at this point in the history
  • Loading branch information
testwill committed May 31, 2023
1 parent 7a0f6d1 commit e48f154
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 32 deletions.
4 changes: 2 additions & 2 deletions httpreplay/cmd/httpr/httpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"io"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -101,7 +101,7 @@ func handleInitial(pr *proxy.Proxy) http.HandlerFunc {
}

case "POST":
bytes, err := ioutil.ReadAll(req.Body)
bytes, err := io.ReadAll(req.Body)
req.Body.Close()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
8 changes: 4 additions & 4 deletions httpreplay/cmd/httpr/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
Expand All @@ -49,7 +49,7 @@ func TestIntegration_HTTPR(t *testing.T) {
t.Fatal("set GCLOUD_TESTS_GOLANG_PROJECT_ID and GCLOUD_TESTS_GOLANG_KEY")
}
// Get a unique temporary filename.
f, err := ioutil.TempFile("", "httpreplay")
f, err := os.CreateTemp("", "httpreplay")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func runReplay(t *testing.T, filename string) string {
if res.StatusCode != 200 {
t.Fatalf("from GET: %s", res.Status)
}
bytes, err := ioutil.ReadAll(res.Body)
bytes, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -238,5 +238,5 @@ func getBody(url string) ([]byte, error) {
return nil, fmt.Errorf("response: %s", res.Status)
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
return io.ReadAll(res.Body)
}
9 changes: 4 additions & 5 deletions httpreplay/httpreplay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -186,7 +185,7 @@ func run(t *testing.T, hc *http.Client) (*storage.BucketAttrs, []byte) {
t.Fatal(err)
}
defer r.Close()
contents, err := ioutil.ReadAll(r)
contents, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -304,7 +303,7 @@ func testReadCRC(t *testing.T, hc *http.Client, mode string) {
t.Errorf("%s: %s: %v", mode, test.desc, err)
continue
}
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
_ = r.Close()
if err != nil {
t.Errorf("%s: %s: %v", mode, test.desc, err)
Expand All @@ -318,7 +317,7 @@ func testReadCRC(t *testing.T, hc *http.Client, mode string) {

func TestRemoveAndClear(t *testing.T) {
// Disable logging for this test, since it generates a lot.
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintln(w, "LGTM")
}))
Expand Down Expand Up @@ -421,7 +420,7 @@ func TestRemoveAndClear(t *testing.T) {
}

func tempFilename(t *testing.T, pattern string) string {
f, err := ioutil.TempFile("", pattern)
f, err := os.CreateTemp("", pattern)
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 3 additions & 4 deletions httpreplay/internal/proxy/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package proxy
import (
"bytes"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -174,7 +173,7 @@ func parseRequestBody(contentType string, body []byte) (string, [][]byte, error)
if err != nil {
return "", nil, err
}
part, err := ioutil.ReadAll(p)
part, err := io.ReadAll(p)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -204,12 +203,12 @@ func (c *Converter) convertResponse(res *http.Response) (*Response, error) {
}

func snapshotBody(body *io.ReadCloser) ([]byte, error) {
data, err := ioutil.ReadAll(*body)
data, err := io.ReadAll(*body)
if err != nil {
return nil, err
}
(*body).Close()
*body = ioutil.NopCloser(bytes.NewReader(data))
*body = io.NopCloser(bytes.NewReader(data))
return data, nil
}

Expand Down
4 changes: 2 additions & 2 deletions httpreplay/internal/proxy/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package proxy

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestConvertRequest(t *testing.T) {
in := &http.Request{
Method: "GET",
URL: url,
Body: ioutil.NopCloser(bytes.NewReader(body)),
Body: io.NopCloser(bytes.NewReader(body)),
Header: http.Header{
"Content-Type": {"text/plain"},
"Authorization": {"oauth2-token"},
Expand Down
4 changes: 2 additions & 2 deletions httpreplay/internal/proxy/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package proxy
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"strconv"
"sync"
Expand Down Expand Up @@ -157,7 +157,7 @@ func toHTTPResponse(lr *Response, req *http.Request) *http.Response {
ProtoMajor: lr.ProtoMajor,
ProtoMinor: lr.ProtoMinor,
Header: lr.Header,
Body: ioutil.NopCloser(bytes.NewReader(lr.Body)),
Body: io.NopCloser(bytes.NewReader(lr.Body)),
ContentLength: int64(len(lr.Body)),
}
res.Request = req
Expand Down
8 changes: 4 additions & 4 deletions httpreplay/internal/proxy/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package proxy

import (
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand All @@ -35,13 +35,13 @@ func TestLogger(t *testing.T) {
Path: "a/b/c",
},
Header: http.Header{"H1": {"v1", "v2"}, "Content-Type": {"text/plain"}},
Body: ioutil.NopCloser(strings.NewReader("hello")),
Body: io.NopCloser(strings.NewReader("hello")),
Trailer: http.Header{"T1": {"v3", "v4"}},
}
res := &http.Response{
Request: req,
StatusCode: 204,
Body: ioutil.NopCloser(strings.NewReader("goodbye")),
Body: io.NopCloser(strings.NewReader("goodbye")),
Header: http.Header{"H2": {"v5"}},
Trailer: http.Header{"T2": {"v6", "v7"}},
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestEmptyBody(t *testing.T) {
Host: "example.com",
Path: "a/b/c",
},
Body: ioutil.NopCloser(strings.NewReader("")),
Body: io.NopCloser(strings.NewReader("")),
}
l := newLogger()
_, remove, err := martian.TestContext(req, nil, nil)
Expand Down
4 changes: 2 additions & 2 deletions httpreplay/internal/proxy/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -216,7 +216,7 @@ func (p *Proxy) writeLog() error {
if err != nil {
return err
}
return ioutil.WriteFile(p.filename, bytes, 0600) // only accessible by owner
return os.WriteFile(p.filename, bytes, 0600) // only accessible by owner
}

// skipLoggingByHost disables logging for traffic to a particular host.
Expand Down
4 changes: 2 additions & 2 deletions httpreplay/internal/proxy/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"reflect"
"sync"

Expand Down Expand Up @@ -63,7 +63,7 @@ func ForReplaying(filename string, port int) (*Proxy, error) {
}

func readLog(filename string) (*Log, error) {
bytes, err := ioutil.ReadFile(filename)
bytes, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/pretty/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package pretty

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"syscall"
Expand Down Expand Up @@ -67,7 +66,7 @@ func Diff(want, got interface{}) (string, bool, error) {
}

func writeToTemp(v interface{}) (string, error) {
f, err := ioutil.TempFile("", "prettyDiff")
f, err := os.CreateTemp("", "prettyDiff")
if err != nil {
return "", err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/testutil/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -70,7 +69,7 @@ func CredentialsEnv(ctx context.Context, envVar string, scopes ...string) *googl
return creds
}

data, err := ioutil.ReadFile(key)
data, err := os.ReadFile(key)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -141,7 +140,7 @@ func jwtConfigFromFile(filename string, scopes []string) (*jwt.Config, error) {
if filename == "" {
return nil, nil
}
jsonKey, err := ioutil.ReadFile(filename)
jsonKey, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("cannot read the JSON key file, err: %v", err)
}
Expand Down

0 comments on commit e48f154

Please sign in to comment.