summaryrefslogtreecommitdiff
path: root/cache
diff options
context:
space:
mode:
authorewy <ewy0@protonmail.com>2026-04-22 18:29:37 +0200
committerewy <ewy0@protonmail.com>2026-04-22 18:29:37 +0200
commit374ed5e1a4dc635c42e33e4133729d40cf3e0e35 (patch)
tree01f12ca3eea0154b61afde3d1ee27df2094644e1 /cache
parentee481fa406bb685a16d8b568fca0c9f592c06180 (diff)
work on tests
also replace the annoying search return with a struct
Diffstat (limited to 'cache')
-rw-r--r--cache/cache.go42
-rw-r--r--cache/cache_test.go30
2 files changed, 59 insertions, 13 deletions
diff --git a/cache/cache.go b/cache/cache.go
index 98bdbe8..8a17592 100644
--- a/cache/cache.go
+++ b/cache/cache.go
@@ -16,6 +16,7 @@ type Cache struct {
Entries []Entry
}
+// Merge combines two caches and filters duplicate keys
func (c Cache) Merge(other Cache) Cache {
mp := make(map[string]string)
for _, e := range append(c.Entries, other.Entries...) {
@@ -33,22 +34,28 @@ type Entry struct {
Label string
}
+// Path is the file path to the "contexts" cache file
var Path = path.Join(paths.Cache, "contexts")
+// FsPath is the Path with the leading slash removed, to be opened from fs.FS
+var FsPath = Path[1:]
+
var UnexpectedEntryError = errors.New("unexpected cache entry")
-func Load() (Cache, error) {
- fd, err := os.Open(Path)
+func LoadFile(root fs.FS, path string) (Cache, error) {
+ fd, err := root.Open(path)
if errors.Is(err, os.ErrNotExist) {
return Cache{}, nil
} else if err != nil {
return Cache{}, err
}
- defer fd.Close()
- return FromReader(fd)
+ if fd != nil {
+ defer fd.Close()
+ }
+ return Load(fd)
}
-func FromReader(r io.Reader) (Cache, error) {
+func Load(r io.Reader) (Cache, error) {
c := Cache{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
@@ -95,13 +102,22 @@ func New(st *model.State) Cache {
return *c
}
-func Save(s *model.State) error {
- ld, err := Load()
+func SaveFile(path string, s *model.State, loaded Cache) error {
+ fd, err := os.Create(path)
if err != nil {
return err
}
- c := New(s).Merge(ld)
- return os.WriteFile(Path, []byte(c.String()), os.ModePerm)
+ if fd != nil {
+ defer fd.Close()
+ }
+ return Save(s, fd, loaded)
+}
+
+func Save(s *model.State, w io.Writer, loaded Cache) error {
+ result := New(s).Merge(loaded)
+ _, err := w.Write([]byte(result.String()))
+ return err
+
}
func LoadState(f fs.FS, cache Cache, indexers []model.Indexer, runners []model.Runner) (*model.State, []error) {
@@ -127,3 +143,11 @@ outer:
Entries: result,
}
}
+
+func Touch() error {
+ fd, err := os.Create(Path)
+ if fd != nil {
+ defer fd.Close()
+ }
+ return err
+}
diff --git a/cache/cache_test.go b/cache/cache_test.go
index c140154..499c0f5 100644
--- a/cache/cache_test.go
+++ b/cache/cache_test.go
@@ -10,7 +10,7 @@ func TestFromReader_Blank(t *testing.T) {
input := `
`
sr := strings.NewReader(input)
- c, err := FromReader(sr)
+ c, err := Load(sr)
assert.Nil(t, err)
assert.Len(t, c.Entries, 0)
}
@@ -18,7 +18,7 @@ func TestFromReader_Blank(t *testing.T) {
func TestFromReader_OneEntry(t *testing.T) {
input := `/abc/def # deffers`
sr := strings.NewReader(input)
- c, err := FromReader(sr)
+ c, err := Load(sr)
assert.Nil(t, err)
assert.Len(t, c.Entries, 1)
assert.Equal(t, c.Entries[0], Entry{
@@ -33,7 +33,7 @@ func TestFromReader_ManyEntries(t *testing.T) {
/path/src # da source
`
sr := strings.NewReader(input)
- c, err := FromReader(sr)
+ c, err := Load(sr)
assert.Nil(t, err)
assert.Len(t, c.Entries, 3)
assert.Equal(t, c.Entries[0], Entry{
@@ -61,7 +61,7 @@ func TestFromReader_Comments(t *testing.T) {
# // comment
`
sr := strings.NewReader(input)
- c, err := FromReader(sr)
+ c, err := Load(sr)
assert.Nil(t, err)
assert.Len(t, c.Entries, 3)
assert.Equal(t, c.Entries[0], Entry{
@@ -91,3 +91,25 @@ func TestStrip_Nothing(t *testing.T) {
result := c.Strip(old)
assert.Equal(t, c, result)
}
+
+func TestMerge(t *testing.T) {
+ a := Entry{
+ Path: "/usr/share/asdf",
+ }
+ b := Entry{
+ Path: "/test/location",
+ }
+ c := Entry{
+ Path: "/new/mypath",
+ Label: "mypath",
+ }
+ base := Cache{Entries: []Entry{
+ a, b,
+ }}
+ other := Cache{Entries: []Entry{
+ b, c,
+ }}
+ result := base.Merge(other)
+ assert.Len(t, result.Entries, 3)
+ assert.Contains(t, result.Entries, a, b, c)
+}