test: introduce afero-based mock filesystem for hermetic tests

This commit is contained in:
2026-05-23 13:12:15 +01:00
parent 2334ed7922
commit 7bd70bfaeb
10 changed files with 379 additions and 47 deletions
+17 -7
View File
@@ -5,7 +5,10 @@ import (
"path/filepath"
"strings"
"github.com/spf13/afero"
"gopkg.in/yaml.v3"
"github.com/lukaszraczylo/harness-sync/internal/fsx"
)
// Doc mirrors canonical.Skill / canonical.Agent without importing them.
@@ -35,15 +38,15 @@ func ParseFrontmatter(b []byte) (name, description string) {
return meta.Name, meta.Description
}
// ImportMarkdownTree walks dir and returns one Doc per markdown file. When
// requiredFilename is non-empty (e.g. "SKILL.md"), only files with that exact
// basename are included; otherwise any *.md.
func ImportMarkdownTree(dir, requiredFilename string) ([]Doc, error) {
// ImportMarkdownTreeFS walks dir on fs and returns one Doc per matching file.
// When requiredFilename is non-empty (e.g. "SKILL.md"), only files with that
// exact basename are included; otherwise any *.md.
func ImportMarkdownTreeFS(fs fsx.FS, dir, requiredFilename string) ([]Doc, error) {
var docs []Doc
if !DirExists(dir) {
if !DirExistsFS(fs, dir) {
return docs, nil
}
err := filepath.Walk(dir, func(p string, info os.FileInfo, err error) error {
err := afero.Walk(fs, dir, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
@@ -56,7 +59,7 @@ func ImportMarkdownTree(dir, requiredFilename string) ([]Doc, error) {
if requiredFilename == "" && !strings.HasSuffix(p, ".md") {
return nil
}
body, err := os.ReadFile(p)
body, err := afero.ReadFile(fs, p)
if err != nil {
return err
}
@@ -75,3 +78,10 @@ func ImportMarkdownTree(dir, requiredFilename string) ([]Doc, error) {
})
return docs, err
}
// ImportMarkdownTree walks dir and returns one Doc per markdown file. When
// requiredFilename is non-empty (e.g. "SKILL.md"), only files with that exact
// basename are included; otherwise any *.md.
func ImportMarkdownTree(dir, requiredFilename string) ([]Doc, error) {
return ImportMarkdownTreeFS(fsx.OS(), dir, requiredFilename)
}