Skip to content

Commit e5cd59a

Browse files
authored
dev: replace ioutil with io and os (#2318)
1 parent e612577 commit e5cd59a

File tree

21 files changed

+40
-51
lines changed

21 files changed

+40
-51
lines changed

Diff for: internal/cache/cache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify
370370
// Truncate the file only *after* writing it.
371371
// (This should be a no-op, but truncate just in case of previous corruption.)
372372
//
373-
// This differs from ioutil.WriteFile, which truncates to 0 *before* writing
373+
// This differs from os.WriteFile, which truncates to 0 *before* writing
374374
// via os.O_TRUNC. Truncating only after writing ensures that a second write
375375
// of the same content to the same file is idempotent, and does not — even
376376
// temporarily! — undo the effect of the first write.

Diff for: internal/cache/cache_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/binary"
1010
"fmt"
11-
"io/ioutil"
1211
"os"
1312
"path/filepath"
1413
"testing"
@@ -22,7 +21,7 @@ func init() {
2221
func TestBasic(t *testing.T) {
2322
t.Parallel()
2423

25-
dir, err := ioutil.TempDir("", "cachetest-")
24+
dir, err := os.MkdirTemp("", "cachetest-")
2625
if err != nil {
2726
t.Fatal(err)
2827
}
@@ -69,7 +68,7 @@ func TestBasic(t *testing.T) {
6968
func TestGrowth(t *testing.T) {
7069
t.Parallel()
7170

72-
dir, err := ioutil.TempDir("", "cachetest-")
71+
dir, err := os.MkdirTemp("", "cachetest-")
7372
if err != nil {
7473
t.Fatal(err)
7574
}
@@ -122,7 +121,7 @@ func TestVerifyPanic(t *testing.T) {
122121
t.Fatal("initEnv did not set verify")
123122
}
124123

125-
dir, err := ioutil.TempDir("", "cachetest-")
124+
dir, err := os.MkdirTemp("", "cachetest-")
126125
if err != nil {
127126
t.Fatal(err)
128127
}
@@ -157,7 +156,7 @@ func dummyID(x int) [HashSize]byte {
157156
func TestCacheTrim(t *testing.T) {
158157
t.Parallel()
159158

160-
dir, err := ioutil.TempDir("", "cachetest-")
159+
dir, err := os.MkdirTemp("", "cachetest-")
161160
if err != nil {
162161
t.Fatal(err)
163162
}
@@ -213,7 +212,7 @@ func TestCacheTrim(t *testing.T) {
213212
t.Fatal(err)
214213
}
215214
c.OutputFile(entry.OutputID)
216-
data, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt"))
215+
data, err := os.ReadFile(filepath.Join(dir, "trim.txt"))
217216
if err != nil {
218217
t.Fatal(err)
219218
}
@@ -226,7 +225,7 @@ func TestCacheTrim(t *testing.T) {
226225
t.Fatal(err)
227226
}
228227
c.OutputFile(entry.OutputID)
229-
data2, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt"))
228+
data2, err := os.ReadFile(filepath.Join(dir, "trim.txt"))
230229
if err != nil {
231230
t.Fatal(err)
232231
}

Diff for: internal/cache/default.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cache
66

77
import (
88
"fmt"
9-
"io/ioutil"
109
"log"
1110
"os"
1211
"path/filepath"
@@ -39,7 +38,7 @@ func initDefaultCache() {
3938
}
4039
if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
4140
// Best effort.
42-
if wErr := ioutil.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666); wErr != nil {
41+
if wErr := os.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666); wErr != nil {
4342
log.Fatalf("Failed to write README file to cache dir %s: %s", dir, err)
4443
}
4544
}

Diff for: internal/cache/hash_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cache
66

77
import (
88
"fmt"
9-
"io/ioutil"
109
"os"
1110
"testing"
1211
)
@@ -28,7 +27,7 @@ func TestHash(t *testing.T) {
2827
}
2928

3029
func TestHashFile(t *testing.T) {
31-
f, err := ioutil.TempFile("", "cmd-go-test-")
30+
f, err := os.CreateTemp("", "cmd-go-test-")
3231
if err != nil {
3332
t.Fatal(err)
3433
}

Diff for: internal/renameio/renameio.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func Pattern(filename string) string {
2424
return filepath.Join(filepath.Dir(filename), filepath.Base(filename)+patternSuffix)
2525
}
2626

27-
// WriteFile is like ioutil.WriteFile, but first writes data to an arbitrary
27+
// WriteFile is like os.WriteFile, but first writes data to an arbitrary
2828
// file in the same directory as filename, then renames it atomically to the
2929
// final name.
3030
//
@@ -79,7 +79,7 @@ func tempFile(dir, prefix string, perm os.FileMode) (f *os.File, err error) {
7979
return
8080
}
8181

82-
// ReadFile is like ioutil.ReadFile, but on Windows retries spurious errors that
82+
// ReadFile is like os.ReadFile, but on Windows retries spurious errors that
8383
// may occur if the file is concurrently replaced.
8484
//
8585
// Errors are classified heuristically and retries are bounded, so even this

Diff for: internal/renameio/renameio_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package renameio
88

99
import (
1010
"encoding/binary"
11-
"io/ioutil"
1211
"math/rand"
1312
"os"
1413
"path/filepath"
@@ -23,7 +22,7 @@ import (
2322
)
2423

2524
func TestConcurrentReadsAndWrites(t *testing.T) {
26-
dir, err := ioutil.TempDir("", "renameio")
25+
dir, err := os.MkdirTemp("", "renameio")
2726
if err != nil {
2827
t.Fatal(err)
2928
}

Diff for: internal/renameio/umask_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
package renameio
88

99
import (
10-
"io/ioutil"
1110
"os"
1211
"path/filepath"
1312
"syscall"
1413
"testing"
1514
)
1615

1716
func TestWriteFileModeAppliesUmask(t *testing.T) {
18-
dir, err := ioutil.TempDir("", "renameio")
17+
dir, err := os.MkdirTemp("", "renameio")
1918
if err != nil {
2019
t.Fatalf("Failed to create temporary directory: %v", err)
2120
}

Diff for: internal/robustio/robustio.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Rename(oldpath, newpath string) error {
2222
return rename(oldpath, newpath)
2323
}
2424

25-
// ReadFile is like ioutil.ReadFile, but on Windows retries errors that may
25+
// ReadFile is like os.ReadFile, but on Windows retries errors that may
2626
// occur if the file is concurrently replaced.
2727
//
2828
// (See golang.org/issue/31247 and golang.org/issue/32188.)

Diff for: internal/robustio/robustio_flaky.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package robustio
88

99
import (
10-
"io/ioutil"
1110
"math/rand"
1211
"os"
1312
"syscall"
@@ -70,11 +69,11 @@ func rename(oldpath, newpath string) (err error) {
7069
})
7170
}
7271

73-
// readFile is like ioutil.ReadFile, but retries ephemeral errors.
72+
// readFile is like os.ReadFile, but retries ephemeral errors.
7473
func readFile(filename string) ([]byte, error) {
7574
var b []byte
7675
err := retry(func() (err error, mayRetry bool) {
77-
b, err = ioutil.ReadFile(filename)
76+
b, err = os.ReadFile(filename)
7877

7978
// Unlike in rename, we do not retry errFileNotFound here: it can occur
8079
// as a spurious error, but the file may also genuinely not exist, so the

Diff for: internal/robustio/robustio_other.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package robustio
88

99
import (
10-
"io/ioutil"
1110
"os"
1211
)
1312

@@ -16,7 +15,7 @@ func rename(oldpath, newpath string) error {
1615
}
1716

1817
func readFile(filename string) ([]byte, error) {
19-
return ioutil.ReadFile(filename)
18+
return os.ReadFile(filename)
2019
}
2120

2221
func removeAll(path string) error {

Diff for: pkg/commands/run.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package commands
33
import (
44
"context"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"log"
88
"os"
99
"runtime"
@@ -390,7 +390,7 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
390390

391391
if !logutils.HaveDebugTag("linters_output") {
392392
// Don't allow linters and loader to print anything
393-
log.SetOutput(ioutil.Discard)
393+
log.SetOutput(io.Discard)
394394
savedStdout, savedStderr := e.setOutputToDevNull()
395395
defer func() {
396396
os.Stdout, os.Stderr = savedStdout, savedStderr

Diff for: pkg/fsutils/filecache.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package fsutils
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"os"
66
"sync"
77

88
"github.com/pkg/errors"
@@ -24,7 +24,7 @@ func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) {
2424
return cachedBytes.([]byte), nil
2525
}
2626

27-
fileBytes, err := ioutil.ReadFile(filePath)
27+
fileBytes, err := os.ReadFile(filePath)
2828
if err != nil {
2929
return nil, errors.Wrapf(err, "can't read file %s", filePath)
3030
}

Diff for: pkg/golinters/gofumpt.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package golinters
33
import (
44
"bytes"
55
"fmt"
6-
"io/ioutil"
6+
"os"
77
"sync"
88

99
"github.com/pkg/errors"
@@ -50,7 +50,7 @@ func NewGofumpt() *goanalysis.Linter {
5050
var issues []goanalysis.Issue
5151

5252
for _, f := range fileNames {
53-
input, err := ioutil.ReadFile(f)
53+
input, err := os.ReadFile(f)
5454
if err != nil {
5555
return nil, fmt.Errorf("unable to open file %s: %w", f, err)
5656
}

Diff for: pkg/golinters/gosec.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package golinters
33
import (
44
"fmt"
55
"go/token"
6-
"io/ioutil"
6+
"io"
77
"log"
88
"strconv"
99
"strings"
@@ -42,7 +42,7 @@ func NewGosec(settings *config.GoSecSettings) *goanalysis.Linter {
4242

4343
ruleDefinitions := rules.Generate(filters...)
4444

45-
logger := log.New(ioutil.Discard, "", 0)
45+
logger := log.New(io.Discard, "", 0)
4646

4747
analyzer := &analysis.Analyzer{
4848
Name: gosecName,

Diff for: pkg/golinters/revive.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"go/token"
8-
"io/ioutil"
8+
"os"
99
"reflect"
1010

1111
"github.com/BurntSushi/toml"
@@ -65,7 +65,7 @@ func NewRevive(cfg *config.ReviveSettings) *goanalysis.Linter {
6565
return nil, err
6666
}
6767

68-
revive := lint.New(ioutil.ReadFile)
68+
revive := lint.New(os.ReadFile)
6969

7070
lintingRules, err := reviveConfig.GetLintingRules(conf)
7171
if err != nil {

Diff for: pkg/result/processors/diff.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"os"
98
"strings"
109

@@ -44,7 +43,7 @@ func (p Diff) Process(issues []result.Issue) ([]result.Issue, error) {
4443

4544
var patchReader io.Reader
4645
if p.patchFilePath != "" {
47-
patch, err := ioutil.ReadFile(p.patchFilePath)
46+
patch, err := os.ReadFile(p.patchFilePath)
4847
if err != nil {
4948
return nil, fmt.Errorf("can't read from patch file %s: %s", p.patchFilePath, err)
5049
}

Diff for: scripts/expand_website_templates/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"encoding/json"
88
"flag"
99
"fmt"
10-
"io/ioutil"
10+
"io"
1111
"log"
1212
"net/http"
1313
"os"
@@ -96,7 +96,7 @@ func rewriteDocs(replacements map[string]string) error {
9696
}
9797

9898
func processDoc(path string, replacements map[string]string, madeReplacements map[string]bool) error {
99-
contentBytes, err := ioutil.ReadFile(path)
99+
contentBytes, err := os.ReadFile(path)
100100
if err != nil {
101101
return fmt.Errorf("failed to read %s: %w", path, err)
102102
}
@@ -147,7 +147,7 @@ func getLatestVersion() (string, error) {
147147
return "", fmt.Errorf("failed to get http response for the latest tag: %s", err)
148148
}
149149
defer resp.Body.Close()
150-
body, err := ioutil.ReadAll(resp.Body)
150+
body, err := io.ReadAll(resp.Body)
151151
if err != nil {
152152
return "", fmt.Errorf("failed to read a body for the latest tag: %s", err)
153153
}
@@ -160,7 +160,7 @@ func getLatestVersion() (string, error) {
160160
}
161161

162162
func buildTemplateContext() (map[string]string, error) {
163-
golangciYamlExample, err := ioutil.ReadFile(".golangci.example.yml")
163+
golangciYamlExample, err := os.ReadFile(".golangci.example.yml")
164164
if err != nil {
165165
return nil, fmt.Errorf("can't read .golangci.example.yml: %s", err)
166166
}
@@ -191,7 +191,7 @@ func buildTemplateContext() (map[string]string, error) {
191191

192192
helpLines := bytes.Split(help, []byte("\n"))
193193
shortHelp := bytes.Join(helpLines[2:], []byte("\n"))
194-
changeLog, err := ioutil.ReadFile("CHANGELOG.md")
194+
changeLog, err := os.ReadFile("CHANGELOG.md")
195195
if err != nil {
196196
return nil, err
197197
}

Diff for: test/errchk.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"io/ioutil"
87
"log"
8+
"os"
99
"regexp"
1010
"strconv"
1111
"strings"
@@ -183,7 +183,7 @@ var (
183183
func wantedErrors(file, short, defaultLinter string) (errs []wantedError) {
184184
cache := make(map[string]*regexp.Regexp)
185185

186-
src, err := ioutil.ReadFile(file)
186+
src, err := os.ReadFile(file)
187187
if err != nil {
188188
log.Fatal(err)
189189
}

Diff for: test/fix_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package test
22

33
import (
4-
"io/ioutil"
54
"os"
65
"os/exec"
76
"path/filepath"
@@ -54,10 +53,10 @@ func TestFix(t *testing.T) {
5453
require.NoError(t, err)
5554

5655
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...)
57-
output, err := ioutil.ReadFile(input)
56+
output, err := os.ReadFile(input)
5857
require.NoError(t, err)
5958

60-
expectedOutput, err := ioutil.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
59+
expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
6160
require.NoError(t, err)
6261

6362
require.Equal(t, string(expectedOutput), string(output))

0 commit comments

Comments
 (0)