From 5e1e840bbcdf8348ae62145e1b7231764fcc5a97 Mon Sep 17 00:00:00 2001 From: ewy Date: Wed, 22 Apr 2026 19:27:25 +0200 Subject: update script experiment --- cache/cache.go | 8 -------- cache/cache_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) (limited to 'cache') diff --git a/cache/cache.go b/cache/cache.go index 8a17592..f036052 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -143,11 +143,3 @@ 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 499c0f5..38c100a 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -1,11 +1,37 @@ +//go:build test + package cache import ( "github.com/stretchr/testify/assert" + . "pik/testx" "strings" "testing" + "testing/fstest" ) +func TestLoadFile(t *testing.T) { + input := ` + +# comment +/asdf/hjkl # label + +# other comment +//comment + +` + f := fstest.MapFS{ + "contexts": &fstest.MapFile{ + Data: []byte(input), + }, + } + res, err := LoadFile(f, "contexts") + assert.NoError(t, err) + assert.Len(t, res.Entries, 1) + assert.Equal(t, res.Entries[0].Path, "/asdf/hjkl") + assert.Equal(t, res.Entries[0].Label, "label") +} + func TestFromReader_Blank(t *testing.T) { input := ` ` @@ -113,3 +139,29 @@ func TestMerge(t *testing.T) { assert.Len(t, result.Entries, 3) assert.Contains(t, result.Entries, a, b, c) } + +func TestCache_String(t *testing.T) { + c := Cache{Entries: []Entry{{Path: "/asdf/hjkl", Label: "hjkl"}}} + output := c.String() + for _, e := range c.Entries { + assert.Contains(t, output, e.Path) + assert.Contains(t, output, e.Label) + } +} + +func TestNew(t *testing.T) { + st := TState(TSource("/test/location", "target1", "target2"), TSource("/other/place/asdf/hjkl", "1target", "2target")) + c := New(st) + assert.NotNil(t, c) + assert.Len(t, c.Entries, 2) + assert.Equal(t, "/test/location", c.Entries[0].Path) + assert.Equal(t, "/other/place/asdf/hjkl", c.Entries[1].Path) +} + +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) + +} -- cgit v1.3.1