aboutsummaryrefslogtreecommitdiff
path: root/internal/kwcache/kwcache_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/kwcache/kwcache_test.go')
-rw-r--r--internal/kwcache/kwcache_test.go49
1 files changed, 46 insertions, 3 deletions
diff --git a/internal/kwcache/kwcache_test.go b/internal/kwcache/kwcache_test.go
index 8395b2d..077ecd3 100644
--- a/internal/kwcache/kwcache_test.go
+++ b/internal/kwcache/kwcache_test.go
@@ -6,9 +6,13 @@ import (
"os"
"path/filepath"
"reflect"
+ "strings"
"testing"
)
+// allKeys is every keyword the tests store, for Save calls that keep them all.
+var allKeys = []string{"k:acme", "k:faktura", "k:nip", "k:new", "k:x"}
+
var (
a = ID{Dev: 1, Ino: 10, Size: 100, MTime: 1000}
b = ID{Dev: 1, Ino: 11, Size: 200, MTime: 2000}
@@ -17,7 +21,7 @@ var (
func saved(t *testing.T, c *Cache, present ...ID) string {
t.Helper()
path := filepath.Join(t.TempDir(), "sub", "dl.cache")
- if err := c.Save(path, present); err != nil {
+ if err := c.Save(path, present, allKeys); err != nil {
t.Fatal(err)
}
return path
@@ -94,7 +98,7 @@ func TestSaveKeepsOnlyPresentFiles(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if err := next.Save(path, []ID{b}); err != nil {
+ if err := next.Save(path, []ID{b}, allKeys); err != nil {
t.Fatal(err)
}
last, err := Load(path, "fp")
@@ -171,10 +175,49 @@ func TestLoadMissingAndCorrupt(t *testing.T) {
// has no file yet, creates none.
func TestSaveWithNothingWritesNothing(t *testing.T) {
path := filepath.Join(t.TempDir(), "sub", "dl.cache")
- if err := New("fp").Save(path, []ID{a}); err != nil {
+ if err := New("fp").Save(path, []ID{a}, allKeys); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Dir(path)); !os.IsNotExist(err) {
t.Errorf("Save created %s for an empty cache", filepath.Dir(path))
}
}
+
+// TestSaveTrimsToCurrentKeywords: a keyword no longer in the configuration
+// is not kept in the cache file (review cache F6).
+func TestSaveTrimsToCurrentKeywords(t *testing.T) {
+ c := New("fp")
+ c.Store(a, map[string]bool{"k:keep": true, "k:gone client": true})
+ path := filepath.Join(t.TempDir(), "dl.cache")
+ if err := c.Save(path, []ID{a}, []string{"k:keep"}); err != nil {
+ t.Fatal(err)
+ }
+ b, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Contains(string(b), "gone client") {
+ t.Errorf("a removed keyword is still stored:\n%s", b)
+ }
+ got, _ := Load(path, "fp")
+ if hits, ok := got.Lookup(a, []string{"k:keep"}); !ok || !hits[0] {
+ t.Errorf("the kept keyword's answer was lost: %v %v", hits, ok)
+ }
+}
+
+// TestSaveTightensAnExistingDirectory: the cache directory is private even
+// when it already existed with wider permissions (review cache F7).
+func TestSaveTightensAnExistingDirectory(t *testing.T) {
+ dir := filepath.Join(t.TempDir(), "krino")
+ if err := os.Mkdir(dir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ c := New("fp")
+ c.Store(a, map[string]bool{"k:acme": true})
+ if err := c.Save(filepath.Join(dir, "dl.cache"), []ID{a}, []string{"k:acme"}); err != nil {
+ t.Fatal(err)
+ }
+ if fi, err := os.Stat(dir); err != nil || fi.Mode().Perm() != 0o700 {
+ t.Errorf("directory mode %v, %v; want 0700", fi.Mode().Perm(), err)
+ }
+}