Skip to content

Commit f080a30

Browse files
committed
all: update references to symbols moved from io/ioutil to io
1 parent 6704843 commit f080a30

File tree

15 files changed

+188
-27
lines changed

15 files changed

+188
-27
lines changed

doc/articles/wiki/wiki_test.go

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main_test
6+
7+
import (
8+
"bytes"
9+
"fmt"
10+
"io"
11+
"io/ioutil"
12+
"net/http"
13+
"os"
14+
"os/exec"
15+
"path/filepath"
16+
"strings"
17+
"testing"
18+
)
19+
20+
func TestSnippetsCompile(t *testing.T) {
21+
if testing.Short() {
22+
t.Skip("skipping slow builds in short mode")
23+
}
24+
25+
goFiles, err := filepath.Glob("*.go")
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
30+
for _, f := range goFiles {
31+
if strings.HasSuffix(f, "_test.go") {
32+
continue
33+
}
34+
f := f
35+
t.Run(f, func(t *testing.T) {
36+
t.Parallel()
37+
38+
cmd := exec.Command("go", "build", "-o", os.DevNull, f)
39+
out, err := cmd.CombinedOutput()
40+
if err != nil {
41+
t.Errorf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, out)
42+
}
43+
})
44+
}
45+
}
46+
47+
func TestWikiServer(t *testing.T) {
48+
must := func(err error) {
49+
if err != nil {
50+
t.Helper()
51+
t.Fatal(err)
52+
}
53+
}
54+
55+
dir, err := ioutil.TempDir("", t.Name())
56+
must(err)
57+
defer os.RemoveAll(dir)
58+
59+
// We're testing a walkthrough example of how to write a server.
60+
//
61+
// That server hard-codes a port number to make the walkthrough simpler, but
62+
// we can't assume that the hard-coded port is available on an arbitrary
63+
// builder. So we'll patch out the hard-coded port, and replace it with a
64+
// function that writes the server's address to stdout
65+
// so that we can read it and know where to send the test requests.
66+
67+
finalGo, err := ioutil.ReadFile("final.go")
68+
must(err)
69+
const patchOld = `log.Fatal(http.ListenAndServe(":8080", nil))`
70+
patched := bytes.ReplaceAll(finalGo, []byte(patchOld), []byte(`log.Fatal(serve())`))
71+
if bytes.Equal(patched, finalGo) {
72+
t.Fatalf("Can't patch final.go: %q not found.", patchOld)
73+
}
74+
must(ioutil.WriteFile(filepath.Join(dir, "final_patched.go"), patched, 0644))
75+
76+
// Build the server binary from the patched sources.
77+
// The 'go' command requires that they all be in the same directory.
78+
// final_test.go provides the implemtation for our serve function.
79+
must(copyFile(filepath.Join(dir, "final_srv.go"), "final_test.go"))
80+
cmd := exec.Command("go", "build",
81+
"-o", filepath.Join(dir, "final.exe"),
82+
filepath.Join(dir, "final_patched.go"),
83+
filepath.Join(dir, "final_srv.go"))
84+
out, err := cmd.CombinedOutput()
85+
if err != nil {
86+
t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, out)
87+
}
88+
89+
// Run the server in our temporary directory so that it can
90+
// write its content there. It also needs a couple of template files,
91+
// and looks for them in the same directory.
92+
must(copyFile(filepath.Join(dir, "edit.html"), "edit.html"))
93+
must(copyFile(filepath.Join(dir, "view.html"), "view.html"))
94+
cmd = exec.Command(filepath.Join(dir, "final.exe"))
95+
cmd.Dir = dir
96+
stderr := bytes.NewBuffer(nil)
97+
cmd.Stderr = stderr
98+
stdout, err := cmd.StdoutPipe()
99+
must(err)
100+
must(cmd.Start())
101+
102+
defer func() {
103+
cmd.Process.Kill()
104+
err := cmd.Wait()
105+
if stderr.Len() > 0 {
106+
t.Logf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, stderr)
107+
}
108+
}()
109+
110+
var addr string
111+
if _, err := fmt.Fscanln(stdout, &addr); err != nil || addr == "" {
112+
t.Fatalf("Failed to read server address: %v", err)
113+
}
114+
115+
// The server is up and has told us its address.
116+
// Make sure that its HTTP API works as described in the article.
117+
118+
r, err := http.Get(fmt.Sprintf("http://%s/edit/Test", addr))
119+
must(err)
120+
responseMustMatchFile(t, r, "test_edit.good")
121+
122+
r, err = http.Post(fmt.Sprintf("http://%s/save/Test", addr),
123+
"application/x-www-form-urlencoded",
124+
strings.NewReader("body=some%20content"))
125+
must(err)
126+
responseMustMatchFile(t, r, "test_view.good")
127+
128+
gotTxt, err := ioutil.ReadFile(filepath.Join(dir, "Test.txt"))
129+
must(err)
130+
wantTxt, err := ioutil.ReadFile("test_Test.txt.good")
131+
must(err)
132+
if !bytes.Equal(wantTxt, gotTxt) {
133+
t.Fatalf("Test.txt differs from expected after posting to /save.\ngot:\n%s\nwant:\n%s", gotTxt, wantTxt)
134+
}
135+
136+
r, err = http.Get(fmt.Sprintf("http://%s/view/Test", addr))
137+
must(err)
138+
responseMustMatchFile(t, r, "test_view.good")
139+
}
140+
141+
func responseMustMatchFile(t *testing.T, r *http.Response, filename string) {
142+
t.Helper()
143+
144+
defer r.Body.Close()
145+
body, err := io.ReadAll(r.Body)
146+
if err != nil {
147+
t.Fatal(err)
148+
}
149+
150+
wantBody, err := ioutil.ReadFile(filename)
151+
if err != nil {
152+
t.Fatal(err)
153+
}
154+
155+
if !bytes.Equal(body, wantBody) {
156+
t.Fatalf("%v: body does not match %s.\ngot:\n%s\nwant:\n%s", r.Request.URL, filename, body, wantBody)
157+
}
158+
}
159+
160+
func copyFile(dst, src string) error {
161+
buf, err := ioutil.ReadFile(src)
162+
if err != nil {
163+
return err
164+
}
165+
return ioutil.WriteFile(dst, buf, 0644)
166+
}

misc/android/go_android_exec.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func adbCopyGoroot() error {
276276
if err := syscall.Flock(int(stat.Fd()), syscall.LOCK_EX); err != nil {
277277
return err
278278
}
279-
s, err := ioutil.ReadAll(stat)
279+
s, err := io.ReadAll(stat)
280280
if err != nil {
281281
return err
282282
}

misc/cgo/testcarchive/testdata/libgo6/sigprof.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
package main
66

77
import (
8-
"io/ioutil"
8+
"io"
99
"runtime/pprof"
1010
)
1111

1212
import "C"
1313

1414
//export go_start_profile
1515
func go_start_profile() {
16-
pprof.StartCPUProfile(ioutil.Discard)
16+
pprof.StartCPUProfile(io.Discard)
1717
}
1818

1919
//export go_stop_profile

misc/cgo/testsanitizers/testdata/tsan9.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void spin() {
4444
import "C"
4545

4646
import (
47-
"io/ioutil"
47+
"io"
4848
"runtime/pprof"
4949
"time"
5050
)
@@ -60,7 +60,7 @@ func goSpin() {
6060
}
6161

6262
func main() {
63-
pprof.StartCPUProfile(ioutil.Discard)
63+
pprof.StartCPUProfile(io.Discard)
6464
go C.spin()
6565
goSpin()
6666
pprof.StopCPUProfile()

misc/linkcheck/linkcheck.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"errors"
1212
"flag"
1313
"fmt"
14-
"io/ioutil"
14+
"io"
1515
"log"
1616
"net/http"
1717
"os"
@@ -144,7 +144,7 @@ func doCrawl(url string) error {
144144
if res.StatusCode != 200 {
145145
return errors.New(res.Status)
146146
}
147-
slurp, err := ioutil.ReadAll(res.Body)
147+
slurp, err := io.ReadAll(res.Body)
148148
res.Body.Close()
149149
if err != nil {
150150
log.Fatalf("Error reading %s body: %v", url, err)

src/net/http/fs_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ func TestServeIndexHtml(t *testing.T) {
593593
if err != nil {
594594
t.Fatal(err)
595595
}
596-
b, err := ioutil.ReadAll(res.Body)
596+
b, err := io.ReadAll(res.Body)
597597
if err != nil {
598598
t.Fatal("reading Body:", err)
599599
}

src/testing/fstest/testfs.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"io"
1212
"io/fs"
13-
"io/ioutil"
1413
"path"
1514
"reflect"
1615
"sort"
@@ -514,7 +513,7 @@ func (t *fsTester) checkFile(file string) {
514513
return
515514
}
516515

517-
data, err := ioutil.ReadAll(f)
516+
data, err := io.ReadAll(f)
518517
if err != nil {
519518
f.Close()
520519
t.errorf("%s: Open+ReadAll: %v", file, err)

test/bench/go1/gob_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"bytes"
1111
"encoding/gob"
1212
"encoding/json"
13-
"io/ioutil"
13+
"io"
1414
"log"
1515
"reflect"
1616
"testing"
@@ -73,7 +73,7 @@ func gobdec() {
7373
}
7474

7575
func gobenc() {
76-
if err := gob.NewEncoder(ioutil.Discard).Encode(&gobdata); err != nil {
76+
if err := gob.NewEncoder(io.Discard).Encode(&gobdata); err != nil {
7777
panic(err)
7878
}
7979
}

test/bench/go1/gzip_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"bytes"
1111
gz "compress/gzip"
1212
"io"
13-
"io/ioutil"
1413
"testing"
1514
)
1615

@@ -28,7 +27,7 @@ func init() {
2827
}
2928

3029
func gzip() {
31-
c := gz.NewWriter(ioutil.Discard)
30+
c := gz.NewWriter(io.Discard)
3231
if _, err := c.Write(jsongunz); err != nil {
3332
panic(err)
3433
}
@@ -42,7 +41,7 @@ func gunzip() {
4241
if err != nil {
4342
panic(err)
4443
}
45-
if _, err := io.Copy(ioutil.Discard, r); err != nil {
44+
if _, err := io.Copy(io.Discard, r); err != nil {
4645
panic(err)
4746
}
4847
r.Close()

test/bench/go1/http_test.go

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

77
import (
88
"bytes"
9-
"io/ioutil"
9+
"io"
1010
"net/http"
1111
"net/http/httptest"
1212
"testing"
@@ -34,7 +34,7 @@ func BenchmarkHTTPClientServer(b *testing.B) {
3434
if err != nil {
3535
b.Fatal("Get:", err)
3636
}
37-
all, err := ioutil.ReadAll(res.Body)
37+
all, err := io.ReadAll(res.Body)
3838
if err != nil {
3939
b.Fatal("ReadAll:", err)
4040
}

test/bench/go1/json_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"encoding/base64"
1313
"encoding/json"
1414
"io"
15-
"io/ioutil"
1615
"testing"
1716
)
1817

@@ -26,7 +25,7 @@ func makeJsonBytes() []byte {
2625
r = bytes.NewReader(bytes.Replace(jsonbz2_base64, []byte{'\n'}, nil, -1))
2726
r = base64.NewDecoder(base64.StdEncoding, r)
2827
r = bzip2.NewReader(r)
29-
b, err := ioutil.ReadAll(r)
28+
b, err := io.ReadAll(r)
3029
if err != nil {
3130
panic(err)
3231
}

test/bench/go1/parser_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"go/parser"
1313
"go/token"
1414
"io"
15-
"io/ioutil"
1615
"strings"
1716
"testing"
1817
)
@@ -26,7 +25,7 @@ func makeParserBytes() []byte {
2625
r = strings.NewReader(parserbz2_base64)
2726
r = base64.NewDecoder(base64.StdEncoding, r)
2827
r = bzip2.NewReader(r)
29-
b, err := ioutil.ReadAll(r)
28+
b, err := io.ReadAll(r)
3029
if err != nil {
3130
panic(err)
3231
}

test/bench/go1/revcomp_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package go1
1010
import (
1111
"bufio"
1212
"bytes"
13-
"io/ioutil"
13+
"io"
1414
"testing"
1515
)
1616

@@ -35,7 +35,7 @@ var revCompTable = [256]uint8{
3535

3636
func revcomp(data []byte) {
3737
in := bufio.NewReader(bytes.NewBuffer(data))
38-
out := ioutil.Discard
38+
out := io.Discard
3939
buf := make([]byte, 1024*1024)
4040
line, err := in.ReadSlice('\n')
4141
for err == nil {

test/bench/go1/template_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package go1
99

1010
import (
1111
"bytes"
12-
"io/ioutil"
12+
"io"
1313
"strings"
1414
"testing"
1515
"text/template"
@@ -63,7 +63,7 @@ func init() {
6363
}
6464

6565
func tmplexec() {
66-
if err := tmpl.Execute(ioutil.Discard, &jsondata); err != nil {
66+
if err := tmpl.Execute(io.Discard, &jsondata); err != nil {
6767
panic(err)
6868
}
6969
}

0 commit comments

Comments
 (0)