Add recursive directory search with depth limits

This commit is contained in:
2026-10-19 20:18:39 +00:00
parent 032679bf34
commit 77d97296ea
4 changed files with 285 additions and 44 deletions

View File

@@ -4,6 +4,7 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"testing/fstest"
)
@@ -38,7 +39,7 @@ func TestSelectShallowDirectoriesUnionsRootsAndPatterns(t *testing.T) {
t.Fatal(diagnostics)
}
accounting := &importAccounting{}
selection, diagnostics := selectShallowDirectoryImportCandidates(access, ".", specs[0], policy, defaultImportLimits(), accounting)
selection, diagnostics := selectDirectoryImportCandidates(access, ".", specs[0], policy, defaultImportLimits(), accounting)
if len(diagnostics) != 0 {
t.Fatalf("selection diagnostics = %#v", diagnostics)
}
@@ -85,7 +86,7 @@ func TestSelectShallowDirectoriesIncludeUnrestrictedHiddenAndExtensionlessFiles(
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
selection, diagnostics := selectShallowDirectoryImportCandidates(access, root, specs[0], policy, defaultImportLimits(), &importAccounting{})
selection, diagnostics := selectDirectoryImportCandidates(access, root, specs[0], policy, defaultImportLimits(), &importAccounting{})
if len(diagnostics) != 0 {
t.Fatalf("selection diagnostics = %#v", diagnostics)
}
@@ -144,7 +145,7 @@ func TestSelectShallowDirectoriesApplyRequirednessToFinalUnion(t *testing.T) {
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
selection, diagnostics := selectShallowDirectoryImportCandidates(access, ".", specs[0], policy, defaultImportLimits(), &importAccounting{})
selection, diagnostics := selectDirectoryImportCandidates(access, ".", specs[0], policy, defaultImportLimits(), &importAccounting{})
if test.wantError {
if len(diagnostics) != 1 || diagnostics[0].Code != CodeImportMissing {
t.Fatalf("diagnostics = %#v, want required empty-union error", diagnostics)
@@ -176,7 +177,7 @@ func TestSelectShallowDirectoryRejectsInvalidRootInsteadOfTreatingItAsMissing(t
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
_, diagnostics = selectShallowDirectoryImportCandidates(access, ".", specs[0], policy, defaultImportLimits(), &importAccounting{})
_, diagnostics = selectDirectoryImportCandidates(access, ".", specs[0], policy, defaultImportLimits(), &importAccounting{})
if len(diagnostics) != 1 || diagnostics[0].Code != CodeImportInvalid {
t.Fatalf("invalid-root diagnostics = %#v", diagnostics)
}
@@ -191,7 +192,7 @@ func TestSelectShallowDirectoryRejectsInvalidRootInsteadOfTreatingItAsMissing(t
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
_, diagnostics = selectShallowDirectoryImportCandidates(access, ".", specs[0], policy, defaultImportLimits(), &importAccounting{})
_, diagnostics = selectDirectoryImportCandidates(access, ".", specs[0], policy, defaultImportLimits(), &importAccounting{})
if len(diagnostics) != 1 || diagnostics[0].Code != CodeImportInvalid {
t.Fatalf("inaccessible-root diagnostics = %#v", diagnostics)
}
@@ -221,8 +222,150 @@ func TestSelectShallowDirectoryFiltersSymlinkAliasSpelling(t *testing.T) {
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
selection, diagnostics := selectShallowDirectoryImportCandidates(access, root, specs[0], policy, defaultImportLimits(), &importAccounting{})
selection, diagnostics := selectDirectoryImportCandidates(access, root, specs[0], policy, defaultImportLimits(), &importAccounting{})
if len(diagnostics) != 0 || len(selection.candidates) != 1 || filepath.Base(selection.candidates[0].path) != "welcome.yaml" {
t.Fatalf("alias filtering selection, diagnostics = %#v, %#v", selection, diagnostics)
}
}
func TestSelectDirectoriesApplyRecursiveDepthToFileLevels(t *testing.T) {
t.Parallel()
root := t.TempDir()
for name := range map[string]struct{}{
"direct.yaml": {},
"child/child.yaml": {},
"child/grand/grand.yaml": {},
} {
filename := filepath.Join(root, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(filename), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filename, []byte("value: true\n"), 0o600); err != nil {
t.Fatal(err)
}
}
access, err := openHostAccess(root)
if err != nil {
t.Fatal(err)
}
defer access.close()
for _, test := range []struct {
name string
yaml string
defaults importDefaults
wantPaths []string
}{
{
name: "depth one includes direct files",
yaml: " - dir: .\n recursive: true\n depth: 1\n pattern: '*.yaml'\n",
wantPaths: []string{"direct.yaml"},
},
{
name: "depth two includes one descendant level",
yaml: " - dir: .\n recursive: true\n depth: 2\n pattern: '*.yaml'\n",
wantPaths: []string{"child.yaml", "direct.yaml"},
},
{
name: "zero depth is unlimited",
yaml: " - dir: .\n recursive: true\n depth: 0\n pattern: '*.yaml'\n",
wantPaths: []string{"child.yaml", "grand.yaml", "direct.yaml"},
},
{
name: "recursion stays disabled by default",
yaml: " - dir: .\n pattern: '*.yaml'\n",
wantPaths: []string{"direct.yaml"},
},
{
name: "explicit false overrides application recursion",
yaml: " - dir: .\n recursive: false\n pattern: '*.yaml'\n",
defaults: importDefaults{recursive: boolPointer(true)},
wantPaths: []string{"direct.yaml"},
},
{
name: "application default enables recursion for depth",
yaml: " - dir: .\n depth: 2\n pattern: '*.yaml'\n",
defaults: importDefaults{recursive: boolPointer(true)},
wantPaths: []string{"child.yaml", "direct.yaml"},
},
{
name: "large depth remains a valid unlimited traversal bound",
yaml: " - dir: .\n recursive: true\n depth: 999999999999999999999999999999\n pattern: '*.yaml'\n",
wantPaths: []string{"child.yaml", "grand.yaml", "direct.yaml"},
},
{
name: "filename filter does not prune directories",
yaml: " - dir: .\n recursive: true\n depth: 3\n pattern: grand.yaml\n",
wantPaths: []string{"grand.yaml"},
},
} {
t.Run(test.name, func(t *testing.T) {
specs, diagnostics := parseImportSpecsYAML(t, test.yaml)
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
policy, diagnostics := resolveImportPolicy(specs[0], test.defaults)
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
selection, diagnostics := selectDirectoryImportCandidates(access, root, specs[0], policy, defaultImportLimits(), &importAccounting{})
if len(diagnostics) != 0 {
t.Fatalf("selection diagnostics = %#v", diagnostics)
}
var got []string
for _, candidate := range selection.candidates {
got = append(got, filepath.Base(candidate.path))
}
if strings.Join(got, ",") != strings.Join(test.wantPaths, ",") {
t.Fatalf("selected basenames = %#v, want %#v", got, test.wantPaths)
}
})
}
}
func TestSelectDirectoriesRetainsEmptySearchAndDirectoryVisitLimits(t *testing.T) {
t.Parallel()
root := t.TempDir()
for _, name := range []string{"direct.yaml", "child/child.yaml", "child/grand/grand.yaml"} {
filename := filepath.Join(root, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(filename), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filename, []byte("value: true\n"), 0o600); err != nil {
t.Fatal(err)
}
}
access, err := openHostAccess(root)
if err != nil {
t.Fatal(err)
}
defer access.close()
specs, diagnostics := parseImportSpecsYAML(t, " - dir: .\n recursive: true\n pattern: []\n")
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
policy, diagnostics := resolveImportPolicy(specs[0], importDefaults{})
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
accounting := &importAccounting{}
selection, diagnostics := selectDirectoryImportCandidates(access, root, specs[0], policy, defaultImportLimits(), accounting)
if len(diagnostics) != 0 || selection.outcome != importSelectionNone || len(selection.candidates) != 0 {
t.Fatalf("empty recursive selection, diagnostics = %#v, %#v", selection, diagnostics)
}
if accounting.entries != 5 {
t.Fatalf("empty search visited %d entries, want all 5 within the recursive search", accounting.entries)
}
limited := defaultImportLimits()
limited.entries = 2
_, diagnostics = selectDirectoryImportCandidates(access, root, specs[0], policy, limited, &importAccounting{})
if len(diagnostics) != 1 || diagnostics[0].Code != CodeImportLimitExceeded {
t.Fatalf("directory limit diagnostics = %#v", diagnostics)
}
}
func boolPointer(value bool) *bool { return &value }