Skip to content

Commit c1f5833

Browse files
committed
all: replace deprecated io/ioutil calls
The io/ioutil package's features were moved to the io and os packages in Go 1.16. x/net depends on Go 1.18. Drop ioutil calls, so gopls doesn't warn about them. Change-Id: Ibdb576d94f250808ae285aa142e2fd41e7e9afc9 Reviewed-on: https://go-review.googlesource.com/c/net/+/586244 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 9545aea commit c1f5833

25 files changed

+77
-97
lines changed

bpf/instructions_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package bpf
66

77
import (
88
"fmt"
9-
"io/ioutil"
9+
"os"
1010
"reflect"
1111
"strconv"
1212
"strings"
@@ -98,7 +98,7 @@ func TestInterop(t *testing.T) {
9898
}
9999
t.Logf("Assembled program is %d instructions long", len(out))
100100

101-
bs, err := ioutil.ReadFile(allInstructionsExpected)
101+
bs, err := os.ReadFile(allInstructionsExpected)
102102
if err != nil {
103103
t.Fatalf("reading %s: %s", allInstructionsExpected, err)
104104
}

context/ctxhttp/ctxhttp_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package ctxhttp
99
import (
1010
"context"
1111
"io"
12-
"io/ioutil"
1312
"net/http"
1413
"net/http/httptest"
1514
"testing"
@@ -49,7 +48,7 @@ func TestNoTimeout(t *testing.T) {
4948
t.Fatal(err)
5049
}
5150
defer res.Body.Close()
52-
slurp, err := ioutil.ReadAll(res.Body)
51+
slurp, err := io.ReadAll(res.Body)
5352
if err != nil {
5453
t.Fatal(err)
5554
}
@@ -102,7 +101,7 @@ func TestCancelAfterHangingRequest(t *testing.T) {
102101
done := make(chan struct{})
103102

104103
go func() {
105-
b, err := ioutil.ReadAll(resp.Body)
104+
b, err := io.ReadAll(resp.Body)
106105
if len(b) != 0 || err == nil {
107106
t.Errorf(`Read got (%q, %v); want ("", error)`, b, err)
108107
}

dns/dnsmessage/message_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package dnsmessage
77
import (
88
"bytes"
99
"fmt"
10-
"io/ioutil"
10+
"os"
1111
"path/filepath"
1212
"reflect"
1313
"strings"
@@ -1611,7 +1611,7 @@ func TestNoFmt(t *testing.T) {
16111611
// Could use something complex like go/build or x/tools/go/packages,
16121612
// but there's no reason for "fmt" to appear (in quotes) in the source
16131613
// otherwise, so just use a simple substring search.
1614-
data, err := ioutil.ReadFile(file)
1614+
data, err := os.ReadFile(file)
16151615
if err != nil {
16161616
t.Fatal(err)
16171617
}

html/atom/gen.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"flag"
1515
"fmt"
1616
"go/format"
17-
"io/ioutil"
1817
"math/rand"
1918
"os"
2019
"sort"
@@ -48,7 +47,7 @@ func genFile(name string, buf *bytes.Buffer) {
4847
fmt.Fprintln(os.Stderr, err)
4948
os.Exit(1)
5049
}
51-
if err := ioutil.WriteFile(name, b, 0644); err != nil {
50+
if err := os.WriteFile(name, b, 0644); err != nil {
5251
fmt.Fprintln(os.Stderr, err)
5352
os.Exit(1)
5453
}

html/charset/charset_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ package charset
77
import (
88
"bytes"
99
"encoding/xml"
10-
"io/ioutil"
10+
"io"
11+
"os"
1112
"runtime"
1213
"strings"
1314
"testing"
@@ -17,7 +18,7 @@ import (
1718

1819
func transformString(t transform.Transformer, s string) (string, error) {
1920
r := transform.NewReader(strings.NewReader(s), t)
20-
b, err := ioutil.ReadAll(r)
21+
b, err := io.ReadAll(r)
2122
return string(b), err
2223
}
2324

@@ -142,7 +143,7 @@ func TestSniff(t *testing.T) {
142143
}
143144

144145
for _, tc := range sniffTestCases {
145-
content, err := ioutil.ReadFile("testdata/" + tc.filename)
146+
content, err := os.ReadFile("testdata/" + tc.filename)
146147
if err != nil {
147148
t.Errorf("%s: error reading file: %v", tc.filename, err)
148149
continue
@@ -163,7 +164,7 @@ func TestReader(t *testing.T) {
163164
}
164165

165166
for _, tc := range sniffTestCases {
166-
content, err := ioutil.ReadFile("testdata/" + tc.filename)
167+
content, err := os.ReadFile("testdata/" + tc.filename)
167168
if err != nil {
168169
t.Errorf("%s: error reading file: %v", tc.filename, err)
169170
continue
@@ -175,14 +176,14 @@ func TestReader(t *testing.T) {
175176
continue
176177
}
177178

178-
got, err := ioutil.ReadAll(r)
179+
got, err := io.ReadAll(r)
179180
if err != nil {
180181
t.Errorf("%s: error reading from charset.NewReader: %v", tc.filename, err)
181182
continue
182183
}
183184

184185
e, _ := Lookup(tc.want)
185-
want, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader(content), e.NewDecoder()))
186+
want, err := io.ReadAll(transform.NewReader(bytes.NewReader(content), e.NewDecoder()))
186187
if err != nil {
187188
t.Errorf("%s: error decoding with hard-coded charset name: %v", tc.filename, err)
188189
continue

html/parse_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"errors"
1111
"fmt"
1212
"io"
13-
"io/ioutil"
1413
"os"
1514
"path/filepath"
1615
"runtime"
@@ -477,7 +476,7 @@ func TestParseFragmentForeignContentTemplates(t *testing.T) {
477476
}
478477

479478
func BenchmarkParser(b *testing.B) {
480-
buf, err := ioutil.ReadFile("testdata/go1.html")
479+
buf, err := os.ReadFile("testdata/go1.html")
481480
if err != nil {
482481
b.Fatalf("could not read testdata/go1.html: %v", err)
483482
}

html/token_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package html
77
import (
88
"bytes"
99
"io"
10-
"io/ioutil"
10+
"os"
1111
"reflect"
1212
"runtime"
1313
"strings"
@@ -680,7 +680,7 @@ tests:
680680
}
681681
}
682682
// Anything tokenized along with untokenized input or data left in the reader.
683-
assembled, err := ioutil.ReadAll(io.MultiReader(&tokenized, bytes.NewReader(z.Buffered()), r))
683+
assembled, err := io.ReadAll(io.MultiReader(&tokenized, bytes.NewReader(z.Buffered()), r))
684684
if err != nil {
685685
t.Errorf("%s: ReadAll: %v", test.desc, err)
686686
continue tests
@@ -866,7 +866,7 @@ const (
866866
)
867867

868868
func benchmarkTokenizer(b *testing.B, level int) {
869-
buf, err := ioutil.ReadFile("testdata/go1.html")
869+
buf, err := os.ReadFile("testdata/go1.html")
870870
if err != nil {
871871
b.Fatalf("could not read testdata/go1.html: %v", err)
872872
}

http2/h2c/h2c_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"crypto/tls"
1010
"fmt"
1111
"io"
12-
"io/ioutil"
1312
"log"
1413
"net"
1514
"net/http"
@@ -68,7 +67,7 @@ func TestContext(t *testing.T) {
6867
if err != nil {
6968
t.Fatal(err)
7069
}
71-
_, err = ioutil.ReadAll(resp.Body)
70+
_, err = io.ReadAll(resp.Body)
7271
if err != nil {
7372
t.Fatal(err)
7473
}
@@ -162,7 +161,7 @@ func TestMaxBytesHandler(t *testing.T) {
162161
t.Fatal(err)
163162
}
164163
defer resp.Body.Close()
165-
_, err = ioutil.ReadAll(resp.Body)
164+
_, err = io.ReadAll(resp.Body)
166165
if err != nil {
167166
t.Fatal(err)
168167
}

http2/hpack/gen.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"bytes"
1111
"fmt"
1212
"go/format"
13-
"io/ioutil"
1413
"os"
1514
"sort"
1615

@@ -176,7 +175,7 @@ func genFile(name string, buf *bytes.Buffer) {
176175
fmt.Fprintln(os.Stderr, err)
177176
os.Exit(1)
178177
}
179-
if err := ioutil.WriteFile(name, b, 0644); err != nil {
178+
if err := os.WriteFile(name, b, 0644); err != nil {
180179
fmt.Fprintln(os.Stderr, err)
181180
os.Exit(1)
182181
}

http2/http2_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"flag"
1010
"fmt"
11-
"io/ioutil"
1211
"net/http"
1312
"os"
1413
"path/filepath"
@@ -266,7 +265,7 @@ func TestNoUnicodeStrings(t *testing.T) {
266265
return nil
267266
}
268267

269-
contents, err := ioutil.ReadFile(path)
268+
contents, err := os.ReadFile(path)
270269
if err != nil {
271270
t.Fatal(err)
272271
}

http2/pipe_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"errors"
1010
"io"
11-
"io/ioutil"
1211
"testing"
1312
)
1413

@@ -85,7 +84,7 @@ func TestPipeCloseWithError(t *testing.T) {
8584
io.WriteString(p, body)
8685
a := errors.New("test error")
8786
p.CloseWithError(a)
88-
all, err := ioutil.ReadAll(p)
87+
all, err := io.ReadAll(p)
8988
if string(all) != body {
9089
t.Errorf("read bytes = %q; want %q", all, body)
9190
}
@@ -112,7 +111,7 @@ func TestPipeBreakWithError(t *testing.T) {
112111
io.WriteString(p, "foo")
113112
a := errors.New("test err")
114113
p.BreakWithError(a)
115-
all, err := ioutil.ReadAll(p)
114+
all, err := io.ReadAll(p)
116115
if string(all) != "" {
117116
t.Errorf("read bytes = %q; want empty string", all)
118117
}

http2/server_push_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"io/ioutil"
1211
"net/http"
1312
"reflect"
1413
"runtime"
@@ -40,7 +39,7 @@ func TestServer_Push_Success(t *testing.T) {
4039
if r.Body == nil {
4140
return fmt.Errorf("nil Body")
4241
}
43-
if buf, err := ioutil.ReadAll(r.Body); err != nil || len(buf) != 0 {
42+
if buf, err := io.ReadAll(r.Body); err != nil || len(buf) != 0 {
4443
return fmt.Errorf("ReadAll(Body)=%q,%v, want '',nil", buf, err)
4544
}
4645
return nil

http2/server_test.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"flag"
1515
"fmt"
1616
"io"
17-
"io/ioutil"
1817
"log"
1918
"net"
2019
"net/http"
@@ -38,7 +37,7 @@ func stderrv() io.Writer {
3837
return os.Stderr
3938
}
4039

41-
return ioutil.Discard
40+
return io.Discard
4241
}
4342

4443
type safeBuffer struct {
@@ -896,7 +895,7 @@ func testBodyContents(t *testing.T, wantContentLength int64, wantBody string, wr
896895
if r.ContentLength != wantContentLength {
897896
t.Errorf("ContentLength = %v; want %d", r.ContentLength, wantContentLength)
898897
}
899-
all, err := ioutil.ReadAll(r.Body)
898+
all, err := io.ReadAll(r.Body)
900899
if err != nil {
901900
t.Fatal(err)
902901
}
@@ -917,7 +916,7 @@ func testBodyContentsFail(t *testing.T, wantContentLength int64, wantReadError s
917916
if r.ContentLength != wantContentLength {
918917
t.Errorf("ContentLength = %v; want %d", r.ContentLength, wantContentLength)
919918
}
920-
all, err := ioutil.ReadAll(r.Body)
919+
all, err := io.ReadAll(r.Body)
921920
if err == nil {
922921
t.Fatalf("expected an error (%q) reading from the body. Successfully read %q instead.",
923922
wantReadError, all)
@@ -2992,7 +2991,7 @@ func TestServerReadsTrailers(t *testing.T) {
29922991
if !reflect.DeepEqual(r.Trailer, wantTrailer) {
29932992
t.Errorf("initial Trailer = %v; want %v", r.Trailer, wantTrailer)
29942993
}
2995-
slurp, err := ioutil.ReadAll(r.Body)
2994+
slurp, err := io.ReadAll(r.Body)
29962995
if string(slurp) != testBody {
29972996
t.Errorf("read body %q; want %q", slurp, testBody)
29982997
}
@@ -3188,7 +3187,7 @@ func BenchmarkServerPosts(b *testing.B) {
31883187
// Consume the (empty) body from th peer before replying, otherwise
31893188
// the server will sometimes (depending on scheduling) send the peer a
31903189
// a RST_STREAM with the CANCEL error code.
3191-
if n, err := io.Copy(ioutil.Discard, r.Body); n != 0 || err != nil {
3190+
if n, err := io.Copy(io.Discard, r.Body); n != 0 || err != nil {
31923191
b.Errorf("Copy error; got %v, %v; want 0, nil", n, err)
31933192
}
31943193
io.WriteString(w, msg)
@@ -3252,7 +3251,7 @@ func benchmarkServerToClientStream(b *testing.B, newServerOpts ...interface{}) {
32523251
// Consume the (empty) body from th peer before replying, otherwise
32533252
// the server will sometimes (depending on scheduling) send the peer a
32543253
// a RST_STREAM with the CANCEL error code.
3255-
if n, err := io.Copy(ioutil.Discard, r.Body); n != 0 || err != nil {
3254+
if n, err := io.Copy(io.Discard, r.Body); n != 0 || err != nil {
32563255
b.Errorf("Copy error; got %v, %v; want 0, nil", n, err)
32573256
}
32583257
for i := 0; i < b.N; i += 1 {
@@ -3533,7 +3532,7 @@ func BenchmarkServer_GetRequest(b *testing.B) {
35333532
b.ReportAllocs()
35343533
const msg = "Hello, world."
35353534
st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) {
3536-
n, err := io.Copy(ioutil.Discard, r.Body)
3535+
n, err := io.Copy(io.Discard, r.Body)
35373536
if err != nil || n > 0 {
35383537
b.Errorf("Read %d bytes, error %v; want 0 bytes.", n, err)
35393538
}
@@ -3565,7 +3564,7 @@ func BenchmarkServer_PostRequest(b *testing.B) {
35653564
b.ReportAllocs()
35663565
const msg = "Hello, world."
35673566
st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) {
3568-
n, err := io.Copy(ioutil.Discard, r.Body)
3567+
n, err := io.Copy(io.Discard, r.Body)
35693568
if err != nil || n > 0 {
35703569
b.Errorf("Read %d bytes, error %v; want 0 bytes.", n, err)
35713570
}
@@ -3639,7 +3638,7 @@ func TestServerHandleCustomConn(t *testing.T) {
36393638
EndStream: true,
36403639
EndHeaders: true,
36413640
})
3642-
go io.Copy(ioutil.Discard, c2)
3641+
go io.Copy(io.Discard, c2)
36433642
<-handlerDone
36443643
}()
36453644
const testString = "my custom ConnectionState"
@@ -4001,7 +4000,7 @@ func TestIssue20704Race(t *testing.T) {
40014000

40024001
func TestServer_Rejects_TooSmall(t *testing.T) {
40034002
testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
4004-
ioutil.ReadAll(r.Body)
4003+
io.ReadAll(r.Body)
40054004
return nil
40064005
}, func(st *serverTester) {
40074006
st.writeHeaders(HeadersFrameParam{

0 commit comments

Comments
 (0)