forked from goproxy/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacher_test.go
More file actions
95 lines (83 loc) · 2.79 KB
/
cacher_test.go
File metadata and controls
95 lines (83 loc) · 2.79 KB
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
package goproxy
import (
"context"
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
)
func TestDirCacher(t *testing.T) {
t.Run("Normal", func(t *testing.T) {
dirCacher := DirCacher(t.TempDir())
if err := dirCacher.Put(context.Background(), "a/b/c", strings.NewReader("foobar")); err != nil {
t.Fatalf("unexpected error %v", err)
}
if fi, err := os.Stat(filepath.Join(string(dirCacher), filepath.FromSlash("a/b"))); err != nil {
t.Errorf("unexpected error %v", 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.Errorf("unexpected error %v", 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.Errorf("unexpected error %v", err)
} else if rc == nil {
t.Error("unexpected nil")
} else if b, err := io.ReadAll(rc); err != nil {
t.Errorf("unexpected error %v", err)
} else if err := rc.Close(); err != nil {
t.Errorf("unexpected error %v", err)
} else if got, want := string(b), "foobar"; got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("GetNonExistentFile", func(t *testing.T) {
dirCacher := DirCacher(t.TempDir())
rc, err := dirCacher.Get(context.Background(), "a/b/c")
if err == nil {
t.Fatal("expected error")
}
if got, want := err, fs.ErrNotExist; !compareErrors(got, want) {
t.Errorf("got %v, want %v", got, want)
}
if got := rc; got != nil {
t.Errorf("got %#v, want nil", got)
}
})
t.Run("PutWithReadError", func(t *testing.T) {
dirCacher := DirCacher(t.TempDir())
errRead := errors.New("cannot read")
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, errRead
},
})
if err == nil {
t.Fatal("expected error")
}
if got, want := err, errRead; !compareErrors(got, want) {
t.Errorf("got %v, want %v", got, want)
}
})
t.Run("PutWithInvalidDirectory", func(t *testing.T) {
cacheDir := t.TempDir()
if err := os.MkdirAll(filepath.Join(cacheDir, filepath.FromSlash("a/b")), 0o755); err != nil {
t.Fatalf("unexpected error %v", err)
}
if err := os.WriteFile(filepath.Join(cacheDir, filepath.FromSlash("a/b/c")), []byte("foobar"), 0o644); err != nil {
t.Fatalf("unexpected error %v", err)
}
dirCacher := DirCacher(filepath.Join(cacheDir, filepath.FromSlash("a/b/c")))
err := dirCacher.Put(context.Background(), "d/e/f", strings.NewReader("foobar"))
if err == nil {
t.Fatal("expected error")
}
})
}