85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package filecatalog
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestFindYAMLFilesNestedSortedAndFiltered(t *testing.T) {
|
|
root := t.TempDir()
|
|
mustWriteFile(t, filepath.Join(root, "z", "prompt.yml"), "id: z")
|
|
mustWriteFile(t, filepath.Join(root, "a", "profile.yaml"), "id: a")
|
|
mustWriteFile(t, filepath.Join(root, "a", "ignore.txt"), "not yaml")
|
|
mustWriteFile(t, filepath.Join(root, "b", "ignore.yaml.bak"), "not yaml")
|
|
|
|
got, err := FindYAMLFiles(context.Background(), root)
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got %v", err)
|
|
}
|
|
|
|
want := []string{
|
|
filepath.Join(root, "a", "profile.yaml"),
|
|
filepath.Join(root, "z", "prompt.yml"),
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("expected sorted YAML files %v, got %v", want, got)
|
|
}
|
|
}
|
|
|
|
func TestFindYAMLFilesHonorsContextCancellation(t *testing.T) {
|
|
root := t.TempDir()
|
|
mustWriteFile(t, filepath.Join(root, "one.yaml"), "id: one")
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
_, err := FindYAMLFiles(ctx, root)
|
|
if !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("expected context.Canceled, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRelativePathNested(t *testing.T) {
|
|
root := t.TempDir()
|
|
path := filepath.Join(root, "nested", "profiles", "local.yaml")
|
|
got := RelativePath(root, path)
|
|
want := filepath.Join("nested", "profiles", "local.yaml")
|
|
if got != want {
|
|
t.Fatalf("expected relative path %q, got %q", want, got)
|
|
}
|
|
}
|
|
|
|
func TestStemStripsYAMLExtensions(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{name: "yaml", in: "prompt.yaml", want: "prompt"},
|
|
{name: "yml", in: "profile.yml", want: "profile"},
|
|
{name: "other", in: "file.txt", want: "file.txt"},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := Stem(tc.in); got != tc.want {
|
|
t.Fatalf("expected %q, got %q", tc.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func mustWriteFile(t *testing.T, path string, content string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatalf("failed to create directory: %v", err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
|
t.Fatalf("failed to write file %q: %v", path, err)
|
|
}
|
|
}
|