diff options
| author | ewy <ewy0@protonmail.com> | 2026-04-22 20:44:06 +0200 |
|---|---|---|
| committer | ewy <ewy0@protonmail.com> | 2026-04-22 20:44:06 +0200 |
| commit | 086a0cc0eb75c0c3a15cf6a715427df1e2d589a2 (patch) | |
| tree | 0bcc881b202f94a7ea39f94f13aea89ca2046312 /cache | |
| parent | c6a362a039b1662dfc0badeb8badc4458e446787 (diff) | |
slowly working on that test coverage
Diffstat (limited to 'cache')
| -rw-r--r-- | cache/cache.go | 22 | ||||
| -rw-r--r-- | cache/cache_test.go | 29 |
2 files changed, 40 insertions, 11 deletions
diff --git a/cache/cache.go b/cache/cache.go index f036052..c51acbc 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -34,6 +34,8 @@ type Entry struct { Label string } +var Empty = Cache{} + // Path is the file path to the "contexts" cache file var Path = path.Join(paths.Cache, "contexts") @@ -42,9 +44,11 @@ var FsPath = Path[1:] var UnexpectedEntryError = errors.New("unexpected cache entry") +// LoadFile creates a Cache from a file or an empty one if the file does not exist +// this handles opening a reader for Unmarshal func LoadFile(root fs.FS, path string) (Cache, error) { fd, err := root.Open(path) - if errors.Is(err, os.ErrNotExist) { + if errors.Is(err, fs.ErrNotExist) { return Cache{}, nil } else if err != nil { return Cache{}, err @@ -52,10 +56,11 @@ func LoadFile(root fs.FS, path string) (Cache, error) { if fd != nil { defer fd.Close() } - return Load(fd) + return Unmarshal(fd) } -func Load(r io.Reader) (Cache, error) { +// Unmarshal attempts to create a Cache from reader content +func Unmarshal(r io.Reader) (Cache, error) { c := Cache{} scanner := bufio.NewScanner(r) for scanner.Scan() { @@ -80,7 +85,8 @@ func Load(r io.Reader) (Cache, error) { return c, nil } -func (c Cache) String() string { +// Marshal returns the file representation of the Cache +func (c Cache) Marshal() []byte { b := strings.Builder{} for _, e := range c.Entries { b.WriteString(e.Path) @@ -88,7 +94,11 @@ func (c Cache) String() string { b.WriteString(e.Label) b.WriteString("\n") } - return b.String() + return []byte(b.String()) +} + +func (c Cache) String() string { + return string(c.Marshal()) } func New(st *model.State) Cache { @@ -115,7 +125,7 @@ func SaveFile(path string, s *model.State, loaded Cache) error { func Save(s *model.State, w io.Writer, loaded Cache) error { result := New(s).Merge(loaded) - _, err := w.Write([]byte(result.String())) + _, err := w.Write([]byte(result.Marshal())) return err } diff --git a/cache/cache_test.go b/cache/cache_test.go index 38c100a..1a9c46d 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -4,6 +4,7 @@ package cache import ( "github.com/stretchr/testify/assert" + "path/filepath" . "pik/testx" "strings" "testing" @@ -36,7 +37,7 @@ func TestFromReader_Blank(t *testing.T) { input := ` ` sr := strings.NewReader(input) - c, err := Load(sr) + c, err := Unmarshal(sr) assert.Nil(t, err) assert.Len(t, c.Entries, 0) } @@ -44,7 +45,7 @@ func TestFromReader_Blank(t *testing.T) { func TestFromReader_OneEntry(t *testing.T) { input := `/abc/def # deffers` sr := strings.NewReader(input) - c, err := Load(sr) + c, err := Unmarshal(sr) assert.Nil(t, err) assert.Len(t, c.Entries, 1) assert.Equal(t, c.Entries[0], Entry{ @@ -59,7 +60,7 @@ func TestFromReader_ManyEntries(t *testing.T) { /path/src # da source ` sr := strings.NewReader(input) - c, err := Load(sr) + c, err := Unmarshal(sr) assert.Nil(t, err) assert.Len(t, c.Entries, 3) assert.Equal(t, c.Entries[0], Entry{ @@ -87,7 +88,7 @@ func TestFromReader_Comments(t *testing.T) { # // comment ` sr := strings.NewReader(input) - c, err := Load(sr) + c, err := Unmarshal(sr) assert.Nil(t, err) assert.Len(t, c.Entries, 3) assert.Equal(t, c.Entries[0], Entry{ @@ -162,6 +163,24 @@ func TestLoadFile_NotExist(t *testing.T) { f := fstest.MapFS{} c, err := LoadFile(f, "anything is fine") assert.Nil(t, c.Entries) - assert.Error(t, err) + assert.NoError(t, err) + +} +func TestSaveFile(t *testing.T) { + dir := t.TempDir() + loc := filepath.Join(dir, "savefile") + st := TState(TSource("source_one", "target"), TSource("second_source", "t1", "t2", "t3")) + c := Cache{Entries: []Entry{ + { + Path: "path", + Label: "label", + }, + { + Path: "/otherpath/123", + Label: "", + }, + }} + err := SaveFile(loc, st, c) + assert.NoError(t, err) } |
