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

internal/cache/cache.go

Lines changed: 1 addition & 1 deletion
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.

internal/cache/cache_test.go

Lines changed: 6 additions & 7 deletions
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
}

internal/cache/default.go

Lines changed: 1 addition & 2 deletions
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
}

internal/cache/hash_test.go

Lines changed: 1 addition & 2 deletions
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
}

internal/renameio/renameio.go

Lines changed: 2 additions & 2 deletions
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

internal/renameio/renameio_test.go

Lines changed: 1 addition & 2 deletions
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
}

internal/renameio/umask_test.go

Lines changed: 1 addition & 2 deletions
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
}

internal/robustio/robustio.go

Lines changed: 1 addition & 1 deletion
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.)

internal/robustio/robustio_flaky.go

Lines changed: 2 additions & 3 deletions
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

internal/robustio/robustio_other.go

Lines changed: 1 addition & 2 deletions
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 {

0 commit comments

Comments
 (0)