Update deprecated io/ioutil functions

ioutil has been deprecated since go 1.16, this fixes all of the
deprecated functions we are using:

ioutil.ReadFile -> os.ReadFile
ioutil.ReadAll -> io.ReadAll
ioutil.WriteFile -> os.WriteFile
ioutil.TempFile -> os.CreateTemp
ioutil.TempDir -> os.MkdirTemp

All of the above are a simple name change, the function arguments and
results are exactly the same as before.

ioutil.ReadDir -> os.ReadDir

now returns a os.DirEntry but the IsDir and Name functions work the
same. The difference is that the FileInfo must be retrieved with the
Info() function which can also return an error.

These were identified by running:
golangci-lint run --build-tags=integration ./...
This commit is contained in:
Brian C. Lane 2023-03-06 11:07:30 -08:00 committed by Brian C. Lane
parent 0e4a5b34b2
commit 7a4bb863dd
37 changed files with 113 additions and 126 deletions

View file

@ -18,7 +18,6 @@ package jsondb
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -93,7 +92,7 @@ func (db *JSONDatabase) Write(name string, document interface{}) error {
// writing succeeded. `writer` gets passed the open file handle to write to and
// does not need to take care of closing it.
func writeFileAtomically(dir, filename string, mode os.FileMode, writer func(f *os.File) error) error {
tmpfile, err := ioutil.TempFile(dir, filename+"-*.tmp")
tmpfile, err := os.CreateTemp(dir, filename+"-*.tmp")
if err != nil {
return err
}

View file

@ -2,7 +2,6 @@ package jsondb
import (
"errors"
"io/ioutil"
"os"
"path"
"testing"
@ -26,14 +25,16 @@ func TestWriteFileAtomically(t *testing.T) {
require.NoError(t, err)
// ensure that there are no stray temporary files
infos, err := ioutil.ReadDir(dir)
infos, err := os.ReadDir(dir)
require.NoError(t, err)
require.Equal(t, 1, len(infos))
require.Equal(t, "octopus", infos[0].Name())
require.Equal(t, perm, infos[0].Mode())
i, err := infos[0].Info()
require.Nil(t, err)
require.Equal(t, perm, i.Mode())
filename := path.Join(dir, "octopus")
contents, err := ioutil.ReadFile(filename)
contents, err := os.ReadFile(filename)
require.NoError(t, err)
require.Equal(t, octopus, contents)
@ -51,7 +52,7 @@ func TestWriteFileAtomically(t *testing.T) {
require.Error(t, err)
// ensure there are no stray temporary files
infos, err := ioutil.ReadDir(dir)
infos, err := os.ReadDir(dir)
require.NoError(t, err)
require.Equal(t, 0, len(infos))
})

View file

@ -1,7 +1,6 @@
package jsondb_test
import (
"io/ioutil"
"os"
"path"
"testing"
@ -42,7 +41,7 @@ func TestDegenerate(t *testing.T) {
db := jsondb.New(dir, 0755)
// write-only file
err := ioutil.WriteFile(path.Join(dir, "one.json"), []byte("{"), 0600)
err := os.WriteFile(path.Join(dir, "one.json"), []byte("{"), 0600)
require.NoError(t, err)
var d document
@ -54,7 +53,7 @@ func TestDegenerate(t *testing.T) {
func TestCorrupt(t *testing.T) {
dir := t.TempDir()
err := ioutil.WriteFile(path.Join(dir, "one.json"), []byte("{"), 0600)
err := os.WriteFile(path.Join(dir, "one.json"), []byte("{"), 0600)
require.NoError(t, err)
db := jsondb.New(dir, 0755)
@ -66,7 +65,7 @@ func TestCorrupt(t *testing.T) {
func TestRead(t *testing.T) {
dir := t.TempDir()
err := ioutil.WriteFile(path.Join(dir, "one.json"), []byte("true"), 0600)
err := os.WriteFile(path.Join(dir, "one.json"), []byte("true"), 0600)
require.NoError(t, err)
db := jsondb.New(dir, 0755)