Mount, MountEmbed, Open, ReadFile, ReadString, Sub, GetAsset, GetAssetBytes, ScanAssets, GeneratePack, Extract → all return Result. Data.ReadFile, ReadString, List, ListNames, Extract → Result. Data.New uses Mount's Result internally. Internal helpers (WalkDir callback, copyFile) stay error — they're not public API. 231 tests, 77.4% coverage. Co-Authored-By: Virgil <virgil@lethean.io>
129 lines
3.3 KiB
Go
129 lines
3.3 KiB
Go
package core_test
|
|
|
|
import (
|
|
"embed"
|
|
"io"
|
|
"testing"
|
|
|
|
. "forge.lthn.ai/core/go/pkg/core"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
//go:embed testdata
|
|
var testFS embed.FS
|
|
|
|
// --- Data (Embedded Content Mounts) ---
|
|
|
|
func TestData_New_Good(t *testing.T) {
|
|
c := New()
|
|
r := c.Data().New(Options{
|
|
{K: "name", V: "test"},
|
|
{K: "source", V: testFS},
|
|
{K: "path", V: "testdata"},
|
|
})
|
|
assert.True(t, r.OK)
|
|
assert.NotNil(t, r.Value)
|
|
}
|
|
|
|
func TestData_New_Bad(t *testing.T) {
|
|
c := New()
|
|
|
|
r := c.Data().New(Options{{K: "source", V: testFS}})
|
|
assert.False(t, r.OK)
|
|
|
|
r = c.Data().New(Options{{K: "name", V: "test"}})
|
|
assert.False(t, r.OK)
|
|
|
|
r = c.Data().New(Options{{K: "name", V: "test"}, {K: "source", V: "not-an-fs"}})
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestData_ReadString_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{{K: "name", V: "app"}, {K: "source", V: testFS}, {K: "path", V: "testdata"}})
|
|
r := c.Data().ReadString("app/test.txt")
|
|
assert.True(t, r.OK)
|
|
assert.Equal(t, "hello from testdata\n", r.Value.(string))
|
|
}
|
|
|
|
func TestData_ReadString_Bad(t *testing.T) {
|
|
c := New()
|
|
r := c.Data().ReadString("nonexistent/file.txt")
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestData_ReadFile_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{{K: "name", V: "app"}, {K: "source", V: testFS}, {K: "path", V: "testdata"}})
|
|
r := c.Data().ReadFile("app/test.txt")
|
|
assert.True(t, r.OK)
|
|
assert.Equal(t, "hello from testdata\n", string(r.Value.([]byte)))
|
|
}
|
|
|
|
func TestData_Get_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{{K: "name", V: "brain"}, {K: "source", V: testFS}, {K: "path", V: "testdata"}})
|
|
emb := c.Data().Get("brain")
|
|
assert.NotNil(t, emb)
|
|
|
|
r := emb.Open("test.txt")
|
|
assert.True(t, r.OK)
|
|
file := r.Value.(io.ReadCloser)
|
|
defer file.Close()
|
|
content, _ := io.ReadAll(file)
|
|
assert.Equal(t, "hello from testdata\n", string(content))
|
|
}
|
|
|
|
func TestData_Get_Bad(t *testing.T) {
|
|
c := New()
|
|
emb := c.Data().Get("nonexistent")
|
|
assert.Nil(t, emb)
|
|
}
|
|
|
|
func TestData_Mounts_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{{K: "name", V: "a"}, {K: "source", V: testFS}, {K: "path", V: "testdata"}})
|
|
c.Data().New(Options{{K: "name", V: "b"}, {K: "source", V: testFS}, {K: "path", V: "testdata"}})
|
|
mounts := c.Data().Mounts()
|
|
assert.Len(t, mounts, 2)
|
|
}
|
|
|
|
func TestEmbed_Legacy_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{{K: "name", V: "app"}, {K: "source", V: testFS}, {K: "path", V: "testdata"}})
|
|
assert.NotNil(t, c.Embed())
|
|
}
|
|
|
|
func TestData_List_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{{K: "name", V: "app"}, {K: "source", V: testFS}, {K: "path", V: "."}})
|
|
r := c.Data().List("app/testdata")
|
|
assert.True(t, r.OK)
|
|
}
|
|
|
|
func TestData_List_Bad(t *testing.T) {
|
|
c := New()
|
|
r := c.Data().List("nonexistent/path")
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestData_ListNames_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{{K: "name", V: "app"}, {K: "source", V: testFS}, {K: "path", V: "."}})
|
|
r := c.Data().ListNames("app/testdata")
|
|
assert.True(t, r.OK)
|
|
assert.Contains(t, r.Value.([]string), "test")
|
|
}
|
|
|
|
func TestData_Extract_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{{K: "name", V: "app"}, {K: "source", V: testFS}, {K: "path", V: "."}})
|
|
r := c.Data().Extract("app/testdata", t.TempDir(), nil)
|
|
assert.True(t, r.OK)
|
|
}
|
|
|
|
func TestData_Extract_Bad(t *testing.T) {
|
|
c := New()
|
|
r := c.Data().Extract("nonexistent/path", t.TempDir(), nil)
|
|
assert.False(t, r.OK)
|
|
}
|