Skip to content

Commit

Permalink
dev: replace ioutil with io and os (#2318)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrtr authored Nov 1, 2021
1 parent e612577 commit e5cd59a
Show file tree
Hide file tree
Showing 21 changed files with 40 additions and 51 deletions.
2 changes: 1 addition & 1 deletion internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 6 additions & 7 deletions internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/cache/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cache

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/cache/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cache

import (
"fmt"
"io/ioutil"
"os"
"testing"
)
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/renameio/renameio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/renameio/renameio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package renameio

import (
"encoding/binary"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand All @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/renameio/umask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
package renameio

import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
"testing"
)

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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/robustio/robustio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down
5 changes: 2 additions & 3 deletions internal/robustio/robustio_flaky.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package robustio

import (
"io/ioutil"
"math/rand"
"os"
"syscall"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/robustio/robustio_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package robustio

import (
"io/ioutil"
"os"
)

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package commands
import (
"context"
"fmt"
"io/ioutil"
"io"
"log"
"os"
"runtime"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/fsutils/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fsutils

import (
"fmt"
"io/ioutil"
"os"
"sync"

"github.com/pkg/errors"
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/golinters/gofumpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package golinters
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"sync"

"github.com/pkg/errors"
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/golinters/gosec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package golinters
import (
"fmt"
"go/token"
"io/ioutil"
"io"
"log"
"strconv"
"strings"
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions pkg/golinters/revive.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"
"go/token"
"io/ioutil"
"os"
"reflect"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/result/processors/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions scripts/expand_website_templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions test/errchk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions test/fix_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package test

import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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))
Expand Down
Loading

0 comments on commit e5cd59a

Please sign in to comment.