Remove io.Writer from fs.File

It was only used in a single test, which now uses plain *os.File instead.
This commit is contained in:
greatroar 2020-02-17 00:20:38 +01:00
parent 9abef3bf1a
commit 1b20f6beec
4 changed files with 32 additions and 41 deletions

View file

@ -252,10 +252,6 @@ func (f fakeFile) Seek(int64, int) (int64, error) {
return 0, os.ErrInvalid return 0, os.ErrInvalid
} }
func (f fakeFile) Write(p []byte) (int, error) {
return 0, os.ErrInvalid
}
func (f fakeFile) Read(p []byte) (int, error) { func (f fakeFile) Read(p []byte) (int, error) {
return 0, os.ErrInvalid return 0, os.ErrInvalid
} }

View file

@ -1,10 +1,6 @@
package fs package fs
import ( import "os"
"io/ioutil"
"os"
"testing"
)
// IsRegularFile returns true if fi belongs to a normal file. If fi is nil, // IsRegularFile returns true if fi belongs to a normal file. If fi is nil,
// false is returned. // false is returned.
@ -15,22 +11,3 @@ func IsRegularFile(fi os.FileInfo) bool {
return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0 return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0
} }
// TestTempFile returns a new temporary file, which is removed when cleanup()
// is called.
func TestTempFile(t testing.TB, prefix string) (File, func()) {
f, err := ioutil.TempFile("", prefix)
if err != nil {
t.Fatal(err)
}
cleanup := func() {
_ = f.Close()
err = Remove(f.Name())
if err != nil {
t.Fatal(err)
}
}
return f, cleanup
}

View file

@ -26,7 +26,6 @@ type FS interface {
// File is an open file on a file system. // File is an open file on a file system.
type File interface { type File interface {
io.Reader io.Reader
io.Writer
io.Closer io.Closer
Fd() uintptr Fd() uintptr

View file

@ -3,25 +3,44 @@ package textfile
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"io/ioutil"
"os"
"testing" "testing"
"github.com/restic/restic/internal/fs"
) )
func writeTempfile(t testing.TB, data []byte) (fs.File, func()) { // writeTempfile writes data to a new temporary file and returns its name
f, removeTempfile := fs.TestTempFile(t, "restic-test-textfile-read-") // and a callback that removes it.
func writeTempfile(t testing.TB, data []byte) (string, func()) {
t.Helper()
_, err := f.Write(data) f, err := ioutil.TempFile("", "restic-test-textfile-read-")
if err != nil {
t.Fatal(err)
}
name := f.Name()
defer func() {
closeErr := f.Close()
if err == nil && closeErr != nil {
err = closeErr
}
if err != nil {
os.Remove(name)
t.Fatal(err)
}
}()
_, err = f.Write(data)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = f.Close() return name, func() {
if err != nil { err := os.Remove(name)
t.Fatal(err) if err != nil {
t.Fatal(err)
}
} }
return f, removeTempfile
} }
func dec(s string) []byte { func dec(s string) []byte {
@ -60,10 +79,10 @@ func TestRead(t *testing.T) {
want = test.data want = test.data
} }
f, cleanup := writeTempfile(t, test.data) tempname, cleanup := writeTempfile(t, test.data)
defer cleanup() defer cleanup()
data, err := Read(f.Name()) data, err := Read(tempname)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }