package api
import (
"context"
"os"
"os/exec"
"path/filepath"
"testing"
"craftdesign.group/quarry/internal/ops"
"craftdesign.group/quarry/internal/zitcli"
)
// tree.list over nested folders answers the same entries the full listing
// did: the folders first with their recursive file count, then the files
// with their blob and size.
func TestTreeListNestedFolders(t *testing.T) {
if _, err := exec.LookPath("zit"); err != nil {
t.Skip("zit not on PATH")
}
dir := t.TempDir()
sh := func(args ...string) {
t.Helper()
cmd := exec.Command("zit", args...)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("zit %v: %v\n%s", args, err, out)
}
}
sh("init")
for _, f := range []string{"top.txt", "a/one.txt", "a/b/two.txt", "a/b/c/three.txt", "z/only.txt"} {
if err := os.MkdirAll(filepath.Dir(filepath.Join(dir, f)), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, f), []byte(f+"\n"), 0o644); err != nil {
t.Fatal(err)
}
sh("add", "--", f)
}
sh("commit", "-m", "nested")
r := ops.NewRegistry()
d := Deps{Version: "test", Repos: NewRepos(map[string]*Repo{"t": {Chat: "t", Zit: zitcli.New(dir)}})}
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
type row struct {
name, path string
dir bool
files int
size int64
}
list := func(path string) []row {
t.Helper()
got, err := r.Invoke(context.Background(), "tree.list", treeIn{Repo: "t", Version: "trunk", Path: path})
if err != nil {
t.Fatalf("FAIL: tree.list %q: %v", path, err)
}
var rs []row
for _, e := range got.(treeOut).Entries {
rs = append(rs, row{e.Name, e.Path, e.Dir, e.Files, e.Size})
}
return rs
}
for _, tc := range []struct {
path string
want []row
}{
{"", []row{{"a", "a", true, 3, 0}, {"z", "z", true, 1, 0}, {"top.txt", "top.txt", false, 0, 8}}},
{"a", []row{{"b", "a/b", true, 2, 0}, {"one.txt", "a/one.txt", false, 0, 10}}},
{"a/b/c", []row{{"three.txt", "a/b/c/three.txt", false, 0, 16}}},
} {
got := list(tc.path)
if len(got) != len(tc.want) {
t.Errorf("FAIL: path %q: got %v, want %v", tc.path, got, tc.want)
continue
}
for i := range got {
if got[i] != tc.want[i] {
t.Errorf("FAIL: path %q row %d: got %v, want %v", tc.path, i, got[i], tc.want[i])
}
}
}
if !t.Failed() {
t.Log("ok")
}
}