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.
// (This should be a no-op, but truncate just in case of previous corruption.)
//
// This differs from ioutil.WriteFile, which truncates to 0 *before* writing
// This differs from os.WriteFile, which truncates to 0 *before* writing
// via os.O_TRUNC. Truncating only after writing ensures that a second write
// of the same content to the same file is idempotent, and does not — even
// temporarily! — undo the effect of the first write.

View File

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

View File

@ -6,7 +6,6 @@ package cache
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -39,7 +38,7 @@ func initDefaultCache() {
}
if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
// 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)
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"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 {
contentBytes, err := ioutil.ReadFile(path)
contentBytes, err := os.ReadFile(path)
if err != nil {
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)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
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) {
golangciYamlExample, err := ioutil.ReadFile(".golangci.example.yml")
golangciYamlExample, err := os.ReadFile(".golangci.example.yml")
if err != nil {
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"))
shortHelp := bytes.Join(helpLines[2:], []byte("\n"))
changeLog, err := ioutil.ReadFile("CHANGELOG.md")
changeLog, err := os.ReadFile("CHANGELOG.md")
if err != nil {
return nil, err
}

View File

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

View File

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

View File

@ -2,7 +2,6 @@ package test
import (
"bufio"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -99,7 +98,7 @@ func TestGciLocal(t *testing.T) {
}
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)
cfgPath = f.Name() + ".yml"

View File

@ -1,7 +1,6 @@
package testshared
import (
"io/ioutil"
"os"
"os/exec"
"strings"
@ -134,7 +133,7 @@ func (r *LintRunner) RunWithYamlConfig(cfg 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)
f.Close()
@ -149,7 +148,7 @@ func (r *LintRunner) RunCommandWithYamlConfig(cfg, command string, args ...strin
cfg = strings.TrimSpace(cfg)
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)
pargs := append([]string{"-c", cfgPath}, args...)