forked from goproxy/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacher_test.go
68 lines (59 loc) · 2.13 KB
/
cacher_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
package goproxy
import (
"context"
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
)
func TestDirCacher(t *testing.T) {
dirCacher := DirCacher(t.TempDir())
if rc, err := dirCacher.Get(context.Background(), "a/b/c"); err == nil {
t.Fatal("expected error")
} else if got, want := err, fs.ErrNotExist; !compareErrors(got, want) {
t.Fatalf("got %q, want %q", got, want)
} else if got := rc; got != nil {
t.Errorf("got %#v, want nil", got)
}
if err := dirCacher.Put(context.Background(), "a/b/c", strings.NewReader("foobar")); err != nil {
t.Fatalf("unexpected error %q", err)
}
if fi, err := os.Stat(filepath.Join(string(dirCacher), filepath.FromSlash("a/b"))); err != nil {
t.Fatalf("unexpected error %q", err)
} else if got, want := fi.Mode().Perm(), os.FileMode(0o755).Perm(); got != want {
t.Errorf("got %d, want %d", got, want)
}
if fi, err := os.Stat(filepath.Join(string(dirCacher), filepath.FromSlash("a/b/c"))); err != nil {
t.Fatalf("unexpected error %q", err)
} else if got, want := fi.Mode().Perm(), os.FileMode(0o644).Perm(); got != want {
t.Errorf("got %d, want %d", got, want)
}
if rc, err := dirCacher.Get(context.Background(), "a/b/c"); err != nil {
t.Fatalf("unexpected error %q", err)
} else if rc == nil {
t.Fatal("unexpected nil")
} else if b, err := io.ReadAll(rc); err != nil {
t.Fatalf("unexpected error %q", err)
} else if err := rc.Close(); err != nil {
t.Fatalf("unexpected error %q", err)
} else if got, want := string(b), "foobar"; got != want {
t.Errorf("got %q, want %q", got, want)
}
if err := dirCacher.Put(context.Background(), "d/e/f", &testReadSeeker{
ReadSeeker: strings.NewReader("foobar"),
read: func(rs io.ReadSeeker, p []byte) (n int, err error) {
return 0, errors.New("cannot read")
},
}); err == nil {
t.Fatal("expected error")
} else if got, want := err.Error(), "cannot read"; got != want {
t.Errorf("got %q, want %q", got, want)
}
dirCacher = DirCacher(filepath.Join(string(dirCacher), filepath.FromSlash("a/b/c")))
if err := dirCacher.Put(context.Background(), "d/e/f", strings.NewReader("foobar")); err == nil {
t.Fatal("expected error")
}
}