Skip to content

Commit

Permalink
all: update to use os.ReadFile, os.WriteFile, os.CreateTemp, os.Mkdir…
Browse files Browse the repository at this point in the history
…Temp

As part of #42026, these helpers from io/ioutil were moved to os.
(ioutil.TempFile and TempDir became os.CreateTemp and MkdirTemp.)

Update the Go tree to use the preferred names.

As usual, code compiled with the Go 1.4 bootstrap toolchain
and code vendored from other sources is excluded.

ReadDir changes are in a separate CL, because they are not a
simple search and replace.

For #42026.

Change-Id: If318df0216d57e95ea0c4093b89f65e5b0ababb3
Reviewed-on: https://go-review.googlesource.com/c/go/+/266365
Trust: Russ Cox <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
rsc committed Dec 9, 2020
1 parent 5627a4d commit 4f1b0a4
Show file tree
Hide file tree
Showing 215 changed files with 556 additions and 704 deletions.
3 changes: 1 addition & 2 deletions src/archive/tar/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path"
Expand Down Expand Up @@ -773,7 +772,7 @@ func TestReadTruncation(t *testing.T) {
"testdata/pax-path-hdr.tar",
"testdata/sparse-formats.tar",
} {
buf, err := ioutil.ReadFile(p)
buf, err := os.ReadFile(p)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions src/archive/tar/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"internal/testenv"
"io"
"io/fs"
"io/ioutil"
"math"
"os"
"path"
Expand Down Expand Up @@ -263,7 +262,7 @@ func TestFileInfoHeaderDir(t *testing.T) {
func TestFileInfoHeaderSymlink(t *testing.T) {
testenv.MustHaveSymlink(t)

tmpdir, err := ioutil.TempDir("", "TestFileInfoHeaderSymlink")
tmpdir, err := os.MkdirTemp("", "TestFileInfoHeaderSymlink")
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions src/archive/tar/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/hex"
"errors"
"io"
"io/ioutil"
"os"
"path"
"reflect"
Expand Down Expand Up @@ -520,7 +519,7 @@ func TestWriter(t *testing.T) {
}

if v.file != "" {
want, err := ioutil.ReadFile(v.file)
want, err := os.ReadFile(v.file)
if err != nil {
t.Fatalf("ReadFile() = %v, want nil", err)
}
Expand Down
11 changes: 5 additions & 6 deletions src/archive/zip/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"internal/obscuretestdata"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -629,7 +628,7 @@ func readTestFile(t *testing.T, zt ZipTest, ft ZipTestFile, f *File) {
var c []byte
if ft.Content != nil {
c = ft.Content
} else if c, err = ioutil.ReadFile("testdata/" + ft.File); err != nil {
} else if c, err = os.ReadFile("testdata/" + ft.File); err != nil {
t.Error(err)
return
}
Expand Down Expand Up @@ -685,7 +684,7 @@ func TestInvalidFiles(t *testing.T) {
}

func messWith(fileName string, corrupter func(b []byte)) (r io.ReaderAt, size int64) {
data, err := ioutil.ReadFile(filepath.Join("testdata", fileName))
data, err := os.ReadFile(filepath.Join("testdata", fileName))
if err != nil {
panic("Error reading " + fileName + ": " + err.Error())
}
Expand Down Expand Up @@ -792,17 +791,17 @@ func returnRecursiveZip() (r io.ReaderAt, size int64) {
//
// func main() {
// bigZip := makeZip("big.file", io.LimitReader(zeros{}, 1<<32-1))
// if err := ioutil.WriteFile("/tmp/big.zip", bigZip, 0666); err != nil {
// if err := os.WriteFile("/tmp/big.zip", bigZip, 0666); err != nil {
// log.Fatal(err)
// }
//
// biggerZip := makeZip("big.zip", bytes.NewReader(bigZip))
// if err := ioutil.WriteFile("/tmp/bigger.zip", biggerZip, 0666); err != nil {
// if err := os.WriteFile("/tmp/bigger.zip", biggerZip, 0666); err != nil {
// log.Fatal(err)
// }
//
// biggestZip := makeZip("bigger.zip", bytes.NewReader(biggerZip))
// if err := ioutil.WriteFile("/tmp/biggest.zip", biggestZip, 0666); err != nil {
// if err := os.WriteFile("/tmp/biggest.zip", biggestZip, 0666); err != nil {
// log.Fatal(err)
// }
// }
Expand Down
4 changes: 2 additions & 2 deletions src/archive/zip/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"math/rand"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestWriterTime(t *testing.T) {
t.Fatalf("unexpected Close error: %v", err)
}

want, err := ioutil.ReadFile("testdata/time-go.zip")
want, err := os.ReadFile("testdata/time-go.zip")
if err != nil {
t.Fatalf("unexpected ReadFile error: %v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions src/cmd/addr2line/addr2line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bufio"
"bytes"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -98,16 +97,16 @@ func testAddr2Line(t *testing.T, exepath, addr string) {
if !os.SameFile(fi1, fi2) {
t.Fatalf("addr2line_test.go and %s are not same file", srcPath)
}
if srcLineNo != "107" {
t.Fatalf("line number = %v; want 107", srcLineNo)
if srcLineNo != "106" {
t.Fatalf("line number = %v; want 106", srcLineNo)
}
}

// This is line 106. The test depends on that.
func TestAddr2Line(t *testing.T) {
testenv.MustHaveGoBuild(t)

tmpDir, err := ioutil.TempDir("", "TestAddr2Line")
tmpDir, err := os.MkdirTemp("", "TestAddr2Line")
if err != nil {
t.Fatal("TempDir failed: ", err)
}
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/api/goapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"go/token"
"go/types"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -342,7 +341,7 @@ func fileFeatures(filename string) []string {
if filename == "" {
return nil
}
bs, err := ioutil.ReadFile(filename)
bs, err := os.ReadFile(filename)
if err != nil {
log.Fatalf("Error reading file %s: %v", filename, err)
}
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/api/goapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestGolden(t *testing.T) {
f.Close()
}

bs, err := ioutil.ReadFile(goldenFile)
bs, err := os.ReadFile(goldenFile)
if err != nil {
t.Fatalf("opening golden.txt for package %q: %v", fi.Name(), err)
}
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/cover/cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"sort"
Expand Down Expand Up @@ -304,7 +303,7 @@ func (f *File) Visit(node ast.Node) ast.Visitor {

func annotate(name string) {
fset := token.NewFileSet()
content, err := ioutil.ReadFile(name)
content, err := os.ReadFile(name)
if err != nil {
log.Fatalf("cover: %s: %s", name, err)
}
Expand Down
31 changes: 15 additions & 16 deletions src/cmd/cover/cover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"go/parser"
"go/token"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -81,7 +80,7 @@ var debug = flag.Bool("debug", false, "keep rewritten files for debugging")
// We use TestMain to set up a temporary directory and remove it when
// the tests are done.
func TestMain(m *testing.M) {
dir, err := ioutil.TempDir("", "go-testcover")
dir, err := os.MkdirTemp("", "go-testcover")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down Expand Up @@ -173,7 +172,7 @@ func TestCover(t *testing.T) {
buildCover(t)

// Read in the test file (testTest) and write it, with LINEs specified, to coverInput.
file, err := ioutil.ReadFile(testTest)
file, err := os.ReadFile(testTest)
if err != nil {
t.Fatal(err)
}
Expand All @@ -192,7 +191,7 @@ func TestCover(t *testing.T) {
[]byte("}"))
lines = append(lines, []byte("func unFormatted2(b bool) {if b{}else{}}"))

if err := ioutil.WriteFile(coverInput, bytes.Join(lines, []byte("\n")), 0666); err != nil {
if err := os.WriteFile(coverInput, bytes.Join(lines, []byte("\n")), 0666); err != nil {
t.Fatal(err)
}

Expand All @@ -208,19 +207,19 @@ func TestCover(t *testing.T) {

// Copy testmain to testTempDir, so that it is in the same directory
// as coverOutput.
b, err := ioutil.ReadFile(testMain)
b, err := os.ReadFile(testMain)
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(tmpTestMain, b, 0444); err != nil {
if err := os.WriteFile(tmpTestMain, b, 0444); err != nil {
t.Fatal(err)
}

// go run ./testdata/main.go ./testdata/test.go
cmd = exec.Command(testenv.GoToolPath(t), "run", tmpTestMain, coverOutput)
run(cmd, t)

file, err = ioutil.ReadFile(coverOutput)
file, err = os.ReadFile(coverOutput)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -251,7 +250,7 @@ func TestDirectives(t *testing.T) {
// Read the source file and find all the directives. We'll keep
// track of whether each one has been seen in the output.
testDirectives := filepath.Join(testdata, "directives.go")
source, err := ioutil.ReadFile(testDirectives)
source, err := os.ReadFile(testDirectives)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -398,7 +397,7 @@ func TestCoverHTML(t *testing.T) {

// Extract the parts of the HTML with comment markers,
// and compare against a golden file.
entireHTML, err := ioutil.ReadFile(htmlHTML)
entireHTML, err := os.ReadFile(htmlHTML)
if err != nil {
t.Fatal(err)
}
Expand All @@ -420,7 +419,7 @@ func TestCoverHTML(t *testing.T) {
if scan.Err() != nil {
t.Error(scan.Err())
}
golden, err := ioutil.ReadFile(htmlGolden)
golden, err := os.ReadFile(htmlGolden)
if err != nil {
t.Fatalf("reading golden file: %v", err)
}
Expand Down Expand Up @@ -457,7 +456,7 @@ func TestHtmlUnformatted(t *testing.T) {
t.Fatal(err)
}

if err := ioutil.WriteFile(filepath.Join(htmlUDir, "go.mod"), []byte("module htmlunformatted\n"), 0666); err != nil {
if err := os.WriteFile(filepath.Join(htmlUDir, "go.mod"), []byte("module htmlunformatted\n"), 0666); err != nil {
t.Fatal(err)
}

Expand All @@ -474,10 +473,10 @@ lab:

const htmlUTestContents = `package htmlunformatted`

if err := ioutil.WriteFile(htmlU, []byte(htmlUContents), 0444); err != nil {
if err := os.WriteFile(htmlU, []byte(htmlUContents), 0444); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(htmlUTest, []byte(htmlUTestContents), 0444); err != nil {
if err := os.WriteFile(htmlUTest, []byte(htmlUTestContents), 0444); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -540,13 +539,13 @@ func TestFuncWithDuplicateLines(t *testing.T) {
t.Fatal(err)
}

if err := ioutil.WriteFile(filepath.Join(lineDupDir, "go.mod"), []byte("module linedup\n"), 0666); err != nil {
if err := os.WriteFile(filepath.Join(lineDupDir, "go.mod"), []byte("module linedup\n"), 0666); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(lineDupGo, []byte(lineDupContents), 0444); err != nil {
if err := os.WriteFile(lineDupGo, []byte(lineDupContents), 0444); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(lineDupTestGo, []byte(lineDupTestContents), 0444); err != nil {
if err := os.WriteFile(lineDupTestGo, []byte(lineDupTestContents), 0444); err != nil {
t.Fatal(err)
}

Expand Down
5 changes: 2 additions & 3 deletions src/cmd/cover/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"html/template"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -43,7 +42,7 @@ func htmlOutput(profile, outfile string) error {
if err != nil {
return err
}
src, err := ioutil.ReadFile(file)
src, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("can't read %q: %v", fn, err)
}
Expand All @@ -62,7 +61,7 @@ func htmlOutput(profile, outfile string) error {
var out *os.File
if outfile == "" {
var dir string
dir, err = ioutil.TempDir("", "cover")
dir, err = os.MkdirTemp("", "cover")
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/fix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"go/token"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -217,7 +216,7 @@ func processFile(filename string, useStdin bool) error {
return nil
}

return ioutil.WriteFile(f.Name(), newSrc, 0)
return os.WriteFile(f.Name(), newSrc, 0)
}

func gofmt(n interface{}) string {
Expand Down
7 changes: 3 additions & 4 deletions src/cmd/fix/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -162,12 +161,12 @@ func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[interface{}]string, ass
if err != nil {
return err
}
dir, err := ioutil.TempDir(os.TempDir(), "fix_cgo_typecheck")
dir, err := os.MkdirTemp(os.TempDir(), "fix_cgo_typecheck")
if err != nil {
return err
}
defer os.RemoveAll(dir)
err = ioutil.WriteFile(filepath.Join(dir, "in.go"), txt, 0600)
err = os.WriteFile(filepath.Join(dir, "in.go"), txt, 0600)
if err != nil {
return err
}
Expand All @@ -176,7 +175,7 @@ func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[interface{}]string, ass
if err != nil {
return err
}
out, err := ioutil.ReadFile(filepath.Join(dir, "_cgo_gotypes.go"))
out, err := os.ReadFile(filepath.Join(dir, "_cgo_gotypes.go"))
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 4f1b0a4

Please sign in to comment.