Verify current catalog examples through native ConfigKit loading
This commit is contained in:
87
internal/validation/examples_test.go
Normal file
87
internal/validation/examples_test.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func checkCurrentExamples(root fs.FS, set *Set) error {
|
||||
for _, schema := range set.Current() {
|
||||
directory := path.Join(path.Dir(schema.Record.Path), "examples")
|
||||
entries, err := fs.ReadDir(root, directory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", directory, err)
|
||||
}
|
||||
count := 0
|
||||
for _, entry := range entries {
|
||||
ext := path.Ext(entry.Name())
|
||||
if entry.IsDir() || (ext != ".yml" && ext != ".yaml" && ext != ".json") {
|
||||
continue
|
||||
}
|
||||
count++
|
||||
file := path.Join(directory, entry.Name())
|
||||
data, err := fs.ReadFile(root, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ext == ".json" && !json.Valid(data) {
|
||||
return fmt.Errorf("%s: expected JSON syntax", file)
|
||||
}
|
||||
value, err := LoadDomain(file, data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", file, err)
|
||||
}
|
||||
if err := schema.Validate(value); err != nil {
|
||||
return fmt.Errorf("%s (%s): %w", file, schema.Record.Version, err)
|
||||
}
|
||||
}
|
||||
if count == 0 {
|
||||
return fmt.Errorf("%s: no document examples", directory)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestCurrentExamples(t *testing.T) {
|
||||
if err := checkCurrentExamples(os.DirFS("../.."), loadSet(t)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExampleDiscovery(t *testing.T) {
|
||||
record := Record{Name: "weather", Version: "0.1.0", Contract: "maximumdirect.weather.v1", Path: "weather/v0.1.0/schema.json", ID: schemaBase + "weather/v0.1.0/schema.json"}
|
||||
data, err := json.Marshal(Catalog{FormatVersion: 1, Schemas: []Record{record}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
schema, err := json.Marshal(map[string]any{"$schema": "https://json-schema.org/draft/2020-12/schema", "$id": record.ID, "type": "object", "required": []string{"schema_version"}, "properties": map[string]any{"schema_version": map[string]any{"const": record.Contract}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
example := "weather/v0.1.0/examples/weather.yml"
|
||||
files := fstest.MapFS{"catalog.json": {Data: data}, record.Path: {Data: schema}, example: {Data: []byte("schema_version: maximumdirect.weather.v1\nunit: Celsius")}}
|
||||
set, err := Load(files)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := checkCurrentExamples(files, set); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files[example] = &fstest.MapFile{Data: []byte("unit: Celsius")}
|
||||
if err := checkCurrentExamples(files, set); err == nil {
|
||||
t.Fatal("incompatible new-name example passed")
|
||||
}
|
||||
delete(files, example)
|
||||
if err := checkCurrentExamples(files, set); err == nil {
|
||||
t.Fatal("missing example coverage passed")
|
||||
}
|
||||
files[path.Dir(example)] = &fstest.MapFile{Mode: fs.ModeDir}
|
||||
if err := checkCurrentExamples(files, set); err == nil {
|
||||
t.Fatal("empty example directory passed")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user