tools: replace references to obsolete package ioutils

ioutil defines 7 functions. 6 of these are replaced by
functions in io or os with the same signature.
ReadDir is deprecated, but the suggested replacement has a different
signature.

These changes were generated by a program, with some manual adjutments.
The program replaces ReadDir with a call to a function named ioutilReadDir
that has the same signature. The code for this function
is added to files if necessary. The program replaces all the others
with their new versions. The program removes the 'io/ioutil' import
and adds, as necessary, 'os', 'io', and 'io/fs', the latter being
needed for the signature of ioutilReadDir.

The automatic process fails in a few ways:
1. ReadFile occurs only in a comment but the program adds an unneeded import.
2. ioutilReadDir is added to more than one file in the same package
Both of these could be viewed as bugs and fixed by looking harder.

After manual adjustment, two tests failed:
1. gopls/internal/lsp/regtesg/mis:TestGenerateProgress. The reason
	 was a use of ioutil in a txtar constant. The calls were changed,
	 but the code is not smart enough to change the import inside the
	 string constant. (Or it's not smart enough not to change the
	 contents of a string.)
2. gopls/internal/lsp/analysis/deprecated, which wants to see a use
	 of ioutil

These tests were adjused by hand, and all tests (-short) pass.

Change-Id: If9efe40bbb0edda36173d9a88afaf71245db8e79
Reviewed-on: https://go-review.googlesource.com/c/tools/+/527675
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Peter Weinberger <pjw@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Peter Weinbergr 2023-09-12 10:11:42 -04:00 коммит произвёл Peter Weinberger
Родитель 0b3914d335
Коммит 559c4300da
117 изменённых файлов: 389 добавлений и 346 удалений

Просмотреть файл

@ -16,7 +16,6 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
@ -41,7 +40,7 @@ func main() {
path := os.Args[1]
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return

Просмотреть файл

@ -79,7 +79,6 @@ import (
"go/printer"
"go/token"
"go/types"
"io/ioutil"
"log"
"os"
"strconv"
@ -149,7 +148,7 @@ func main() {
log.Fatal(err)
}
if *outputFile != "" {
err := ioutil.WriteFile(*outputFile, code, 0666)
err := os.WriteFile(*outputFile, code, 0666)
if err != nil {
log.Fatal(err)
}

Просмотреть файл

@ -6,7 +6,6 @@ package main
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"runtime"
@ -18,7 +17,7 @@ import (
func TestBundle(t *testing.T) { packagestest.TestAll(t, testBundle) }
func testBundle(t *testing.T, x packagestest.Exporter) {
load := func(name string) string {
data, err := ioutil.ReadFile(name)
data, err := os.ReadFile(name)
if err != nil {
t.Fatal(err)
}
@ -53,7 +52,7 @@ func testBundle(t *testing.T, x packagestest.Exporter) {
if got, want := string(out), load("testdata/out.golden"); got != want {
t.Errorf("-- got --\n%s\n-- want --\n%s\n-- diff --", got, want)
if err := ioutil.WriteFile("testdata/out.got", out, 0644); err != nil {
if err := os.WriteFile("testdata/out.got", out, 0644); err != nil {
t.Fatal(err)
}
t.Log(diff("testdata/out.golden", "testdata/out.got"))

Просмотреть файл

@ -81,7 +81,6 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -388,7 +387,7 @@ func (c compile) run(name string, count int) error {
opath := pkg.Dir + "/_compilebench_.o"
if *flagObj {
// TODO(josharian): object files are big; just read enough to find what we seek.
data, err := ioutil.ReadFile(opath)
data, err := os.ReadFile(opath)
if err != nil {
log.Print(err)
}
@ -498,7 +497,7 @@ func runBuildCmd(name string, count int, dir, tool string, args []string) error
haveAllocs, haveRSS := false, false
var allocs, allocbytes, rssbytes int64
if *flagAlloc || *flagMemprofile != "" {
out, err := ioutil.ReadFile(dir + "/_compilebench_.memprof")
out, err := os.ReadFile(dir + "/_compilebench_.memprof")
if err != nil {
log.Print("cannot find memory profile after compilation")
}
@ -531,7 +530,7 @@ func runBuildCmd(name string, count int, dir, tool string, args []string) error
if *flagCount != 1 {
outpath = fmt.Sprintf("%s_%d", outpath, count)
}
if err := ioutil.WriteFile(outpath, out, 0666); err != nil {
if err := os.WriteFile(outpath, out, 0666); err != nil {
log.Print(err)
}
}
@ -539,7 +538,7 @@ func runBuildCmd(name string, count int, dir, tool string, args []string) error
}
if *flagCpuprofile != "" {
out, err := ioutil.ReadFile(dir + "/_compilebench_.cpuprof")
out, err := os.ReadFile(dir + "/_compilebench_.cpuprof")
if err != nil {
log.Print(err)
}
@ -547,7 +546,7 @@ func runBuildCmd(name string, count int, dir, tool string, args []string) error
if *flagCount != 1 {
outpath = fmt.Sprintf("%s_%d", outpath, count)
}
if err := ioutil.WriteFile(outpath, out, 0666); err != nil {
if err := os.WriteFile(outpath, out, 0666); err != nil {
log.Print(err)
}
os.Remove(dir + "/_compilebench_.cpuprof")

Просмотреть файл

@ -15,7 +15,6 @@ import (
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -77,7 +76,7 @@ func doMain() error {
if err != nil {
return err
}
template, err := ioutil.ReadFile(tAbs)
template, err := os.ReadFile(tAbs)
if err != nil {
return err
}

Просмотреть файл

@ -25,7 +25,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -51,7 +50,7 @@ func dirWriter(dir string) func([]byte) error {
if err := os.MkdirAll(dir, 0777); err != nil {
return err
}
if err := ioutil.WriteFile(name, b, 0666); err != nil {
if err := os.WriteFile(name, b, 0666); err != nil {
os.Remove(name)
return err
}
@ -98,14 +97,14 @@ func convert(inputArgs []string, outputArg string) error {
output = dirWriter(outputArg)
} else {
output = func(b []byte) error {
return ioutil.WriteFile(outputArg, b, 0666)
return os.WriteFile(outputArg, b, 0666)
}
}
}
}
for _, f := range input {
b, err := ioutil.ReadAll(f)
b, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("unable to read input: %s", err)
}

Просмотреть файл

@ -5,7 +5,6 @@
package main
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -118,9 +117,9 @@ func TestFile2Fuzz(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tmp, err := ioutil.TempDir(os.TempDir(), "file2fuzz")
tmp, err := os.MkdirTemp(os.TempDir(), "file2fuzz")
if err != nil {
t.Fatalf("ioutil.TempDir failed: %s", err)
t.Fatalf("os.MkdirTemp failed: %s", err)
}
defer os.RemoveAll(tmp)
for _, f := range tc.inputFiles {
@ -129,7 +128,7 @@ func TestFile2Fuzz(t *testing.T) {
t.Fatalf("failed to create test directory: %s", err)
}
} else {
if err := ioutil.WriteFile(filepath.Join(tmp, f.name), []byte(f.content), 0666); err != nil {
if err := os.WriteFile(filepath.Join(tmp, f.name), []byte(f.content), 0666); err != nil {
t.Fatalf("failed to create test input file: %s", err)
}
}
@ -146,7 +145,7 @@ func TestFile2Fuzz(t *testing.T) {
}
for _, f := range tc.expectedFiles {
c, err := ioutil.ReadFile(filepath.Join(tmp, f.name))
c, err := os.ReadFile(filepath.Join(tmp, f.name))
if err != nil {
t.Fatalf("failed to read expected output file %q: %s", f.name, err)
}

Просмотреть файл

@ -76,7 +76,6 @@ import (
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"path"
@ -100,7 +99,7 @@ var (
// seams for testing
var (
stderr io.Writer = os.Stderr
writeFile = ioutil.WriteFile
writeFile = os.WriteFile
)
const usage = `fiximports: rewrite import paths to use canonical package names.

Просмотреть файл

@ -15,7 +15,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@ -51,7 +50,7 @@ func downloadGoVersion(version, ops, arch, dest string) error {
}
defer resp.Body.Close()
tmpf, err := ioutil.TempFile("", "go")
tmpf, err := os.CreateTemp("", "go")
if err != nil {
return err
}
@ -75,7 +74,7 @@ func downloadGoVersion(version, ops, arch, dest string) error {
return fmt.Errorf("Downloading Go sha256 from %s.sha256 failed with HTTP status %s", uri, sresp.Status)
}
shasum, err := ioutil.ReadAll(sresp.Body)
shasum, err := io.ReadAll(sresp.Body)
if err != nil {
return err
}
@ -174,7 +173,7 @@ func getLatestGoVersion() (string, error) {
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 1024))
b, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
return "", fmt.Errorf("Could not get current Go release: HTTP %d: %q", resp.StatusCode, b)
}
var releases []struct {

Просмотреть файл

@ -8,7 +8,6 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -19,7 +18,7 @@ func TestDownloadGoVersion(t *testing.T) {
t.Skipf("Skipping download in short mode")
}
tmpd, err := ioutil.TempDir("", "go")
tmpd, err := os.MkdirTemp("", "go")
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -10,7 +10,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
@ -37,7 +36,7 @@ func TestMain(m *testing.M) {
}
func createTmpHome(t *testing.T) string {
tmpd, err := ioutil.TempDir("", "testgetgo")
tmpd, err := os.MkdirTemp("", "testgetgo")
if err != nil {
t.Fatalf("creating test tempdir failed: %v", err)
}
@ -86,7 +85,7 @@ func TestCommandVerbose(t *testing.T) {
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(shellConfig)
b, err := os.ReadFile(shellConfig)
if err != nil {
t.Fatal(err)
}
@ -122,7 +121,7 @@ func TestCommandPathExists(t *testing.T) {
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(shellConfig)
b, err := os.ReadFile(shellConfig)
if err != nil {
t.Fatal(err)
}
@ -146,7 +145,7 @@ export PATH=$PATH:%s/go/bin
t.Fatal(err)
}
b, err = ioutil.ReadFile(shellConfig)
b, err = os.ReadFile(shellConfig)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -8,7 +8,6 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -16,7 +15,7 @@ import (
)
func TestAppendPath(t *testing.T) {
tmpd, err := ioutil.TempDir("", "go")
tmpd, err := os.MkdirTemp("", "go")
if err != nil {
t.Fatal(err)
}
@ -35,7 +34,7 @@ func TestAppendPath(t *testing.T) {
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(shellConfig)
b, err := os.ReadFile(shellConfig)
if err != nil {
t.Fatal(err)
}
@ -49,7 +48,7 @@ func TestAppendPath(t *testing.T) {
if err := appendToPATH(filepath.Join(GOPATH, "bin")); err != nil {
t.Fatal(err)
}
b, err = ioutil.ReadFile(shellConfig)
b, err = os.ReadFile(shellConfig)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -14,7 +14,6 @@ import (
"fmt"
"go/build"
exec "golang.org/x/sys/execabs"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -66,7 +65,7 @@ func detectrepo() string {
var googleSourceRx = regexp.MustCompile(`(?m)^(go|go-review)?\.googlesource.com\b`)
func checkCLA() {
slurp, err := ioutil.ReadFile(cookiesFile())
slurp, err := os.ReadFile(cookiesFile())
if err != nil && !os.IsNotExist(err) {
log.Fatal(err)
}
@ -135,7 +134,7 @@ func checkGoroot() {
"your GOROOT or set it to the path of your development version\n"+
"of Go.", v)
}
slurp, err := ioutil.ReadFile(filepath.Join(v, "VERSION"))
slurp, err := os.ReadFile(filepath.Join(v, "VERSION"))
if err == nil {
slurp = bytes.TrimSpace(slurp)
log.Fatalf("Your GOROOT environment variable is set to %q\n"+

Просмотреть файл

@ -10,7 +10,7 @@ import (
"fmt"
"go/build"
"go/types"
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"strings"
@ -197,7 +197,7 @@ func genPrefixes(out chan string, all bool) {
}
func walkDir(dirname, prefix string, out chan string) {
fiList, err := ioutil.ReadDir(dirname)
fiList, err := ioutilReadDir(dirname)
if err != nil {
return
}
@ -209,3 +209,20 @@ func walkDir(dirname, prefix string, out chan string) {
}
}
}
func ioutilReadDir(dirname string) ([]fs.FileInfo, error) {
entries, err := os.ReadDir(dirname)
if err != nil {
return nil, err
}
infos := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return infos, err
}
infos = append(infos, info)
}
return infos, nil
}

Просмотреть файл

@ -9,7 +9,7 @@ import (
"context"
"fmt"
"go/build"
"io/ioutil"
"io"
"net"
"net/http"
"os"
@ -111,7 +111,7 @@ func waitForServer(t *testing.T, ctx context.Context, url, match string, reverse
if err != nil {
continue
}
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil || res.StatusCode != http.StatusOK {
continue
@ -396,7 +396,7 @@ package a; import _ "godoc.test/repo2/a"; const Name = "repo1a"`,
t.Errorf("GET %s failed: %s", url, err)
continue
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
strBody := string(body)
resp.Body.Close()
if err != nil {

Просмотреть файл

@ -13,7 +13,6 @@ import (
"go/scanner"
exec "golang.org/x/sys/execabs"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -106,7 +105,7 @@ func processFile(filename string, in io.Reader, out io.Writer, argType argumentT
in = f
}
src, err := ioutil.ReadAll(in)
src, err := io.ReadAll(in)
if err != nil {
return err
}
@ -159,7 +158,7 @@ func processFile(filename string, in io.Reader, out io.Writer, argType argumentT
if fi, err := os.Stat(filename); err == nil {
perms = fi.Mode() & os.ModePerm
}
err = ioutil.WriteFile(filename, res, perms)
err = os.WriteFile(filename, res, perms)
if err != nil {
return err
}
@ -296,7 +295,7 @@ func gofmtMain() {
}
func writeTempFile(dir, prefix string, data []byte) (string, error) {
file, err := ioutil.TempFile(dir, prefix)
file, err := os.CreateTemp(dir, prefix)
if err != nil {
return "", err
}

Просмотреть файл

@ -6,7 +6,6 @@ package main_test
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -315,7 +314,7 @@ func buildGorename(t *testing.T) (tmp, bin string, cleanup func()) {
t.Skipf("the dependencies are not available on android")
}
tmp, err := ioutil.TempDir("", "gorename-regtest-")
tmp, err := os.MkdirTemp("", "gorename-regtest-")
if err != nil {
t.Fatal(err)
}
@ -352,7 +351,7 @@ func setUpPackages(t *testing.T, dir string, packages map[string][]string) (clea
// Write the packages files
for i, val := range files {
file := filepath.Join(pkgDir, strconv.Itoa(i)+".go")
if err := ioutil.WriteFile(file, []byte(val), os.ModePerm); err != nil {
if err := os.WriteFile(file, []byte(val), os.ModePerm); err != nil {
t.Fatal(err)
}
}
@ -373,7 +372,7 @@ func modifiedFiles(t *testing.T, dir string, packages map[string][]string) (resu
for i, val := range files {
file := filepath.Join(pkgDir, strconv.Itoa(i)+".go")
// read file contents and compare to val
if contents, err := ioutil.ReadFile(file); err != nil {
if contents, err := os.ReadFile(file); err != nil {
t.Fatalf("File missing: %s", err)
} else if string(contents) != val {
results = append(results, strings.TrimPrefix(dir, file))

Просмотреть файл

@ -97,7 +97,7 @@ import (
"go/scanner"
"go/token"
"go/types"
"io/ioutil"
"io"
"os"
"path/filepath"
"sync"
@ -197,7 +197,7 @@ func parse(filename string, src interface{}) (*ast.File, error) {
}
func parseStdin() (*ast.File, error) {
src, err := ioutil.ReadAll(os.Stdin)
src, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, err
}

Просмотреть файл

@ -50,7 +50,6 @@ import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"math"
"os"
"strconv"
@ -3209,7 +3208,7 @@ func exit(status int) {
}
func gofmt() {
src, err := ioutil.ReadFile(oflag)
src, err := os.ReadFile(oflag)
if err != nil {
return
}
@ -3217,7 +3216,7 @@ func gofmt() {
if err != nil {
return
}
ioutil.WriteFile(oflag, src, 0666)
os.WriteFile(oflag, src, 0666)
}
var yaccpar string // will be processed version of yaccpartext: s/$$/prefix/g

Просмотреть файл

@ -34,7 +34,6 @@ import (
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
@ -83,7 +82,7 @@ func parseRegexp(text string) (*regexp.Regexp, error) {
// parseQueries parses and returns the queries in the named file.
func parseQueries(t *testing.T, filename string) []*query {
filedata, err := ioutil.ReadFile(filename)
filedata, err := os.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
@ -266,7 +265,7 @@ func TestGuru(t *testing.T) {
json := strings.Contains(filename, "-json/")
queries := parseQueries(t, filename)
golden := filename + "lden"
gotfh, err := ioutil.TempFile("", filepath.Base(filename)+"t")
gotfh, err := os.CreateTemp("", filepath.Base(filename)+"t")
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -765,7 +765,7 @@ func (r *referrersPackageResult) foreachRef(f func(id *ast.Ident, text string))
}
}
// readFile is like ioutil.ReadFile, but
// readFile is like os.ReadFile, but
// it goes through the virtualized build.Context.
// If non-nil, buf must have been reset.
func readFile(ctxt *build.Context, filename string, buf *bytes.Buffer) ([]byte, error) {

Просмотреть файл

@ -7,7 +7,6 @@ package main
import (
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -27,7 +26,7 @@ func TestIssue17515(t *testing.T) {
// (4) symlink & absolute: GOPATH=$HOME/src file= $HOME/go/src/test/test.go
// Create a temporary home directory under /tmp
home, err := ioutil.TempDir(os.TempDir(), "home")
home, err := os.MkdirTemp(os.TempDir(), "home")
if err != nil {
t.Errorf("Unable to create a temporary directory in %s", os.TempDir())
}

Просмотреть файл

@ -25,7 +25,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
@ -84,7 +83,7 @@ func main() {
// If writeBack is true, the converted version is written back to file.
// If writeBack is false, the converted version is printed to standard output.
func convert(r io.Reader, file string, writeBack bool) error {
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return err
}
@ -184,7 +183,7 @@ func convert(r io.Reader, file string, writeBack bool) error {
os.Stdout.Write(md.Bytes())
return nil
}
return ioutil.WriteFile(file, md.Bytes(), 0666)
return os.WriteFile(file, md.Bytes(), 0666)
}
func printSectionBody(file string, depth int, w *bytes.Buffer, elems []present.Elem) {

Просмотреть файл

@ -12,7 +12,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
@ -196,7 +195,7 @@ func (c *config) gen(singlepk int, singlefn int) {
func (c *config) action(cmd []string, outfile string, emitout bool) int {
st := docmdout(cmd, c.gendir, outfile)
if emitout {
content, err := ioutil.ReadFile(outfile)
content, err := os.ReadFile(outfile)
if err != nil {
log.Fatal(err)
}
@ -405,7 +404,7 @@ func main() {
}
verb(1, "in main, verblevel=%d", *verbflag)
tmpdir, err := ioutil.TempDir("", "fuzzrun")
tmpdir, err := os.MkdirTemp("", "fuzzrun")
if err != nil {
fatal("creation of tempdir failed: %v", err)
}

Просмотреть файл

@ -20,7 +20,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
@ -128,7 +127,7 @@ func main() {
}
fails++
dir, path := filepath.Split(*flagOutput)
f, err := ioutil.TempFile(dir, path)
f, err := os.CreateTemp(dir, path)
if err != nil {
fmt.Printf("failed to create temp file: %v\n", err)
os.Exit(1)

Просмотреть файл

@ -128,7 +128,7 @@ import (
"fmt"
exec "golang.org/x/sys/execabs"
"io"
"io/ioutil"
"io/fs"
"log"
"os"
"path/filepath"
@ -553,7 +553,7 @@ func save() {
}
toolDir := filepath.Join(goroot, fmt.Sprintf("pkg/tool/%s_%s", runtime.GOOS, runtime.GOARCH))
files, err := ioutil.ReadDir(toolDir)
files, err := ioutilReadDir(toolDir)
if err != nil {
log.Fatal(err)
}
@ -578,7 +578,7 @@ func save() {
}
func restore() {
files, err := ioutil.ReadDir(stashDir)
files, err := ioutilReadDir(stashDir)
if err != nil {
log.Fatal(err)
}
@ -626,11 +626,28 @@ func cp(src, dst string) {
if *verbose {
fmt.Printf("cp %s %s\n", src, dst)
}
data, err := ioutil.ReadFile(src)
data, err := os.ReadFile(src)
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(dst, data, 0777); err != nil {
if err := os.WriteFile(dst, data, 0777); err != nil {
log.Fatal(err)
}
}
func ioutilReadDir(dirname string) ([]fs.FileInfo, error) {
entries, err := os.ReadDir(dirname)
if err != nil {
return nil, err
}
infos := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return infos, err
}
infos = append(infos, info)
}
return infos, nil
}

Просмотреть файл

@ -13,7 +13,7 @@ import (
"go/parser"
"go/token"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
@ -67,7 +67,7 @@ func checkFile(toolsDir, filename string) (bool, error) {
if strings.HasSuffix(normalized, "cmd/goyacc/yacc.go") {
return false, nil
}
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return false, err
}

Просмотреть файл

@ -11,7 +11,6 @@ import (
"go/format"
"go/token"
"go/types"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -35,7 +34,7 @@ import (
// maps file names to contents). On success it returns the name of the
// directory and a cleanup function to delete it.
func WriteFiles(filemap map[string]string) (dir string, cleanup func(), err error) {
gopath, err := ioutil.TempDir("", "analysistest")
gopath, err := os.MkdirTemp("", "analysistest")
if err != nil {
return "", nil, err
}
@ -44,7 +43,7 @@ func WriteFiles(filemap map[string]string) (dir string, cleanup func(), err erro
for name, content := range filemap {
filename := filepath.Join(gopath, "src", name)
os.MkdirAll(filepath.Dir(filename), 0777) // ignore error
if err := ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
if err := os.WriteFile(filename, []byte(content), 0666); err != nil {
cleanup()
return "", nil, err
}
@ -451,7 +450,7 @@ func check(t Testing, gopath string, pass *analysis.Pass, diagnostics []analysis
// Extract 'want' comments from non-Go files.
// TODO(adonovan): we may need to handle //line directives.
for _, filename := range pass.OtherFiles {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
t.Errorf("can't read '// want' comments from %s: %v", filename, err)
continue

Просмотреть файл

@ -191,7 +191,7 @@ and buildtag, inspect the raw text of Go source files or even non-Go
files such as assembly. To report a diagnostic against a line of a
raw text file, use the following sequence:
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil { ... }
tf := fset.AddFile(filename, -1, len(content))
tf.SetLinesForContent(content)

Просмотреть файл

@ -14,7 +14,6 @@ import (
"fmt"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"strconv"
@ -331,7 +330,7 @@ func PrintPlain(fset *token.FileSet, diag analysis.Diagnostic) {
if !end.IsValid() {
end = posn
}
data, _ := ioutil.ReadFile(posn.Filename)
data, _ := os.ReadFile(posn.Filename)
lines := strings.Split(string(data), "\n")
for i := posn.Line - Context; i <= end.Line+Context; i++ {
if 1 <= i && i <= len(lines) {

Просмотреть файл

@ -17,7 +17,6 @@ import (
"go/format"
"go/token"
"go/types"
"io/ioutil"
"log"
"os"
"reflect"
@ -425,7 +424,7 @@ func applyFixes(roots []*action) error {
// Now we've got a set of valid edits for each file. Apply them.
for path, edits := range editsByPath {
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
return err
}
@ -440,7 +439,7 @@ func applyFixes(roots []*action) error {
out = formatted
}
if err := ioutil.WriteFile(path, out, 0644); err != nil {
if err := os.WriteFile(path, out, 0644); err != nil {
return err
}
}
@ -480,7 +479,7 @@ func validateEdits(edits []diff.Edit) ([]diff.Edit, int) {
// diff3Conflict returns an error describing two conflicting sets of
// edits on a file at path.
func diff3Conflict(path string, xlabel, ylabel string, xedits, yedits []diff.Edit) error {
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
return err
}

Просмотреть файл

@ -7,7 +7,7 @@ package checker_test
import (
"fmt"
"go/ast"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
@ -51,7 +51,7 @@ func Foo() {
checker.Fix = true
checker.Run([]string{"file=" + path}, []*analysis.Analyzer{analyzer})
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -6,7 +6,6 @@ package checker_test
import (
"flag"
"io/ioutil"
"os"
"os/exec"
"path"
@ -151,7 +150,7 @@ func Foo() {
for name, want := range fixed {
path := path.Join(dir, "src", name)
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
t.Errorf("error reading %s: %v", path, err)
}
@ -224,7 +223,7 @@ func Foo() {
// No files updated
for name, want := range files {
path := path.Join(dir, "src", name)
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
t.Errorf("error reading %s: %v", path, err)
}
@ -298,7 +297,7 @@ func Foo() {
// No files updated
for name, want := range files {
path := path.Join(dir, "src", name)
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
t.Errorf("error reading %s: %v", path, err)
}

Просмотреть файл

@ -6,7 +6,7 @@ package checker_test
import (
"go/ast"
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -40,7 +40,7 @@ package comment
checker.Fix = true
checker.Run([]string{"file=" + path}, []*analysis.Analyzer{commentAnalyzer})
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -12,7 +12,7 @@ import (
"go/printer"
"go/token"
"go/types"
"io/ioutil"
"os"
)
// Format returns a string representation of the expression.
@ -69,7 +69,7 @@ func Unparen(e ast.Expr) ast.Expr {
// ReadFile reads a file and adds it to the FileSet
// so that we can report errors against it using lineStart.
func ReadFile(fset *token.FileSet, filename string) ([]byte, *token.File, error) {
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return nil, nil, err
}

Просмотреть файл

@ -8,7 +8,6 @@ import (
"fmt"
"go/build"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -76,7 +75,7 @@ func FakeContext(pkgs map[string]map[string]string) *build.Context {
if !ok {
return nil, fmt.Errorf("file not found: %s", filename)
}
return ioutil.NopCloser(strings.NewReader(content)), nil
return io.NopCloser(strings.NewReader(content)), nil
}
ctxt.IsAbsPath = func(path string) bool {
path = filepath.ToSlash(path)

Просмотреть файл

@ -10,7 +10,6 @@ import (
"fmt"
"go/build"
"io"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
@ -33,7 +32,7 @@ func OverlayContext(orig *build.Context, overlay map[string][]byte) *build.Conte
// TODO(dominikh): Implement IsDir, HasSubdir and ReadDir
rc := func(data []byte) (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewBuffer(data)), nil
return io.NopCloser(bytes.NewBuffer(data)), nil
}
copy := *orig // make a copy

Просмотреть файл

@ -6,7 +6,7 @@ package buildutil_test
import (
"go/build"
"io/ioutil"
"io"
"reflect"
"strings"
"testing"
@ -63,7 +63,7 @@ func TestOverlay(t *testing.T) {
if err != nil {
t.Errorf("unexpected error %v", err)
}
b, err := ioutil.ReadAll(f)
b, err := io.ReadAll(f)
if err != nil {
t.Errorf("unexpected error %v", err)
}

Просмотреть файл

@ -11,7 +11,7 @@ import (
"go/parser"
"go/token"
"io"
"io/ioutil"
"io/fs"
"os"
"path"
"path/filepath"
@ -174,13 +174,13 @@ func IsDir(ctxt *build.Context, path string) bool {
return err == nil && fi.IsDir()
}
// ReadDir behaves like ioutil.ReadDir,
// ReadDir behaves like ioutilReadDir,
// but uses the build context's file system interface, if any.
func ReadDir(ctxt *build.Context, path string) ([]os.FileInfo, error) {
if ctxt.ReadDir != nil {
return ctxt.ReadDir(path)
}
return ioutil.ReadDir(path)
return ioutilReadDir(path)
}
// SplitPathList behaves like filepath.SplitList,
@ -207,3 +207,20 @@ func sameFile(x, y string) bool {
}
return false
}
func ioutilReadDir(dirname string) ([]fs.FileInfo, error) {
entries, err := os.ReadDir(dirname)
if err != nil {
return nil, err
}
infos := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return infos, err
}
infos = append(infos, info)
}
return infos, nil
}

Просмотреть файл

@ -6,7 +6,6 @@ package buildutil_test
import (
"go/build"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -53,7 +52,7 @@ func TestContainingPackage(t *testing.T) {
if runtime.GOOS != "windows" && runtime.GOOS != "plan9" {
// Make a symlink to gopath for test
tmp, err := ioutil.TempDir(os.TempDir(), "go")
tmp, err := os.MkdirTemp(os.TempDir(), "go")
if err != nil {
t.Errorf("Unable to create a temporary directory in %s", os.TempDir())
}

Просмотреть файл

@ -16,7 +16,7 @@ import (
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"os"
"sort"
"strings"
"testing"
@ -98,7 +98,7 @@ func TestCHAGenerics(t *testing.T) {
}
func loadProgInfo(filename string, mode ssa.BuilderMode) (*ssa.Program, *ast.File, *ssa.Package, error) {
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return nil, nil, nil, fmt.Errorf("couldn't read file '%s': %s", filename, err)
}

Просмотреть файл

@ -9,7 +9,7 @@ import (
"fmt"
"go/ast"
"go/parser"
"io/ioutil"
"os"
"sort"
"strings"
"testing"
@ -38,7 +38,7 @@ func want(f *ast.File) []string {
// `path`, assumed to define package "testdata," and the
// test want result as list of strings.
func testProg(path string, mode ssa.BuilderMode) (*ssa.Program, []string, error) {
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
return nil, nil, err
}

Просмотреть файл

@ -7,7 +7,7 @@ package expect_test
import (
"bytes"
"go/token"
"io/ioutil"
"os"
"testing"
"golang.org/x/tools/go/expect"
@ -52,7 +52,7 @@ func TestMarker(t *testing.T) {
},
} {
t.Run(tt.filename, func(t *testing.T) {
content, err := ioutil.ReadFile(tt.filename)
content, err := os.ReadFile(tt.filename)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -20,7 +20,6 @@ import (
"go/token"
"go/types"
"io"
"io/ioutil"
"strconv"
"strings"
@ -46,7 +45,7 @@ func CompilerInfo(gccgo string, args ...string) (version, triple string, dirs []
// NewReader returns a reader for the export data section of an object
// (.o) or archive (.a) file read from r.
func NewReader(r io.Reader) (io.Reader, error) {
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}

Просмотреть файл

@ -57,7 +57,6 @@ import (
"go/build"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -70,7 +69,7 @@ import (
// ProcessFiles invokes the cgo preprocessor on bp.CgoFiles, parses
// the output and returns the resulting ASTs.
func ProcessFiles(bp *build.Package, fset *token.FileSet, DisplayPath func(path string) string, mode parser.Mode) ([]*ast.File, error) {
tmpdir, err := ioutil.TempDir("", strings.Replace(bp.ImportPath, "/", "_", -1)+"_C")
tmpdir, err := os.MkdirTemp("", strings.Replace(bp.ImportPath, "/", "_", -1)+"_C")
if err != nil {
return nil, err
}

Просмотреть файл

@ -210,7 +210,7 @@ func GetImporter(searchpaths []string, initmap map[*types.Package]InitData) Impo
// Excluded for now: Standard gccgo doesn't support this import format currently.
// case goimporterMagic:
// var data []byte
// data, err = ioutil.ReadAll(reader)
// data, err = io.ReadAll(reader)
// if err != nil {
// return
// }

Просмотреть файл

@ -15,7 +15,7 @@ import (
"go/build"
"go/token"
"go/types"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
@ -190,7 +190,7 @@ func TestCgoOption(t *testing.T) {
}
// Load the file and check the object is declared at the right place.
b, err := ioutil.ReadFile(posn.Filename)
b, err := os.ReadFile(posn.Filename)
if err != nil {
t.Errorf("can't read %s: %s", posn.Filename, err)
continue

Просмотреть файл

@ -9,7 +9,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
@ -1109,7 +1108,7 @@ func (state *golistState) writeOverlays() (filename string, cleanup func(), err
if len(state.cfg.Overlay) == 0 {
return "", func() {}, nil
}
dir, err := ioutil.TempDir("", "gopackages-*")
dir, err := os.MkdirTemp("", "gopackages-*")
if err != nil {
return "", nil, err
}
@ -1128,7 +1127,7 @@ func (state *golistState) writeOverlays() (filename string, cleanup func(), err
// Create a unique filename for the overlaid files, to avoid
// creating nested directories.
noSeparator := strings.Join(strings.Split(filepath.ToSlash(k), "/"), "")
f, err := ioutil.TempFile(dir, fmt.Sprintf("*-%s", noSeparator))
f, err := os.CreateTemp(dir, fmt.Sprintf("*-%s", noSeparator))
if err != nil {
return "", func() {}, err
}
@ -1146,7 +1145,7 @@ func (state *golistState) writeOverlays() (filename string, cleanup func(), err
}
// Write out the overlay file that contains the filepath mappings.
filename = filepath.Join(dir, "overlay.json")
if err := ioutil.WriteFile(filename, b, 0665); err != nil {
if err := os.WriteFile(filename, b, 0665); err != nil {
return "", func() {}, err
}
return filename, cleanup, nil

Просмотреть файл

@ -6,7 +6,6 @@ package packages_test
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -499,7 +498,7 @@ func TestAdHocOverlays(t *testing.T) {
// This test doesn't use packagestest because we are testing ad-hoc packages,
// which are outside of $GOPATH and outside of a module.
tmp, err := ioutil.TempDir("", "testAdHocOverlays")
tmp, err := os.MkdirTemp("", "testAdHocOverlays")
if err != nil {
t.Fatal(err)
}
@ -554,18 +553,18 @@ func TestOverlayModFileChanges(t *testing.T) {
testenv.NeedsTool(t, "go")
// Create two unrelated modules in a temporary directory.
tmp, err := ioutil.TempDir("", "tmp")
tmp, err := os.MkdirTemp("", "tmp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
// mod1 has a dependency on golang.org/x/xerrors.
mod1, err := ioutil.TempDir(tmp, "mod1")
mod1, err := os.MkdirTemp(tmp, "mod1")
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(mod1, "go.mod"), []byte(`module mod1
if err := os.WriteFile(filepath.Join(mod1, "go.mod"), []byte(`module mod1
require (
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
@ -575,7 +574,7 @@ func TestOverlayModFileChanges(t *testing.T) {
}
// mod2 does not have any dependencies.
mod2, err := ioutil.TempDir(tmp, "mod2")
mod2, err := os.MkdirTemp(tmp, "mod2")
if err != nil {
t.Fatal(err)
}
@ -584,7 +583,7 @@ func TestOverlayModFileChanges(t *testing.T) {
go 1.11
`
if err := ioutil.WriteFile(filepath.Join(mod2, "go.mod"), []byte(want), 0775); err != nil {
if err := os.WriteFile(filepath.Join(mod2, "go.mod"), []byte(want), 0775); err != nil {
t.Fatal(err)
}
@ -610,7 +609,7 @@ func main() {}
}
// Check that mod2/go.mod has not been modified.
got, err := ioutil.ReadFile(filepath.Join(mod2, "go.mod"))
got, err := os.ReadFile(filepath.Join(mod2, "go.mod"))
if err != nil {
t.Fatal(err)
}
@ -1045,7 +1044,7 @@ func TestOverlaysInReplace(t *testing.T) {
// Create module b.com in a temporary directory. Do not add any Go files
// on disk.
tmpPkgs, err := ioutil.TempDir("", "modules")
tmpPkgs, err := os.MkdirTemp("", "modules")
if err != nil {
t.Fatal(err)
}
@ -1055,7 +1054,7 @@ func TestOverlaysInReplace(t *testing.T) {
if err := os.Mkdir(dirB, 0775); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dirB, "go.mod"), []byte(fmt.Sprintf("module %s.com", dirB)), 0775); err != nil {
if err := os.WriteFile(filepath.Join(dirB, "go.mod"), []byte(fmt.Sprintf("module %s.com", dirB)), 0775); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(dirB, "inner"), 0775); err != nil {
@ -1063,7 +1062,7 @@ func TestOverlaysInReplace(t *testing.T) {
}
// Create a separate module that requires and replaces b.com.
tmpWorkspace, err := ioutil.TempDir("", "workspace")
tmpWorkspace, err := os.MkdirTemp("", "workspace")
if err != nil {
t.Fatal(err)
}
@ -1078,7 +1077,7 @@ replace (
b.com => %s
)
`, dirB)
if err := ioutil.WriteFile(filepath.Join(tmpWorkspace, "go.mod"), []byte(goModContent), 0775); err != nil {
if err := os.WriteFile(filepath.Join(tmpWorkspace, "go.mod"), []byte(goModContent), 0775); err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -16,7 +16,6 @@ import (
"go/token"
"go/types"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -1127,7 +1126,7 @@ func (ld *loader) parseFile(filename string) (*ast.File, error) {
var err error
if src == nil {
ioLimit <- true // wait
src, err = ioutil.ReadFile(filename)
src, err = os.ReadFile(filename)
<-ioLimit // signal
}
if err != nil {

Просмотреть файл

@ -15,7 +15,6 @@ import (
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -931,7 +930,7 @@ func TestAdHocPackagesBadImport(t *testing.T) {
// This test doesn't use packagestest because we are testing ad-hoc packages,
// which are outside of $GOPATH and outside of a module.
tmp, err := ioutil.TempDir("", "a")
tmp, err := os.MkdirTemp("", "a")
if err != nil {
t.Fatal(err)
}
@ -942,7 +941,7 @@ func TestAdHocPackagesBadImport(t *testing.T) {
import _ "badimport"
const A = 1
`)
if err := ioutil.WriteFile(filename, content, 0775); err != nil {
if err := os.WriteFile(filename, content, 0775); err != nil {
t.Fatal(err)
}
@ -1748,7 +1747,7 @@ func testAdHocContains(t *testing.T, exporter packagestest.Exporter) {
}}})
defer exported.Cleanup()
tmpfile, err := ioutil.TempFile("", "adhoc*.go")
tmpfile, err := os.CreateTemp("", "adhoc*.go")
filename := tmpfile.Name()
if err != nil {
t.Fatal(err)
@ -1985,11 +1984,11 @@ import "C"`,
}
func buildFakePkgconfig(t *testing.T, env []string) string {
tmpdir, err := ioutil.TempDir("", "fakepkgconfig")
tmpdir, err := os.MkdirTemp("", "fakepkgconfig")
if err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(filepath.Join(tmpdir, "pkg-config.go"), []byte(`
err = os.WriteFile(filepath.Join(tmpdir, "pkg-config.go"), []byte(`
package main
import "fmt"
@ -2452,7 +2451,7 @@ func testIssue37098(t *testing.T, exporter packagestest.Exporter) {
if err != nil {
t.Errorf("Failed to parse file '%s' as a Go source: %v", file, err)
contents, err := ioutil.ReadFile(file)
contents, err := os.ReadFile(file)
if err != nil {
t.Fatalf("Failed to read the un-parsable file '%s': %v", file, err)
}
@ -2633,7 +2632,7 @@ func testExternal_NotHandled(t *testing.T, exporter packagestest.Exporter) {
skipIfShort(t, "builds and links fake driver binaries")
testenv.NeedsGoBuild(t)
tempdir, err := ioutil.TempDir("", "testexternal")
tempdir, err := os.MkdirTemp("", "testexternal")
if err != nil {
t.Fatal(err)
}
@ -2652,7 +2651,7 @@ import (
)
func main() {
ioutil.ReadAll(os.Stdin)
io.ReadAll(os.Stdin)
fmt.Println("{}")
}
`,
@ -2665,7 +2664,7 @@ import (
)
func main() {
ioutil.ReadAll(os.Stdin)
io.ReadAll(os.Stdin)
fmt.Println("{\"NotHandled\": true}")
}
`,
@ -2755,7 +2754,7 @@ func TestEmptyEnvironment(t *testing.T) {
func TestPackageLoadSingleFile(t *testing.T) {
testenv.NeedsTool(t, "go")
tmp, err := ioutil.TempDir("", "a")
tmp, err := os.MkdirTemp("", "a")
if err != nil {
t.Fatal(err)
}
@ -2763,7 +2762,7 @@ func TestPackageLoadSingleFile(t *testing.T) {
filename := filepath.Join(tmp, "a.go")
if err := ioutil.WriteFile(filename, []byte(`package main; func main() { println("hello world") }`), 0775); err != nil {
if err := os.WriteFile(filename, []byte(`package main; func main() { println("hello world") }`), 0775); err != nil {
t.Fatal(err)
}
@ -2899,7 +2898,7 @@ func copyAll(srcPath, dstPath string) error {
if info.IsDir() {
return nil
}
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
return err
}
@ -2911,7 +2910,7 @@ func copyAll(srcPath, dstPath string) error {
if err := os.MkdirAll(filepath.Dir(dstFilePath), 0755); err != nil {
return err
}
if err := ioutil.WriteFile(dstFilePath, contents, 0644); err != nil {
if err := os.WriteFile(dstFilePath, contents, 0644); err != nil {
return err
}
return nil

Просмотреть файл

@ -7,7 +7,6 @@ package packagestest
import (
"fmt"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -226,7 +225,7 @@ func goModMarkers(e *Exported, gomod string) ([]*expect.Note, error) {
}
gomod = strings.TrimSuffix(gomod, ".temp")
// If we are in Modules mode, copy the original contents file back into go.mod
if err := ioutil.WriteFile(gomod, content, 0644); err != nil {
if err := os.WriteFile(gomod, content, 0644); err != nil {
return nil, nil
}
return expect.Parse(e.ExpectFileSet, gomod, content)

Просмотреть файл

@ -69,7 +69,6 @@ import (
"fmt"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -198,7 +197,7 @@ func Export(t testing.TB, exporter Exporter, modules []Module) *Exported {
dirname := strings.Replace(t.Name(), "/", "_", -1)
dirname = strings.Replace(dirname, "#", "_", -1) // duplicate subtests get a #NNN suffix.
temp, err := ioutil.TempDir("", dirname)
temp, err := os.MkdirTemp("", dirname)
if err != nil {
t.Fatal(err)
}
@ -254,7 +253,7 @@ func Export(t testing.TB, exporter Exporter, modules []Module) *Exported {
t.Fatal(err)
}
case string:
if err := ioutil.WriteFile(fullpath, []byte(value), 0644); err != nil {
if err := os.WriteFile(fullpath, []byte(value), 0644); err != nil {
t.Fatal(err)
}
default:
@ -278,7 +277,7 @@ func Export(t testing.TB, exporter Exporter, modules []Module) *Exported {
// It is intended for source files that are shell scripts.
func Script(contents string) Writer {
return func(filename string) error {
return ioutil.WriteFile(filename, []byte(contents), 0755)
return os.WriteFile(filename, []byte(contents), 0755)
}
}
@ -659,7 +658,7 @@ func (e *Exported) FileContents(filename string) ([]byte, error) {
if content, found := e.Config.Overlay[filename]; found {
return content, nil
}
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}

Просмотреть файл

@ -5,7 +5,6 @@
package packagestest_test
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -197,7 +196,7 @@ func TestMustCopyFiles(t *testing.T) {
"nested/b/b.go": "package b",
}
tmpDir, err := ioutil.TempDir("", t.Name())
tmpDir, err := os.MkdirTemp("", t.Name())
if err != nil {
t.Fatalf("failed to create a temporary directory: %v", err)
}
@ -208,7 +207,7 @@ func TestMustCopyFiles(t *testing.T) {
if err := os.MkdirAll(filepath.Dir(fullpath), 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(fullpath, []byte(contents), 0644); err != nil {
if err := os.WriteFile(fullpath, []byte(contents), 0644); err != nil {
t.Fatal(err)
}
}

Просмотреть файл

@ -7,7 +7,6 @@ package packagestest
import (
"context"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -90,11 +89,11 @@ func (modules) Finalize(exported *Exported) error {
// If the primary module already has a go.mod, write the contents to a temp
// go.mod for now and then we will reset it when we are getting all the markers.
if gomod := exported.written[exported.primary]["go.mod"]; gomod != "" {
contents, err := ioutil.ReadFile(gomod)
contents, err := os.ReadFile(gomod)
if err != nil {
return err
}
if err := ioutil.WriteFile(gomod+".temp", contents, 0644); err != nil {
if err := os.WriteFile(gomod+".temp", contents, 0644); err != nil {
return err
}
}
@ -115,7 +114,7 @@ func (modules) Finalize(exported *Exported) error {
primaryGomod += fmt.Sprintf("\t%v %v\n", other, version)
}
primaryGomod += ")\n"
if err := ioutil.WriteFile(filepath.Join(primaryDir, "go.mod"), []byte(primaryGomod), 0644); err != nil {
if err := os.WriteFile(filepath.Join(primaryDir, "go.mod"), []byte(primaryGomod), 0644); err != nil {
return err
}
@ -136,7 +135,7 @@ func (modules) Finalize(exported *Exported) error {
if v, ok := versions[module]; ok {
module = v.module
}
if err := ioutil.WriteFile(modfile, []byte("module "+module+"\n"), 0644); err != nil {
if err := os.WriteFile(modfile, []byte("module "+module+"\n"), 0644); err != nil {
return err
}
files["go.mod"] = modfile
@ -193,7 +192,7 @@ func (modules) Finalize(exported *Exported) error {
func writeModuleFiles(rootDir, module, ver string, filePaths map[string]string) error {
fileData := make(map[string][]byte)
for name, path := range filePaths {
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
return err
}

Просмотреть файл

@ -13,7 +13,6 @@ import (
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"os"
"runtime"
"strings"
@ -33,7 +32,7 @@ func TestObjValueLookup(t *testing.T) {
}
conf := loader.Config{ParserMode: parser.ParseComments}
src, err := ioutil.ReadFile("testdata/objlookup.go")
src, err := os.ReadFile("testdata/objlookup.go")
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -16,7 +16,6 @@ import (
htmlpkg "html"
htmltemplate "html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
@ -85,7 +84,7 @@ func (h *handlerServer) GetPageInfo(abspath, relpath string, mode PageInfoMode,
if err != nil {
return nil, err
}
return ioutil.NopCloser(bytes.NewReader(data)), nil
return io.NopCloser(bytes.NewReader(data)), nil
}
// Make the syscall/js package always visible by default.

Просмотреть файл

@ -10,7 +10,7 @@ import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"unicode"
)
@ -71,7 +71,7 @@ func Generate() ([]byte, error) {
fmt.Fprintf(buf, "%v\n\n%v\n\npackage static\n\n", license, warning)
fmt.Fprintf(buf, "var Files = map[string]string{\n")
for _, fn := range files {
b, err := ioutil.ReadFile(fn)
b, err := os.ReadFile(fn)
if err != nil {
return b, err
}

Просмотреть файл

@ -6,7 +6,7 @@ package static
import (
"bytes"
"io/ioutil"
"os"
"runtime"
"strconv"
"testing"
@ -17,7 +17,7 @@ func TestStaticIsUpToDate(t *testing.T) {
if runtime.GOOS == "android" {
t.Skip("files not available on android")
}
oldBuf, err := ioutil.ReadFile("static.go")
oldBuf, err := os.ReadFile("static.go")
if err != nil {
t.Errorf("error while reading static.go: %v\n", err)
}

Просмотреть файл

@ -11,7 +11,6 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"golang.org/x/tools/godoc/static"
@ -29,7 +28,7 @@ func makestatic() error {
if err != nil {
return fmt.Errorf("error while generating static.go: %v\n", err)
}
err = ioutil.WriteFile("static.go", buf, 0666)
err = os.WriteFile("static.go", buf, 0666)
if err != nil {
return fmt.Errorf("error while writing static.go: %v\n", err)
}

Просмотреть файл

@ -5,7 +5,7 @@
package mapfs
import (
"io/ioutil"
"io"
"os"
"reflect"
"testing"
@ -36,7 +36,7 @@ func TestOpenRoot(t *testing.T) {
t.Errorf("Open(%q) = %v", tt.path, err)
continue
}
slurp, err := ioutil.ReadAll(rsc)
slurp, err := io.ReadAll(rsc)
if err != nil {
t.Error(err)
}

Просмотреть файл

@ -77,7 +77,7 @@ const debugNS = false
// just "/src" in the final two calls.
//
// OS is itself an implementation of a file system: it implements
// OS(`c:\Go`).ReadDir("/src/pkg/code") as ioutil.ReadDir(`c:\Go\src\pkg\code`).
// OS(`c:\Go`).ReadDir("/src/pkg/code") as ioutilReadDir(`c:\Go\src\pkg\code`).
//
// Because the new path is evaluated by fs (here OS(root)), another way
// to read the mount table is to mentally combine fs+new, so that this table:

Просмотреть файл

@ -7,7 +7,7 @@ package vfs
import (
"fmt"
"go/build"
"io/ioutil"
"io/fs"
"os"
pathpkg "path"
"path/filepath"
@ -101,5 +101,22 @@ func (root osFS) Stat(path string) (os.FileInfo, error) {
}
func (root osFS) ReadDir(path string) ([]os.FileInfo, error) {
return ioutil.ReadDir(root.resolve(path)) // is sorted
return ioutilReadDir(root.resolve(path)) // is sorted
}
func ioutilReadDir(dirname string) ([]fs.FileInfo, error) {
entries, err := os.ReadDir(dirname)
if err != nil {
return nil, err
}
infos := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return infos, err
}
infos = append(infos, info)
}
return infos, nil
}

Просмотреть файл

@ -8,7 +8,6 @@ package vfs // import "golang.org/x/tools/godoc/vfs"
import (
"io"
"io/ioutil"
"os"
)
@ -54,5 +53,5 @@ func ReadFile(fs Opener, path string) ([]byte, error) {
return nil, err
}
defer rc.Close()
return ioutil.ReadAll(rc)
return io.ReadAll(rc)
}

Просмотреть файл

@ -8,7 +8,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"testing"
@ -174,7 +173,7 @@ func TestZipFSOpenSeek(t *testing.T) {
// test Seek() multiple times
for i := 0; i < 3; i++ {
all, err := ioutil.ReadAll(f)
all, err := io.ReadAll(f)
if err != nil {
t.Error(err)
return

Просмотреть файл

@ -18,7 +18,6 @@ import (
"go/token"
"go/types"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -573,7 +572,7 @@ func fileForPos(pkg *packages.Package, pos token.Pos) (*ast.File, error) {
}
func rewriteFile(file string, api *source.APIJSON, write bool, rewrite func([]byte, *source.APIJSON) ([]byte, error)) (bool, error) {
old, err := ioutil.ReadFile(file)
old, err := os.ReadFile(file)
if err != nil {
return false, err
}
@ -587,7 +586,7 @@ func rewriteFile(file string, api *source.APIJSON, write bool, rewrite func([]by
return bytes.Equal(old, new), nil
}
if err := ioutil.WriteFile(file, new, 0); err != nil {
if err := os.WriteFile(file, new, 0); err != nil {
return false, err
}

Просмотреть файл

@ -7,7 +7,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path"
@ -56,11 +56,11 @@ func download(artifactURL string) error {
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("got status code %d from GCS", resp.StatusCode)
}
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("reading result: %v", err)
}
if err := ioutil.WriteFile(name, data, 0644); err != nil {
if err := os.WriteFile(name, data, 0644); err != nil {
return fmt.Errorf("writing artifact: %v", err)
}
return nil

Просмотреть файл

@ -7,7 +7,6 @@ package hooks
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -42,7 +41,7 @@ var (
// save writes a JSON record of statistics about diff requests to a temporary file.
func (s *diffstat) save() {
diffStatsOnce.Do(func() {
f, err := ioutil.TempFile("", "gopls-diff-stats-*")
f, err := os.CreateTemp("", "gopls-diff-stats-*")
if err != nil {
log.Printf("can't create diff stats temp file: %v", err) // e.g. disk full
return
@ -91,7 +90,7 @@ func disaster(before, after string) string {
// We use NUL as a separator: it should never appear in Go source.
data := before + "\x00" + after
if err := ioutil.WriteFile(filename, []byte(data), 0600); err != nil {
if err := os.WriteFile(filename, []byte(data), 0600); err != nil {
log.Printf("failed to write diff bug report: %v", err)
return ""
}

Просмотреть файл

@ -5,7 +5,6 @@
package hooks
import (
"io/ioutil"
"os"
"testing"
@ -20,7 +19,7 @@ func TestDisaster(t *testing.T) {
a := "This is a string,(\u0995) just for basic\nfunctionality"
b := "This is another string, (\u0996) to see if disaster will store stuff correctly"
fname := disaster(a, b)
buf, err := ioutil.ReadFile(fname)
buf, err := os.ReadFile(fname)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -6,7 +6,7 @@ package hooks
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"runtime"
"testing"
@ -23,7 +23,7 @@ func TestLicenses(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skip("generating licenses only works on Unixes")
}
tmp, err := ioutil.TempFile("", "")
tmp, err := os.CreateTemp("", "")
if err != nil {
t.Fatal(err)
}
@ -33,11 +33,11 @@ func TestLicenses(t *testing.T) {
t.Fatalf("generating licenses failed: %q, %v", out, err)
}
got, err := ioutil.ReadFile(tmp.Name())
got, err := os.ReadFile(tmp.Name())
if err != nil {
t.Fatal(err)
}
want, err := ioutil.ReadFile("licenses.go")
want, err := os.ReadFile("licenses.go")
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -7,7 +7,6 @@ package cmd
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -28,15 +27,15 @@ func TestCapabilities(t *testing.T) {
// Is there some missing error reporting somewhere?
testenv.NeedsTool(t, "go")
tmpDir, err := ioutil.TempDir("", "fake")
tmpDir, err := os.MkdirTemp("", "fake")
if err != nil {
t.Fatal(err)
}
tmpFile := filepath.Join(tmpDir, "fake.go")
if err := ioutil.WriteFile(tmpFile, []byte(""), 0775); err != nil {
if err := os.WriteFile(tmpFile, []byte(""), 0775); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte("module fake\n\ngo 1.12\n"), 0775); err != nil {
if err := os.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte("module fake\n\ngo 1.12\n"), 0775); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)

Просмотреть файл

@ -11,7 +11,6 @@ import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"reflect"
@ -703,7 +702,7 @@ func (c *cmdClient) getFile(uri span.URI) *cmdFile {
c.files[uri] = file
}
if file.mapper == nil {
content, err := ioutil.ReadFile(uri.Filename())
content, err := os.ReadFile(uri.Filename())
if err != nil {
file.err = fmt.Errorf("getFile: %v: %v", uri, err)
return file

Просмотреть файл

@ -8,7 +8,7 @@ import (
"bytes"
"context"
"flag"
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -41,12 +41,12 @@ func TestHelpFiles(t *testing.T) {
helpFile := filepath.Join("usage", name+".hlp")
got := buf.Bytes()
if *updateHelpFiles {
if err := ioutil.WriteFile(helpFile, got, 0666); err != nil {
if err := os.WriteFile(helpFile, got, 0666); err != nil {
t.Errorf("Failed writing %v: %v", helpFile, err)
}
return
}
want, err := ioutil.ReadFile(helpFile)
want, err := os.ReadFile(helpFile)
if err != nil {
t.Fatalf("Missing help file %q", helpFile)
}

Просмотреть файл

@ -11,7 +11,6 @@ import (
"fmt"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"unicode/utf8"
@ -87,7 +86,7 @@ func (c *semtok) Run(ctx context.Context, args ...string) error {
return err
}
buf, err := ioutil.ReadFile(args[0])
buf, err := os.ReadFile(args[0])
if err != nil {
return err
}
@ -162,7 +161,7 @@ func markLine(m mark, lines [][]byte) {
}
func decorate(file string, result []uint32) error {
buf, err := ioutil.ReadFile(file)
buf, err := os.ReadFile(file)
if err != nil {
return err
}

Просмотреть файл

@ -5,7 +5,7 @@
package command_test
import (
"io/ioutil"
"os"
"testing"
"github.com/google/go-cmp/cmp"
@ -17,7 +17,7 @@ func TestGenerated(t *testing.T) {
testenv.NeedsGoPackages(t)
testenv.NeedsLocalXTools(t)
onDisk, err := ioutil.ReadFile("command_gen.go")
onDisk, err := os.ReadFile("command_gen.go")
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -8,7 +8,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -92,7 +91,7 @@ func NewSandbox(config *SandboxConfig) (_ *Sandbox, err error) {
rootDir := config.RootDir
if rootDir == "" {
rootDir, err = ioutil.TempDir(config.RootDir, "gopls-sandbox-")
rootDir, err = os.MkdirTemp(config.RootDir, "gopls-sandbox-")
if err != nil {
return nil, fmt.Errorf("creating temporary workdir: %v", err)
}
@ -150,7 +149,7 @@ func NewSandbox(config *SandboxConfig) (_ *Sandbox, err error) {
// is the responsibility of the caller to call os.RemoveAll on the returned
// file path when it is no longer needed.
func Tempdir(files map[string][]byte) (string, error) {
dir, err := ioutil.TempDir("", "gopls-tempdir-")
dir, err := os.MkdirTemp("", "gopls-tempdir-")
if err != nil {
return "", err
}

Просмотреть файл

@ -10,7 +10,6 @@ import (
"crypto/sha256"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -57,7 +56,7 @@ func writeFileData(path string, content []byte, rel RelativeTo) error {
}
backoff := 1 * time.Millisecond
for {
err := ioutil.WriteFile(fp, []byte(content), 0644)
err := os.WriteFile(fp, []byte(content), 0644)
if err != nil {
// This lock file violation is not handled by the robustio package, as it
// indicates a real race condition that could be avoided.
@ -160,7 +159,7 @@ func toURI(fp string) protocol.DocumentURI {
func (w *Workdir) ReadFile(path string) ([]byte, error) {
backoff := 1 * time.Millisecond
for {
b, err := ioutil.ReadFile(w.AbsPath(path))
b, err := os.ReadFile(w.AbsPath(path))
if err != nil {
if runtime.GOOS == "plan9" && strings.HasSuffix(err.Error(), " exclusive use file already open") {
// Plan 9 enforces exclusive access to locked files.
@ -370,7 +369,7 @@ func (w *Workdir) pollFiles() ([]protocol.FileEvent, error) {
// must read the file contents.
id := fileID{mtime: info.ModTime()}
if time.Since(info.ModTime()) < 2*time.Second {
data, err := ioutil.ReadFile(fp)
data, err := os.ReadFile(fp)
if err != nil {
return err
}
@ -397,7 +396,7 @@ func (w *Workdir) pollFiles() ([]protocol.FileEvent, error) {
// In this case, read the content to check whether the file actually
// changed.
if oldID.mtime.Equal(id.mtime) && oldID.hash != "" && id.hash == "" {
data, err := ioutil.ReadFile(fp)
data, err := os.ReadFile(fp)
if err != nil {
return err
}

Просмотреть файл

@ -6,7 +6,6 @@ package fake
import (
"context"
"io/ioutil"
"os"
"sync"
"testing"
@ -32,7 +31,7 @@ Hello World!
func newWorkdir(t *testing.T, txt string) (*Workdir, *eventBuffer, func()) {
t.Helper()
tmpdir, err := ioutil.TempDir("", "goplstest-workdir-")
tmpdir, err := os.MkdirTemp("", "goplstest-workdir-")
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -11,7 +11,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
@ -77,7 +76,7 @@ func TestMain(m *testing.M) {
func getTempDir() string {
makeTempDirOnce.Do(func() {
var err error
tempDir, err = ioutil.TempDir("", "gopls-bench")
tempDir, err = os.MkdirTemp("", "gopls-bench")
if err != nil {
log.Fatal(err)
}

Просмотреть файл

@ -27,12 +27,11 @@ go 1.14
package main
import (
"io/ioutil"
"os"
)
func main() {
ioutil.WriteFile("generated.go", []byte("package " + os.Args[1] + "\n\nconst Answer = 21"), 0644)
os.WriteFile("generated.go", []byte("package " + os.Args[1] + "\n\nconst Answer = 21"), 0644)
}
-- lib1/lib.go --

Просмотреть файл

@ -5,7 +5,6 @@
package misc
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -175,7 +174,7 @@ import "example.com/x"
var _, _ = x.X, y.Y
`
modcache, err := ioutil.TempDir("", "TestGOMODCACHE-modcache")
modcache, err := os.MkdirTemp("", "TestGOMODCACHE-modcache")
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -13,7 +13,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
@ -41,7 +40,7 @@ import (
// The returned DB's Clean method must be called to clean up the
// generated database.
func NewDatabase(ctx context.Context, txtarReports []byte) (*DB, error) {
disk, err := ioutil.TempDir("", "vulndb-test")
disk, err := os.MkdirTemp("", "vulndb-test")
if err != nil {
return nil, err
}

Просмотреть файл

@ -10,7 +10,6 @@ package vulntest
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -19,7 +18,7 @@ import (
)
func readAll(t *testing.T, filename string) io.Reader {
d, err := ioutil.ReadFile(filename)
d, err := os.ReadFile(filename)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -15,7 +15,6 @@ import (
"flag"
"fmt"
"go/types"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -109,7 +108,7 @@ func validateHardcodedVersion(version string) error {
func validateGoModFile(goplsDir string) error {
filename := filepath.Join(goplsDir, "go.mod")
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
return err
}

Просмотреть файл

@ -7,8 +7,8 @@
package imports // import "golang.org/x/tools/imports"
import (
"io/ioutil"
"log"
"os"
"golang.org/x/tools/internal/gocommand"
intimp "golang.org/x/tools/internal/imports"
@ -44,7 +44,7 @@ var LocalPrefix string
func Process(filename string, src []byte, opt *Options) ([]byte, error) {
var err error
if src == nil {
src, err = ioutil.ReadFile(filename)
src, err = os.ReadFile(filename)
if err != nil {
return nil, err
}

Просмотреть файл

@ -8,7 +8,6 @@ import (
"bufio"
"fmt"
"go/types"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -21,7 +20,7 @@ import (
)
func TestChanges(t *testing.T) {
dir, err := ioutil.TempDir("", "apidiff_test")
dir, err := os.MkdirTemp("", "apidiff_test")
if err != nil {
t.Fatal(err)
}
@ -66,7 +65,7 @@ func splitIntoPackages(t *testing.T, dir string) (incompatibles, compatibles []s
if err := os.MkdirAll(filepath.Join(dir, "src", "apidiff"), 0700); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dir, "src", "apidiff", "go.mod"), []byte("module apidiff\n"), 0666); err != nil {
if err := os.WriteFile(filepath.Join(dir, "src", "apidiff", "go.mod"), []byte("module apidiff\n"), 0666); err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -9,7 +9,6 @@ package difftest_test
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
@ -41,7 +40,7 @@ func TestVerifyUnified(t *testing.T) {
}
func getDiffOutput(a, b string) (string, error) {
fileA, err := ioutil.TempFile("", "myers.in")
fileA, err := os.CreateTemp("", "myers.in")
if err != nil {
return "", err
}
@ -52,7 +51,7 @@ func getDiffOutput(a, b string) (string, error) {
if err := fileA.Close(); err != nil {
return "", err
}
fileB, err := ioutil.TempFile("", "myers.in")
fileB, err := os.CreateTemp("", "myers.in")
if err != nil {
return "", err
}

Просмотреть файл

@ -6,9 +6,9 @@ package lcs
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"strings"
"testing"
)
@ -218,7 +218,7 @@ func genBench(set string, n int) []struct{ before, after string } {
// itself minus the last byte is faster still; I don't know why.
// There is much low-hanging fruit here for further improvement.
func BenchmarkLargeFileSmallDiff(b *testing.B) {
data, err := ioutil.ReadFile("old.go") // large file
data, err := os.ReadFile("old.go") // large file
if err != nil {
log.Fatal(err)
}

Просмотреть файл

@ -6,7 +6,7 @@ package event_test
import (
"context"
"io/ioutil"
"io"
"log"
"testing"
@ -119,7 +119,7 @@ func Benchmark(b *testing.B) {
b.Run(t.name+"Noop", t.test)
}
event.SetExporter(export.Spans(export.LogWriter(ioutil.Discard, false)))
event.SetExporter(export.Spans(export.LogWriter(io.Discard, false)))
for _, t := range benchmarks {
b.Run(t.name, t.test)
}
@ -150,7 +150,7 @@ func (hooks Hooks) runBenchmark(b *testing.B) {
}
func init() {
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
}
func noopExporter(ctx context.Context, ev core.Event, lm label.Map) context.Context {

Просмотреть файл

@ -9,7 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"sync"
"testing"
@ -191,7 +191,7 @@ func (s *fakeSender) RoundTrip(req *http.Request) (*http.Response, error) {
if s.data == nil {
s.data = make(map[string][]byte)
}
data, err := ioutil.ReadAll(req.Body)
data, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}

Просмотреть файл

@ -40,7 +40,7 @@ import (
"encoding/gob"
"fmt"
"go/types"
"io/ioutil"
"io"
"log"
"reflect"
"sort"
@ -356,7 +356,7 @@ func (s *Set) Encode(skipMethodSorting bool) []byte {
if err := gob.NewEncoder(&buf).Encode(gobFacts); err != nil {
// Fact encoding should never fail. Identify the culprit.
for _, gf := range gobFacts {
if err := gob.NewEncoder(ioutil.Discard).Encode(gf); err != nil {
if err := gob.NewEncoder(io.Discard).Encode(gf); err != nil {
fact := gf.Fact
pkgpath := reflect.TypeOf(fact).Elem().PkgPath()
log.Panicf("internal error: gob encoding of analysis fact %s failed: %v; please report a bug against fact %T in package %q",

Просмотреть файл

@ -8,7 +8,7 @@
package fastwalk
import (
"io/ioutil"
"io/fs"
"os"
)
@ -17,7 +17,7 @@ import (
// If fn returns a non-nil error, readDir returns with that error
// immediately.
func readDir(dirName string, fn func(dirName, entName string, typ os.FileMode) error) error {
fis, err := ioutil.ReadDir(dirName)
fis, err := ioutilReadDir(dirName)
if err != nil {
return err
}
@ -36,3 +36,20 @@ func readDir(dirName string, fn func(dirName, entName string, typ os.FileMode) e
}
return nil
}
func ioutilReadDir(dirname string) ([]fs.FileInfo, error) {
entries, err := os.ReadDir(dirname)
if err != nil {
return nil, err
}
infos := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return infos, err
}
infos = append(infos, info)
}
return infos, nil
}

Просмотреть файл

@ -8,7 +8,6 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -35,7 +34,7 @@ func formatFileModes(m map[string]os.FileMode) string {
}
func testFastWalk(t *testing.T, files map[string]string, callback func(path string, typ os.FileMode) error, want map[string]os.FileMode) {
tempdir, err := ioutil.TempDir("", "test-fast-walk")
tempdir, err := os.MkdirTemp("", "test-fast-walk")
if err != nil {
t.Fatal(err)
}
@ -51,7 +50,7 @@ func testFastWalk(t *testing.T, files map[string]string, callback func(path stri
if strings.HasPrefix(contents, "LINK:") {
symlinks[file] = filepath.FromSlash(strings.TrimPrefix(contents, "LINK:"))
} else {
err = ioutil.WriteFile(file, []byte(contents), 0644)
err = os.WriteFile(file, []byte(contents), 0644)
}
if err != nil {
t.Fatal(err)
@ -63,7 +62,7 @@ func testFastWalk(t *testing.T, files map[string]string, callback func(path stri
for file, dst := range symlinks {
err = os.Symlink(dst, file)
if err != nil {
if writeErr := ioutil.WriteFile(file, []byte(dst), 0644); writeErr == nil {
if writeErr := os.WriteFile(file, []byte(dst), 0644); writeErr == nil {
// Couldn't create symlink, but could write the file.
// Probably this filesystem doesn't support symlinks.
// (Perhaps we are on an older Windows and not running as administrator.)

Просмотреть файл

@ -29,7 +29,6 @@ import (
"go/token"
"go/types"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -221,7 +220,7 @@ func Import(packages map[string]*types.Package, path, srcDir string, lookup func
switch hdr {
case "$$B\n":
var data []byte
data, err = ioutil.ReadAll(buf)
data, err = io.ReadAll(buf)
if err != nil {
break
}

Просмотреть файл

@ -17,7 +17,7 @@ import (
goparser "go/parser"
"go/token"
"go/types"
"io/ioutil"
"io/fs"
"os"
"os/exec"
"path"
@ -105,7 +105,7 @@ func testPath(t *testing.T, path, srcDir string) *types.Package {
}
func mktmpdir(t *testing.T) string {
tmpdir, err := ioutil.TempDir("", "gcimporter_test")
tmpdir, err := os.MkdirTemp("", "gcimporter_test")
if err != nil {
t.Fatal("mktmpdir:", err)
}
@ -286,7 +286,7 @@ func TestVersionHandling(t *testing.T) {
needsCompiler(t, "gc")
const dir = "./testdata/versions"
list, err := ioutil.ReadDir(dir)
list, err := ioutilReadDir(dir)
if err != nil {
t.Fatal(err)
}
@ -321,7 +321,7 @@ func TestVersionHandling(t *testing.T) {
// create file with corrupted export data
// 1) read file
data, err := ioutil.ReadFile(filepath.Join(dir, name))
data, err := os.ReadFile(filepath.Join(dir, name))
if err != nil {
t.Fatal(err)
}
@ -338,7 +338,7 @@ func TestVersionHandling(t *testing.T) {
// 4) write the file
pkgpath += "_corrupted"
filename := filepath.Join(corruptdir, pkgpath) + ".a"
ioutil.WriteFile(filename, data, 0666)
os.WriteFile(filename, data, 0666)
// test that importing the corrupted file results in an error
_, err = Import(make(map[string]*types.Package), pkgpath, corruptdir, nil)
@ -1008,3 +1008,20 @@ func lookupObj(t *testing.T, scope *types.Scope, name string) types.Object {
t.Fatalf("%s not found", name)
return nil
}
func ioutilReadDir(dirname string) ([]fs.FileInfo, error) {
entries, err := os.ReadDir(dirname)
if err != nil {
return nil, err
}
infos := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return infos, err
}
infos = append(infos, info)
}
return infos, nil
}

Просмотреть файл

@ -19,7 +19,7 @@ import (
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"io"
"math/big"
"os"
"reflect"
@ -55,7 +55,7 @@ func readExportFile(filename string) ([]byte, error) {
return nil, fmt.Errorf("unexpected byte: %v", ch)
}
return ioutil.ReadAll(buf)
return io.ReadAll(buf)
}
func iexport(fset *token.FileSet, version int, pkg *types.Package) ([]byte, error) {

Просмотреть файл

@ -5,7 +5,6 @@
package gopathwalk
import (
"io/ioutil"
"log"
"os"
"path/filepath"
@ -22,7 +21,7 @@ func TestShouldTraverse(t *testing.T) {
t.Skipf("skipping symlink-requiring test on %s", runtime.GOOS)
}
dir, err := ioutil.TempDir("", "goimports-")
dir, err := os.MkdirTemp("", "goimports-")
if err != nil {
t.Fatal(err)
}
@ -90,7 +89,7 @@ func TestShouldTraverse(t *testing.T) {
// TestSkip tests that various goimports rules are followed in non-modules mode.
func TestSkip(t *testing.T) {
dir, err := ioutil.TempDir("", "goimports-")
dir, err := os.MkdirTemp("", "goimports-")
if err != nil {
t.Fatal(err)
}
@ -125,7 +124,7 @@ func TestSkip(t *testing.T) {
// TestSkipFunction tests that scan successfully skips directories from user callback.
func TestSkipFunction(t *testing.T) {
dir, err := ioutil.TempDir("", "goimports-")
dir, err := os.MkdirTemp("", "goimports-")
if err != nil {
t.Fatal(err)
}
@ -165,7 +164,7 @@ func mapToDir(destDir string, files map[string]string) error {
if strings.HasPrefix(contents, "LINK:") {
err = os.Symlink(strings.TrimPrefix(contents, "LINK:"), file)
} else {
err = ioutil.WriteFile(file, []byte(contents), 0644)
err = os.WriteFile(file, []byte(contents), 0644)
}
if err != nil {
return err

Просмотреть файл

@ -13,7 +13,6 @@ import (
"go/build"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -107,7 +106,7 @@ func parseOtherFiles(fset *token.FileSet, srcDir, filename string) []*ast.File {
considerTests := strings.HasSuffix(filename, "_test.go")
fileBase := filepath.Base(filename)
packageFileInfos, err := ioutil.ReadDir(srcDir)
packageFileInfos, err := ioutilReadDir(srcDir)
if err != nil {
return nil
}
@ -974,7 +973,7 @@ func (e *ProcessEnv) buildContext() (*build.Context, error) {
// HACK: setting any of the Context I/O hooks prevents Import from invoking
// 'go list', regardless of GO111MODULE. This is undocumented, but it's
// unlikely to change before GOPATH support is removed.
ctx.ReadDir = ioutil.ReadDir
ctx.ReadDir = ioutilReadDir
return &ctx, nil
}
@ -1469,7 +1468,7 @@ func VendorlessPath(ipath string) string {
func loadExportsFromFiles(ctx context.Context, env *ProcessEnv, dir string, includeTest bool) (string, []string, error) {
// Look for non-test, buildable .go files which could provide exports.
all, err := ioutil.ReadDir(dir)
all, err := ioutilReadDir(dir)
if err != nil {
return "", nil, err
}

Просмотреть файл

@ -9,8 +9,8 @@ import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"sort"
@ -1700,7 +1700,7 @@ func (t *goimportTest) process(module, file string, contents []byte, opts *Optio
func (t *goimportTest) processNonModule(file string, contents []byte, opts *Options) ([]byte, error) {
if contents == nil {
var err error
contents, err = ioutil.ReadFile(file)
contents, err = os.ReadFile(file)
if err != nil {
return nil, err
}

Просмотреть файл

@ -19,7 +19,6 @@ import (
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"path"
@ -88,7 +87,7 @@ func main() {
}
// Write out source file.
err = ioutil.WriteFile("pkgindex.go", src, 0644)
err = os.WriteFile("pkgindex.go", src, 0644)
if err != nil {
log.Fatal(err)
}

Некоторые файлы не были показаны из-за слишком большого количества измененных файлов Показать больше