switch from deprecated io/ioutil to os package

This commit is contained in:
itchyny 2023-04-09 12:22:40 +09:00
parent 0d6dba4de7
commit 8d31a98aa7
2 changed files with 7 additions and 9 deletions

View file

@ -4,7 +4,6 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"os/exec" "os/exec"
@ -86,7 +85,7 @@ func rename(args []string) error {
xs[src] = true xs[src] = true
} }
f, err := ioutil.TempFile("", name+"-") f, err := os.CreateTemp("", name+"-")
if err != nil { if err != nil {
return err return err
} }
@ -120,7 +119,7 @@ func rename(args []string) error {
return fmt.Errorf("abort renames: %s", err) return fmt.Errorf("abort renames: %s", err)
} }
cnt, err := ioutil.ReadFile(f.Name()) cnt, err := os.ReadFile(f.Name())
if err != nil { if err != nil {
return err return err
} }

View file

@ -1,7 +1,6 @@
package mmv package mmv
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -385,9 +384,9 @@ func TestRename(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "mmv-") dir, err := os.MkdirTemp("", "mmv-")
if err != nil { if err != nil {
t.Fatalf("ioutil.TempDir returned an error: %s", err) t.Fatalf("os.MkdirTemp returned an error: %s", err)
} }
t.Cleanup(func() { os.RemoveAll(dir) }) t.Cleanup(func() { os.RemoveAll(dir) })
if err := os.Chdir(dir); err != nil { if err := os.Chdir(dir); err != nil {
@ -423,7 +422,7 @@ func setupFiles(contents map[string]string) error {
return err return err
} }
} }
if err := ioutil.WriteFile(f, []byte(cnt), 0o600); err != nil { if err := os.WriteFile(f, []byte(cnt), 0o600); err != nil {
return err return err
} }
} }
@ -432,7 +431,7 @@ func setupFiles(contents map[string]string) error {
func fileContents(dir string) map[string]string { func fileContents(dir string) map[string]string {
m := make(map[string]string) m := make(map[string]string)
fis, _ := ioutil.ReadDir(dir) fis, _ := os.ReadDir(dir)
for _, fi := range fis { for _, fi := range fis {
if fi.IsDir() { if fi.IsDir() {
for k, v := range fileContents(filepath.Join(dir, fi.Name())) { for k, v := range fileContents(filepath.Join(dir, fi.Name())) {
@ -440,7 +439,7 @@ func fileContents(dir string) map[string]string {
} }
} else { } else {
path := filepath.Join(dir, fi.Name()) path := filepath.Join(dir, fi.Name())
cnt, _ := ioutil.ReadFile(path) cnt, _ := os.ReadFile(path)
m[filepath.ToSlash(path)] = string(cnt) m[filepath.ToSlash(path)] = string(cnt)
} }
} }