Inspect wildcard directories before advancing to parents
This commit is contained in:
@@ -41,7 +41,20 @@ func selectGlobImportCandidates(access *filesystemAccess, basePath string, spec
|
||||
for _, parent := range candidates {
|
||||
if !patterned {
|
||||
childPath := access.join(parent.path, literal)
|
||||
childAccessPath := access.join(parent.accessPath, literal)
|
||||
parentAccessPath := parent.accessPath
|
||||
if literal == ".." {
|
||||
target, err := access.importEntryTarget(parent.accessPath, nil, true)
|
||||
if err != nil {
|
||||
return importSelection{}, []Diagnostic{globCandidateDiagnostic(err, site.location, "glob parent directory could not be inspected")}
|
||||
}
|
||||
if !target.dir {
|
||||
continue
|
||||
}
|
||||
if target.identity != "" {
|
||||
parentAccessPath = target.identity
|
||||
}
|
||||
}
|
||||
childAccessPath := access.join(parentAccessPath, literal)
|
||||
candidate, missing, diagnostics := explicitGlobCandidate(access, basePath, childPath, childAccessPath, site, last)
|
||||
if len(diagnostics) != 0 {
|
||||
return importSelection{}, diagnostics
|
||||
|
||||
@@ -189,8 +189,9 @@ func selectGlobForTestWithDiagnostics(t *testing.T, access *filesystemAccess, ba
|
||||
}
|
||||
|
||||
type observedGlobFilesystem struct {
|
||||
files fstest.MapFS
|
||||
opened []string
|
||||
files fstest.MapFS
|
||||
opened []string
|
||||
failStat string
|
||||
}
|
||||
|
||||
func (filesystem *observedGlobFilesystem) Open(name string) (fs.File, error) {
|
||||
@@ -199,5 +200,68 @@ func (filesystem *observedGlobFilesystem) Open(name string) (fs.File, error) {
|
||||
}
|
||||
|
||||
func (filesystem *observedGlobFilesystem) Stat(name string) (fs.FileInfo, error) {
|
||||
if name == filesystem.failStat {
|
||||
return nil, fs.ErrPermission
|
||||
}
|
||||
return fs.Stat(filesystem.files, name)
|
||||
}
|
||||
|
||||
func TestSelectGlobInspectsWildcardBeforeParent(t *testing.T) {
|
||||
t.Parallel()
|
||||
filesystem := &observedGlobFilesystem{files: fstest.MapFS{
|
||||
"services/unit/marker": &fstest.MapFile{},
|
||||
"services/shared/one.yml": &fstest.MapFile{Data: []byte("value: one\n")},
|
||||
"services/irrelevant": &fstest.MapFile{},
|
||||
}}
|
||||
access := newNativeAccess(filesystem, ".", &namespaceToken{})
|
||||
declaration := " - glob: services/*/../shared/*.yml\n required: true\n"
|
||||
selection, diagnostics := selectGlobForTestWithDiagnostics(t, access, ".", declaration)
|
||||
if len(diagnostics) != 0 || !equalStrings(selectedPaths(selection), []string{"services/shared/one.yml"}) {
|
||||
t.Fatalf("selection=%#v, diagnostics=%#v", selection, diagnostics)
|
||||
}
|
||||
filesystem.failStat = "services/unit"
|
||||
_, diagnostics = selectGlobForTestWithDiagnostics(t, access, ".", declaration+" exclude: '*.yml'\n")
|
||||
if len(diagnostics) == 0 || diagnostics[0].Code != CodeImportPermission {
|
||||
t.Fatalf("matched directory failure=%#v", diagnostics)
|
||||
}
|
||||
missing := selectGlobForTest(t, access, ".", " - glob: services/absent*/../shared/*.yml\n")
|
||||
if len(missing.candidates) != 0 {
|
||||
t.Fatalf("unmatched wildcard was cleaned away: %#v", missing)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectGlobParentUsesMatchedLinkTarget(t *testing.T) {
|
||||
root := canonicalTestPath(t, t.TempDir())
|
||||
writeImportFile(t, root, "targets/unit/marker", "marker")
|
||||
file := writeImportFile(t, root, "targets/shared/one.yml", "value: one\n")
|
||||
if err := os.MkdirAll(filepath.Join(root, "services"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink("../targets/unit", filepath.Join(root, "services", "alias")); err != nil {
|
||||
t.Skipf("symlinks unavailable: %v", err)
|
||||
}
|
||||
host, err := openHostAccess(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer host.close()
|
||||
native := newNativeAccess(os.DirFS(root), ".", &namespaceToken{})
|
||||
for _, mode := range []struct {
|
||||
name string
|
||||
access *filesystemAccess
|
||||
base, want string
|
||||
}{
|
||||
{"host", host, root, file}, {"native", native, ".", "targets/shared/one.yml"},
|
||||
} {
|
||||
t.Run(mode.name, func(t *testing.T) {
|
||||
selection, diagnostics := selectGlobForTestWithDiagnostics(t, mode.access, mode.base, " - glob: services/*/../shared/*.yml\n required: true\n")
|
||||
if len(diagnostics) != 0 || len(selection.candidates) != 1 || selection.candidates[0].accessPath != mode.want {
|
||||
t.Fatalf("selection=%#v, diagnostics=%#v", selection, diagnostics)
|
||||
}
|
||||
selection = selectGlobForTest(t, mode.access, mode.base, " - glob: services/*/../shared/*.yml\n follow: false\n")
|
||||
if len(selection.candidates) != 0 {
|
||||
t.Fatalf("unfollowed link selected=%#v", selection)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user