package engine import ( "io/fs" "os" "path/filepath" "testing" "testing/fstest" ) func TestSelectShallowDirectoriesUnionsRootsAndPatterns(t *testing.T) { t.Parallel() filesystem := fstest.MapFS{ "one": &fstest.MapFile{Mode: fs.ModeDir | 0o700}, "one/a.yaml": &fstest.MapFile{Data: []byte("a: 1\n")}, "one/README": &fstest.MapFile{Data: []byte("readme\n")}, "one/.hidden": &fstest.MapFile{Data: []byte("hidden\n")}, "one/draft.draft.yaml": &fstest.MapFile{Data: []byte("draft\n")}, "one/nested/deep.yaml": &fstest.MapFile{Data: []byte("deep: 1\n")}, "two": &fstest.MapFile{Mode: fs.ModeDir | 0o700}, "two/a.yaml": &fstest.MapFile{Data: []byte("a: 2\n")}, "three": &fstest.MapFile{Mode: fs.ModeDir | 0o700}, "three/extensionless": &fstest.MapFile{Data: []byte("plain\n")}, } access := newNativeAccess(filesystem, ".", &namespaceToken{}) specs, diagnostics := parseImportSpecsYAML(t, ` - dir: [one, one, missing, two] pattern: ['*.yaml', 'a.*', 'README'] exclude: '*.draft.yaml' required: true `) if len(diagnostics) != 0 { t.Fatal(diagnostics) } policy, diagnostics := resolveImportPolicy(specs[0], importDefaults{}) if len(diagnostics) != 0 { t.Fatal(diagnostics) } accounting := &importAccounting{} selection, diagnostics := selectShallowDirectoryImportCandidates(access, ".", specs[0], policy, defaultImportLimits(), accounting) if len(diagnostics) != 0 { t.Fatalf("selection diagnostics = %#v", diagnostics) } if selection.outcome != importSelectionHasCandidates || len(selection.candidates) != 3 { t.Fatalf("selection = %#v, want three unioned matches", selection) } if selection.candidates[0].path != "one/README" || selection.candidates[1].path != "one/a.yaml" || selection.candidates[2].path != "two/a.yaml" { t.Fatalf("selected paths = %#v", selection.candidates) } if accounting.entries != 6 { t.Fatalf("entry visits = %d, want 6 entries across distinct roots", accounting.entries) } } func TestSelectShallowDirectoriesIncludeUnrestrictedHiddenAndExtensionlessFiles(t *testing.T) { t.Parallel() root := t.TempDir() for name, value := range map[string]string{ "a.yaml": "a: 1\n", ".private": "private\n", "README": "readme\n", "nested/deep": "deep\n", } { 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), 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") if len(diagnostics) != 0 { t.Fatal(diagnostics) } policy, diagnostics := resolveImportPolicy(specs[0], importDefaults{}) if len(diagnostics) != 0 { t.Fatal(diagnostics) } selection, diagnostics := selectShallowDirectoryImportCandidates(access, root, specs[0], policy, defaultImportLimits(), &importAccounting{}) if len(diagnostics) != 0 { t.Fatalf("selection diagnostics = %#v", diagnostics) } if len(selection.candidates) != 3 { t.Fatalf("selected files = %#v, want yaml, hidden, and extensionless files", selection.candidates) } for _, candidate := range selection.candidates { if filepath.Base(candidate.path) == "deep" { t.Fatalf("shallow search descended into a child directory: %#v", selection.candidates) } } } func TestSelectShallowDirectoriesApplyRequirednessToFinalUnion(t *testing.T) { t.Parallel() filesystem := fstest.MapFS{ "available": &fstest.MapFile{Mode: fs.ModeDir | 0o700}, "available/match.yaml": &fstest.MapFile{Data: []byte("match: true\n")}, "available/excluded.yaml": &fstest.MapFile{Data: []byte("excluded: true\n")}, } access := newNativeAccess(filesystem, ".", &namespaceToken{}) for _, test := range []struct { name string yaml string wantError bool wantCount int }{ { name: "missing root alongside a match", yaml: " - dir: [missing, available]\n pattern: '*.yaml'\n exclude: excluded.yaml\n required: true\n", wantCount: 1, }, { name: "all matches excluded", yaml: " - dir: available\n pattern: '*.yaml'\n exclude: '*.yaml'\n required: true\n", wantError: true, }, { name: "explicit empty patterns select nothing", yaml: " - dir: available\n pattern: []\n", wantCount: 0, }, { name: "empty search roots", yaml: " - dir: []\n required: true\n", wantError: true, }, } { 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], importDefaults{}) if len(diagnostics) != 0 { t.Fatal(diagnostics) } selection, diagnostics := selectShallowDirectoryImportCandidates(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) } return } if len(diagnostics) != 0 || len(selection.candidates) != test.wantCount { t.Fatalf("selection, diagnostics = %#v, %#v", selection, diagnostics) } if test.wantCount == 1 && selection.candidates[0].path != "available/match.yaml" { t.Fatalf("selected path = %q, want available/match.yaml", selection.candidates[0].path) } }) } } func TestSelectShallowDirectoryRejectsInvalidRootInsteadOfTreatingItAsMissing(t *testing.T) { t.Parallel() filesystem := fstest.MapFS{ "file.yaml": &fstest.MapFile{Data: []byte("not a directory\n")}, } access := newNativeAccess(filesystem, ".", &namespaceToken{}) specs, diagnostics := parseImportSpecsYAML(t, " - dir: file.yaml\n") if len(diagnostics) != 0 { t.Fatal(diagnostics) } policy, diagnostics := resolveImportPolicy(specs[0], importDefaults{}) if len(diagnostics) != 0 { t.Fatal(diagnostics) } _, diagnostics = selectShallowDirectoryImportCandidates(access, ".", specs[0], policy, defaultImportLimits(), &importAccounting{}) if len(diagnostics) != 1 || diagnostics[0].Code != CodeImportInvalid { t.Fatalf("invalid-root diagnostics = %#v", diagnostics) } denied := &statOnlyImportFilesystem{statErrors: map[string]error{"denied": fs.ErrPermission}} access = newNativeAccess(denied, ".", &namespaceToken{}) specs, diagnostics = parseImportSpecsYAML(t, " - dir: denied\n") if len(diagnostics) != 0 { t.Fatal(diagnostics) } policy, diagnostics = resolveImportPolicy(specs[0], importDefaults{}) if len(diagnostics) != 0 { t.Fatal(diagnostics) } _, diagnostics = selectShallowDirectoryImportCandidates(access, ".", specs[0], policy, defaultImportLimits(), &importAccounting{}) if len(diagnostics) != 1 || diagnostics[0].Code != CodeImportInvalid { t.Fatalf("inaccessible-root diagnostics = %#v", diagnostics) } } func TestSelectShallowDirectoryFiltersSymlinkAliasSpelling(t *testing.T) { t.Parallel() root := t.TempDir() if err := os.WriteFile(filepath.Join(root, "welcome.yaml"), []byte("welcome: true\n"), 0o600); err != nil { t.Fatal(err) } if err := os.Symlink("welcome.yaml", filepath.Join(root, "welcome.draft.yaml")); err != nil { t.Skipf("symlinks are unavailable: %v", err) } access, err := openHostAccess(root) if err != nil { t.Fatal(err) } defer access.close() specs, diagnostics := parseImportSpecsYAML(t, " - dir: .\n pattern: '*.yaml'\n exclude: '*.draft.yaml'\n") if len(diagnostics) != 0 { t.Fatal(diagnostics) } policy, diagnostics := resolveImportPolicy(specs[0], importDefaults{}) if len(diagnostics) != 0 { t.Fatal(diagnostics) } selection, diagnostics := selectShallowDirectoryImportCandidates(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) } }