dev: replace ioutil with io and os (#2318)

This commit is contained in:
Benjamin 2021-11-02 03:21:26 +08:00 committed by GitHub
parent e612577dfc
commit e5cd59a607
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 40 additions and 51 deletions

View File

@ -370,7 +370,7 @@ func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify
// Truncate the file only *after* writing it. // Truncate the file only *after* writing it.
// (This should be a no-op, but truncate just in case of previous corruption.) // (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 // 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 // of the same content to the same file is idempotent, and does not — even
// temporarily! — undo the effect of the first write. // temporarily! — undo the effect of the first write.

View File

@ -8,7 +8,6 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -22,7 +21,7 @@ func init() {
func TestBasic(t *testing.T) { func TestBasic(t *testing.T) {
t.Parallel() t.Parallel()
dir, err := ioutil.TempDir("", "cachetest-") dir, err := os.MkdirTemp("", "cachetest-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -69,7 +68,7 @@ func TestBasic(t *testing.T) {
func TestGrowth(t *testing.T) { func TestGrowth(t *testing.T) {
t.Parallel() t.Parallel()
dir, err := ioutil.TempDir("", "cachetest-") dir, err := os.MkdirTemp("", "cachetest-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -122,7 +121,7 @@ func TestVerifyPanic(t *testing.T) {
t.Fatal("initEnv did not set verify") t.Fatal("initEnv did not set verify")
} }
dir, err := ioutil.TempDir("", "cachetest-") dir, err := os.MkdirTemp("", "cachetest-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -157,7 +156,7 @@ func dummyID(x int) [HashSize]byte {
func TestCacheTrim(t *testing.T) { func TestCacheTrim(t *testing.T) {
t.Parallel() t.Parallel()
dir, err := ioutil.TempDir("", "cachetest-") dir, err := os.MkdirTemp("", "cachetest-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -213,7 +212,7 @@ func TestCacheTrim(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
c.OutputFile(entry.OutputID) 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -226,7 +225,7 @@ func TestCacheTrim(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
c.OutputFile(entry.OutputID) 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -6,7 +6,6 @@ package cache
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -39,7 +38,7 @@ func initDefaultCache() {
} }
if _, err := os.Stat(filepath.Join(dir, "README")); err != nil { if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
// Best effort. // Best effort.
if wErr := ioutil.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666); wErr != nil { if wErr := os.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666); wErr != nil {
log.Fatalf("Failed to write README file to cache dir %s: %s", dir, err) log.Fatalf("Failed to write README file to cache dir %s: %s", dir, err)
} }
} }

View File

@ -6,7 +6,6 @@ package cache
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"testing" "testing"
) )
@ -28,7 +27,7 @@ func TestHash(t *testing.T) {
} }
func TestHashFile(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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -24,7 +24,7 @@ func Pattern(filename string) string {
return filepath.Join(filepath.Dir(filename), filepath.Base(filename)+patternSuffix) 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 // file in the same directory as filename, then renames it atomically to the
// final name. // final name.
// //
@ -79,7 +79,7 @@ func tempFile(dir, prefix string, perm os.FileMode) (f *os.File, err error) {
return return
} }
// 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. // may occur if the file is concurrently replaced.
// //
// Errors are classified heuristically and retries are bounded, so even this // Errors are classified heuristically and retries are bounded, so even this

View File

@ -8,7 +8,6 @@ package renameio
import ( import (
"encoding/binary" "encoding/binary"
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"path/filepath" "path/filepath"
@ -23,7 +22,7 @@ import (
) )
func TestConcurrentReadsAndWrites(t *testing.T) { func TestConcurrentReadsAndWrites(t *testing.T) {
dir, err := ioutil.TempDir("", "renameio") dir, err := os.MkdirTemp("", "renameio")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -7,7 +7,6 @@
package renameio package renameio
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"syscall" "syscall"
@ -15,7 +14,7 @@ import (
) )
func TestWriteFileModeAppliesUmask(t *testing.T) { func TestWriteFileModeAppliesUmask(t *testing.T) {
dir, err := ioutil.TempDir("", "renameio") dir, err := os.MkdirTemp("", "renameio")
if err != nil { if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err) 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) 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. // occur if the file is concurrently replaced.
// //
// (See golang.org/issue/31247 and golang.org/issue/32188.) // (See golang.org/issue/31247 and golang.org/issue/32188.)

View File

@ -7,7 +7,6 @@
package robustio package robustio
import ( import (
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"syscall" "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) { func readFile(filename string) ([]byte, error) {
var b []byte var b []byte
err := retry(func() (err error, mayRetry bool) { 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 // 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 // as a spurious error, but the file may also genuinely not exist, so the

View File

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

View File

@ -3,7 +3,7 @@ package commands
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"os" "os"
"runtime" "runtime"
@ -390,7 +390,7 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
if !logutils.HaveDebugTag("linters_output") { if !logutils.HaveDebugTag("linters_output") {
// Don't allow linters and loader to print anything // Don't allow linters and loader to print anything
log.SetOutput(ioutil.Discard) log.SetOutput(io.Discard)
savedStdout, savedStderr := e.setOutputToDevNull() savedStdout, savedStderr := e.setOutputToDevNull()
defer func() { defer func() {
os.Stdout, os.Stderr = savedStdout, savedStderr os.Stdout, os.Stderr = savedStdout, savedStderr

View File

@ -2,7 +2,7 @@ package fsutils
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
"sync" "sync"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -24,7 +24,7 @@ func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) {
return cachedBytes.([]byte), nil return cachedBytes.([]byte), nil
} }
fileBytes, err := ioutil.ReadFile(filePath) fileBytes, err := os.ReadFile(filePath)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "can't read file %s", filePath) return nil, errors.Wrapf(err, "can't read file %s", filePath)
} }

View File

@ -3,7 +3,7 @@ package golinters
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "os"
"sync" "sync"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -50,7 +50,7 @@ func NewGofumpt() *goanalysis.Linter {
var issues []goanalysis.Issue var issues []goanalysis.Issue
for _, f := range fileNames { for _, f := range fileNames {
input, err := ioutil.ReadFile(f) input, err := os.ReadFile(f)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to open file %s: %w", f, err) return nil, fmt.Errorf("unable to open file %s: %w", f, err)
} }

View File

@ -3,7 +3,7 @@ package golinters
import ( import (
"fmt" "fmt"
"go/token" "go/token"
"io/ioutil" "io"
"log" "log"
"strconv" "strconv"
"strings" "strings"
@ -42,7 +42,7 @@ func NewGosec(settings *config.GoSecSettings) *goanalysis.Linter {
ruleDefinitions := rules.Generate(filters...) ruleDefinitions := rules.Generate(filters...)
logger := log.New(ioutil.Discard, "", 0) logger := log.New(io.Discard, "", 0)
analyzer := &analysis.Analyzer{ analyzer := &analysis.Analyzer{
Name: gosecName, Name: gosecName,

View File

@ -5,7 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"go/token" "go/token"
"io/ioutil" "os"
"reflect" "reflect"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
@ -65,7 +65,7 @@ func NewRevive(cfg *config.ReviveSettings) *goanalysis.Linter {
return nil, err return nil, err
} }
revive := lint.New(ioutil.ReadFile) revive := lint.New(os.ReadFile)
lintingRules, err := reviveConfig.GetLintingRules(conf) lintingRules, err := reviveConfig.GetLintingRules(conf)
if err != nil { if err != nil {

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"strings" "strings"
@ -44,7 +43,7 @@ func (p Diff) Process(issues []result.Issue) ([]result.Issue, error) {
var patchReader io.Reader var patchReader io.Reader
if p.patchFilePath != "" { if p.patchFilePath != "" {
patch, err := ioutil.ReadFile(p.patchFilePath) patch, err := os.ReadFile(p.patchFilePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("can't read from patch file %s: %s", p.patchFilePath, err) return nil, fmt.Errorf("can't read from patch file %s: %s", p.patchFilePath, err)
} }

View File

@ -7,7 +7,7 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -96,7 +96,7 @@ func rewriteDocs(replacements map[string]string) error {
} }
func processDoc(path string, replacements map[string]string, madeReplacements map[string]bool) error { func processDoc(path string, replacements map[string]string, madeReplacements map[string]bool) error {
contentBytes, err := ioutil.ReadFile(path) contentBytes, err := os.ReadFile(path)
if err != nil { if err != nil {
return fmt.Errorf("failed to read %s: %w", path, err) return fmt.Errorf("failed to read %s: %w", path, err)
} }
@ -147,7 +147,7 @@ func getLatestVersion() (string, error) {
return "", fmt.Errorf("failed to get http response for the latest tag: %s", err) return "", fmt.Errorf("failed to get http response for the latest tag: %s", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to read a body for the latest tag: %s", err) return "", fmt.Errorf("failed to read a body for the latest tag: %s", err)
} }
@ -160,7 +160,7 @@ func getLatestVersion() (string, error) {
} }
func buildTemplateContext() (map[string]string, error) { func buildTemplateContext() (map[string]string, error) {
golangciYamlExample, err := ioutil.ReadFile(".golangci.example.yml") golangciYamlExample, err := os.ReadFile(".golangci.example.yml")
if err != nil { if err != nil {
return nil, fmt.Errorf("can't read .golangci.example.yml: %s", err) return nil, fmt.Errorf("can't read .golangci.example.yml: %s", err)
} }
@ -191,7 +191,7 @@ func buildTemplateContext() (map[string]string, error) {
helpLines := bytes.Split(help, []byte("\n")) helpLines := bytes.Split(help, []byte("\n"))
shortHelp := bytes.Join(helpLines[2:], []byte("\n")) shortHelp := bytes.Join(helpLines[2:], []byte("\n"))
changeLog, err := ioutil.ReadFile("CHANGELOG.md") changeLog, err := os.ReadFile("CHANGELOG.md")
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -4,8 +4,8 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -183,7 +183,7 @@ var (
func wantedErrors(file, short, defaultLinter string) (errs []wantedError) { func wantedErrors(file, short, defaultLinter string) (errs []wantedError) {
cache := make(map[string]*regexp.Regexp) cache := make(map[string]*regexp.Regexp)
src, err := ioutil.ReadFile(file) src, err := os.ReadFile(file)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -1,7 +1,6 @@
package test package test
import ( import (
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -54,10 +53,10 @@ func TestFix(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...) testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...)
output, err := ioutil.ReadFile(input) output, err := os.ReadFile(input)
require.NoError(t, err) require.NoError(t, err)
expectedOutput, err := ioutil.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input))) expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, string(expectedOutput), string(output)) require.Equal(t, string(expectedOutput), string(output))

View File

@ -2,7 +2,6 @@ package test
import ( import (
"bufio" "bufio"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -99,7 +98,7 @@ func TestGciLocal(t *testing.T) {
} }
func saveConfig(t *testing.T, cfg map[string]interface{}) (cfgPath string, finishFunc func()) { func saveConfig(t *testing.T, cfg map[string]interface{}) (cfgPath string, finishFunc func()) {
f, err := ioutil.TempFile("", "golangci_lint_test") f, err := os.CreateTemp("", "golangci_lint_test")
require.NoError(t, err) require.NoError(t, err)
cfgPath = f.Name() + ".yml" cfgPath = f.Name() + ".yml"

View File

@ -1,7 +1,6 @@
package testshared package testshared
import ( import (
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
@ -134,7 +133,7 @@ func (r *LintRunner) RunWithYamlConfig(cfg string, args ...string) *RunResult {
} }
func (r *LintRunner) RunCommandWithYamlConfig(cfg, command string, args ...string) *RunResult { func (r *LintRunner) RunCommandWithYamlConfig(cfg, command string, args ...string) *RunResult {
f, err := ioutil.TempFile("", "golangci_lint_test") f, err := os.CreateTemp("", "golangci_lint_test")
assert.NoError(r.t, err) assert.NoError(r.t, err)
f.Close() f.Close()
@ -149,7 +148,7 @@ func (r *LintRunner) RunCommandWithYamlConfig(cfg, command string, args ...strin
cfg = strings.TrimSpace(cfg) cfg = strings.TrimSpace(cfg)
cfg = strings.Replace(cfg, "\t", " ", -1) cfg = strings.Replace(cfg, "\t", " ", -1)
err = ioutil.WriteFile(cfgPath, []byte(cfg), os.ModePerm) err = os.WriteFile(cfgPath, []byte(cfg), os.ModePerm)
assert.NoError(r.t, err) assert.NoError(r.t, err)
pargs := append([]string{"-c", cfgPath}, args...) pargs := append([]string{"-c", cfgPath}, args...)