Verify versioned contracts and exact offline distribution selection
This commit is contained in:
@@ -2,10 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"forge.maximumdirect.net/eric/schemas/internal/validation"
|
||||
)
|
||||
|
||||
func TestCommand(t *testing.T) {
|
||||
@@ -39,6 +42,70 @@ func TestCommand(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExactDefinitions(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
data, err := os.ReadFile("../../catalog.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var catalog validation.Catalog
|
||||
if err := json.Unmarshal(data, &catalog); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
write := func(path string, body []byte) {
|
||||
t.Helper()
|
||||
target := filepath.Join(root, path)
|
||||
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(target, body, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var glossary validation.Record
|
||||
for _, record := range catalog.Schemas {
|
||||
data, err := os.ReadFile("../../" + record.Path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
write(record.Path, data)
|
||||
if record.Name == "glossary" {
|
||||
glossary = record
|
||||
}
|
||||
}
|
||||
data, err = os.ReadFile("../../" + glossary.Path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var schema map[string]any
|
||||
if err := json.Unmarshal(data, &schema); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
glossary.Version = "0.2.0"
|
||||
glossary.Path = "glossary/v0.2.0/schema.json"
|
||||
glossary.ID = "https://schemas.maximumdirect.net/" + glossary.Path
|
||||
schema["$id"] = glossary.ID
|
||||
schema["required"] = []string{"schema_version", "collections", "synthetic_test_field"}
|
||||
data, err = json.Marshal(schema)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
write(glossary.Path, data)
|
||||
catalog.Schemas = append(catalog.Schemas, glossary)
|
||||
data, err = json.Marshal(catalog)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
write("catalog.json", data)
|
||||
write("document.json", []byte(`{"schema_version":"maximumdirect.glossary.v1","collections":{}}`))
|
||||
for selection, want := range map[string]int{"glossary@0.1.0": 0, "glossary@0.2.0": 1} {
|
||||
var stdout, stderr bytes.Buffer
|
||||
if got := run([]string{"-root", root, "-schema", selection, filepath.Join(root, "document.json")}, &stdout, &stderr); got != want {
|
||||
t.Fatalf("%s exit %d, want %d: %s", selection, got, want, &stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONSyntax(t *testing.T) {
|
||||
file := filepath.Join(t.TempDir(), "document.json")
|
||||
if err := os.WriteFile(file, []byte("schema_version: maximumdirect.rpg.campaign.v1\nid: sample"), 0600); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user