Skip to content

Commit

Permalink
all: replace io/ioutil with io and os package
Browse files Browse the repository at this point in the history
For #45557

Change-Id: Icd0ff87bf46592e89cd4c88754cf07c3b0c300e6
GitHub-Last-Rev: f13b442
GitHub-Pull-Request: #43
Reviewed-on: https://go-review.googlesource.com/c/exp/+/430795
Run-TryBot: Jenny Rakoczy <[email protected]>
Reviewed-by: Jenny Rakoczy <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Ian Lance Taylor <[email protected]>
Run-TryBot: Ian Lance Taylor <[email protected]>
Reviewed-by: Meng Zhuo <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Run-TryBot: Meng Zhuo <[email protected]>
Auto-Submit: Jenny Rakoczy <[email protected]>
  • Loading branch information
cuishuang authored and gopherbot committed Sep 15, 2022
1 parent 2d61f44 commit 840b380
Show file tree
Hide file tree
Showing 25 changed files with 77 additions and 92 deletions.
5 changes: 2 additions & 3 deletions apidiff/apidiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"fmt"
"go/types"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -18,7 +17,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)
}
Expand Down Expand Up @@ -63,7 +62,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)
}

Expand Down
33 changes: 16 additions & 17 deletions cmd/gorelease/gorelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ import (
"fmt"
"go/build"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -166,7 +165,7 @@ func runRelease(ctx context.Context, w io.Writer, dir string, args []string) (su
// test without printing to stderr.
fs := flag.NewFlagSet("gorelease", flag.ContinueOnError)
fs.Usage = func() {}
fs.SetOutput(ioutil.Discard)
fs.SetOutput(io.Discard)
var baseOpt, releaseVersion string
fs.StringVar(&baseOpt, "base", "", "previous version to compare against")
fs.StringVar(&releaseVersion, "version", "", "proposed version to be released")
Expand Down Expand Up @@ -309,7 +308,7 @@ func loadLocalModule(ctx context.Context, modRoot, repoRoot, version string) (m
m.diagnostics = append(m.diagnostics, fmt.Sprintf("Version %s is lower than most pseudo-versions. Consider releasing v0.1.0-0 instead.", version))
}

m.goModData, err = ioutil.ReadFile(m.goModPath)
m.goModData, err = os.ReadFile(m.goModPath)
if err != nil {
return moduleInfo{}, err
}
Expand Down Expand Up @@ -371,7 +370,7 @@ func loadLocalModule(ctx context.Context, modRoot, repoRoot, version string) (m
// Modules with major version suffixes can be defined in two places
// (e.g., sub/go.mod and sub/v2/go.mod). They must not be defined in both.
if altGoModPath != "" {
if data, err := ioutil.ReadFile(altGoModPath); err == nil {
if data, err := os.ReadFile(altGoModPath); err == nil {
if altModPath := modfile.ModulePath(data); m.modPath == altModPath {
goModRel, _ := filepath.Rel(repoRoot, m.goModPath)
altGoModRel, _ := filepath.Rel(repoRoot, altGoModPath)
Expand Down Expand Up @@ -511,7 +510,7 @@ func loadDownloadedModule(ctx context.Context, modPath, version, max string) (m
if m.modRoot, m.goModPath, err = downloadModule(ctx, v); err != nil {
return moduleInfo{}, err
}
if m.goModData, err = ioutil.ReadFile(m.goModPath); err != nil {
if m.goModData, err = os.ReadFile(m.goModPath); err != nil {
return moduleInfo{}, err
}
if m.goModFile, err = modfile.ParseLax(m.goModPath, m.goModData, nil); err != nil {
Expand Down Expand Up @@ -774,7 +773,7 @@ func queryVersion(ctx context.Context, modPath, query string) (resolved string,
return "", errors.New("query is based on requirements in main go.mod file")
}

tmpDir, err := ioutil.TempDir("", "")
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return "", err
}
Expand Down Expand Up @@ -805,7 +804,7 @@ func loadVersions(ctx context.Context, modPath string) (versions []string, err e
}
}()

tmpDir, err := ioutil.TempDir("", "")
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -882,7 +881,7 @@ func copyModuleToTempDir(repoRoot, modPath, modRoot string) (dir string, err err
}
m := module.Version{Path: modPath, Version: version}

zipFile, err := ioutil.TempFile("", "gorelease-*.zip")
zipFile, err := os.CreateTemp("", "gorelease-*.zip")
if err != nil {
return "", err
}
Expand All @@ -891,7 +890,7 @@ func copyModuleToTempDir(repoRoot, modPath, modRoot string) (dir string, err err
os.Remove(zipFile.Name())
}()

dir, err = ioutil.TempDir("", "gorelease")
dir, err = os.MkdirTemp("", "gorelease")
if err != nil {
return "", err
}
Expand Down Expand Up @@ -980,7 +979,7 @@ func downloadModule(ctx context.Context, m module.Version) (modRoot, goModPath s
// TODO(golang.org/issue/36812): 'go mod download' reads go.mod even though
// we don't need information about the main module or the build list.
// If it didn't read go.mod in this case, we wouldn't need a temp directory.
tmpDir, err := ioutil.TempDir("", "gorelease-download")
tmpDir, err := os.MkdirTemp("", "gorelease-download")
if err != nil {
return "", "", err
}
Expand Down Expand Up @@ -1073,7 +1072,7 @@ func prepareLoadDir(ctx context.Context, modFile *modfile.File, modPath, modRoot
}
}

dir, err = ioutil.TempDir("", "gorelease-load")
dir, err = os.MkdirTemp("", "gorelease-load")
if err != nil {
return "", nil, nil, nil, nil, err
}
Expand All @@ -1096,15 +1095,15 @@ func prepareLoadDir(ctx context.Context, modFile *modfile.File, modPath, modRoot
if err != nil {
return "", nil, nil, nil, nil, err
}
if err := ioutil.WriteFile(filepath.Join(dir, "go.mod"), goModData, 0666); err != nil {
if err := os.WriteFile(filepath.Join(dir, "go.mod"), goModData, 0666); err != nil {
return "", nil, nil, nil, nil, err
}

goSumData, err = ioutil.ReadFile(filepath.Join(modRoot, "go.sum"))
goSumData, err = os.ReadFile(filepath.Join(modRoot, "go.sum"))
if err != nil && !os.IsNotExist(err) {
return "", nil, nil, nil, nil, err
}
if err := ioutil.WriteFile(filepath.Join(dir, "go.sum"), goSumData, 0666); err != nil {
if err := os.WriteFile(filepath.Join(dir, "go.sum"), goSumData, 0666); err != nil {
return "", nil, nil, nil, nil, err
}

Expand All @@ -1119,7 +1118,7 @@ func prepareLoadDir(ctx context.Context, modFile *modfile.File, modPath, modRoot
for _, imp := range imps {
fmt.Fprintf(fakeImports, "import _ %q\n", imp)
}
if err := ioutil.WriteFile(filepath.Join(dir, "tmp.go"), []byte(fakeImports.String()), 0666); err != nil {
if err := os.WriteFile(filepath.Join(dir, "tmp.go"), []byte(fakeImports.String()), 0666); err != nil {
return "", nil, nil, nil, nil, err
}

Expand Down Expand Up @@ -1148,7 +1147,7 @@ func prepareLoadDir(ctx context.Context, modFile *modfile.File, modPath, modRoot
if err != nil {
return "", nil, nil, nil, nil, err
}
newGoModData, err := ioutil.ReadFile(goModPath)
newGoModData, err := os.ReadFile(goModPath)
if err != nil {
return "", nil, nil, nil, nil, err
}
Expand Down Expand Up @@ -1187,7 +1186,7 @@ func prepareLoadDir(ctx context.Context, modFile *modfile.File, modPath, modRoot
if !cached {
// Check if 'go get' added new hashes to go.sum.
goSumPath := filepath.Join(dir, "go.sum")
newGoSumData, err := ioutil.ReadFile(goSumPath)
newGoSumData, err := os.ReadFile(goSumPath)
if err != nil {
if !os.IsNotExist(err) {
return "", nil, nil, nil, nil, err
Expand Down
15 changes: 7 additions & 8 deletions cmd/gorelease/gorelease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -61,7 +60,7 @@ func prepareProxy(proxyVersions map[module.Version]bool, tests []*test) (ctx con
}
env = append(env, fmt.Sprintf("GOPROXY=%s", proxyURL))

cacheDir, err := ioutil.TempDir("", "gorelease_test-gocache")
cacheDir, err := os.MkdirTemp("", "gorelease_test-gocache")
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -267,7 +266,7 @@ func updateTest(t *test, want []byte) error {

wantFile.Data = want
data := txtar.Format(&t.Archive)
return ioutil.WriteFile(t.testPath, data, 0666)
return os.WriteFile(t.testPath, data, 0666)
}

func TestRelease(t *testing.T) {
Expand Down Expand Up @@ -303,7 +302,7 @@ func TestRelease(t *testing.T) {
func TestRelease_gitRepo_uncommittedChanges(t *testing.T) {
ctx := context.Background()
buf := &bytes.Buffer{}
releaseDir, err := ioutil.TempDir("", "")
releaseDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -314,7 +313,7 @@ func TestRelease_gitRepo_uncommittedChanges(t *testing.T) {
// Create an uncommitted change.
bContents := `package b
const B = "b"`
if err := ioutil.WriteFile(filepath.Join(releaseDir, "b.go"), []byte(bContents), 0644); err != nil {
if err := os.WriteFile(filepath.Join(releaseDir, "b.go"), []byte(bContents), 0644); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -347,7 +346,7 @@ func testRelease(ctx context.Context, tests []*test, test *test) func(t *testing

// Extract the files in the release version. They may be part of the
// test archive or in testdata/mod.
testDir, err := ioutil.TempDir("", "")
testDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -447,7 +446,7 @@ func hgInit(t *testing.T, dir string) {
t.Fatal(err)
}

if err := ioutil.WriteFile(filepath.Join(dir, ".hg", "branch"), []byte("default"), 0777); err != nil {
if err := os.WriteFile(filepath.Join(dir, ".hg", "branch"), []byte("default"), 0777); err != nil {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -488,7 +487,7 @@ func goModInit(t *testing.T, dir string) {

aContents := `package a
const A = "a"`
if err := ioutil.WriteFile(filepath.Join(dir, "a.go"), []byte(aContents), 0644); err != nil {
if err := os.WriteFile(filepath.Join(dir, "a.go"), []byte(aContents), 0644); err != nil {
t.Fatal(err)
}

Expand Down
15 changes: 7 additions & 8 deletions cmd/gorelease/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand All @@ -30,7 +29,7 @@ import (
// versions is non-empty, only those modules in mod/ that match an entry in
// proxyVersions will be included.
func buildProxyDir(proxyVersions map[module.Version]bool, tests []*test) (proxyDir, proxyURL string, err error) {
proxyDir, err = ioutil.TempDir("", "gorelease-proxy")
proxyDir, err = os.MkdirTemp("", "gorelease-proxy")
if err != nil {
return "", "", err
}
Expand Down Expand Up @@ -101,7 +100,7 @@ func buildProxyDir(proxyVersions map[module.Version]bool, tests []*test) (proxyD
haveMod = true
}
outPath := filepath.Join(modDir, version+af.Name)
if err := ioutil.WriteFile(outPath, af.Data, 0666); err != nil {
if err := os.WriteFile(outPath, af.Data, 0666); err != nil {
return "", "", err
}
continue
Expand All @@ -119,13 +118,13 @@ func buildProxyDir(proxyVersions map[module.Version]bool, tests []*test) (proxyD
if !haveInfo {
outPath := filepath.Join(modDir, version+".info")
outContent := fmt.Sprintf(`{"Version":"%s"}`, version)
if err := ioutil.WriteFile(outPath, []byte(outContent), 0666); err != nil {
if err := os.WriteFile(outPath, []byte(outContent), 0666); err != nil {
return "", "", err
}
}
if !haveMod && goMod.Name != "" {
outPath := filepath.Join(modDir, version+".mod")
if err := ioutil.WriteFile(outPath, goMod.Data, 0666); err != nil {
if err := os.WriteFile(outPath, goMod.Data, 0666); err != nil {
return "", "", err
}
}
Expand Down Expand Up @@ -155,7 +154,7 @@ func buildProxyDir(proxyVersions map[module.Version]bool, tests []*test) (proxyD
for _, v := range versions {
fmt.Fprintln(buf, v)
}
if err := ioutil.WriteFile(outPath, buf.Bytes(), 0666); err != nil {
if err := os.WriteFile(outPath, buf.Bytes(), 0666); err != nil {
return "", "", err
}
buf.Reset()
Expand All @@ -179,7 +178,7 @@ type txtarFile struct {
func (f txtarFile) Path() string { return f.f.Name }
func (f txtarFile) Lstat() (os.FileInfo, error) { return txtarFileInfo{f.f}, nil }
func (f txtarFile) Open() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(f.f.Data)), nil
return io.NopCloser(bytes.NewReader(f.f.Data)), nil
}

type txtarFileInfo struct {
Expand All @@ -199,7 +198,7 @@ func extractTxtar(destDir string, arc *txtar.Archive) error {
if err := os.MkdirAll(filepath.Dir(outPath), 0777); err != nil {
return err
}
if err := ioutil.WriteFile(outPath, f.Data, 0666); err != nil {
if err := os.WriteFile(outPath, f.Data, 0666); err != nil {
return err
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/macos-roots-test/root_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
Expand Down Expand Up @@ -142,7 +141,7 @@ func verifyCertWithSystem(cert *x509.Certificate) bool {
Type: "CERTIFICATE", Bytes: cert.Raw,
})

f, err := ioutil.TempFile("", "cert")
f, err := os.CreateTemp("", "cert")
if err != nil {
fmt.Fprintf(os.Stderr, "can't create temporary file for cert: %v", err)
return false
Expand Down
10 changes: 5 additions & 5 deletions cmd/txtar/txtar.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"io"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -79,7 +79,7 @@ func main() {
}

func extract() (err error) {
b, err := ioutil.ReadAll(os.Stdin)
b, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func extract() (err error) {
if err := os.MkdirAll(filepath.Dir(fileName), 0777); err != nil {
return err
}
if err := ioutil.WriteFile(fileName, f.Data, 0666); err != nil {
if err := os.WriteFile(fileName, f.Data, 0666); err != nil {
return err
}
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func archive(paths []string) (err error) {
}
name := filepath.ToSlash(filepath.Join(p, suffix))

data, err := ioutil.ReadFile(fileName)
data, err := os.ReadFile(fileName)
if err != nil {
return err
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func archive(paths []string) (err error) {
timer := time.AfterFunc(200*time.Millisecond, func() {
fmt.Fprintln(os.Stderr, "Enter comment:")
})
comment, err := ioutil.ReadAll(os.Stdin)
comment, err := io.ReadAll(os.Stdin)
timer.Stop()
if err != nil {
return fmt.Errorf("reading comment from %s: %v", os.Stdin.Name(), err)
Expand Down
Loading

0 comments on commit 840b380

Please sign in to comment.