1
0
mirror of https://github.com/golang/go synced 2024-11-11 19:51:37 -07:00

all: update to use os.ReadFile, os.WriteFile, os.CreateTemp, os.MkdirTemp

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 <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Russ Cox 2020-10-29 14:17:47 -04:00
parent 5627a4dc30
commit 4f1b0a44cb
215 changed files with 556 additions and 704 deletions

View File

@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path"
@ -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)
}

View File

@ -11,7 +11,6 @@ import (
"internal/testenv"
"io"
"io/fs"
"io/ioutil"
"math"
"os"
"path"
@ -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)
}

View File

@ -9,7 +9,6 @@ import (
"encoding/hex"
"errors"
"io"
"io/ioutil"
"os"
"path"
"reflect"
@ -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)
}

View File

@ -11,7 +11,6 @@ import (
"internal/obscuretestdata"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"regexp"
@ -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
}
@ -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())
}
@ -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)
// }
// }

View File

@ -10,8 +10,8 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"math/rand"
"os"
"strings"
"testing"
"time"
@ -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)
}

View File

@ -8,7 +8,6 @@ import (
"bufio"
"bytes"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -98,8 +97,8 @@ 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)
}
}
@ -107,7 +106,7 @@ func testAddr2Line(t *testing.T, exepath, addr string) {
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)
}

View File

@ -17,7 +17,6 @@ import (
"go/token"
"go/types"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
@ -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)
}

View File

@ -9,7 +9,6 @@ import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"sort"
@ -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)
}

View File

@ -12,7 +12,6 @@ import (
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"sort"
@ -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)
}

View File

@ -13,7 +13,6 @@ import (
"go/parser"
"go/token"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -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)
@ -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)
}
@ -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)
}
@ -208,11 +207,11 @@ 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)
}
@ -220,7 +219,7 @@ func TestCover(t *testing.T) {
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)
}
@ -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)
}
@ -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)
}
@ -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)
}
@ -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)
}
@ -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)
}
@ -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)
}

View File

@ -11,7 +11,6 @@ import (
"fmt"
"html/template"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
@ -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)
}
@ -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
}

View File

@ -15,7 +15,6 @@ import (
"go/token"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"sort"
@ -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 {

View File

@ -9,7 +9,6 @@ import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -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
}
@ -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
}

View File

@ -17,7 +17,6 @@ import (
"internal/testenv"
"io"
"io/fs"
"io/ioutil"
"log"
"os"
"os/exec"
@ -100,7 +99,7 @@ func TestMain(m *testing.M) {
// Run with a temporary TMPDIR to check that the tests don't
// leave anything behind.
topTmpdir, err := ioutil.TempDir("", "cmd-go-test-")
topTmpdir, err := os.MkdirTemp("", "cmd-go-test-")
if err != nil {
log.Fatal(err)
}
@ -109,7 +108,7 @@ func TestMain(m *testing.M) {
}
os.Setenv(tempEnvName(), topTmpdir)
dir, err := ioutil.TempDir(topTmpdir, "tmpdir")
dir, err := os.MkdirTemp(topTmpdir, "tmpdir")
if err != nil {
log.Fatal(err)
}
@ -616,7 +615,7 @@ func (tg *testgoData) makeTempdir() {
tg.t.Helper()
if tg.tempdir == "" {
var err error
tg.tempdir, err = ioutil.TempDir("", "gotest")
tg.tempdir, err = os.MkdirTemp("", "gotest")
tg.must(err)
}
}
@ -633,7 +632,7 @@ func (tg *testgoData) tempFile(path, contents string) {
bytes = formatted
}
}
tg.must(ioutil.WriteFile(filepath.Join(tg.tempdir, path), bytes, 0644))
tg.must(os.WriteFile(filepath.Join(tg.tempdir, path), bytes, 0644))
}
// tempDir adds a temporary directory for a run of testgo.
@ -833,7 +832,7 @@ func TestNewReleaseRebuildsStalePackagesInGOPATH(t *testing.T) {
return err
}
dest := filepath.Join("goroot", copydir, srcrel)
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
@ -850,18 +849,18 @@ func TestNewReleaseRebuildsStalePackagesInGOPATH(t *testing.T) {
tg.setenv("GOROOT", tg.path("goroot"))
addVar := func(name string, idx int) (restore func()) {
data, err := ioutil.ReadFile(name)
data, err := os.ReadFile(name)
if err != nil {
t.Fatal(err)
}
old := data
data = append(data, fmt.Sprintf("var DummyUnusedVar%d bool\n", idx)...)
if err := ioutil.WriteFile(name, append(data, '\n'), 0666); err != nil {
if err := os.WriteFile(name, append(data, '\n'), 0666); err != nil {
t.Fatal(err)
}
tg.sleep()
return func() {
if err := ioutil.WriteFile(name, old, 0666); err != nil {
if err := os.WriteFile(name, old, 0666); err != nil {
t.Fatal(err)
}
}
@ -2674,7 +2673,7 @@ echo $* >>`+tg.path("pkg-config.out"))
tg.setenv("GOPATH", tg.path("."))
tg.setenv("PKG_CONFIG", tg.path("pkg-config.sh"))
tg.run("build", "x")
out, err := ioutil.ReadFile(tg.path("pkg-config.out"))
out, err := os.ReadFile(tg.path("pkg-config.out"))
tg.must(err)
out = bytes.TrimSpace(out)
want := "--cflags --static --static -- a a\n--libs --static --static -- a a"

View File

@ -5,7 +5,6 @@
package main_test
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -20,14 +19,14 @@ func TestAbsolutePath(t *testing.T) {
defer tg.cleanup()
tg.parallel()
tmp, err := ioutil.TempDir("", "TestAbsolutePath")
tmp, err := os.MkdirTemp("", "TestAbsolutePath")
if err != nil {
t.Fatal(err)
}
defer robustio.RemoveAll(tmp)
file := filepath.Join(tmp, "a.go")
err = ioutil.WriteFile(file, []byte{}, 0644)
err = os.WriteFile(file, []byte{}, 0644)
if err != nil {
t.Fatal(err)
}

View File

@ -6,7 +6,7 @@ package main_test
import (
"bytes"
"io/ioutil"
"os"
"testing"
"cmd/go/internal/help"
@ -23,7 +23,7 @@ func TestDocsUpToDate(t *testing.T) {
buf := new(bytes.Buffer)
// Match the command in mkalldocs.sh that generates alldocs.go.
help.Help(buf, []string{"documentation"})
data, err := ioutil.ReadFile("alldocs.go")
data, err := os.ReadFile("alldocs.go")
if err != nil {
t.Fatalf("error reading alldocs.go: %v", err)
}

View File

@ -5,7 +5,6 @@
package auth
import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -99,7 +98,7 @@ func readNetrc() {
return
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
if !os.IsNotExist(err) {
netrcErr = err

View File

@ -10,7 +10,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
urlpkg "net/url"
"os"
"os/exec"
@ -117,7 +116,7 @@ func printOSDetails(w io.Writer) {
case "illumos", "solaris":
// Be sure to use the OS-supplied uname, in "/usr/bin":
printCmdOut(w, "uname -srv: ", "/usr/bin/uname", "-srv")
out, err := ioutil.ReadFile("/etc/release")
out, err := os.ReadFile("/etc/release")
if err == nil {
fmt.Fprintf(w, "/etc/release: %s\n", out)
} else {
@ -177,7 +176,7 @@ func printGlibcVersion(w io.Writer) {
src := []byte(`int main() {}`)
srcfile := filepath.Join(tempdir, "go-bug.c")
outfile := filepath.Join(tempdir, "go-bug")
err := ioutil.WriteFile(srcfile, src, 0644)
err := os.WriteFile(srcfile, src, 0644)
if err != nil {
return
}

View File

@ -13,7 +13,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@ -239,7 +238,7 @@ func (c *Cache) GetBytes(id ActionID) ([]byte, Entry, error) {
if err != nil {
return nil, entry, err
}
data, _ := ioutil.ReadFile(c.OutputFile(entry.OutputID))
data, _ := os.ReadFile(c.OutputFile(entry.OutputID))
if sha256.Sum256(data) != entry.OutputID {
return nil, entry, &entryNotFoundError{Err: errors.New("bad checksum")}
}
@ -378,7 +377,7 @@ func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify
// Truncate the file only *after* writing it.
// (This should be a no-op, but truncate just in case of previous corruption.)
//
// This differs from ioutil.WriteFile, which truncates to 0 *before* writing
// This differs from os.WriteFile, which truncates to 0 *before* writing
// via os.O_TRUNC. Truncating only after writing ensures that a second write
// of the same content to the same file is idempotent, and does not — even
// temporarily! — undo the effect of the first write.

View File

@ -8,7 +8,6 @@ import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -20,7 +19,7 @@ func init() {
}
func TestBasic(t *testing.T) {
dir, err := ioutil.TempDir("", "cachetest-")
dir, err := os.MkdirTemp("", "cachetest-")
if err != nil {
t.Fatal(err)
}
@ -65,7 +64,7 @@ func TestBasic(t *testing.T) {
}
func TestGrowth(t *testing.T) {
dir, err := ioutil.TempDir("", "cachetest-")
dir, err := os.MkdirTemp("", "cachetest-")
if err != nil {
t.Fatal(err)
}
@ -118,7 +117,7 @@ func TestVerifyPanic(t *testing.T) {
t.Fatal("initEnv did not set verify")
}
dir, err := ioutil.TempDir("", "cachetest-")
dir, err := os.MkdirTemp("", "cachetest-")
if err != nil {
t.Fatal(err)
}
@ -151,7 +150,7 @@ func dummyID(x int) [HashSize]byte {
}
func TestCacheTrim(t *testing.T) {
dir, err := ioutil.TempDir("", "cachetest-")
dir, err := os.MkdirTemp("", "cachetest-")
if err != nil {
t.Fatal(err)
}
@ -207,7 +206,7 @@ func TestCacheTrim(t *testing.T) {
t.Fatal(err)
}
c.OutputFile(entry.OutputID)
data, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt"))
data, err := os.ReadFile(filepath.Join(dir, "trim.txt"))
if err != nil {
t.Fatal(err)
}
@ -220,7 +219,7 @@ func TestCacheTrim(t *testing.T) {
t.Fatal(err)
}
c.OutputFile(entry.OutputID)
data2, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt"))
data2, err := os.ReadFile(filepath.Join(dir, "trim.txt"))
if err != nil {
t.Fatal(err)
}

View File

@ -6,7 +6,6 @@ package cache
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
@ -49,7 +48,7 @@ func initDefaultCache() {
}
if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
// Best effort.
ioutil.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666)
os.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666)
}
c, err := Open(dir)

View File

@ -6,7 +6,6 @@ package cache
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
@ -28,7 +27,7 @@ func TestHash(t *testing.T) {
}
func TestHashFile(t *testing.T) {
f, err := ioutil.TempFile("", "cmd-go-test-")
f, err := os.CreateTemp("", "cmd-go-test-")
if err != nil {
t.Fatal(err)
}

View File

@ -12,7 +12,6 @@ import (
"go/build"
"internal/cfg"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -187,7 +186,7 @@ func initEnvCache() {
if file == "" {
return
}
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return
}

View File

@ -10,7 +10,6 @@ import (
"encoding/json"
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -452,7 +451,7 @@ func updateEnvFile(add map[string]string, del map[string]bool) {
if file == "" {
base.Fatalf("go env: cannot find go env config: %v", err)
}
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil && (!os.IsNotExist(err) || len(add) == 0) {
base.Fatalf("go env: reading go env config: %v", err)
}
@ -506,11 +505,11 @@ func updateEnvFile(add map[string]string, del map[string]bool) {
}
data = []byte(strings.Join(lines, ""))
err = ioutil.WriteFile(file, data, 0666)
err = os.WriteFile(file, data, 0666)
if err != nil {
// Try creating directory.
os.MkdirAll(filepath.Dir(file), 0777)
err = ioutil.WriteFile(file, data, 0666)
err = os.WriteFile(file, data, 0666)
if err != nil {
base.Fatalf("go env: writing go env config: %v", err)
}

View File

@ -86,7 +86,7 @@ func Init(wd string) error {
return nil
}
b, err := ioutil.ReadFile(OverlayFile)
b, err := os.ReadFile(OverlayFile)
if err != nil {
return fmt.Errorf("reading overlay file: %v", err)
}

View File

@ -8,7 +8,6 @@ import (
"internal/testenv"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -47,7 +46,7 @@ func initOverlay(t *testing.T, config string) {
if err := os.MkdirAll(filepath.Dir(name), 0777); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(name, f.Data, 0666); err != nil {
if err := os.WriteFile(name, f.Data, 0666); err != nil {
t.Fatal(err)
}
}

View File

@ -13,7 +13,6 @@ import (
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
@ -201,7 +200,7 @@ func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
// generate runs the generation directives for a single file.
func generate(absFile string) bool {
src, err := ioutil.ReadFile(absFile)
src, err := os.ReadFile(absFile)
if err != nil {
log.Fatalf("generate: %s", err)
}

View File

@ -8,6 +8,7 @@ import (
"bytes"
"internal/testenv"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
@ -66,7 +67,7 @@ func TestScanDir(t *testing.T) {
continue
}
t.Run(dir.Name(), func(t *testing.T) {
tagsData, err := ioutil.ReadFile(filepath.Join("testdata", dir.Name(), "tags.txt"))
tagsData, err := os.ReadFile(filepath.Join("testdata", dir.Name(), "tags.txt"))
if err != nil {
t.Fatalf("error reading tags: %v", err)
}
@ -75,7 +76,7 @@ func TestScanDir(t *testing.T) {
tags[t] = true
}
wantData, err := ioutil.ReadFile(filepath.Join("testdata", dir.Name(), "want.txt"))
wantData, err := os.ReadFile(filepath.Join("testdata", dir.Name(), "want.txt"))
if err != nil {
t.Fatalf("error reading want: %v", err)
}

View File

@ -1147,7 +1147,7 @@ var (
// goModPath returns the module path in the go.mod in dir, if any.
func goModPath(dir string) (path string) {
return goModPathCache.Do(dir, func() interface{} {
data, err := ioutil.ReadFile(filepath.Join(dir, "go.mod"))
data, err := os.ReadFile(filepath.Join(dir, "go.mod"))
if err != nil {
return ""
}
@ -1728,7 +1728,7 @@ func (p *Package) load(ctx context.Context, path string, stk *ImportStack, impor
// not work for any package that lacks a Target — such as a non-main
// package in module mode. We should probably fix that.
shlibnamefile := p.Target[:len(p.Target)-2] + ".shlibname"
shlib, err := ioutil.ReadFile(shlibnamefile)
shlib, err := os.ReadFile(shlibnamefile)
if err != nil && !os.IsNotExist(err) {
base.Fatalf("reading shlibname: %v", err)
}

View File

@ -9,7 +9,6 @@ package filelock_test
import (
"fmt"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -51,9 +50,9 @@ func mustTempFile(t *testing.T) (f *os.File, remove func()) {
t.Helper()
base := filepath.Base(t.Name())
f, err := ioutil.TempFile("", base)
f, err := os.CreateTemp("", base)
if err != nil {
t.Fatalf(`ioutil.TempFile("", %q) = %v`, base, err)
t.Fatalf(`os.CreateTemp("", %q) = %v`, base, err)
}
t.Logf("fd %d = %s", f.Fd(), f.Name())

View File

@ -10,7 +10,6 @@ package lockedfile_test
import (
"fmt"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -23,7 +22,7 @@ import (
func mustTempDir(t *testing.T) (dir string, remove func()) {
t.Helper()
dir, err := ioutil.TempDir("", filepath.Base(t.Name()))
dir, err := os.MkdirTemp("", filepath.Base(t.Name()))
if err != nil {
t.Fatal(err)
}
@ -155,8 +154,8 @@ func TestCanLockExistingFile(t *testing.T) {
defer remove()
path := filepath.Join(dir, "existing.txt")
if err := ioutil.WriteFile(path, []byte("ok"), 0777); err != nil {
t.Fatalf("ioutil.WriteFile: %v", err)
if err := os.WriteFile(path, []byte("ok"), 0777); err != nil {
t.Fatalf("os.WriteFile: %v", err)
}
f, err := lockedfile.Edit(path)
@ -201,7 +200,7 @@ func TestSpuriousEDEADLK(t *testing.T) {
}
defer b.Close()
if err := ioutil.WriteFile(filepath.Join(dir, "locked"), []byte("ok"), 0666); err != nil {
if err := os.WriteFile(filepath.Join(dir, "locked"), []byte("ok"), 0666); err != nil {
t.Fatal(err)
}

View File

@ -155,7 +155,7 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
base.Fatalf("go mod vendor: %v", err)
}
if err := ioutil.WriteFile(filepath.Join(vdir, "modules.txt"), buf.Bytes(), 0666); err != nil {
if err := os.WriteFile(filepath.Join(vdir, "modules.txt"), buf.Bytes(), 0666); err != nil {
base.Fatalf("go mod vendor: %v", err)
}
}

View File

@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"runtime"
@ -87,7 +86,7 @@ func verifyMod(mod module.Version) []error {
_, zipErr = os.Stat(zip)
}
dir, dirErr := modfetch.DownloadDir(mod)
data, err := ioutil.ReadFile(zip + "hash")
data, err := os.ReadFile(zip + "hash")
if err != nil {
if zipErr != nil && errors.Is(zipErr, fs.ErrNotExist) &&
dirErr != nil && errors.Is(dirErr, fs.ErrNotExist) {

View File

@ -9,7 +9,6 @@ import (
"context"
"fmt"
"internal/testenv"
"io/ioutil"
"log"
"os"
"os/exec"
@ -37,7 +36,7 @@ func testMain(m *testing.M) int {
return 0
}
dir, err := ioutil.TempDir("", "modconv-test-")
dir, err := os.MkdirTemp("", "modconv-test-")
if err != nil {
log.Fatal(err)
}
@ -167,7 +166,7 @@ func TestConvertLegacyConfig(t *testing.T) {
for name := range Converters {
file := filepath.Join(dir, name)
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err == nil {
f := new(modfile.File)
f.AddModuleStmt(tt.path)

View File

@ -7,7 +7,7 @@ package modconv
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
@ -42,7 +42,7 @@ func Test(t *testing.T) {
if Converters[extMap[ext]] == nil {
t.Fatalf("Converters[%q] == nil", extMap[ext])
}
data, err := ioutil.ReadFile(test)
data, err := os.ReadFile(test)
if err != nil {
t.Fatal(err)
}
@ -50,7 +50,7 @@ func Test(t *testing.T) {
if err != nil {
t.Fatal(err)
}
want, err := ioutil.ReadFile(test[:len(test)-len(ext)] + ".out")
want, err := os.ReadFile(test[:len(test)-len(ext)] + ".out")
if err != nil {
t.Error(err)
}

View File

@ -5,14 +5,13 @@
package modfetch
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestWriteDiskCache(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "go-writeCache-test-")
tmpdir, err := os.MkdirTemp("", "go-writeCache-test-")
if err != nil {
t.Fatal(err)
}

View File

@ -12,7 +12,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -189,7 +188,7 @@ func WorkDir(typ, name string) (dir, lockfile string, err error) {
}
defer unlock()
data, err := ioutil.ReadFile(dir + ".info")
data, err := os.ReadFile(dir + ".info")
info, err2 := os.Stat(dir)
if err == nil && err2 == nil && info.IsDir() {
// Info file and directory both already exist: reuse.
@ -211,7 +210,7 @@ func WorkDir(typ, name string) (dir, lockfile string, err error) {
if err := os.MkdirAll(dir, 0777); err != nil {
return "", "", err
}
if err := ioutil.WriteFile(dir+".info", []byte(key), 0666); err != nil {
if err := os.WriteFile(dir+".info", []byte(key), 0666); err != nil {
os.RemoveAll(dir)
return "", "", err
}

View File

@ -12,7 +12,6 @@ import (
"internal/testenv"
"io"
"io/fs"
"io/ioutil"
"log"
"os"
"os/exec"
@ -54,7 +53,7 @@ func testMain(m *testing.M) int {
return 0
}
dir, err := ioutil.TempDir("", "gitrepo-test-")
dir, err := os.MkdirTemp("", "gitrepo-test-")
if err != nil {
log.Fatal(err)
}

View File

@ -15,7 +15,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
@ -124,7 +123,7 @@ func main() {
}
if f[3] != "-" {
if err := ioutil.WriteFile(f[3], data, 0666); err != nil {
if err := os.WriteFile(f[3], data, 0666); err != nil {
fmt.Fprintf(os.Stderr, "?%s\n", err)
continue
}

View File

@ -10,7 +10,6 @@ import (
"internal/lazyregexp"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"sort"
@ -433,7 +432,7 @@ func (r *vcsRepo) ReadZip(rev, subdir string, maxSize int64) (zip io.ReadCloser,
if rev == "latest" {
rev = r.cmd.latest
}
f, err := ioutil.TempFile("", "go-readzip-*.zip")
f, err := os.CreateTemp("", "go-readzip-*.zip")
if err != nil {
return nil, err
}

View File

@ -11,7 +11,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path"
"sort"
@ -966,7 +965,7 @@ func (r *codeRepo) Zip(dst io.Writer, version string) error {
subdir = strings.Trim(subdir, "/")
// Spool to local file.
f, err := ioutil.TempFile("", "go-codehost-")
f, err := os.CreateTemp("", "go-codehost-")
if err != nil {
dl.Close()
return err

View File

@ -11,7 +11,6 @@ import (
"hash"
"internal/testenv"
"io"
"io/ioutil"
"log"
"os"
"reflect"
@ -38,7 +37,7 @@ func testMain(m *testing.M) int {
// code, bypass the sum database.
cfg.GOSUMDB = "off"
dir, err := ioutil.TempDir("", "gitrepo-test-")
dir, err := os.MkdirTemp("", "gitrepo-test-")
if err != nil {
log.Fatal(err)
}
@ -424,7 +423,7 @@ var codeRepoTests = []codeRepoTest{
func TestCodeRepo(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
tmpdir, err := ioutil.TempDir("", "modfetch-test-")
tmpdir, err := os.MkdirTemp("", "modfetch-test-")
if err != nil {
t.Fatal(err)
}
@ -491,9 +490,9 @@ func TestCodeRepo(t *testing.T) {
needHash := !testing.Short() && (tt.zipFileHash != "" || tt.zipSum != "")
if tt.zip != nil || tt.zipErr != "" || needHash {
f, err := ioutil.TempFile(tmpdir, tt.version+".zip.")
f, err := os.CreateTemp(tmpdir, tt.version+".zip.")
if err != nil {
t.Fatalf("ioutil.TempFile: %v", err)
t.Fatalf("os.CreateTemp: %v", err)
}
zipfile := f.Name()
defer func() {
@ -655,7 +654,7 @@ var codeRepoVersionsTests = []struct {
func TestCodeRepoVersions(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
tmpdir, err := ioutil.TempDir("", "vgo-modfetch-test-")
tmpdir, err := os.MkdirTemp("", "vgo-modfetch-test-")
if err != nil {
t.Fatal(err)
}
@ -729,7 +728,7 @@ var latestTests = []struct {
func TestLatest(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
tmpdir, err := ioutil.TempDir("", "vgo-modfetch-test-")
tmpdir, err := os.MkdirTemp("", "vgo-modfetch-test-")
if err != nil {
t.Fatal(err)
}

View File

@ -12,7 +12,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"sort"
@ -136,7 +135,7 @@ func download(ctx context.Context, mod module.Version) (dir string, err error) {
if err := os.MkdirAll(parentDir, 0777); err != nil {
return "", err
}
if err := ioutil.WriteFile(partialPath, nil, 0666); err != nil {
if err := os.WriteFile(partialPath, nil, 0666); err != nil {
return "", err
}
if err := modzip.Unzip(dir, mod, zipfile); err != nil {
@ -223,7 +222,7 @@ func downloadZip(ctx context.Context, mod module.Version, zipfile string) (err e
// contents of the file (by hashing it) before we commit it. Because the file
// is zip-compressed, we need an actual file — or at least an io.ReaderAt — to
// validate it: we can't just tee the stream as we write it.
f, err := ioutil.TempFile(filepath.Dir(zipfile), filepath.Base(renameio.Pattern(zipfile)))
f, err := os.CreateTemp(filepath.Dir(zipfile), filepath.Base(renameio.Pattern(zipfile)))
if err != nil {
return err
}

View File

@ -24,7 +24,6 @@ import (
"fmt"
"internal/testenv"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -81,7 +80,7 @@ func TestZipSums(t *testing.T) {
if *modCacheDir != "" {
cfg.BuildContext.GOPATH = *modCacheDir
} else {
tmpDir, err := ioutil.TempDir("", "TestZipSums")
tmpDir, err := os.MkdirTemp("", "TestZipSums")
if err != nil {
t.Fatal(err)
}

View File

@ -632,7 +632,7 @@ func setDefaultBuildMod() {
func convertLegacyConfig(modPath string) (from string, err error) {
for _, name := range altConfigs {
cfg := filepath.Join(modRoot, name)
data, err := ioutil.ReadFile(cfg)
data, err := os.ReadFile(cfg)
if err == nil {
convert := modconv.Converters[name]
if convert == nil {
@ -753,7 +753,7 @@ func findModulePath(dir string) (string, error) {
}
// Look for Godeps.json declaring import path.
data, _ := ioutil.ReadFile(filepath.Join(dir, "Godeps/Godeps.json"))
data, _ := os.ReadFile(filepath.Join(dir, "Godeps/Godeps.json"))
var cfg1 struct{ ImportPath string }
json.Unmarshal(data, &cfg1)
if cfg1.ImportPath != "" {
@ -761,7 +761,7 @@ func findModulePath(dir string) (string, error) {
}
// Look for vendor.json declaring import path.
data, _ = ioutil.ReadFile(filepath.Join(dir, "vendor/vendor.json"))
data, _ = os.ReadFile(filepath.Join(dir, "vendor/vendor.json"))
var cfg2 struct{ RootPath string }
json.Unmarshal(data, &cfg2)
if cfg2.RootPath != "" {
@ -813,7 +813,7 @@ var (
)
func findImportComment(file string) string {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return ""
}

View File

@ -7,7 +7,6 @@ package modload
import (
"context"
"internal/testenv"
"io/ioutil"
"log"
"os"
"path"
@ -27,7 +26,7 @@ func TestMain(m *testing.M) {
func testMain(m *testing.M) int {
cfg.GOPROXY = "direct"
dir, err := ioutil.TempDir("", "modload-test-")
dir, err := os.MkdirTemp("", "modload-test-")
if err != nil {
log.Fatal(err)
}

View File

@ -8,7 +8,7 @@ import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
@ -40,7 +40,7 @@ func readVendorList() {
vendorPkgModule = make(map[string]module.Version)
vendorVersion = make(map[string]string)
vendorMeta = make(map[module.Version]vendorMetadata)
data, err := ioutil.ReadFile(filepath.Join(ModRoot(), "vendor/modules.txt"))
data, err := os.ReadFile(filepath.Join(ModRoot(), "vendor/modules.txt"))
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
base.Fatalf("go: %s", err)

View File

@ -25,7 +25,7 @@ func Pattern(filename string) string {
return filepath.Join(filepath.Dir(filename), filepath.Base(filename)+patternSuffix)
}
// WriteFile is like ioutil.WriteFile, but first writes data to an arbitrary
// WriteFile is like os.WriteFile, but first writes data to an arbitrary
// file in the same directory as filename, then renames it atomically to the
// final name.
//
@ -67,7 +67,7 @@ func WriteToFile(filename string, data io.Reader, perm fs.FileMode) (err error)
return robustio.Rename(f.Name(), filename)
}
// ReadFile is like ioutil.ReadFile, but on Windows retries spurious errors that
// ReadFile is like os.ReadFile, but on Windows retries spurious errors that
// may occur if the file is concurrently replaced.
//
// Errors are classified heuristically and retries are bounded, so even this

View File

@ -10,7 +10,6 @@ import (
"encoding/binary"
"errors"
"internal/testenv"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
@ -30,7 +29,7 @@ func TestConcurrentReadsAndWrites(t *testing.T) {
testenv.SkipFlaky(t, 33041)
}
dir, err := ioutil.TempDir("", "renameio")
dir, err := os.MkdirTemp("", "renameio")
if err != nil {
t.Fatal(err)
}

View File

@ -8,7 +8,6 @@ package renameio
import (
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"syscall"
@ -16,7 +15,7 @@ import (
)
func TestWriteFileModeAppliesUmask(t *testing.T) {
dir, err := ioutil.TempDir("", "renameio")
dir, err := os.MkdirTemp("", "renameio")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}

View File

@ -22,7 +22,7 @@ func Rename(oldpath, newpath string) error {
return rename(oldpath, newpath)
}
// ReadFile is like ioutil.ReadFile, but on Windows retries errors that may
// ReadFile is like os.ReadFile, but on Windows retries errors that may
// occur if the file is concurrently replaced.
//
// (See golang.org/issue/31247 and golang.org/issue/32188.)

View File

@ -8,7 +8,6 @@ package robustio
import (
"errors"
"io/ioutil"
"math/rand"
"os"
"syscall"
@ -70,11 +69,11 @@ func rename(oldpath, newpath string) (err error) {
})
}
// readFile is like ioutil.ReadFile, but retries ephemeral errors.
// readFile is like os.ReadFile, but retries ephemeral errors.
func readFile(filename string) ([]byte, error) {
var b []byte
err := retry(func() (err error, mayRetry bool) {
b, err = ioutil.ReadFile(filename)
b, err = os.ReadFile(filename)
// Unlike in rename, we do not retry errFileNotFound here: it can occur
// as a spurious error, but the file may also genuinely not exist, so the

View File

@ -7,7 +7,6 @@
package robustio
import (
"io/ioutil"
"os"
)
@ -16,7 +15,7 @@ func rename(oldpath, newpath string) error {
}
func readFile(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
return os.ReadFile(filename)
}
func removeAll(path string) error {

View File

@ -884,7 +884,7 @@ func builderTest(b *work.Builder, ctx context.Context, p *load.Package) (buildAc
if !cfg.BuildN {
// writeTestmain writes _testmain.go,
// using the test description gathered in t.
if err := ioutil.WriteFile(testDir+"_testmain.go", *pmain.Internal.TestmainGo, 0666); err != nil {
if err := os.WriteFile(testDir+"_testmain.go", *pmain.Internal.TestmainGo, 0666); err != nil {
return nil, nil, nil, err
}
}
@ -1616,7 +1616,7 @@ func (c *runCache) saveOutput(a *work.Action) {
}
// See comment about two-level lookup in tryCacheWithID above.
testlog, err := ioutil.ReadFile(a.Objdir + "testlog.txt")
testlog, err := os.ReadFile(a.Objdir + "testlog.txt")
if err != nil || !bytes.HasPrefix(testlog, testlogMagic) || testlog[len(testlog)-1] != '\n' {
if cache.DebugTest {
if err != nil {

View File

@ -34,7 +34,7 @@ package txtar
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
)
@ -66,7 +66,7 @@ func Format(a *Archive) []byte {
// ParseFile parses the named file as an archive.
func ParseFile(file string) (*Archive, error) {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return nil, err
}

View File

@ -7,7 +7,6 @@ package vcs
import (
"errors"
"internal/testenv"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -208,7 +207,7 @@ func TestRepoRootForImportPath(t *testing.T) {
// Test that vcsFromDir correctly inspects a given directory and returns the right VCS and root.
func TestFromDir(t *testing.T) {
tempDir, err := ioutil.TempDir("", "vcstest")
tempDir, err := os.MkdirTemp("", "vcstest")
if err != nil {
t.Fatal(err)
}

View File

@ -7,7 +7,6 @@ package web
import (
"errors"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -16,7 +15,7 @@ import (
func TestGetFileURL(t *testing.T) {
const content = "Hello, file!\n"
f, err := ioutil.TempFile("", "web-TestGetFileURL")
f, err := os.CreateTemp("", "web-TestGetFileURL")
if err != nil {
t.Fatal(err)
}

View File

@ -14,7 +14,6 @@ import (
"debug/elf"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -253,7 +252,7 @@ func (b *Builder) Init() {
if cfg.BuildN {
b.WorkDir = "$WORK"
} else {
tmp, err := ioutil.TempDir(cfg.Getenv("GOTMPDIR"), "go-build")
tmp, err := os.MkdirTemp(cfg.Getenv("GOTMPDIR"), "go-build")
if err != nil {
base.Fatalf("go: creating work dir: %v", err)
}

View File

@ -8,7 +8,6 @@ import (
"bytes"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -170,7 +169,7 @@ func TestSharedLibName(t *testing.T) {
for _, data := range testData {
func() {
if data.rootedAt != "" {
tmpGopath, err := ioutil.TempDir("", "gopath")
tmpGopath, err := os.MkdirTemp("", "gopath")
if err != nil {
t.Fatal(err)
}
@ -238,7 +237,7 @@ func TestRespectSetgidDir(t *testing.T) {
return cmdBuf.WriteString(fmt.Sprint(a...))
}
setgiddir, err := ioutil.TempDir("", "SetGroupID")
setgiddir, err := os.MkdirTemp("", "SetGroupID")
if err != nil {
t.Fatal(err)
}
@ -258,9 +257,9 @@ func TestRespectSetgidDir(t *testing.T) {
t.Fatal(err)
}
pkgfile, err := ioutil.TempFile("", "pkgfile")
pkgfile, err := os.CreateTemp("", "pkgfile")
if err != nil {
t.Fatalf("ioutil.TempFile(\"\", \"pkgfile\"): %v", err)
t.Fatalf("os.CreateTemp(\"\", \"pkgfile\"): %v", err)
}
defer os.Remove(pkgfile.Name())
defer pkgfile.Close()

View File

@ -7,7 +7,6 @@ package work
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
@ -344,7 +343,7 @@ func (b *Builder) gccgoBuildIDFile(a *Action) (string, error) {
}
}
if err := ioutil.WriteFile(sfile, buf.Bytes(), 0666); err != nil {
if err := os.WriteFile(sfile, buf.Bytes(), 0666); err != nil {
return "", err
}

View File

@ -16,7 +16,6 @@ import (
"internal/lazyregexp"
"io"
"io/fs"
"io/ioutil"
"log"
"math/rand"
"os"
@ -94,7 +93,7 @@ func (b *Builder) Do(ctx context.Context, root *Action) {
base.Fatalf("go: refusing to write action graph to %v\n", file)
}
js := actionGraphJSON(root)
if err := ioutil.WriteFile(file, []byte(js), 0666); err != nil {
if err := os.WriteFile(file, []byte(js), 0666); err != nil {
fmt.Fprintf(os.Stderr, "go: writing action graph: %v\n", err)
base.SetExitStatus(1)
}
@ -636,7 +635,7 @@ OverlayLoop:
sfiles, gccfiles = filter(sfiles, sfiles[:0], gccfiles)
} else {
for _, sfile := range sfiles {
data, err := ioutil.ReadFile(filepath.Join(a.Package.Dir, sfile))
data, err := os.ReadFile(filepath.Join(a.Package.Dir, sfile))
if err == nil {
if bytes.HasPrefix(data, []byte("TEXT")) || bytes.Contains(data, []byte("\nTEXT")) ||
bytes.HasPrefix(data, []byte("DATA")) || bytes.Contains(data, []byte("\nDATA")) ||
@ -1471,7 +1470,7 @@ func (b *Builder) installShlibname(ctx context.Context, a *Action) error {
// TODO: BuildN
a1 := a.Deps[0]
err := ioutil.WriteFile(a.Target, []byte(filepath.Base(a1.Target)+"\n"), 0666)
err := os.WriteFile(a.Target, []byte(filepath.Base(a1.Target)+"\n"), 0666)
if err != nil {
return err
}
@ -1788,7 +1787,7 @@ func (b *Builder) writeFile(file string, text []byte) error {
if cfg.BuildN {
return nil
}
return ioutil.WriteFile(file, text, 0666)
return os.WriteFile(file, text, 0666)
}
// Install the cgo export header file, if there is one.
@ -2537,7 +2536,7 @@ func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool {
tmp := os.DevNull
if runtime.GOOS == "windows" {
f, err := ioutil.TempFile(b.WorkDir, "")
f, err := os.CreateTemp(b.WorkDir, "")
if err != nil {
return false
}
@ -2840,7 +2839,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
continue
}
src, err := ioutil.ReadFile(f)
src, err := os.ReadFile(f)
if err != nil {
return nil, nil, err
}
@ -3070,7 +3069,7 @@ func (b *Builder) swigDoIntSize(objdir string) (intsize string, err error) {
return "$INTBITS", nil
}
src := filepath.Join(b.WorkDir, "swig_intsize.go")
if err = ioutil.WriteFile(src, []byte(swigIntSizeCode), 0666); err != nil {
if err = os.WriteFile(src, []byte(swigIntSizeCode), 0666); err != nil {
return
}
srcs := []string{src}
@ -3230,7 +3229,7 @@ func passLongArgsInResponseFiles(cmd *exec.Cmd) (cleanup func()) {
return
}
tf, err := ioutil.TempFile("", "args")
tf, err := os.CreateTemp("", "args")
if err != nil {
log.Fatalf("error writing long arguments to response file: %v", err)
}

View File

@ -9,7 +9,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -426,11 +425,11 @@ func toolVerify(a *Action, b *Builder, p *load.Package, newTool string, ofile st
if err := b.run(a, p.Dir, p.ImportPath, nil, newArgs...); err != nil {
return err
}
data1, err := ioutil.ReadFile(ofile)
data1, err := os.ReadFile(ofile)
if err != nil {
return err
}
data2, err := ioutil.ReadFile(ofile + ".new")
data2, err := os.ReadFile(ofile + ".new")
if err != nil {
return err
}
@ -580,7 +579,7 @@ func pluginPath(a *Action) string {
}
fmt.Fprintf(h, "build ID: %s\n", buildID)
for _, file := range str.StringList(p.GoFiles, p.CgoFiles, p.SFiles) {
data, err := ioutil.ReadFile(filepath.Join(p.Dir, file))
data, err := os.ReadFile(filepath.Join(p.Dir, file))
if err != nil {
base.Fatalf("go: %s", err)
}

View File

@ -6,7 +6,6 @@ package work
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -271,7 +270,7 @@ func (tools gccgoToolchain) link(b *Builder, root *Action, out, importcfg string
}
readCgoFlags := func(flagsFile string) error {
flags, err := ioutil.ReadFile(flagsFile)
flags, err := os.ReadFile(flagsFile)
if err != nil {
return err
}

View File

@ -15,7 +15,6 @@ import (
"go/build"
"internal/testenv"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -220,7 +219,7 @@ func (ts *testScript) run() {
for _, f := range a.Files {
name := ts.mkabs(ts.expand(f.Name, false))
ts.check(os.MkdirAll(filepath.Dir(name), 0777))
ts.check(ioutil.WriteFile(name, f.Data, 0666))
ts.check(os.WriteFile(name, f.Data, 0666))
}
// With -v or -testwork, start log with full environment.
@ -377,19 +376,19 @@ var (
func isCaseSensitive(t *testing.T) bool {
onceCaseSensitive.Do(func() {
tmpdir, err := ioutil.TempDir("", "case-sensitive")
tmpdir, err := os.MkdirTemp("", "case-sensitive")
if err != nil {
t.Fatal("failed to create directory to determine case-sensitivity:", err)
}
defer os.RemoveAll(tmpdir)
fcap := filepath.Join(tmpdir, "FILE")
if err := ioutil.WriteFile(fcap, []byte{}, 0644); err != nil {
if err := os.WriteFile(fcap, []byte{}, 0644); err != nil {
t.Fatal("error writing file to determine case-sensitivity:", err)
}
flow := filepath.Join(tmpdir, "file")
_, err = ioutil.ReadFile(flow)
_, err = os.ReadFile(flow)
switch {
case err == nil:
caseSensitive = false
@ -450,9 +449,9 @@ func (ts *testScript) cmdAddcrlf(want simpleStatus, args []string) {
for _, file := range args {
file = ts.mkabs(file)
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
ts.check(err)
ts.check(ioutil.WriteFile(file, bytes.ReplaceAll(data, []byte("\n"), []byte("\r\n")), 0666))
ts.check(os.WriteFile(file, bytes.ReplaceAll(data, []byte("\n"), []byte("\r\n")), 0666))
}
}
@ -557,12 +556,12 @@ func (ts *testScript) doCmdCmp(args []string, env, quiet bool) {
} else if name1 == "stderr" {
text1 = ts.stderr
} else {
data, err := ioutil.ReadFile(ts.mkabs(name1))
data, err := os.ReadFile(ts.mkabs(name1))
ts.check(err)
text1 = string(data)
}
data, err := ioutil.ReadFile(ts.mkabs(name2))
data, err := os.ReadFile(ts.mkabs(name2))
ts.check(err)
text2 = string(data)
@ -614,14 +613,14 @@ func (ts *testScript) cmdCp(want simpleStatus, args []string) {
info, err := os.Stat(src)
ts.check(err)
mode = info.Mode() & 0777
data, err = ioutil.ReadFile(src)
data, err = os.ReadFile(src)
ts.check(err)
}
targ := dst
if dstDir {
targ = filepath.Join(dst, filepath.Base(src))
}
err := ioutil.WriteFile(targ, data, mode)
err := os.WriteFile(targ, data, mode)
switch want {
case failure:
if err == nil {
@ -897,7 +896,7 @@ func scriptMatch(ts *testScript, want simpleStatus, args []string, text, name st
isGrep := name == "grep"
if isGrep {
name = args[1] // for error messages
data, err := ioutil.ReadFile(ts.mkabs(args[1]))
data, err := os.ReadFile(ts.mkabs(args[1]))
ts.check(err)
text = string(data)
}

View File

@ -23,7 +23,6 @@ import (
"flag"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"os/exec"
@ -58,7 +57,7 @@ func main() {
log.SetFlags(0)
var err error
tmpdir, err = ioutil.TempDir("", "addmod-")
tmpdir, err = os.MkdirTemp("", "addmod-")
if err != nil {
log.Fatal(err)
}
@ -82,7 +81,7 @@ func main() {
exitCode := 0
for _, arg := range flag.Args() {
if err := ioutil.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte("module m\n"), 0666); err != nil {
if err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte("module m\n"), 0666); err != nil {
fatalf("%v", err)
}
run(goCmd, "get", "-d", arg)
@ -98,13 +97,13 @@ func main() {
continue
}
path, vers, dir := f[0], f[1], f[2]
mod, err := ioutil.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".mod"))
mod, err := os.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".mod"))
if err != nil {
log.Printf("%s: %v", arg, err)
exitCode = 1
continue
}
info, err := ioutil.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".info"))
info, err := os.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".info"))
if err != nil {
log.Printf("%s: %v", arg, err)
exitCode = 1
@ -128,7 +127,7 @@ func main() {
}
name := info.Name()
if name == "go.mod" || strings.HasSuffix(name, ".go") {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
@ -144,7 +143,7 @@ func main() {
data := txtar.Format(a)
target := filepath.Join("mod", strings.ReplaceAll(path, "/", "_")+"_"+vers+".txt")
if err := ioutil.WriteFile(target, data, 0666); err != nil {
if err := os.WriteFile(target, data, 0666); err != nil {
log.Printf("%s: %v", arg, err)
exitCode = 1
continue

View File

@ -18,7 +18,6 @@ import (
"flag"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -63,7 +62,7 @@ func main() {
if !info.Type().IsRegular() {
return nil
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
}

View File

@ -81,7 +81,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
)
@ -100,7 +99,7 @@ func truncateLike(t, p time.Time) time.Time {
func main() {
var t1 time.Time
b1, err := ioutil.ReadFile(os.Args[1])
b1, err := os.ReadFile(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
@ -111,7 +110,7 @@ func main() {
}
var t2 time.Time
b2, err := ioutil.ReadFile(os.Args[2])
b2, err := os.ReadFile(os.Args[2])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)

View File

@ -121,7 +121,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
@ -131,7 +130,7 @@ import (
func main() {
exe := os.Args[1]
data, err := ioutil.ReadFile(exe)
data, err := os.ReadFile(exe)
if err != nil {
log.Fatal(err)
}

View File

@ -54,7 +54,6 @@ func Test(t *testing.T) {}
package main
import (
"io/ioutil"
"log"
"os"
"strings"
@ -62,13 +61,13 @@ import (
func main() {
log.SetFlags(0)
b, err := ioutil.ReadFile(os.Args[1])
b, err := os.ReadFile(os.Args[1])
if err != nil {
log.Fatal(err)
}
s := strings.ReplaceAll(string(b), "p.go:4:2:", "p.go:4:")
s = strings.ReplaceAll(s, "p1.go:6:2:", "p1.go:6:")
ioutil.WriteFile(os.Args[1], []byte(s), 0644)
os.WriteFile(os.Args[1], []byte(s), 0644)
if err != nil {
log.Fatal(err)
}

View File

@ -45,7 +45,6 @@ package main
import (
"bytes"
"io/ioutil"
"log"
"os"
)
@ -57,11 +56,11 @@ func main() {
base := []byte(os.Args[1])
path := os.Args[2]
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(path, bytes.ReplaceAll(data, base, append(base, "XXX"...)), 0644)
err = os.WriteFile(path, bytes.ReplaceAll(data, base, append(base, "XXX"...)), 0644)
if err != nil {
log.Fatal(err)
}

View File

@ -25,7 +25,6 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
@ -45,7 +44,7 @@ func main() {
// don't need to clean the cache or synchronize closing files after each
// iteration.
func run() (err error) {
tmpDir, err := ioutil.TempDir("", "")
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return err
}

View File

@ -69,7 +69,6 @@ package main
import (
"bytes"
"encoding/hex"
"io/ioutil"
"log"
"os"
@ -77,7 +76,7 @@ import (
)
func main() {
b, err := ioutil.ReadFile(os.Args[0])
b, err := os.ReadFile(os.Args[0])
if err != nil {
log.Fatal(err)
}

View File

@ -51,26 +51,25 @@ bar
package foo_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestWriteTmp(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
err = ioutil.WriteFile(filepath.Join(dir, "x"), nil, 0666)
err = os.WriteFile(filepath.Join(dir, "x"), nil, 0666)
if err != nil {
t.Fatal(err)
}
}
func TestReadTestdata(t *testing.T) {
_, err := ioutil.ReadFile("testdata/foo.txt")
_, err := os.ReadFile("testdata/foo.txt")
if err != nil {
t.Fatal(err)
}

View File

@ -1,7 +1,7 @@
[short] skip
# Ensure that the target of 'go build -o' can be an existing, empty file so that
# its name can be reserved using ioutil.TempFile or the 'mktemp` command.
# its name can be reserved using os.CreateTemp or the 'mktemp` command.
go build -o empty-file$GOEXE main.go

View File

@ -12,7 +12,6 @@ package x
import (
"os"
"path/filepath"
"io/ioutil"
"regexp"
"testing"
)
@ -23,7 +22,7 @@ func Test(t *testing.T) {
t.Fatal(err)
}
testmainPath := filepath.Join(filepath.Dir(exePath), "_testmain.go")
source, err := ioutil.ReadFile(testmainPath)
source, err := os.ReadFile(testmainPath)
if err != nil {
t.Fatal(err)
}

View File

@ -29,7 +29,6 @@ go 1.16
package main
import (
"io/ioutil"
"encoding/json"
"fmt"
"os"
@ -37,7 +36,7 @@ import (
)
func main() {
b, err := ioutil.ReadFile(os.Args[1])
b, err := os.ReadFile(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
@ -59,7 +58,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
)
@ -67,7 +65,7 @@ import (
func main() {
var t1 time.Time
b1, err := ioutil.ReadFile(os.Args[1])
b1, err := os.ReadFile(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
@ -78,7 +76,7 @@ func main() {
}
var t2 time.Time
b2, err := ioutil.ReadFile(os.Args[2])
b2, err := os.ReadFile(os.Args[2])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)

View File

@ -15,7 +15,6 @@ import (
"go/token"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -137,7 +136,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
if err != nil {
return err
}
err = ioutil.WriteFile(filename, res, perm)
err = os.WriteFile(filename, res, perm)
if err != nil {
os.Rename(bakname, filename)
return err
@ -278,7 +277,7 @@ const chmodSupported = runtime.GOOS != "windows"
// the chosen file name.
func backupFile(filename string, data []byte, perm fs.FileMode) (string, error) {
// create backup file
f, err := ioutil.TempFile(filepath.Dir(filename), filepath.Base(filename))
f, err := os.CreateTemp(filepath.Dir(filename), filepath.Base(filename))
if err != nil {
return "", err
}

View File

@ -7,7 +7,6 @@ package main
import (
"bytes"
"flag"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -93,7 +92,7 @@ func runTest(t *testing.T, in, out string) {
return
}
expected, err := ioutil.ReadFile(out)
expected, err := os.ReadFile(out)
if err != nil {
t.Error(err)
return
@ -102,7 +101,7 @@ func runTest(t *testing.T, in, out string) {
if got := buf.Bytes(); !bytes.Equal(got, expected) {
if *update {
if in != out {
if err := ioutil.WriteFile(out, got, 0666); err != nil {
if err := os.WriteFile(out, got, 0666); err != nil {
t.Error(err)
}
return
@ -116,7 +115,7 @@ func runTest(t *testing.T, in, out string) {
if err == nil {
t.Errorf("%s", d)
}
if err := ioutil.WriteFile(in+".gofmt", got, 0666); err != nil {
if err := os.WriteFile(in+".gofmt", got, 0666); err != nil {
t.Error(err)
}
}
@ -157,7 +156,7 @@ func TestCRLF(t *testing.T) {
const input = "testdata/crlf.input" // must contain CR/LF's
const golden = "testdata/crlf.golden" // must not contain any CR's
data, err := ioutil.ReadFile(input)
data, err := os.ReadFile(input)
if err != nil {
t.Error(err)
}
@ -165,7 +164,7 @@ func TestCRLF(t *testing.T) {
t.Errorf("%s contains no CR/LF's", input)
}
data, err = ioutil.ReadFile(golden)
data, err = os.ReadFile(golden)
if err != nil {
t.Error(err)
}
@ -175,7 +174,7 @@ func TestCRLF(t *testing.T) {
}
func TestBackupFile(t *testing.T) {
dir, err := ioutil.TempDir("", "gofmt_test")
dir, err := os.MkdirTemp("", "gofmt_test")
if err != nil {
t.Fatal(err)
}

View File

@ -8,7 +8,6 @@ import (
"fmt"
"internal/obscuretestdata"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -31,7 +30,7 @@ func testMain(m *testing.M) int {
return 0
}
tmpDir, err := ioutil.TempDir("", "TestNM")
tmpDir, err := os.MkdirTemp("", "TestNM")
if err != nil {
fmt.Println("TempDir failed:", err)
return 2
@ -88,7 +87,7 @@ func TestNonGoExecs(t *testing.T) {
func testGoExec(t *testing.T, iscgo, isexternallinker bool) {
t.Parallel()
tmpdir, err := ioutil.TempDir("", "TestGoExec")
tmpdir, err := os.MkdirTemp("", "TestGoExec")
if err != nil {
t.Fatal(err)
}
@ -222,7 +221,7 @@ func TestGoExec(t *testing.T) {
func testGoLib(t *testing.T, iscgo bool) {
t.Parallel()
tmpdir, err := ioutil.TempDir("", "TestGoLib")
tmpdir, err := os.MkdirTemp("", "TestGoLib")
if err != nil {
t.Fatal(err)
}
@ -245,7 +244,7 @@ func testGoLib(t *testing.T, iscgo bool) {
err = e
}
if err == nil {
err = ioutil.WriteFile(filepath.Join(libpath, "go.mod"), []byte("module mylib\n"), 0666)
err = os.WriteFile(filepath.Join(libpath, "go.mod"), []byte("module mylib\n"), 0666)
}
if err != nil {
t.Fatal(err)

View File

@ -10,7 +10,6 @@ import (
"fmt"
"go/build"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -39,7 +38,7 @@ func TestMain(m *testing.M) {
func buildObjdump() error {
var err error
tmp, err = ioutil.TempDir("", "TestObjDump")
tmp, err = os.MkdirTemp("", "TestObjDump")
if err != nil {
return fmt.Errorf("TempDir failed: %v", err)
}
@ -320,7 +319,7 @@ func TestGoobjFileNumber(t *testing.T) {
t.Parallel()
tmpdir, err := ioutil.TempDir("", "TestGoobjFileNumber")
tmpdir, err := os.MkdirTemp("", "TestGoobjFileNumber")
if err != nil {
t.Fatal(err)
}

View File

@ -12,7 +12,6 @@ import (
"internal/testenv"
"io"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -22,7 +21,7 @@ import (
// tmpDir creates a temporary directory and returns its name.
func tmpDir(t *testing.T) string {
name, err := ioutil.TempDir("", "pack")
name, err := os.MkdirTemp("", "pack")
if err != nil {
t.Fatal(err)
}
@ -158,7 +157,7 @@ func TestExtract(t *testing.T) {
ar = openArchive(name, os.O_RDONLY, []string{goodbyeFile.name})
ar.scan(ar.extractContents)
ar.a.File().Close()
data, err := ioutil.ReadFile(goodbyeFile.name)
data, err := os.ReadFile(goodbyeFile.name)
if err != nil {
t.Fatal(err)
}
@ -183,7 +182,7 @@ func TestHello(t *testing.T) {
println("hello world")
}
`
err := ioutil.WriteFile(hello, []byte(prog), 0666)
err := os.WriteFile(hello, []byte(prog), 0666)
if err != nil {
t.Fatal(err)
}
@ -251,7 +250,7 @@ func TestLargeDefs(t *testing.T) {
println("ok")
}
`
err = ioutil.WriteFile(main, []byte(prog), 0666)
err = os.WriteFile(main, []byte(prog), 0666)
if err != nil {
t.Fatal(err)
}
@ -281,13 +280,13 @@ func TestIssue21703(t *testing.T) {
defer os.RemoveAll(dir)
const aSrc = `package a; const X = "\n!\n"`
err := ioutil.WriteFile(filepath.Join(dir, "a.go"), []byte(aSrc), 0666)
err := os.WriteFile(filepath.Join(dir, "a.go"), []byte(aSrc), 0666)
if err != nil {
t.Fatal(err)
}
const bSrc = `package b; import _ "a"`
err = ioutil.WriteFile(filepath.Join(dir, "b.go"), []byte(bSrc), 0666)
err = os.WriteFile(filepath.Join(dir, "b.go"), []byte(bSrc), 0666)
if err != nil {
t.Fatal(err)
}

View File

@ -12,7 +12,7 @@ import (
"flag"
"fmt"
traceparser "internal/trace"
"io/ioutil"
"os"
"reflect"
"runtime/debug"
"runtime/trace"
@ -386,7 +386,7 @@ func saveTrace(buf *bytes.Buffer, name string) {
if !*saveTraces {
return
}
if err := ioutil.WriteFile(name+".trace", buf.Bytes(), 0600); err != nil {
if err := os.WriteFile(name+".trace", buf.Bytes(), 0600); err != nil {
panic(fmt.Errorf("failed to write trace file: %v", err))
}
}

View File

@ -11,7 +11,6 @@ import (
"fmt"
"internal/trace"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
@ -294,7 +293,7 @@ func serveSVGProfile(prof func(w io.Writer, r *http.Request) error) http.Handler
return
}
blockf, err := ioutil.TempFile("", "block")
blockf, err := os.CreateTemp("", "block")
if err != nil {
http.Error(w, fmt.Sprintf("failed to create temp file: %v", err), http.StatusInternalServerError)
return

View File

@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"internal/testenv"
"io/ioutil"
"log"
"os"
"os/exec"
@ -32,7 +31,7 @@ func TestMain(m *testing.M) {
}
func testMain(m *testing.M) int {
dir, err := ioutil.TempDir("", "vet_test")
dir, err := os.MkdirTemp("", "vet_test")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
@ -345,7 +344,7 @@ var (
func wantedErrors(file, short string) (errs []wantedError) {
cache := make(map[string]*regexp.Regexp)
src, err := ioutil.ReadFile(file)
src, err := os.ReadFile(file)
if err != nil {
log.Fatal(err)
}

View File

@ -9,7 +9,7 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"testing"
)
@ -22,7 +22,7 @@ func mustDecodeHex(s string) []byte {
}
func mustLoadFile(f string) []byte {
b, err := ioutil.ReadFile(f)
b, err := os.ReadFile(f)
if err != nil {
panic(err)
}

View File

@ -10,8 +10,8 @@ import (
"fmt"
"internal/testenv"
"io"
"io/ioutil"
"math/rand"
"os"
"reflect"
"runtime/debug"
"sync"
@ -387,7 +387,7 @@ func TestDeflateInflateString(t *testing.T) {
t.Skip("skipping in short mode")
}
for _, test := range deflateInflateStringTests {
gold, err := ioutil.ReadFile(test.filename)
gold, err := os.ReadFile(test.filename)
if err != nil {
t.Error(err)
}
@ -685,7 +685,7 @@ func (w *failWriter) Write(b []byte) (int, error) {
func TestWriterPersistentError(t *testing.T) {
t.Parallel()
d, err := ioutil.ReadFile("../../testdata/Isaac.Newton-Opticks.txt")
d, err := os.ReadFile("../../testdata/Isaac.Newton-Opticks.txt")
if err != nil {
t.Fatalf("ReadFile: %v", err)
}

View File

@ -8,7 +8,6 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -38,7 +37,7 @@ func TestBlockHuff(t *testing.T) {
}
func testBlockHuff(t *testing.T, in, out string) {
all, err := ioutil.ReadFile(in)
all, err := os.ReadFile(in)
if err != nil {
t.Error(err)
return
@ -49,7 +48,7 @@ func testBlockHuff(t *testing.T, in, out string) {
bw.flush()
got := buf.Bytes()
want, err := ioutil.ReadFile(out)
want, err := os.ReadFile(out)
if err != nil && !*update {
t.Error(err)
return
@ -60,7 +59,7 @@ func testBlockHuff(t *testing.T, in, out string) {
if *update {
if in != out {
t.Logf("Updating %q", out)
if err := ioutil.WriteFile(out, got, 0666); err != nil {
if err := os.WriteFile(out, got, 0666); err != nil {
t.Error(err)
}
return
@ -70,7 +69,7 @@ func testBlockHuff(t *testing.T, in, out string) {
}
t.Errorf("%q != %q (see %q)", in, out, in+".got")
if err := ioutil.WriteFile(in+".got", got, 0666); err != nil {
if err := os.WriteFile(in+".got", got, 0666); err != nil {
t.Error(err)
}
return
@ -85,7 +84,7 @@ func testBlockHuff(t *testing.T, in, out string) {
got = buf.Bytes()
if !bytes.Equal(got, want) {
t.Errorf("after reset %q != %q (see %q)", in, out, in+".reset.got")
if err := ioutil.WriteFile(in+".reset.got", got, 0666); err != nil {
if err := os.WriteFile(in+".reset.got", got, 0666); err != nil {
t.Error(err)
}
return
@ -186,7 +185,7 @@ func testBlock(t *testing.T, test huffTest, ttype string) {
if *update {
if test.input != "" {
t.Logf("Updating %q", test.want)
input, err := ioutil.ReadFile(test.input)
input, err := os.ReadFile(test.input)
if err != nil {
t.Error(err)
return
@ -216,12 +215,12 @@ func testBlock(t *testing.T, test huffTest, ttype string) {
if test.input != "" {
t.Logf("Testing %q", test.want)
input, err := ioutil.ReadFile(test.input)
input, err := os.ReadFile(test.input)
if err != nil {
t.Error(err)
return
}
want, err := ioutil.ReadFile(test.want)
want, err := os.ReadFile(test.want)
if err != nil {
t.Error(err)
return
@ -233,7 +232,7 @@ func testBlock(t *testing.T, test huffTest, ttype string) {
got := buf.Bytes()
if !bytes.Equal(got, want) {
t.Errorf("writeBlock did not yield expected result for file %q with input. See %q", test.want, test.want+".got")
if err := ioutil.WriteFile(test.want+".got", got, 0666); err != nil {
if err := os.WriteFile(test.want+".got", got, 0666); err != nil {
t.Error(err)
}
}
@ -247,7 +246,7 @@ func testBlock(t *testing.T, test huffTest, ttype string) {
got = buf.Bytes()
if !bytes.Equal(got, want) {
t.Errorf("reset: writeBlock did not yield expected result for file %q with input. See %q", test.want, test.want+".reset.got")
if err := ioutil.WriteFile(test.want+".reset.got", got, 0666); err != nil {
if err := os.WriteFile(test.want+".reset.got", got, 0666); err != nil {
t.Error(err)
}
return
@ -256,7 +255,7 @@ func testBlock(t *testing.T, test huffTest, ttype string) {
testWriterEOF(t, "wb", test, true)
}
t.Logf("Testing %q", test.wantNoInput)
wantNI, err := ioutil.ReadFile(test.wantNoInput)
wantNI, err := os.ReadFile(test.wantNoInput)
if err != nil {
t.Error(err)
return
@ -268,7 +267,7 @@ func testBlock(t *testing.T, test huffTest, ttype string) {
got := buf.Bytes()
if !bytes.Equal(got, wantNI) {
t.Errorf("writeBlock did not yield expected result for file %q with input. See %q", test.wantNoInput, test.wantNoInput+".got")
if err := ioutil.WriteFile(test.want+".got", got, 0666); err != nil {
if err := os.WriteFile(test.want+".got", got, 0666); err != nil {
t.Error(err)
}
} else if got[0]&1 == 1 {
@ -286,7 +285,7 @@ func testBlock(t *testing.T, test huffTest, ttype string) {
got = buf.Bytes()
if !bytes.Equal(got, wantNI) {
t.Errorf("reset: writeBlock did not yield expected result for file %q without input. See %q", test.want, test.want+".reset.got")
if err := ioutil.WriteFile(test.want+".reset.got", got, 0666); err != nil {
if err := os.WriteFile(test.want+".reset.got", got, 0666); err != nil {
t.Error(err)
}
return
@ -325,7 +324,7 @@ func testWriterEOF(t *testing.T, ttype string, test huffTest, useInput bool) {
var input []byte
if useInput {
var err error
input, err = ioutil.ReadFile(test.input)
input, err = os.ReadFile(test.input)
if err != nil {
t.Error(err)
return

View File

@ -7,7 +7,7 @@ package flate
import (
"bytes"
"io"
"io/ioutil"
"os"
"runtime"
"strings"
"testing"
@ -80,7 +80,7 @@ var sizes = []struct {
func doBench(b *testing.B, f func(b *testing.B, buf []byte, level, n int)) {
for _, suite := range suites {
buf, err := ioutil.ReadFile(suite.file)
buf, err := os.ReadFile(suite.file)
if err != nil {
b.Fatal(err)
}

View File

@ -8,8 +8,8 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"runtime"
"strconv"
"strings"
@ -218,7 +218,7 @@ func TestNoLongerSavingPriorExpansions(t *testing.T) {
}
func BenchmarkDecoder(b *testing.B) {
buf, err := ioutil.ReadFile("../testdata/e.txt")
buf, err := os.ReadFile("../testdata/e.txt")
if err != nil {
b.Fatal(err)
}

View File

@ -8,7 +8,6 @@ import (
"fmt"
"internal/testenv"
"io"
"io/ioutil"
"math"
"os"
"runtime"
@ -125,7 +124,7 @@ func TestSmallLitWidth(t *testing.T) {
}
func BenchmarkEncoder(b *testing.B) {
buf, err := ioutil.ReadFile("../testdata/e.txt")
buf, err := os.ReadFile("../testdata/e.txt")
if err != nil {
b.Fatal(err)
}

View File

@ -9,7 +9,6 @@ import (
"fmt"
"internal/testenv"
"io"
"io/ioutil"
"os"
"testing"
)
@ -95,7 +94,7 @@ func testFileLevelDictReset(t *testing.T, fn string, level int, dict []byte) {
var b0 []byte
var err error
if fn != "" {
b0, err = ioutil.ReadFile(fn)
b0, err = os.ReadFile(fn)
if err != nil {
t.Errorf("%s (level=%d): %v", fn, level, err)
return

View File

@ -15,8 +15,8 @@ import (
"bytes"
"flag"
"go/format"
"io/ioutil"
"log"
"os"
"strings"
"text/template"
)
@ -37,7 +37,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(*filename, data, 0644)
err = os.WriteFile(*filename, data, 0644)
if err != nil {
log.Fatal(err)
}

View File

@ -13,7 +13,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
@ -224,7 +223,7 @@ func parseTestData(r io.Reader) (flows [][]byte, err error) {
// tempFile creates a temp file containing contents and returns its path.
func tempFile(contents string) string {
file, err := ioutil.TempFile("", "go-tls-test")
file, err := os.CreateTemp("", "go-tls-test")
if err != nil {
panic("failed to create temp file: " + err.Error())
}

View File

@ -7,7 +7,6 @@ package tls
import (
"bytes"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -77,7 +76,7 @@ func main() { tls.Dial("", "", nil) }
exeFile := filepath.Join(tmpDir, "x.exe")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ioutil.WriteFile(goFile, []byte(tt.program), 0644); err != nil {
if err := os.WriteFile(goFile, []byte(tt.program), 0644); err != nil {
t.Fatal(err)
}
os.Remove(exeFile)

View File

@ -22,8 +22,8 @@ import (
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
)
@ -222,11 +222,11 @@ func (d *Dialer) DialContext(ctx context.Context, network, addr string) (net.Con
// form a certificate chain. On successful return, Certificate.Leaf will
// be nil because the parsed form of the certificate is not retained.
func LoadX509KeyPair(certFile, keyFile string) (Certificate, error) {
certPEMBlock, err := ioutil.ReadFile(certFile)
certPEMBlock, err := os.ReadFile(certFile)
if err != nil {
return Certificate{}, err
}
keyPEMBlock, err := ioutil.ReadFile(keyFile)
keyPEMBlock, err := os.ReadFile(keyFile)
if err != nil {
return Certificate{}, err
}

View File

@ -14,7 +14,6 @@ import (
"encoding/hex"
"encoding/pem"
"fmt"
"io/ioutil"
"math/big"
"net"
"net/url"
@ -2005,7 +2004,7 @@ func TestConstraintCases(t *testing.T) {
}
func writePEMsToTempFile(certs []*Certificate) *os.File {
file, err := ioutil.TempFile("", "name_constraints_test")
file, err := os.CreateTemp("", "name_constraints_test")
if err != nil {
panic("cannot create tempfile")
}

View File

@ -27,9 +27,9 @@ import (
"fmt"
"go/format"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"sort"
"strings"
@ -155,7 +155,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(*output, source, 0644); err != nil {
if err := os.WriteFile(*output, source, 0644); err != nil {
log.Fatal(err)
}
}

View File

@ -7,7 +7,6 @@
package x509
import (
"io/ioutil"
"os"
)
@ -24,7 +23,7 @@ func loadSystemRoots() (*CertPool, error) {
roots := NewCertPool()
var bestErr error
for _, file := range certFiles {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err == nil {
roots.AppendCertsFromPEM(data)
return roots, nil

View File

@ -40,7 +40,7 @@ func loadSystemRoots() (*CertPool, error) {
var firstErr error
for _, file := range files {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err == nil {
roots.AppendCertsFromPEM(data)
break
@ -68,7 +68,7 @@ func loadSystemRoots() (*CertPool, error) {
continue
}
for _, fi := range fis {
data, err := ioutil.ReadFile(directory + "/" + fi.Name())
data, err := os.ReadFile(directory + "/" + fi.Name())
if err == nil {
roots.AppendCertsFromPEM(data)
}

View File

@ -9,7 +9,6 @@ package x509
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -147,7 +146,7 @@ func TestLoadSystemCertsLoadColonSeparatedDirs(t *testing.T) {
os.Setenv(certFileEnv, origFile)
}()
tmpDir, err := ioutil.TempDir(os.TempDir(), "x509-issue35325")
tmpDir, err := os.MkdirTemp(os.TempDir(), "x509-issue35325")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
@ -166,7 +165,7 @@ func TestLoadSystemCertsLoadColonSeparatedDirs(t *testing.T) {
t.Fatalf("Failed to create certificate dir: %v", err)
}
certOutFile := filepath.Join(certDir, "cert.crt")
if err := ioutil.WriteFile(certOutFile, []byte(certPEM), 0655); err != nil {
if err := os.WriteFile(certOutFile, []byte(certPEM), 0655); err != nil {
t.Fatalf("Failed to write certificate to file: %v", err)
}
certDirs = append(certDirs, certDir)

Some files were not shown because too many files have changed in this diff Show More