From b881b33471ed35d3b5b38f36c5386359947278d5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 4 Oct 2022 12:57:01 +0200 Subject: [PATCH] replace uses of deprecated io/ioutil Signed-off-by: Sebastiaan van Stijn --- assert/assert_ext_test.go | 10 ++-- assert/cmd/gty-migrate-from-testify/main.go | 3 +- .../cmd/gty-migrate-from-testify/main_test.go | 4 +- fs/example_test.go | 5 +- fs/file.go | 5 +- fs/file_test.go | 9 +-- fs/manifest.go | 9 ++- fs/manifest_test.go | 7 +-- fs/ops.go | 55 ++++++++++--------- fs/ops_test.go | 3 +- fs/path.go | 3 +- fs/report.go | 6 +- golden/golden.go | 7 +-- golden/golden_test.go | 5 +- 14 files changed, 62 insertions(+), 69 deletions(-) diff --git a/assert/assert_ext_test.go b/assert/assert_ext_test.go index d450ce2e..8acf44de 100644 --- a/assert/assert_ext_test.go +++ b/assert/assert_ext_test.go @@ -4,7 +4,7 @@ import ( "go/ast" "go/parser" "go/token" - "io/ioutil" + "os" "runtime" "strings" "testing" @@ -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`" @@ -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`" @@ -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`" @@ -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`" diff --git a/assert/cmd/gty-migrate-from-testify/main.go b/assert/cmd/gty-migrate-from-testify/main.go index 91bccc2d..e0663785 100644 --- a/assert/cmd/gty-migrate-from-testify/main.go +++ b/assert/cmd/gty-migrate-from-testify/main.go @@ -8,7 +8,6 @@ import ( "go/ast" "go/format" "go/token" - "io/ioutil" "log" "os" "path" @@ -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) } } diff --git a/assert/cmd/gty-migrate-from-testify/main_test.go b/assert/cmd/gty-migrate-from-testify/main_test.go index e5accbba..d7f59680 100644 --- a/assert/cmd/gty-migrate-from-testify/main_test.go +++ b/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" @@ -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") } diff --git a/fs/example_test.go b/fs/example_test.go index 09ea38df..ece5b4bc 100644 --- a/fs/example_test.go +++ b/fs/example_test.go @@ -1,7 +1,6 @@ package fs_test import ( - "io/ioutil" "os" "testing" @@ -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)) } @@ -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) } diff --git a/fs/file.go b/fs/file.go index eb8e3012..f778e9c8 100644 --- a/fs/file.go +++ b/fs/file.go @@ -5,7 +5,6 @@ contents and structure of a directory. package fs // import "gotest.tools/v3/fs" import ( - "io/ioutil" "os" "path/filepath" "runtime" @@ -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()} @@ -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) diff --git a/fs/file_test.go b/fs/file_test.go index 5e8be4da..c4f73482 100644 --- a/fs/file_test.go +++ b/fs/file_test.go @@ -2,7 +2,6 @@ package fs_test import ( "errors" - "io/ioutil" "os" "path/filepath" "testing" @@ -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) diff --git a/fs/manifest.go b/fs/manifest.go index b657bd96..44348c59 100644 --- a/fs/manifest.go +++ b/fs/manifest.go @@ -3,7 +3,6 @@ package fs import ( "fmt" "io" - "io/ioutil" "os" "path/filepath" @@ -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 } @@ -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) diff --git a/fs/manifest_test.go b/fs/manifest_test.go index 2cbc6108..fdaf8c7a 100644 --- a/fs/manifest_test.go +++ b/fs/manifest_test.go @@ -3,7 +3,6 @@ package fs import ( "bytes" "io" - "io/ioutil" "os" "runtime" "strings" @@ -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 } @@ -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)) } diff --git a/fs/ops.go b/fs/ops.go index 9e1e068b..1bb72e36 100644 --- a/fs/ops.go +++ b/fs/ops.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -43,10 +42,10 @@ 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) } } @@ -54,10 +53,10 @@ func WithContent(content string) PathOp { 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) } } @@ -65,7 +64,7 @@ func WithBytes(raw []byte) PathOp { 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) @@ -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 @@ -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 { @@ -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. diff --git a/fs/ops_test.go b/fs/ops_test.go index 7185f2a5..463aaa3f 100644 --- a/fs/ops_test.go +++ b/fs/ops_test.go @@ -1,7 +1,6 @@ package fs_test import ( - "io/ioutil" "os" "path/filepath" "runtime" @@ -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") }) diff --git a/fs/path.go b/fs/path.go index c301b904..550044a0 100644 --- a/fs/path.go +++ b/fs/path.go @@ -3,7 +3,6 @@ package fs import ( "bytes" "io" - "io/ioutil" "os" "gotest.tools/v3/assert" @@ -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. diff --git a/fs/report.go b/fs/report.go index 1a3c6683..5e79e49b 100644 --- a/fs/report.go +++ b/fs/report.go @@ -3,7 +3,7 @@ package fs import ( "bytes" "fmt" - "io/ioutil" + "io" "os" "path/filepath" "runtime" @@ -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 { diff --git a/golden/golden.go b/golden/golden.go index 581e4553..ce96ba17 100644 --- a/golden/golden.go +++ b/golden/golden.go @@ -12,7 +12,6 @@ import ( "bytes" "flag" "fmt" - "io/ioutil" "os" "path/filepath" @@ -63,7 +62,7 @@ func Get(t assert.TestingT, filename string) []byte { if ht, ok := t.(helperT); ok { ht.Helper() } - expected, err := ioutil.ReadFile(Path(filename)) + expected, err := os.ReadFile(Path(filename)) assert.NilError(t, err) return expected } @@ -168,7 +167,7 @@ func compare(actual []byte, filename string) (cmp.Result, []byte) { if err := update(filename, actual); err != nil { return cmp.ResultFromError(err), nil } - expected, err := ioutil.ReadFile(Path(filename)) + expected, err := os.ReadFile(Path(filename)) if err != nil { return cmp.ResultFromError(err), nil } @@ -187,5 +186,5 @@ func update(filename string, actual []byte) error { return err } } - return ioutil.WriteFile(Path(filename), actual, 0644) + return os.WriteFile(Path(filename), actual, 0644) } diff --git a/golden/golden_test.go b/golden/golden_test.go index 3b0bd027..e0208403 100644 --- a/golden/golden_test.go +++ b/golden/golden_test.go @@ -1,7 +1,6 @@ package golden import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -203,7 +202,7 @@ func setUpdateFlag(t *testing.T) func() { func setupGoldenFileWithDir(t *testing.T, dirname, content string) (string, func()) { dirpath := filepath.Join("testdata", dirname) _ = os.MkdirAll(filepath.Join("testdata", dirname), 0755) - f, err := ioutil.TempFile(dirpath, t.Name()+"-") + f, err := os.CreateTemp(dirpath, t.Name()+"-") assert.NilError(t, err, "fail to create test golden file") defer f.Close() @@ -218,7 +217,7 @@ func setupGoldenFileWithDir(t *testing.T, dirname, content string) (string, func func setupGoldenFile(t *testing.T, content string) (string, func()) { _ = os.Mkdir("testdata", 0755) - f, err := ioutil.TempFile("testdata", t.Name()+"-") + f, err := os.CreateTemp("testdata", t.Name()+"-") assert.NilError(t, err, "fail to create test golden file") defer f.Close()