From 795e854ff348c9cac4fd0033ce04c417705dd0bb Mon Sep 17 00:00:00 2001 From: cui fliter Date: Fri, 16 Sep 2022 02:08:40 +0000 Subject: [PATCH] all: replace io/ioutil with io and os package For golang/go#45557 Change-Id: Ibf78d87cc94baa0d1696e5abc2edad3d2723e42b GitHub-Last-Rev: ad314caaff6e51edb51a63d183c0ff906cf6b2ad GitHub-Pull-Request: golang/text#34 Reviewed-on: https://go-review.googlesource.com/c/text/+/430815 Reviewed-by: Cherry Mui TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Reviewed-by: Meng Zhuo Run-TryBot: Ian Lance Taylor Run-TryBot: Meng Zhuo Auto-Submit: Ian Lance Taylor --- cases/gen.go | 7 +++---- cmd/gotext/main.go | 3 +-- collate/maketables.go | 3 +-- collate/reg_test.go | 3 +-- encoding/encoding_test.go | 4 ++-- encoding/internal/enctest/enctest.go | 12 ++++++------ encoding/traditionalchinese/all_test.go | 4 ++-- feature/plural/message.go | 4 ++-- gen.go | 5 ++--- internal/cldrtree/cldrtree_test.go | 6 +++--- internal/gen/bitfield/bitfield_test.go | 4 ++-- internal/gen/gen.go | 15 +++++++-------- internal/language/gen.go | 3 +-- internal/testtext/codesize.go | 5 ++--- internal/triegen/example_compact_test.go | 5 ++--- internal/triegen/example_test.go | 4 ++-- message/pipeline/pipeline_test.go | 23 +++++++++++------------ transform/transform_test.go | 4 ++-- unicode/cldr/decode.go | 3 +-- unicode/cldr/makexml.go | 3 +-- unicode/norm/normalize_test.go | 5 ++--- 21 files changed, 56 insertions(+), 69 deletions(-) diff --git a/cases/gen.go b/cases/gen.go index a041fede1..2e1d95cc5 100644 --- a/cases/gen.go +++ b/cases/gen.go @@ -16,7 +16,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "log" "reflect" "strconv" @@ -635,9 +634,9 @@ func genTablesTest() { // We discard the output as we know we have perfect functions. We run them // just to verify the properties are correct. - n := printProperties(ioutil.Discard, "DerivedCoreProperties.txt", "Cased", verifyCased) - n += printProperties(ioutil.Discard, "DerivedCoreProperties.txt", "Lowercase", verifyLower) - n += printProperties(ioutil.Discard, "DerivedCoreProperties.txt", "Uppercase", verifyUpper) + n := printProperties(io.Discard, "DerivedCoreProperties.txt", "Cased", verifyCased) + n += printProperties(io.Discard, "DerivedCoreProperties.txt", "Lowercase", verifyLower) + n += printProperties(io.Discard, "DerivedCoreProperties.txt", "Uppercase", verifyUpper) if n > 0 { log.Fatalf("One of the discarded properties does not have a perfect filter.") } diff --git a/cmd/gotext/main.go b/cmd/gotext/main.go index c8dc7990a..aad1d4a14 100644 --- a/cmd/gotext/main.go +++ b/cmd/gotext/main.go @@ -16,7 +16,6 @@ import ( "go/build" "go/format" "io" - "io/ioutil" "log" "os" "strings" @@ -310,7 +309,7 @@ func help(args []string) { if err != nil { logf("Could not format generated docs: %v\n", err) } - if err := ioutil.WriteFile("doc.go", b, 0666); err != nil { + if err := os.WriteFile("doc.go", b, 0666); err != nil { logf("Could not create file alldocs.go: %v\n", err) } } else { diff --git a/collate/maketables.go b/collate/maketables.go index 59afeb9c2..4175143e3 100644 --- a/collate/maketables.go +++ b/collate/maketables.go @@ -17,7 +17,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "os" "regexp" @@ -187,7 +186,7 @@ func failOnError(e error) { func openArchive() *zip.Reader { f := gen.OpenCLDRCoreZip() - buffer, err := ioutil.ReadAll(f) + buffer, err := io.ReadAll(f) f.Close() failOnError(err) archive, err := zip.NewReader(bytes.NewReader(buffer), int64(len(buffer))) diff --git a/collate/reg_test.go b/collate/reg_test.go index 4d3598485..f9c192cb3 100644 --- a/collate/reg_test.go +++ b/collate/reg_test.go @@ -10,7 +10,6 @@ import ( "bytes" "flag" "io" - "io/ioutil" "log" "path" "regexp" @@ -133,7 +132,7 @@ func convHex(line int, s string) int { func loadTestData() []Test { f := gen.OpenUnicodeFile("UCA", "", "CollationTest.zip") - buffer, err := ioutil.ReadAll(f) + buffer, err := io.ReadAll(f) f.Close() Error(err) archive, err := zip.NewReader(bytes.NewReader(buffer), int64(len(buffer))) diff --git a/encoding/encoding_test.go b/encoding/encoding_test.go index 173814762..e459fa230 100644 --- a/encoding/encoding_test.go +++ b/encoding/encoding_test.go @@ -5,7 +5,7 @@ package encoding_test import ( - "io/ioutil" + "io" "strings" "testing" @@ -62,7 +62,7 @@ func TestReplacement(t *testing.T) { want = "AB\x00CD\ufffdYZ" } sr := strings.NewReader("AB\x00CD\x80YZ") - g, err := ioutil.ReadAll(transform.NewReader(sr, enc)) + g, err := io.ReadAll(transform.NewReader(sr, enc)) if err != nil { t.Errorf("%s: ReadAll: %v", direction, err) continue diff --git a/encoding/internal/enctest/enctest.go b/encoding/internal/enctest/enctest.go index 0cccae044..cf5da2675 100644 --- a/encoding/internal/enctest/enctest.go +++ b/encoding/internal/enctest/enctest.go @@ -8,7 +8,7 @@ import ( "bytes" "fmt" "io" - "io/ioutil" + "os" "strings" "testing" @@ -105,7 +105,7 @@ func Benchmark(b *testing.B, enc encoding.Encoding) { b.ResetTimer() for i := 0; i < b.N; i++ { r := transform.NewReader(bytes.NewReader(src), transformer) - io.Copy(ioutil.Discard, r) + io.Copy(io.Discard, r) } }) } @@ -157,15 +157,15 @@ func load(direction string, enc encoding.Encoding) ([]byte, []byte, Transcoder, dstFile, srcFile = srcFile, dstFile coder = enc.NewDecoder() } - dst, err := ioutil.ReadFile(dstFile) + dst, err := os.ReadFile(dstFile) if err != nil { - if dst, err = ioutil.ReadFile("../" + dstFile); err != nil { + if dst, err = os.ReadFile("../" + dstFile); err != nil { return nil, nil, nil, err } } - src, err := ioutil.ReadFile(srcFile) + src, err := os.ReadFile(srcFile) if err != nil { - if src, err = ioutil.ReadFile("../" + srcFile); err != nil { + if src, err = os.ReadFile("../" + srcFile); err != nil { return nil, nil, nil, err } } diff --git a/encoding/traditionalchinese/all_test.go b/encoding/traditionalchinese/all_test.go index 3825c7672..f804e83e1 100644 --- a/encoding/traditionalchinese/all_test.go +++ b/encoding/traditionalchinese/all_test.go @@ -6,7 +6,7 @@ package traditionalchinese import ( "fmt" - "io/ioutil" + "io" "strings" "testing" @@ -103,7 +103,7 @@ func TestBig5CircumflexAndMacron(t *testing.T) { "\x88\xa2\x88\xa3\x88\xa4\x88\xa5\x88\xa6" want := "ÓǑÒ\u00ca\u0304Ế\u00ca\u030cỀÊ " + "ü\u00ea\u0304ế\u00ea\u030cề" - dst, err := ioutil.ReadAll(transform.NewReader( + dst, err := io.ReadAll(transform.NewReader( strings.NewReader(src), Big5.NewDecoder())) if err != nil { t.Fatal(err) diff --git a/feature/plural/message.go b/feature/plural/message.go index 6248d01cc..56d518cc3 100644 --- a/feature/plural/message.go +++ b/feature/plural/message.go @@ -6,7 +6,7 @@ package plural import ( "fmt" - "io/ioutil" + "io" "reflect" "strconv" @@ -56,7 +56,7 @@ type Interface interface { func Selectf(arg int, format string, cases ...interface{}) catalog.Message { var p parser // Intercept the formatting parameters of format by doing a dummy print. - fmt.Fprintf(ioutil.Discard, format, &p) + fmt.Fprintf(io.Discard, format, &p) m := &message{arg, kindDefault, 0, cases} switch p.verb { case 'g': diff --git a/gen.go b/gen.go index fb2fb94a7..566a30355 100644 --- a/gen.go +++ b/gen.go @@ -14,7 +14,6 @@ import ( "flag" "fmt" "go/format" - "io/ioutil" "os" "os/exec" "path" @@ -256,7 +255,7 @@ func copyPackage(dirSrc, dirDst, search, replace string) { filepath.Dir(file) != dirSrc { return nil } - b, err := ioutil.ReadFile(file) + b, err := os.ReadFile(file) if err != nil || bytes.Contains(b, []byte("\n// +build ignore")) { return err } @@ -275,7 +274,7 @@ func copyPackage(dirSrc, dirDst, search, replace string) { } file = filepath.Join(dirDst, base) vprintf("=== COPY %s\n", file) - return ioutil.WriteFile(file, b, 0666) + return os.WriteFile(file, b, 0666) }) if err != nil { fmt.Println("Copying exported files failed:", err) diff --git a/internal/cldrtree/cldrtree_test.go b/internal/cldrtree/cldrtree_test.go index 06c366a05..c42eba30a 100644 --- a/internal/cldrtree/cldrtree_test.go +++ b/internal/cldrtree/cldrtree_test.go @@ -7,9 +7,9 @@ package cldrtree import ( "bytes" "flag" - "io/ioutil" "log" "math/rand" + "os" "path/filepath" "reflect" "regexp" @@ -303,11 +303,11 @@ func TestGen(t *testing.T) { file := filepath.Join("testdata", tc, "output.go") if *genOutput { - ioutil.WriteFile(file, got, 0700) + os.WriteFile(file, got, 0700) t.SkipNow() } - b, err := ioutil.ReadFile(file) + b, err := os.ReadFile(file) if err != nil { t.Fatalf("failed to open file: %v", err) } diff --git a/internal/gen/bitfield/bitfield_test.go b/internal/gen/bitfield/bitfield_test.go index 789f86d1a..a43dda962 100644 --- a/internal/gen/bitfield/bitfield_test.go +++ b/internal/gen/bitfield/bitfield_test.go @@ -7,7 +7,7 @@ package bitfield import ( "bytes" "fmt" - "io/ioutil" + "os" "testing" ) @@ -222,7 +222,7 @@ func (t test1) Baz() int8 { ` func mustRead(filename string) string { - b, err := ioutil.ReadFile(filename) + b, err := os.ReadFile(filename) if err != nil { panic(err) } diff --git a/internal/gen/gen.go b/internal/gen/gen.go index fa289534a..268258dd3 100644 --- a/internal/gen/gen.go +++ b/internal/gen/gen.go @@ -25,7 +25,6 @@ import ( "go/build" "go/format" "io" - "io/ioutil" "log" "net/http" "os" @@ -175,7 +174,7 @@ func getLocalDir() string { if err := os.MkdirAll(dir, permissions); err != nil { log.Fatalf("Could not create directory: %v", err) } - ioutil.WriteFile(readme, []byte(readmeTxt), permissions) + os.WriteFile(readme, []byte(readmeTxt), permissions) } return dir } @@ -213,15 +212,15 @@ func open(file, urlRoot, path string) io.ReadCloser { } r := get(urlRoot, path) defer r.Close() - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { log.Fatalf("Could not download file: %v", err) } os.MkdirAll(filepath.Dir(file), permissions) - if err := ioutil.WriteFile(file, b, permissions); err != nil { + if err := os.WriteFile(file, b, permissions); err != nil { log.Fatalf("Could not create file: %v", err) } - return ioutil.NopCloser(bytes.NewReader(b)) + return io.NopCloser(bytes.NewReader(b)) } func get(root, path string) io.ReadCloser { @@ -280,13 +279,13 @@ func fileToPattern(filename string) string { func updateBuildTags(pattern string) { for _, t := range tags { oldFile := fmt.Sprintf(pattern, t.version) - b, err := ioutil.ReadFile(oldFile) + b, err := os.ReadFile(oldFile) if err != nil { continue } build := fmt.Sprintf("// +build %s", t.buildTags) b = regexp.MustCompile(`// \+build .*`).ReplaceAll(b, []byte(build)) - err = ioutil.WriteFile(oldFile, b, 0644) + err = os.WriteFile(oldFile, b, 0644) if err != nil { log.Fatal(err) } @@ -334,7 +333,7 @@ func WriteGo(w io.Writer, pkg, tags string, b []byte) (n int, err error) { // Repackage rewrites a Go file from belonging to package main to belonging to // the given package. func Repackage(inFile, outFile, pkg string) { - src, err := ioutil.ReadFile(inFile) + src, err := os.ReadFile(inFile) if err != nil { log.Fatalf("reading %s: %v", inFile, err) } diff --git a/internal/language/gen.go b/internal/language/gen.go index 95455ecf4..520f1596f 100644 --- a/internal/language/gen.go +++ b/internal/language/gen.go @@ -15,7 +15,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "math" "reflect" @@ -921,7 +920,7 @@ func (b *builder) writeRegion() { r := gen.OpenIANAFile("domains/root/db") defer r.Close() - buf, err := ioutil.ReadAll(r) + buf, err := io.ReadAll(r) failOnError(err) re := regexp.MustCompile(`"/domains/root/db/([a-z]{2}).html"`) for _, m := range re.FindAllSubmatch(buf, -1) { diff --git a/internal/testtext/codesize.go b/internal/testtext/codesize.go index 88df48747..cdaed81d5 100644 --- a/internal/testtext/codesize.go +++ b/internal/testtext/codesize.go @@ -7,7 +7,6 @@ package testtext import ( "bytes" "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -26,13 +25,13 @@ import ( // See dict_test.go in the display package for an example. func CodeSize(s string) (int, error) { // Write the file. - tmpdir, err := ioutil.TempDir(os.TempDir(), "testtext") + tmpdir, err := os.MkdirTemp(os.TempDir(), "testtext") if err != nil { return 0, fmt.Errorf("testtext: failed to create tmpdir: %v", err) } defer os.RemoveAll(tmpdir) filename := filepath.Join(tmpdir, "main.go") - if err := ioutil.WriteFile(filename, []byte(s), 0644); err != nil { + if err := os.WriteFile(filename, []byte(s), 0644); err != nil { return 0, fmt.Errorf("testtext: failed to write main.go: %v", err) } diff --git a/internal/triegen/example_compact_test.go b/internal/triegen/example_compact_test.go index 7cf604ca4..95486d1ff 100644 --- a/internal/triegen/example_compact_test.go +++ b/internal/triegen/example_compact_test.go @@ -7,7 +7,6 @@ package triegen_test import ( "fmt" "io" - "io/ioutil" "golang.org/x/text/internal/triegen" ) @@ -17,12 +16,12 @@ func ExampleCompacter() { for r := rune(0); r < 10000; r += 64 { t.Insert(r, 0x9015BADA55^uint64(r)) } - sz, _ := t.Gen(ioutil.Discard) + sz, _ := t.Gen(io.Discard) fmt.Printf("Size normal: %5d\n", sz) var c myCompacter - sz, _ = t.Gen(ioutil.Discard, triegen.Compact(&c)) + sz, _ = t.Gen(io.Discard, triegen.Compact(&c)) fmt.Printf("Size compacted: %5d\n", sz) diff --git a/internal/triegen/example_test.go b/internal/triegen/example_test.go index 557a152e7..5823a2c34 100644 --- a/internal/triegen/example_test.go +++ b/internal/triegen/example_test.go @@ -6,7 +6,7 @@ package triegen_test import ( "fmt" - "io/ioutil" + "io" "math/rand" "unicode" @@ -15,7 +15,7 @@ import ( const seed = 0x12345 -var genWriter = ioutil.Discard +var genWriter = io.Discard func randomRunes() map[rune]uint8 { rnd := rand.New(rand.NewSource(seed)) diff --git a/message/pipeline/pipeline_test.go b/message/pipeline/pipeline_test.go index 2adb11fd2..f4cd42a69 100644 --- a/message/pipeline/pipeline_test.go +++ b/message/pipeline/pipeline_test.go @@ -11,7 +11,6 @@ import ( "flag" "fmt" "go/build" - "io/ioutil" "os" "os/exec" "path" @@ -40,7 +39,7 @@ func TestFullCycle(t *testing.T) { t.Skipf("skipping because 'go' command is unavailable: %v", err) } - GOPATH, err := ioutil.TempDir("", "pipeline_test") + GOPATH, err := os.MkdirTemp("", "pipeline_test") if err != nil { t.Fatal(err) } @@ -66,7 +65,7 @@ func TestFullCycle(t *testing.T) { wd, _ := os.Getwd() defer os.Chdir(wd) - dirs, err := ioutil.ReadDir(testdata) + dirs, err := os.ReadDir(testdata) if err != nil { t.Fatal(err) } @@ -124,11 +123,11 @@ func copyTestdata(t *testing.T, dst string) { return os.MkdirAll(filepath.Join(dst, rel), 0755) } - data, err := ioutil.ReadFile(p) + data, err := os.ReadFile(p) if err != nil { return err } - return ioutil.WriteFile(filepath.Join(dst, rel), data, 0644) + return os.WriteFile(filepath.Join(dst, rel), data, 0644) }) if err != nil { t.Fatal(err) @@ -145,17 +144,17 @@ func initTestdataModule(t *testing.T, dst string) { replace golang.org/x/text => %s `, xTextDir) - if err := ioutil.WriteFile(filepath.Join(dst, "go.mod"), []byte(goMod), 0644); err != nil { + if err := os.WriteFile(filepath.Join(dst, "go.mod"), []byte(goMod), 0644); err != nil { t.Fatal(err) } // Copy in the checksums from the parent module so that we won't // need to re-fetch them from the checksum database. - data, err := ioutil.ReadFile(filepath.Join(xTextDir, "go.sum")) + data, err := os.ReadFile(filepath.Join(xTextDir, "go.sum")) if err != nil { t.Fatal(err) } - if err := ioutil.WriteFile(filepath.Join(dst, "go.sum"), data, 0644); err != nil { + if err := os.WriteFile(filepath.Join(dst, "go.sum"), data, 0644); err != nil { t.Fatal(err) } @@ -194,17 +193,17 @@ func checkOutput(t *testing.T, gen string, testdataDir string) { return nil } - got, err := ioutil.ReadFile(gotFile) + got, err := os.ReadFile(gotFile) if err != nil { t.Errorf("failed to read %q", gotFile) return nil } if *genFiles { - if err := ioutil.WriteFile(wantFile, got, 0644); err != nil { + if err := os.WriteFile(wantFile, got, 0644); err != nil { t.Fatal(err) } } - want, err := ioutil.ReadFile(wantFile) + want, err := os.ReadFile(wantFile) if err != nil { t.Errorf("failed to read %q", wantFile) } else { @@ -242,7 +241,7 @@ func writeJSON(t *testing.T, path string, x interface{}) { if err != nil { t.Fatal(err) } - if err := ioutil.WriteFile(path, data, 0644); err != nil { + if err := os.WriteFile(path, data, 0644); err != nil { t.Fatal(err) } } diff --git a/transform/transform_test.go b/transform/transform_test.go index 273abfa52..62fad2bc9 100644 --- a/transform/transform_test.go +++ b/transform/transform_test.go @@ -8,7 +8,7 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" + "io" "strconv" "strings" "testing" @@ -648,7 +648,7 @@ func TestReader(t *testing.T) { // exported API. We override them manually. r.dst = make([]byte, tc.dstSize) r.src = make([]byte, tc.srcSize) - got, err := ioutil.ReadAll(r) + got, err := io.ReadAll(r) str := string(got) if str != tc.wantStr || err != tc.wantErr { t.Errorf("\ngot %q, %v\nwant %q, %v", str, err, tc.wantStr, tc.wantErr) diff --git a/unicode/cldr/decode.go b/unicode/cldr/decode.go index 48f6bd629..1abcb9e1f 100644 --- a/unicode/cldr/decode.go +++ b/unicode/cldr/decode.go @@ -10,7 +10,6 @@ import ( "encoding/xml" "fmt" "io" - "io/ioutil" "log" "os" "path/filepath" @@ -160,7 +159,7 @@ func (zl zipLoader) Reader(i int) (io.ReadCloser, error) { // DecodeZip loads CLDR data from the zip archive for which r is the source. func (d *Decoder) DecodeZip(r io.Reader) (cldr *CLDR, err error) { - buffer, err := ioutil.ReadAll(r) + buffer, err := io.ReadAll(r) if err != nil { return nil, err } diff --git a/unicode/cldr/makexml.go b/unicode/cldr/makexml.go index 3c1fd26ea..528f2c48d 100644 --- a/unicode/cldr/makexml.go +++ b/unicode/cldr/makexml.go @@ -15,7 +15,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "os" "regexp" @@ -30,7 +29,7 @@ func main() { flag.Parse() r := gen.OpenCLDRCoreZip() - buffer, err := ioutil.ReadAll(r) + buffer, err := io.ReadAll(r) if err != nil { log.Fatal("Could not read zip file") } diff --git a/unicode/norm/normalize_test.go b/unicode/norm/normalize_test.go index 8387d4e03..678ca403d 100644 --- a/unicode/norm/normalize_test.go +++ b/unicode/norm/normalize_test.go @@ -9,7 +9,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "os" "os/exec" @@ -927,14 +926,14 @@ func TestString(t *testing.T) { func runNM(code string) (string, error) { // Write the file. - tmpdir, err := ioutil.TempDir(os.TempDir(), "normalize_test") + tmpdir, err := os.MkdirTemp(os.TempDir(), "normalize_test") if err != nil { return "", fmt.Errorf("failed to create tmpdir: %v", err) } defer os.RemoveAll(tmpdir) goTool := filepath.Join(runtime.GOROOT(), "bin", "go") filename := filepath.Join(tmpdir, "main.go") - if err := ioutil.WriteFile(filename, []byte(code), 0644); err != nil { + if err := os.WriteFile(filename, []byte(code), 0644); err != nil { return "", fmt.Errorf("failed to write main.go: %v", err) } outputFile := filepath.Join(tmpdir, "main")