Skip to content

Commit

Permalink
replace uses of deprecated io/ioutil
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Oct 4, 2022
1 parent f0f4764 commit b881b33
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 69 deletions.
10 changes: 5 additions & 5 deletions assert/assert_ext_test.go
Expand Up @@ -4,7 +4,7 @@ import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -33,7 +33,7 @@ that we are testing
`
assert.Equal(t, actual, expectedOne)

raw, err := ioutil.ReadFile(fileName(t))
raw, err := os.ReadFile(fileName(t))
assert.NilError(t, err)

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

raw, err := ioutil.ReadFile(fileName(t))
raw, err := os.ReadFile(fileName(t))
assert.NilError(t, err)

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

assert.Equal(t, actual, expectedInsideFunc)

raw, err := ioutil.ReadFile(fileName(t))
raw, err := os.ReadFile(fileName(t))
assert.NilError(t, err)

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

assert.Equal(t, actual, expectedConstInsideFunc)

raw, err := ioutil.ReadFile(fileName(t))
raw, err := os.ReadFile(fileName(t))
assert.NilError(t, err)

expected := "const expectedConstInsideFunc = `this is the new\nexpected value\nfor const inside function\n`"
Expand Down
3 changes: 1 addition & 2 deletions assert/cmd/gty-migrate-from-testify/main.go
Expand Up @@ -8,7 +8,6 @@ import (
"go/ast"
"go/format"
"go/token"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -126,7 +125,7 @@ func run(opts options) error {
return fmt.Errorf("failed to format %s: %w", filename, err)
}

if err := ioutil.WriteFile(absFilename, raw, 0); err != nil {
if err := os.WriteFile(absFilename, raw, 0); err != nil {
return fmt.Errorf("failed to write file %s: %w", filename, err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions assert/cmd/gty-migrate-from-testify/main_test.go
@@ -1,7 +1,7 @@
package main

import (
"io/ioutil"
"os"
"testing"

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

raw, err := ioutil.ReadFile(dir.Join("src/example.com/example/some_test.go"))
raw, err := os.ReadFile(dir.Join("src/example.com/example/some_test.go"))
assert.NilError(t, err)
golden.Assert(t, string(raw), "full-expected/some_test.go")
}
Expand Down
5 changes: 2 additions & 3 deletions fs/example_test.go
@@ -1,7 +1,6 @@
package fs_test

import (
"io/ioutil"
"os"
"testing"

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

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

content, err := ioutil.ReadFile(file.Path())
content, err := os.ReadFile(file.Path())
assert.NilError(t, err)
assert.Equal(t, "content\n", content)
}
Expand Down
5 changes: 2 additions & 3 deletions fs/file.go
Expand Up @@ -5,7 +5,6 @@ contents and structure of a directory.
package fs // import "gotest.tools/v3/fs"

import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -46,7 +45,7 @@ func NewFile(t assert.TestingT, prefix string, ops ...PathOp) *File {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
tempfile, err := ioutil.TempFile("", cleanPrefix(prefix)+"-")
tempfile, err := os.CreateTemp("", cleanPrefix(prefix)+"-")
assert.NilError(t, err)

file := &File{path: tempfile.Name()}
Expand Down Expand Up @@ -89,7 +88,7 @@ func NewDir(t assert.TestingT, prefix string, ops ...PathOp) *Dir {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
path, err := ioutil.TempDir("", cleanPrefix(prefix)+"-")
path, err := os.MkdirTemp("", cleanPrefix(prefix)+"-")
assert.NilError(t, err)
dir := &Dir{path: path}
cleanup.Cleanup(t, dir.Remove)
Expand Down
9 changes: 2 additions & 7 deletions fs/file_test.go
Expand Up @@ -2,7 +2,6 @@ package fs_test

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -97,15 +96,11 @@ func TestNewDir_IntegrationWithCleanup(t *testing.T) {
}

func TestDirFromPath(t *testing.T) {
tmpdir, err := ioutil.TempDir("", t.Name())
assert.NilError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpdir)
})
tmpdir := t.TempDir()

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

_, err = os.Stat(dir.Join("newfile"))
_, err := os.Stat(dir.Join("newfile"))
assert.NilError(t, err)

assert.Equal(t, dir.Path(), tmpdir)
Expand Down
9 changes: 6 additions & 3 deletions fs/manifest.go
Expand Up @@ -3,7 +3,6 @@ package fs
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

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

func newDirectory(path string, info os.FileInfo) (*directory, error) {
items := make(map[string]dirEntry)
children, err := ioutil.ReadDir(path)
children, err := os.ReadDir(path)
if err != nil {
return nil, err
}
Expand All @@ -103,7 +102,11 @@ func newDirectory(path string, info os.FileInfo) (*directory, error) {
}, nil
}

func getTypedResource(path string, info os.FileInfo) (dirEntry, error) {
func getTypedResource(path string, entry os.DirEntry) (dirEntry, error) {
info, err := entry.Info()
if err != nil {
return nil, err
}
switch {
case info.IsDir():
return newDirectory(path, info)
Expand Down
7 changes: 3 additions & 4 deletions fs/manifest_test.go
Expand Up @@ -3,7 +3,6 @@ package fs
import (
"bytes"
"io"
"io/ioutil"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -92,12 +91,12 @@ var cmpManifest = cmp.Options{
if x == nil || y == nil {
return x == y
}
xContent, err := ioutil.ReadAll(x)
xContent, err := io.ReadAll(x)
if err != nil {
return false
}

yContent, err := ioutil.ReadAll(y)
yContent, err := io.ReadAll(y)
if err != nil {
return false
}
Expand All @@ -106,5 +105,5 @@ var cmpManifest = cmp.Options{
}

func readCloser(s string) io.ReadCloser {
return ioutil.NopCloser(strings.NewReader(s))
return io.NopCloser(strings.NewReader(s))
}
55 changes: 29 additions & 26 deletions fs/ops.go
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -43,29 +42,29 @@ type manifestDirectory interface {
func WithContent(content string) PathOp {
return func(path Path) error {
if m, ok := path.(manifestFile); ok {
m.SetContent(ioutil.NopCloser(strings.NewReader(content)))
m.SetContent(io.NopCloser(strings.NewReader(content)))
return nil
}
return ioutil.WriteFile(path.Path(), []byte(content), defaultFileMode)
return os.WriteFile(path.Path(), []byte(content), defaultFileMode)
}
}

// WithBytes write bytes to a file at Path
func WithBytes(raw []byte) PathOp {
return func(path Path) error {
if m, ok := path.(manifestFile); ok {
m.SetContent(ioutil.NopCloser(bytes.NewReader(raw)))
m.SetContent(io.NopCloser(bytes.NewReader(raw)))
return nil
}
return ioutil.WriteFile(path.Path(), raw, defaultFileMode)
return os.WriteFile(path.Path(), raw, defaultFileMode)
}
}

// WithReaderContent copies the reader contents to the file at Path
func WithReaderContent(r io.Reader) PathOp {
return func(path Path) error {
if m, ok := path.(manifestFile); ok {
m.SetContent(ioutil.NopCloser(r))
m.SetContent(io.NopCloser(r))
return nil
}
f, err := os.OpenFile(path.Path(), os.O_WRONLY, defaultFileMode)
Expand Down Expand Up @@ -107,7 +106,7 @@ func WithFile(filename, content string, ops ...PathOp) PathOp {
}

func createFile(fullpath string, content string) error {
return ioutil.WriteFile(fullpath, []byte(content), defaultFileMode)
return os.WriteFile(fullpath, []byte(content), defaultFileMode)
}

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

func copyDirectory(source, dest string) error {
entries, err := ioutil.ReadDir(source)
entries, err := os.ReadDir(source)
if err != nil {
return err
}
for _, entry := range entries {
sourcePath := filepath.Join(source, entry.Name())
destPath := filepath.Join(dest, entry.Name())
switch {
case entry.IsDir():
if err := os.Mkdir(destPath, 0755); err != nil {
return err
}
if err := copyDirectory(sourcePath, destPath); err != nil {
return err
}
case entry.Mode()&os.ModeSymlink != 0:
if err := copySymLink(sourcePath, destPath); err != nil {
return err
}
default:
if err := copyFile(sourcePath, destPath); err != nil {
return err
}
err = copyEntry(entry, destPath, sourcePath)
if err != nil {
return err
}
}
return nil
}

func copyEntry(entry os.DirEntry, destPath string, sourcePath string) error {
if entry.IsDir() {
if err := os.Mkdir(destPath, 0755); err != nil {
return err
}
return copyDirectory(sourcePath, destPath)
}
info, err := entry.Info()
if err != nil {
return err
}
if info.Mode()&os.ModeSymlink != 0 {
return copySymLink(sourcePath, destPath)
}
return copyFile(sourcePath, destPath)
}

func copySymLink(source, dest string) error {
link, err := os.Readlink(source)
if err != nil {
Expand All @@ -228,11 +231,11 @@ func copySymLink(source, dest string) error {
}

func copyFile(source, dest string) error {
content, err := ioutil.ReadFile(source)
content, err := os.ReadFile(source)
if err != nil {
return err
}
return ioutil.WriteFile(dest, content, 0644)
return os.WriteFile(dest, content, 0644)
}

// WithSymlink creates a symlink in the directory which links to target.
Expand Down
3 changes: 1 addition & 2 deletions fs/ops_test.go
@@ -1,7 +1,6 @@
package fs_test

import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -71,7 +70,7 @@ func TestApply(t *testing.T) {
tmpFile := fs.NewFile(t, "test-update-file", fs.WithContent("contenta"))
defer tmpFile.Remove()
fs.Apply(t, tmpFile, fs.WithContent("contentb"))
content, err := ioutil.ReadFile(tmpFile.Path())
content, err := os.ReadFile(tmpFile.Path())
assert.NilError(t, err)
assert.Equal(t, string(content), "contentb")
})
Expand Down
3 changes: 1 addition & 2 deletions fs/path.go
Expand Up @@ -3,7 +3,6 @@ package fs
import (
"bytes"
"io"
"io/ioutil"
"os"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -124,7 +123,7 @@ func normalizeID(id int) uint32 {
return uint32(id)
}

var anyFileContent = ioutil.NopCloser(bytes.NewReader(nil))
var anyFileContent = io.NopCloser(bytes.NewReader(nil))

// MatchAnyFileContent is a PathOp that updates a Manifest so that the file
// at path may contain any content.
Expand Down
6 changes: 3 additions & 3 deletions fs/report.go
Expand Up @@ -3,7 +3,7 @@ package fs
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -86,9 +86,9 @@ func eqFile(x, y *file) []problem {
return p
}

xContent, xErr := ioutil.ReadAll(x.content)
xContent, xErr := io.ReadAll(x.content)
defer x.content.Close()
yContent, yErr := ioutil.ReadAll(y.content)
yContent, yErr := io.ReadAll(y.content)
defer y.content.Close()

if xErr != nil {
Expand Down

0 comments on commit b881b33

Please sign in to comment.