-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfs_test.go
98 lines (89 loc) · 2.13 KB
/
fs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package kipp
import (
"fmt"
"io"
"os"
"testing"
"time"
"github.com/uhthomas/kipp/database"
)
type fakeFileSystemReader struct{ limit, off int64 }
func (r fakeFileSystemReader) Read(b []byte) (n int, err error) {
if r.limit == r.off {
return 0, io.EOF
}
if l := r.limit - r.off; int64(len(b)) >= l {
b = b[:l]
}
for i := 0; i < len(b); i++ {
b[i] = 0
}
r.off += int64(len(b))
return len(b), nil
}
func (r fakeFileSystemReader) Seek(offset int64, whence int) (n int64, err error) {
switch whence {
case io.SeekStart:
case io.SeekCurrent:
offset += r.off
case io.SeekEnd:
offset += r.limit
default:
return 0, fmt.Errorf("invalid whence: %d", whence)
}
r.off = offset
return offset, nil
}
func (fakeFileSystemReader) Close() error { return nil }
func TestFileReaddir(t *testing.T) {
files, err := (&file{}).Readdir(-1)
if err != nil {
t.Fatal(err)
}
if l := len(files); l > 0 {
t.Fatalf("unexpected number of files; got %d, want 0", l)
}
}
func TestFileStat(t *testing.T) {
e := database.Entry{
Slug: "some slug",
Name: "some name",
Sum: "some sum",
Size: 123456,
Lifetime: nil,
Timestamp: time.Unix(1234, 5678),
}
fi, err := (&file{entry: e}).Stat()
if err != nil {
t.Fatal(err)
}
if got, want := fi.(*fileInfo).entry, e; got != want {
t.Fatalf("entries are not equal; got %#v, want %#v", got, want)
}
}
func TestFileInfo(t *testing.T) {
e := database.Entry{
Name: "some name",
Size: 123456,
Timestamp: time.Unix(1234, 5678),
}
fi := &fileInfo{entry: e}
if got, want := fi.Name(), e.Name; got != want {
t.Fatalf("unexpected name; got %s, want %s", got, want)
}
if got, want := fi.Size(), e.Size; got != want {
t.Fatalf("unexpected size; got %d, want %d", got, want)
}
if got, want := fi.Mode(), os.FileMode(0600); got != want {
t.Fatalf("unexpected mode; got %d, want %d", got, want)
}
if fi.IsDir() {
t.Fatalf("file info reports it's a directory when it shouldn't")
}
if fi.Sys() != nil {
t.Fatal("sys is not nil")
}
if got, want := fi.ModTime(), e.Timestamp; got != want {
t.Fatalf("unexpected mod time; got %s, want %s", got, want)
}
}