98 lines
2.8 KiB
Go
98 lines
2.8 KiB
Go
package publication
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"forge.maximumdirect.net/eric/schemas/internal/validation"
|
|
)
|
|
|
|
func TestPublishedDirectories(t *testing.T) {
|
|
repo := t.TempDir()
|
|
run := func(args ...string) {
|
|
t.Helper()
|
|
if _, err := git(repo, args...); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
run("init", "-b", "main")
|
|
run("config", "user.name", "Schema Test")
|
|
run("config", "user.email", "schemas@example.invalid")
|
|
write := func(file, body string) {
|
|
t.Helper()
|
|
target := filepath.Join(repo, file)
|
|
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(target, []byte(body), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
commit := func() { t.Helper(); run("add", "."); run("commit", "-m", "Synthetic fixture") }
|
|
record := validation.Record{Name: "glossary", Version: "0.1.0", Contract: "maximumdirect.glossary.v1", ID: "https://schemas.maximumdirect.net/glossary/v0.1.0/schema.json", Path: "glossary/v0.1.0/schema.json"}
|
|
catalog := validation.Catalog{FormatVersion: 1, Schemas: []validation.Record{record}}
|
|
writeCatalog := func() {
|
|
t.Helper()
|
|
data, err := json.Marshal(catalog)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
write("catalog.json", string(data))
|
|
}
|
|
writeCatalog()
|
|
write(record.Path, "{}")
|
|
write("glossary/v0.1.0/semantics.md", "Original semantics")
|
|
write("glossary/v0.1.0/examples/sample.yml", "example: original")
|
|
write("VERSION", "2026.10.0")
|
|
commit()
|
|
run("tag", "2026.10.0")
|
|
if err := CheckRemote(repo, repo); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, file := range []string{record.Path, "glossary/v0.1.0/semantics.md", "glossary/v0.1.0/examples/sample.yml"} {
|
|
t.Run(file, func(t *testing.T) {
|
|
original, err := os.ReadFile(filepath.Join(repo, file))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
write(file, "Changed")
|
|
commit()
|
|
if err := CheckTag(repo, "2026.10.0"); err == nil {
|
|
t.Fatal("published modification accepted")
|
|
}
|
|
if err := os.Remove(filepath.Join(repo, file)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
commit()
|
|
if err := CheckTag(repo, "2026.10.0"); err == nil {
|
|
t.Fatal("published removal accepted")
|
|
}
|
|
write(file, string(original))
|
|
commit()
|
|
})
|
|
}
|
|
catalog.Schemas = nil
|
|
writeCatalog()
|
|
commit()
|
|
if err := CheckTag(repo, "2026.10.0"); err == nil {
|
|
t.Fatal("removed catalog record accepted")
|
|
}
|
|
catalog.Schemas = []validation.Record{record}
|
|
writeCatalog()
|
|
commit()
|
|
next := record
|
|
next.Version = "0.2.0"
|
|
next.Path = "glossary/v0.2.0/schema.json"
|
|
next.ID = "https://schemas.maximumdirect.net/" + next.Path
|
|
catalog.Schemas = append(catalog.Schemas, next)
|
|
writeCatalog()
|
|
write(next.Path, "{}")
|
|
write("VERSION", "2026.11.0")
|
|
commit()
|
|
if err := CheckRemote(repo, repo); err != nil {
|
|
t.Fatalf("new schema/distribution changed no published bytes: %v", err)
|
|
}
|
|
}
|