Skip to content

Commit

Permalink
Replace usage of deprecated ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
akrylysov committed Jan 5, 2025
1 parent d0f90ae commit 0c09a25
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 54 deletions.
6 changes: 5 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,11 @@ func (db *DB) FileSize() (int64, error) {
return 0, err
}
for _, file := range files {
size += file.Size()
info, err := file.Info()
if err != nil {
return 0, err
}
size += info.Size()
}
return size, nil
}
24 changes: 15 additions & 9 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -30,7 +29,7 @@ var (
func TestMain(m *testing.M) {
flag.Parse()
if !testing.Verbose() {
SetLogger(log.New(ioutil.Discard, "", 0))
SetLogger(log.New(io.Discard, "", 0))
}
// Run tests against all file systems.
for _, fsys := range []fs.FileSystem{fs.Mem, fs.OSMMap, fs.OS} {
Expand All @@ -43,6 +42,7 @@ func TestMain(m *testing.M) {
os.Exit(exitCode)
}
}
_ = cleanDir(testDBName)
os.Exit(0)
}

Expand Down Expand Up @@ -87,6 +87,17 @@ func TestHeaderSize(t *testing.T) {
}
}

func cleanDir(path string) error {
files, err := testFS.ReadDir(path)
if err != nil {
return err
}
for _, file := range files {
_ = testFS.Remove(filepath.Join(testDBName, file.Name()))
}
return nil
}

func createTestDB(opts *Options) (*DB, error) {
if opts == nil {
opts = &Options{FileSystem: testFS}
Expand All @@ -95,15 +106,10 @@ func createTestDB(opts *Options) (*DB, error) {
opts.FileSystem = testFS
}
}
path := testDBName
files, err := testFS.ReadDir(path)
if err != nil && !os.IsNotExist(err) {
if err := cleanDir(testDBName); err != nil {
return nil, err
}
for _, file := range files {
_ = testFS.Remove(filepath.Join(path, file.Name()))
}
return Open(path, opts)
return Open(testDBName, opts)
}

func TestEmpty(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (fs *errfs) Rename(oldpath, newpath string) error {
return errfileError
}

func (fs *errfs) ReadDir(name string) ([]os.FileInfo, error) {
func (fs *errfs) ReadDir(name string) ([]os.DirEntry, error) {
return nil, errfileError
}

Expand Down
2 changes: 1 addition & 1 deletion fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type FileSystem interface {
Rename(oldpath, newpath string) error

// ReadDir reads the directory and returns a list of directory entries.
ReadDir(name string) ([]os.FileInfo, error)
ReadDir(name string) ([]os.DirEntry, error)

// CreateLockFile creates a lock file.
CreateLockFile(name string, perm os.FileMode) (LockFile, bool, error)
Expand Down
16 changes: 12 additions & 4 deletions fs/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ func (fs *memFS) Rename(oldpath, newpath string) error {
return os.ErrNotExist
}

func (fs *memFS) ReadDir(dir string) ([]os.FileInfo, error) {
func (fs *memFS) ReadDir(dir string) ([]os.DirEntry, error) {
dir = filepath.Clean(dir)
var fis []os.FileInfo
var entries []os.DirEntry
for name, f := range fs.files {
if filepath.Dir(name) == dir {
fis = append(fis, f)
entries = append(entries, f)
}
}
return fis, nil
return entries, nil
}

type memFile struct {
Expand Down Expand Up @@ -223,6 +223,14 @@ func (f *memFile) Sys() interface{} {
return nil
}

func (f *memFile) Type() os.FileMode {
return f.perm
}

func (f *memFile) Info() (os.FileInfo, error) {
return f.Stat()
}

func (f *memFile) Slice(start int64, end int64) ([]byte, error) {
if f.closed {
return nil, os.ErrClosed
Expand Down
5 changes: 2 additions & 3 deletions fs/os.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fs

import (
"io/ioutil"
"os"
)

Expand Down Expand Up @@ -34,8 +33,8 @@ func (fs *osFS) Rename(oldpath, newpath string) error {
return os.Rename(oldpath, newpath)
}

func (fs *osFS) ReadDir(name string) ([]os.FileInfo, error) {
return ioutil.ReadDir(name)
func (fs *osFS) ReadDir(name string) ([]os.DirEntry, error) {
return os.ReadDir(name)
}

type osFile struct {
Expand Down
2 changes: 1 addition & 1 deletion fs/os_mmap_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows
//go:build !windows

package fs

Expand Down
2 changes: 1 addition & 1 deletion fs/os_mmap_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build windows
//go:build windows

package fs

Expand Down
2 changes: 1 addition & 1 deletion fs/os_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows
//go:build !windows

package fs

Expand Down
2 changes: 1 addition & 1 deletion fs/os_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build windows
//go:build windows

package fs

Expand Down
2 changes: 1 addition & 1 deletion fs/sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (fs *subFS) Rename(oldpath, newpath string) error {
return fs.fsys.Rename(subOldpath, subNewpath)
}

func (fs *subFS) ReadDir(name string) ([]os.FileInfo, error) {
func (fs *subFS) ReadDir(name string) ([]os.DirEntry, error) {
subName := filepath.Join(fs.root, name)
return fs.fsys.ReadDir(subName)
}
Expand Down
30 changes: 0 additions & 30 deletions internal/errors/errors_go113_test.go

This file was deleted.

21 changes: 21 additions & 0 deletions internal/errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package errors

import (
"errors"
"testing"

"github.com/akrylysov/pogreb/internal/assert"
Expand All @@ -17,3 +18,23 @@ func TestWrap(t *testing.T) {
assert.Equal(t, "wrapped 11: err1", w11.Error())
assert.Equal(t, "wrapped 12: wrapped 11: err1", w12.Error())
}

func TestIs(t *testing.T) {
err1 := New("err1")
w11 := Wrap(err1, "wrapped 11")
w12 := Wrap(w11, "wrapped 12")

err2 := New("err2")
w21 := Wrap(err2, "wrapped 21")

assert.Equal(t, true, errors.Is(err1, err1))
assert.Equal(t, true, errors.Is(w11, err1))
assert.Equal(t, true, errors.Is(w12, err1))
assert.Equal(t, true, errors.Is(w12, w11))

assert.Equal(t, false, errors.Is(err1, err2))
assert.Equal(t, false, errors.Is(w11, err2))
assert.Equal(t, false, errors.Is(w12, err2))
assert.Equal(t, false, errors.Is(w21, err1))
assert.Equal(t, false, errors.Is(w21, w11))
}

0 comments on commit 0c09a25

Please sign in to comment.