Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor ioutil usage #299

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package exec
import (
"context"
"io"
"io/ioutil"
"os"
osexec "os/exec"
"testing"
Expand Down Expand Up @@ -207,7 +206,7 @@ func TestStdIOPipes(t *testing.T) {
func readAll(t *testing.T, r io.Reader, n string) string {
t.Helper()

b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
t.Fatalf("unexpected error when reading from %s: %v", n, err)
}
Expand Down
4 changes: 2 additions & 2 deletions exec/stdiopipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package exec_test

import (
"fmt"
"io/ioutil"
"io"

"k8s.io/utils/exec"
)
Expand All @@ -33,7 +33,7 @@ func ExampleNew_stderrPipe() {

stderr := make(chan []byte)
go func() {
b, err := ioutil.ReadAll(stderrPipe)
b, err := io.ReadAll(stderrPipe)
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions inotify/inotify_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package inotify

import (
"io/ioutil"
"os"
"sync/atomic"
"testing"
Expand All @@ -22,7 +21,7 @@ func TestInotifyEvents(t *testing.T) {
t.Fatalf("NewWatcher failed: %s", err)
}

dir, err := ioutil.TempDir("", "inotify")
dir, err := os.MkdirTemp("", "inotify")
if err != nil {
t.Fatalf("TempDir failed: %s", err)
}
Expand Down
8 changes: 4 additions & 4 deletions io/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
)

// ErrLimitReached means that the read limit is reached.
Expand All @@ -41,15 +41,15 @@ func ConsistentRead(filename string, attempts int) ([]byte, error) {
// introduces a sync callback that can be used by the tests to mutate the file
// from which the test data is being read
func consistentReadSync(filename string, attempts int, sync func(int)) ([]byte, error) {
oldContent, err := ioutil.ReadFile(filename)
oldContent, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
for i := 0; i < attempts; i++ {
if sync != nil {
sync(i)
}
newContent, err := ioutil.ReadFile(filename)
newContent, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func IsInconsistentReadError(err error) bool {
// when `limit` bytes are read.
func ReadAtMost(r io.Reader, limit int64) ([]byte, error) {
limitedReader := &io.LimitedReader{R: r, N: limit}
data, err := ioutil.ReadAll(limitedReader)
data, err := io.ReadAll(limitedReader)
if err != nil {
return data, err
}
Expand Down
3 changes: 1 addition & 2 deletions io/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package io

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -45,7 +44,7 @@ func writeToPipe(namedPipe string, flaky bool, i int) {
}

func makePipe(t *testing.T) string {
tmp, err := ioutil.TempDir("", "pipe-test")
tmp, err := os.MkdirTemp("", "pipe-test")
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 3 additions & 5 deletions mount/mount_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package mount

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -77,8 +76,7 @@ func TestDoCleanupMountPoint(t *testing.T) {

for name, tt := range tests {
t.Run(name, func(t *testing.T) {

tmpDir, err := ioutil.TempDir("", "unmount-mount-point-test")
tmpDir, err := os.MkdirTemp("", "unmount-mount-point-test")
if err != nil {
t.Fatalf("failed to create tmpdir: %v", err)
}
Expand Down Expand Up @@ -120,15 +118,15 @@ func TestDoCleanupMountPoint(t *testing.T) {
}

func validateDirExists(dir string) error {
_, err := ioutil.ReadDir(dir)
_, err := os.ReadDir(dir)
if err != nil {
return err
}
return nil
}

func validateDirNotExists(dir string) error {
_, err := ioutil.ReadDir(dir)
_, err := os.ReadDir(dir)
if os.IsNotExist(err) {
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions mount/mount_helper_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ limitations under the License.
package mount

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)

func writeFile(content string) (string, string, error) {
tempDir, err := ioutil.TempDir("", "mounter_shared_test")
tempDir, err := os.MkdirTemp("", "mounter_shared_test")
if err != nil {
return "", "", err
}
filename := filepath.Join(tempDir, "mountinfo")
err = ioutil.WriteFile(filename, []byte(content), 0600)
err = os.WriteFile(filename, []byte(content), 0600)
if err != nil {
os.RemoveAll(tempDir)
return "", "", err
Expand Down
3 changes: 1 addition & 2 deletions mount/mount_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ limitations under the License.
package mount

import (
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -419,7 +418,7 @@ func TestSearchMountPoints(t *testing.T) {
nil,
},
}
tmpFile, err := ioutil.TempFile("", "test-get-filetype")
tmpFile, err := os.CreateTemp("", "test-get-filetype")
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions mount/mount_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package mount

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -206,7 +205,8 @@ func TestIsLikelyNotMountPoint(t *testing.T) {
}

for _, test := range tests {
base, err := ioutil.TempDir("", test.fileName)

base, err := os.MkdirTemp("", test.fileName)
if err != nil {
t.Fatalf(err.Error())
}
Expand Down Expand Up @@ -291,7 +291,7 @@ func TestFormatAndMount(t *testing.T) {
Interface: &fakeMounter,
Exec: fakeExec,
}
base, err := ioutil.TempDir("", test.device)
base, err := os.MkdirTemp("", test.device)
if err != nil {
t.Fatalf(err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions mount/safe_format_and_mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package mount

import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -58,7 +57,7 @@ func TestSafeFormatAndMount(t *testing.T) {
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
t.Skipf("not supported on GOOS=%s", runtime.GOOS)
}
mntDir, err := ioutil.TempDir(os.TempDir(), "mount")
mntDir, err := os.MkdirTemp(os.TempDir(), "mount")
if err != nil {
t.Fatalf("failed to create tmp dir: %v", err)
}
Expand Down
13 changes: 6 additions & 7 deletions nsenter/nsenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ limitations under the License.
package nsenter

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -116,7 +115,7 @@ func TestEvalSymlinks(t *testing.T) {
mustExist: true,
prepare: func(tmpdir string) (src string, expectedDst string, err error) {
src = filepath.Join(tmpdir, "src")
err = ioutil.WriteFile(src, []byte{}, 0644)
err = os.WriteFile(src, []byte{}, 0644)
return src, src, err
},
},
Expand Down Expand Up @@ -154,7 +153,7 @@ func TestEvalSymlinks(t *testing.T) {
mustExist: false,
prepare: func(tmpdir string) (src string, expectedDst string, err error) {
dst := filepath.Join(tmpdir, "dst")
if err = ioutil.WriteFile(dst, []byte{}, 0644); err != nil {
if err = os.WriteFile(dst, []byte{}, 0644); err != nil {
return "", "", err
}
src = filepath.Join(tmpdir, "src")
Expand Down Expand Up @@ -192,7 +191,7 @@ func TestEvalSymlinks(t *testing.T) {
return "", "", err
}
dstFile := filepath.Join(dst, "file")
if err = ioutil.WriteFile(dstFile, []byte{}, 0644); err != nil {
if err = os.WriteFile(dstFile, []byte{}, 0644); err != nil {
return "", "", err
}

Expand Down Expand Up @@ -244,7 +243,7 @@ func TestEvalSymlinks(t *testing.T) {
},
}

tmpdir, err := ioutil.TempDir("", "nsenter-hostpath-")
tmpdir, err := os.MkdirTemp("", "nsenter-hostpath-")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -273,7 +272,7 @@ func TestNewNsenter(t *testing.T) {
// Create a symlink /tmp/xyz/rootfs -> / and use it as rootfs path
// It should resolve all binaries correctly, the test runs on Linux

tmpdir, err := ioutil.TempDir("", "nsenter-hostpath-")
tmpdir, err := os.MkdirTemp("", "nsenter-hostpath-")
if err != nil {
t.Fatal(err)
}
Expand All @@ -294,7 +293,7 @@ func TestNewNsenterError(t *testing.T) {
// Create empty dir /tmp/xyz/rootfs and use it as rootfs path
// It should resolve all binaries correctly, the test runs on Linux

tmpdir, err := ioutil.TempDir("", "nsenter-hostpath-")
tmpdir, err := os.MkdirTemp("", "nsenter-hostpath-")
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions temp/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package temp
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
Expand All @@ -45,7 +44,7 @@ var _ Directory = &Dir{}
// CreateTempDir returns a new Directory wrapping a temporary directory
// on disk.
func CreateTempDir(prefix string) (*Dir, error) {
name, err := ioutil.TempDir("", fmt.Sprintf("%s-", prefix))
name, err := os.MkdirTemp("", fmt.Sprintf("%s-", prefix))
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions temp/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package temp

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -44,7 +43,7 @@ func TestTempDir(t *testing.T) {
}

// Verify that the directory is empty
entries, err := ioutil.ReadDir(dir.Name)
entries, err := os.ReadDir(dir.Name)
if err != nil {
t.Fatal(err)
}
Expand All @@ -69,7 +68,7 @@ func TestTempDir(t *testing.T) {
}

// We have created only two files
entries, err = ioutil.ReadDir(dir.Name)
entries, err = os.ReadDir(dir.Name)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading