Attribute glossary schema errors to their source fields
This commit is contained in:
@@ -131,6 +131,43 @@ func TestLoadErrorsAndEmptyInput(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaFailureUsesContributingSource(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
first := filepath.Join(dir, "first.yml")
|
||||
last := filepath.Join(dir, "last.yml")
|
||||
if err := os.WriteFile(last, []byte("schema_version: maximumdirect.glossary.v1\ncollections: {}\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct{ name, entry, path string }{
|
||||
{"invalid value", "{id: a, name: 17, category: person}", "/collections/7~1~0/entries/0/name"},
|
||||
{"missing field", "{id: a, category: person}", "/collections/7~1~0/entries/0"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
body := "schema_version: maximumdirect.glossary.v1\ncollections:\n '7/~':\n entries: [" + tc.entry + "]\n"
|
||||
if err := os.WriteFile(first, []byte(body), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := Load(context.Background(), []ck.Input{{Source: ck.File(first)}, {Source: ck.File(last)}})
|
||||
var loadErr *ck.LoadError
|
||||
if !errors.As(err, &loadErr) {
|
||||
t.Fatalf("expected native load error, got %v", err)
|
||||
}
|
||||
sources := make(map[string]string)
|
||||
for _, source := range loadErr.Sources() {
|
||||
sources[source.ID] = source.Path
|
||||
}
|
||||
diagnostics := loadErr.Diagnostics()
|
||||
if len(diagnostics) != 1 || diagnostics[0].Path.String() != tc.path {
|
||||
t.Fatalf("unexpected diagnostics: %+v", diagnostics)
|
||||
}
|
||||
location := diagnostics[0].Primary
|
||||
if location == nil || sources[location.SourceID] != first {
|
||||
t.Fatalf("diagnostic points to %v with sources %v", location, sources)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadResolvesInheritanceAndPreservesExtensions(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "shared.yml")
|
||||
body := `schema_version: maximumdirect.glossary.v1
|
||||
|
||||
Reference in New Issue
Block a user