jx/internal/fan/dir_test.go

131 lines
2.9 KiB
Go
Raw Normal View History

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package fan
import (
"github.com/stretchr/testify/assert"
"testing"
"decl/internal/folio"
"decl/internal/data"
"log/slog"
"path/filepath"
"os"
"fmt"
)
func TestNewDirSource(t *testing.T) {
s := NewDir()
assert.NotNil(t, s)
}
func TestExtractDirectory(t *testing.T) {
s := NewDir()
assert.NotNil(t, s)
document := folio.DocumentRegistry.NewDocument("")
assert.NotNil(t, document)
assert.Nil(t, s.ExtractDirectory(TempDir, document))
assert.Greater(t, 2, document.Len())
}
func TestIsParent(t *testing.T) {
s := NewDir()
assert.NotNil(t, s)
m := map[string]int{
"/foo/bar": 3,
"/foo": 1,
}
res, count := s.isParent(&m, "/foo/bar/baz/quuz", "/foo/bar")
assert.Equal(t, "/foo/bar", res)
assert.Equal(t, 4, count)
assert.Equal(t, 2, m["/foo"])
}
func TestLCPath(t *testing.T) {
s := NewDir()
assert.NotNil(t, s)
result := s.LCPath([]string{
"/foo/bar/baz/quuz",
"/foo/bar/baz/quuz/abc.txt",
"/foo/bar/baz/quuz/def.txt",
"/foo/bar/baz/quz/ghi.txt",
"/foo/bar/kiw",
"/tmp",
})
assert.Equal(t, "/foo/bar", result)
result = s.LCPath([]string{
"/foo/bar/baz/quuz",
"/foo/eer/voo",
"/foo/bar/baz/quuz/abc.txt",
"/foo/bar/baz/quuz/def.txt",
"/foo/bar/baz/quz/ghi.txt",
"/foo/bar/kiw",
"/tmp",
"/usr",
"/usr/lib",
})
assert.Equal(t, "/foo", result)
}
func BenchmarkLCPath(b *testing.B) {
s := NewDir()
assert.NotNil(b, s)
for i := 0; i < b.N; i++ {
s.LCPath([]string{
"/foo/bar/baz/quuz",
"/foo/eer/voo",
"/foo/bar/baz/quuz/abc.txt",
"/foo/bar/baz/quuz/def.txt",
"/foo/bar/baz/quz/ghi.txt",
"/foo/bar/kiw",
"/tmp",
"/usr",
"/usr/lib",
})
}
}
func TestEmit(t *testing.T) {
s := NewDir()
assert.NotNil(t, s)
contextDir, _ := filepath.Abs(filepath.Join(TempDir, "context"))
etcDir := filepath.Join(contextDir, "etc")
binDir := filepath.Join(contextDir, "bin")
usrDir := filepath.Join(contextDir, "usr")
usrLibDir := filepath.Join(contextDir, "usr/lib")
usrBinDir := filepath.Join(contextDir, "usr/bin")
assert.Nil(t, os.Mkdir(contextDir, os.ModePerm))
assert.Nil(t, os.Mkdir(etcDir, os.ModePerm))
assert.Nil(t, os.Mkdir(binDir, os.ModePerm))
assert.Nil(t, os.Mkdir(usrDir, os.ModePerm))
assert.Nil(t, os.Mkdir(usrLibDir, os.ModePerm))
assert.Nil(t, os.Mkdir(usrBinDir, os.ModePerm))
decl := folio.NewDeclaration()
srcFile := fmt.Sprintf("file://%s", contextDir)
resErr := decl.NewResource(&srcFile)
assert.Nil(t, resErr)
slog.Info("TestEmit()", "file", decl, "res", decl.Attributes)
document, extractErr := s.Extract(decl.Resource(), nil)
slog.Info("TestEmit() - Extract", "document", document, "error", extractErr)
assert.Nil(t, extractErr)
assert.Greater(t, document.Len(), 4)
res, emitErr := s.Emit(document, nil)
slog.Info("TestEmit()", "res", res, "error", emitErr)
assert.Nil(t, emitErr)
assert.Equal(t, contextDir, res.(data.FileResource).FilePath())
}