mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-06 01:05:48 +02:00
Start working on tests for disk cache
This commit is contained in:
@@ -138,7 +138,7 @@ func (dc *DiskCache) get(key string, items []string) map[string]string {
|
||||
}
|
||||
for _, x := range items {
|
||||
p := filepath.Join(base, x)
|
||||
if s, err := os.Stat(p); err != nil || !s.IsDir() {
|
||||
if s, err := os.Stat(p); err != nil || s.IsDir() {
|
||||
continue
|
||||
}
|
||||
ans[x] = p
|
||||
@@ -193,6 +193,11 @@ func (dc *DiskCache) update_timestamp(key string) {
|
||||
func (dc *DiskCache) update_accounting(key string, changed int64) (err error) {
|
||||
if err = dc.ensure_entries(); err == nil {
|
||||
t := dc.entry_map[key]
|
||||
if t == nil {
|
||||
t = &Entry{Key: key}
|
||||
dc.entry_map[key] = t
|
||||
dc.entries.SortedEntries = append(dc.entries.SortedEntries, t)
|
||||
}
|
||||
old_size := t.Size
|
||||
t.Size += changed
|
||||
t.Size = max(0, t.Size)
|
||||
|
||||
41
tools/disk_cache/implementation_test.go
Normal file
41
tools/disk_cache/implementation_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package disk_cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/kovidgoyal/kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func TestDiskCache(t *testing.T) {
|
||||
tdir := t.TempDir()
|
||||
dc, err := NewDiskCache(tdir, 64)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := dc.Get("missing", "one", "two")
|
||||
if diff := cmp.Diff(m, make(map[string]string)); diff != "" {
|
||||
t.Fatalf("Unexpected return from missing: %s", diff)
|
||||
}
|
||||
dc.Add("k1", map[string][]byte{"1": []byte("abcd"), "2": []byte("efgh")})
|
||||
|
||||
ad := func(key string, expected map[string]string) {
|
||||
actual := dc.Get(key, utils.Keys(expected)...)
|
||||
|
||||
for k, path := range actual {
|
||||
d, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual[k] = string(d)
|
||||
}
|
||||
if diff := cmp.Diff(expected, actual); diff != "" {
|
||||
t.Fatalf("Data for %s not equal: %s", key, diff)
|
||||
}
|
||||
}
|
||||
ad("k1", map[string]string{"1": "abcd", "2": "efgh"})
|
||||
}
|
||||
Reference in New Issue
Block a user