mirror of
https://github.com/restic/restic.git
synced 2026-10-13 05:27:13 +00:00
Return error when reading zero byte from stdin
This commit changes the internal file system implementation for reading data from stdin, it now returns an error when no bytes could be read. I think it's worth failing in this case, the user instructed restic to read some data from stdin, and no data was read at all. Maybe it was in a pipe and some earlier stage failed. See #2135 for a short discussion.
This commit is contained in:
1 parent
0dd805421e
commit
c4fbf2c779
3 files changed
+100
-11
No files matched your search
@@ -5,6 +5,7 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -317,3 +318,66 @@ func TestFSReader(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFSReaderMinFileSize(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
data string
|
||||
allowEmpty bool
|
||||
readMustErr bool
|
||||
}{
|
||||
{
|
||||
name: "regular",
|
||||
data: "foobar",
|
||||
},
|
||||
{
|
||||
name: "empty",
|
||||
data: "",
|
||||
allowEmpty: false,
|
||||
readMustErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty2",
|
||||
data: "",
|
||||
allowEmpty: true,
|
||||
readMustErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
fs := &Reader{
|
||||
Name: "testfile",
|
||||
ReadCloser: ioutil.NopCloser(strings.NewReader(test.data)),
|
||||
Mode: 0644,
|
||||
ModTime: time.Now(),
|
||||
AllowEmptyFile: test.allowEmpty,
|
||||
}
|
||||
|
||||
f, err := fs.Open("testfile")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
buf, err := ioutil.ReadAll(f)
|
||||
if test.readMustErr {
|
||||
if err == nil {
|
||||
t.Fatal("expected error not found, got nil")
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if string(buf) != test.data {
|
||||
t.Fatalf("wrong data returned, want %q, got %q", test.data, string(buf))
|
||||
}
|
||||
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user