package engine import ( "path/filepath" "strconv" "testing" "testing/fstest" ) func TestAbsoluteGlobLiteralStartingLocations(t *testing.T) { t.Parallel() hostRoot := canonicalTestPath(t, t.TempDir()) file := writeImportFile(t, hostRoot, "workspace/allowed/fragments/one.yml", "value: one\n") boundary := filepath.Join(hostRoot, "workspace", "allowed") host, err := openHostAccess(boundary) if err != nil { t.Fatal(err) } defer host.close() native := newNativeAccess(fstest.MapFS{"workspace/allowed/fragments/one.yml": &fstest.MapFile{Data: []byte("value: one\n")}}, "workspace/allowed", &namespaceToken{}) for _, mode := range []struct { name string access *filesystemAccess base, root, want string }{ {"host", host, boundary, filepath.ToSlash(boundary), filepath.ToSlash(file)}, {"native", native, "workspace/allowed", "/workspace/allowed", "workspace/allowed/fragments/one.yml"}, } { t.Run(mode.name, func(t *testing.T) { for _, pattern := range []string{mode.root + "/fragments/*.yml", mode.root + "/fragments/one.yml"} { selection, diagnostics := selectGlobForTestWithDiagnostics(t, mode.access, mode.base, " - glob: "+strconv.Quote(pattern)+"\n required: true\n") if len(diagnostics) != 0 || len(selection.candidates) != 1 || selection.candidates[0].path != mode.want { t.Fatalf("pattern %q: selection=%#v, diagnostics=%#v", pattern, selection, diagnostics) } } for _, entry := range []struct { pattern string required bool code Code }{ {mode.root + "/missing/*.yml", false, ""}, {mode.root + "/missing/*.yml", true, CodeImportMissing}, {mode.root + "/missing/../fragments/*.yml", true, CodeImportMissing}, {mode.root + "/../../*/*.yml", true, CodeImportOutsideRoot}, } { declaration := " - glob: " + strconv.Quote(entry.pattern) + "\n required: " + strconv.FormatBool(entry.required) + "\n" selection, diagnostics := selectGlobForTestWithDiagnostics(t, mode.access, mode.base, declaration) if entry.code == "" { if len(diagnostics) != 0 || len(selection.candidates) != 0 { t.Fatalf("optional missing=%#v, %#v", selection, diagnostics) } continue } if len(diagnostics) == 0 || diagnostics[0].Code != entry.code { t.Fatalf("pattern %q diagnostics=%#v", entry.pattern, diagnostics) } } }) } }