Skip to content

Commit b881b33

Browse files
committed
replace uses of deprecated io/ioutil
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent f0f4764 commit b881b33

File tree

14 files changed

+62
-69
lines changed

14 files changed

+62
-69
lines changed

assert/assert_ext_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"go/ast"
55
"go/parser"
66
"go/token"
7-
"io/ioutil"
7+
"os"
88
"runtime"
99
"strings"
1010
"testing"
@@ -33,7 +33,7 @@ that we are testing
3333
`
3434
assert.Equal(t, actual, expectedOne)
3535

36-
raw, err := ioutil.ReadFile(fileName(t))
36+
raw, err := os.ReadFile(fileName(t))
3737
assert.NilError(t, err)
3838

3939
expected := "var expectedOne = `this is the\nactual value\nthat we are testing\n`"
@@ -51,7 +51,7 @@ expected value
5151
`
5252
assert.Equal(t, actual, expectedTwo)
5353

54-
raw, err := ioutil.ReadFile(fileName(t))
54+
raw, err := os.ReadFile(fileName(t))
5555
assert.NilError(t, err)
5656

5757
expected := "const expectedTwo = `this is the new\nexpected value\n`"
@@ -72,7 +72,7 @@ for var inside function
7272

7373
assert.Equal(t, actual, expectedInsideFunc)
7474

75-
raw, err := ioutil.ReadFile(fileName(t))
75+
raw, err := os.ReadFile(fileName(t))
7676
assert.NilError(t, err)
7777

7878
expected := "expectedInsideFunc := `this is the new\nexpected value\nfor var inside function\n`"
@@ -93,7 +93,7 @@ for const inside function
9393

9494
assert.Equal(t, actual, expectedConstInsideFunc)
9595

96-
raw, err := ioutil.ReadFile(fileName(t))
96+
raw, err := os.ReadFile(fileName(t))
9797
assert.NilError(t, err)
9898

9999
expected := "const expectedConstInsideFunc = `this is the new\nexpected value\nfor const inside function\n`"

assert/cmd/gty-migrate-from-testify/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"go/ast"
99
"go/format"
1010
"go/token"
11-
"io/ioutil"
1211
"log"
1312
"os"
1413
"path"
@@ -126,7 +125,7 @@ func run(opts options) error {
126125
return fmt.Errorf("failed to format %s: %w", filename, err)
127126
}
128127

129-
if err := ioutil.WriteFile(absFilename, raw, 0); err != nil {
128+
if err := os.WriteFile(absFilename, raw, 0); err != nil {
130129
return fmt.Errorf("failed to write file %s: %w", filename, err)
131130
}
132131
}

assert/cmd/gty-migrate-from-testify/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"io/ioutil"
4+
"os"
55
"testing"
66

77
"github.com/google/go-cmp/cmp"
@@ -25,7 +25,7 @@ func TestRun(t *testing.T) {
2525
})
2626
assert.NilError(t, err)
2727

28-
raw, err := ioutil.ReadFile(dir.Join("src/example.com/example/some_test.go"))
28+
raw, err := os.ReadFile(dir.Join("src/example.com/example/some_test.go"))
2929
assert.NilError(t, err)
3030
golden.Assert(t, string(raw), "full-expected/some_test.go")
3131
}

fs/example_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fs_test
22

33
import (
4-
"io/ioutil"
54
"os"
65
"testing"
76

@@ -18,7 +17,7 @@ func ExampleNewDir() {
1817
dir := fs.NewDir(t, "test-name", fs.WithFile("file1", "content\n"))
1918
defer dir.Remove()
2019

21-
files, err := ioutil.ReadDir(dir.Path())
20+
files, err := os.ReadDir(dir.Path())
2221
assert.NilError(t, err)
2322
assert.Assert(t, cmp.Len(files, 0))
2423
}
@@ -28,7 +27,7 @@ func ExampleNewFile() {
2827
file := fs.NewFile(t, "test-name", fs.WithContent("content\n"), fs.AsUser(0, 0))
2928
defer file.Remove()
3029

31-
content, err := ioutil.ReadFile(file.Path())
30+
content, err := os.ReadFile(file.Path())
3231
assert.NilError(t, err)
3332
assert.Equal(t, "content\n", content)
3433
}

fs/file.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ contents and structure of a directory.
55
package fs // import "gotest.tools/v3/fs"
66

77
import (
8-
"io/ioutil"
98
"os"
109
"path/filepath"
1110
"runtime"
@@ -46,7 +45,7 @@ func NewFile(t assert.TestingT, prefix string, ops ...PathOp) *File {
4645
if ht, ok := t.(helperT); ok {
4746
ht.Helper()
4847
}
49-
tempfile, err := ioutil.TempFile("", cleanPrefix(prefix)+"-")
48+
tempfile, err := os.CreateTemp("", cleanPrefix(prefix)+"-")
5049
assert.NilError(t, err)
5150

5251
file := &File{path: tempfile.Name()}
@@ -89,7 +88,7 @@ func NewDir(t assert.TestingT, prefix string, ops ...PathOp) *Dir {
8988
if ht, ok := t.(helperT); ok {
9089
ht.Helper()
9190
}
92-
path, err := ioutil.TempDir("", cleanPrefix(prefix)+"-")
91+
path, err := os.MkdirTemp("", cleanPrefix(prefix)+"-")
9392
assert.NilError(t, err)
9493
dir := &Dir{path: path}
9594
cleanup.Cleanup(t, dir.Remove)

fs/file_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package fs_test
22

33
import (
44
"errors"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87
"testing"
@@ -97,15 +96,11 @@ func TestNewDir_IntegrationWithCleanup(t *testing.T) {
9796
}
9897

9998
func TestDirFromPath(t *testing.T) {
100-
tmpdir, err := ioutil.TempDir("", t.Name())
101-
assert.NilError(t, err)
102-
t.Cleanup(func() {
103-
os.RemoveAll(tmpdir)
104-
})
99+
tmpdir := t.TempDir()
105100

106101
dir := fs.DirFromPath(t, tmpdir, fs.WithFile("newfile", ""))
107102

108-
_, err = os.Stat(dir.Join("newfile"))
103+
_, err := os.Stat(dir.Join("newfile"))
109104
assert.NilError(t, err)
110105

111106
assert.Equal(t, dir.Path(), tmpdir)

fs/manifest.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package fs
33
import (
44
"fmt"
55
"io"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98

@@ -84,7 +83,7 @@ func manifestFromDir(path string) (Manifest, error) {
8483

8584
func newDirectory(path string, info os.FileInfo) (*directory, error) {
8685
items := make(map[string]dirEntry)
87-
children, err := ioutil.ReadDir(path)
86+
children, err := os.ReadDir(path)
8887
if err != nil {
8988
return nil, err
9089
}
@@ -103,7 +102,11 @@ func newDirectory(path string, info os.FileInfo) (*directory, error) {
103102
}, nil
104103
}
105104

106-
func getTypedResource(path string, info os.FileInfo) (dirEntry, error) {
105+
func getTypedResource(path string, entry os.DirEntry) (dirEntry, error) {
106+
info, err := entry.Info()
107+
if err != nil {
108+
return nil, err
109+
}
107110
switch {
108111
case info.IsDir():
109112
return newDirectory(path, info)

fs/manifest_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package fs
33
import (
44
"bytes"
55
"io"
6-
"io/ioutil"
76
"os"
87
"runtime"
98
"strings"
@@ -92,12 +91,12 @@ var cmpManifest = cmp.Options{
9291
if x == nil || y == nil {
9392
return x == y
9493
}
95-
xContent, err := ioutil.ReadAll(x)
94+
xContent, err := io.ReadAll(x)
9695
if err != nil {
9796
return false
9897
}
9998

100-
yContent, err := ioutil.ReadAll(y)
99+
yContent, err := io.ReadAll(y)
101100
if err != nil {
102101
return false
103102
}
@@ -106,5 +105,5 @@ var cmpManifest = cmp.Options{
106105
}
107106

108107
func readCloser(s string) io.ReadCloser {
109-
return ioutil.NopCloser(strings.NewReader(s))
108+
return io.NopCloser(strings.NewReader(s))
110109
}

fs/ops.go

Lines changed: 29 additions & 26 deletions
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
"path/filepath"
109
"strings"
@@ -43,29 +42,29 @@ type manifestDirectory interface {
4342
func WithContent(content string) PathOp {
4443
return func(path Path) error {
4544
if m, ok := path.(manifestFile); ok {
46-
m.SetContent(ioutil.NopCloser(strings.NewReader(content)))
45+
m.SetContent(io.NopCloser(strings.NewReader(content)))
4746
return nil
4847
}
49-
return ioutil.WriteFile(path.Path(), []byte(content), defaultFileMode)
48+
return os.WriteFile(path.Path(), []byte(content), defaultFileMode)
5049
}
5150
}
5251

5352
// WithBytes write bytes to a file at Path
5453
func WithBytes(raw []byte) PathOp {
5554
return func(path Path) error {
5655
if m, ok := path.(manifestFile); ok {
57-
m.SetContent(ioutil.NopCloser(bytes.NewReader(raw)))
56+
m.SetContent(io.NopCloser(bytes.NewReader(raw)))
5857
return nil
5958
}
60-
return ioutil.WriteFile(path.Path(), raw, defaultFileMode)
59+
return os.WriteFile(path.Path(), raw, defaultFileMode)
6160
}
6261
}
6362

6463
// WithReaderContent copies the reader contents to the file at Path
6564
func WithReaderContent(r io.Reader) PathOp {
6665
return func(path Path) error {
6766
if m, ok := path.(manifestFile); ok {
68-
m.SetContent(ioutil.NopCloser(r))
67+
m.SetContent(io.NopCloser(r))
6968
return nil
7069
}
7170
f, err := os.OpenFile(path.Path(), os.O_WRONLY, defaultFileMode)
@@ -107,7 +106,7 @@ func WithFile(filename, content string, ops ...PathOp) PathOp {
107106
}
108107

109108
func createFile(fullpath string, content string) error {
110-
return ioutil.WriteFile(fullpath, []byte(content), defaultFileMode)
109+
return os.WriteFile(fullpath, []byte(content), defaultFileMode)
111110
}
112111

113112
// WithFiles creates all the files in the directory at path with their content
@@ -191,34 +190,38 @@ func WithMode(mode os.FileMode) PathOp {
191190
}
192191

193192
func copyDirectory(source, dest string) error {
194-
entries, err := ioutil.ReadDir(source)
193+
entries, err := os.ReadDir(source)
195194
if err != nil {
196195
return err
197196
}
198197
for _, entry := range entries {
199198
sourcePath := filepath.Join(source, entry.Name())
200199
destPath := filepath.Join(dest, entry.Name())
201-
switch {
202-
case entry.IsDir():
203-
if err := os.Mkdir(destPath, 0755); err != nil {
204-
return err
205-
}
206-
if err := copyDirectory(sourcePath, destPath); err != nil {
207-
return err
208-
}
209-
case entry.Mode()&os.ModeSymlink != 0:
210-
if err := copySymLink(sourcePath, destPath); err != nil {
211-
return err
212-
}
213-
default:
214-
if err := copyFile(sourcePath, destPath); err != nil {
215-
return err
216-
}
200+
err = copyEntry(entry, destPath, sourcePath)
201+
if err != nil {
202+
return err
217203
}
218204
}
219205
return nil
220206
}
221207

208+
func copyEntry(entry os.DirEntry, destPath string, sourcePath string) error {
209+
if entry.IsDir() {
210+
if err := os.Mkdir(destPath, 0755); err != nil {
211+
return err
212+
}
213+
return copyDirectory(sourcePath, destPath)
214+
}
215+
info, err := entry.Info()
216+
if err != nil {
217+
return err
218+
}
219+
if info.Mode()&os.ModeSymlink != 0 {
220+
return copySymLink(sourcePath, destPath)
221+
}
222+
return copyFile(sourcePath, destPath)
223+
}
224+
222225
func copySymLink(source, dest string) error {
223226
link, err := os.Readlink(source)
224227
if err != nil {
@@ -228,11 +231,11 @@ func copySymLink(source, dest string) error {
228231
}
229232

230233
func copyFile(source, dest string) error {
231-
content, err := ioutil.ReadFile(source)
234+
content, err := os.ReadFile(source)
232235
if err != nil {
233236
return err
234237
}
235-
return ioutil.WriteFile(dest, content, 0644)
238+
return os.WriteFile(dest, content, 0644)
236239
}
237240

238241
// WithSymlink creates a symlink in the directory which links to target.

fs/ops_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fs_test
22

33
import (
4-
"io/ioutil"
54
"os"
65
"path/filepath"
76
"runtime"
@@ -71,7 +70,7 @@ func TestApply(t *testing.T) {
7170
tmpFile := fs.NewFile(t, "test-update-file", fs.WithContent("contenta"))
7271
defer tmpFile.Remove()
7372
fs.Apply(t, tmpFile, fs.WithContent("contentb"))
74-
content, err := ioutil.ReadFile(tmpFile.Path())
73+
content, err := os.ReadFile(tmpFile.Path())
7574
assert.NilError(t, err)
7675
assert.Equal(t, string(content), "contentb")
7776
})

fs/path.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package fs
33
import (
44
"bytes"
55
"io"
6-
"io/ioutil"
76
"os"
87

98
"gotest.tools/v3/assert"
@@ -124,7 +123,7 @@ func normalizeID(id int) uint32 {
124123
return uint32(id)
125124
}
126125

127-
var anyFileContent = ioutil.NopCloser(bytes.NewReader(nil))
126+
var anyFileContent = io.NopCloser(bytes.NewReader(nil))
128127

129128
// MatchAnyFileContent is a PathOp that updates a Manifest so that the file
130129
// at path may contain any content.

fs/report.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package fs
33
import (
44
"bytes"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"os"
88
"path/filepath"
99
"runtime"
@@ -86,9 +86,9 @@ func eqFile(x, y *file) []problem {
8686
return p
8787
}
8888

89-
xContent, xErr := ioutil.ReadAll(x.content)
89+
xContent, xErr := io.ReadAll(x.content)
9090
defer x.content.Close()
91-
yContent, yErr := ioutil.ReadAll(y.content)
91+
yContent, yErr := io.ReadAll(y.content)
9292
defer y.content.Close()
9393

9494
if xErr != nil {

0 commit comments

Comments
 (0)