shad-go/distbuild/pkg/filecache/filecache_test.go

60 lines
1.2 KiB
Go
Raw Permalink Normal View History

2020-04-04 18:45:29 +00:00
package filecache_test
import (
"errors"
2020-04-04 18:45:29 +00:00
"os"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/slon/shad-go/distbuild/pkg/build"
"gitlab.com/slon/shad-go/distbuild/pkg/filecache"
)
2020-04-04 18:45:29 +00:00
type testCache struct {
*filecache.Cache
tmpDir string
}
func newCache(t *testing.T) *testCache {
tmpDir, err := os.MkdirTemp("", "filecache")
require.NoError(t, err)
2020-04-04 18:45:29 +00:00
c, err := filecache.New(tmpDir)
require.NoError(t, err)
cc := &testCache{Cache: c, tmpDir: tmpDir}
t.Cleanup(cc.cleanup)
return cc
2020-04-04 18:45:29 +00:00
}
2020-04-04 21:55:07 +00:00
func (c *testCache) cleanup() {
_ = os.Remove(c.tmpDir)
2020-04-04 18:45:29 +00:00
}
func TestFileCache(t *testing.T) {
cache := newCache(t)
_, abort, err := cache.Write(build.ID{01})
require.NoError(t, err)
require.NoError(t, abort())
_, _, err = cache.Get(build.ID{01})
2020-04-04 18:45:29 +00:00
require.Truef(t, errors.Is(err, filecache.ErrNotFound), "%v", err)
f, _, err := cache.Write(build.ID{02})
require.NoError(t, err)
_, err = f.Write([]byte("foo bar"))
require.NoError(t, err)
require.NoError(t, f.Close())
path, unlock, err := cache.Get(build.ID{02})
require.NoError(t, err)
defer unlock()
content, err := os.ReadFile(path)
require.NoError(t, err)
require.Equal(t, []byte("foo bar"), content)
}