Files
mesh-catalog/checks/sdkjudge/main_test.go
T

67 lines
3.2 KiB
Go

package sdkjudge
import (
"os"
"path/filepath"
"strings"
"testing"
)
// mesh-sdk carries the contract only (ADR 0578 §4). A row is a source tree of its own: a function gathering a manifest's
// problems, a manifest parser that judges and a table of what a manifest may declare are refused, named; a Problems
// function over something that is no manifest, a test file, a table outside the catalogue package and a function that
// merely decodes are not. Then the SDK beside this check is judged.
func TestMeshSDKCarriesNoManifestJudge(t *testing.T) {
for _, row := range []struct {
name, dir, file, src, refused string
}{
{name: "a function gathering a manifest's problems", dir: "catalogue", refused: "StreamProblems is a function that gathers a manifest's problems",
src: "package catalogue\ntype Manifest struct{}\nfunc StreamProblems(m Manifest) []string { return nil }\n"},
{name: "a method over a manifest", dir: "catalogue", refused: "healthProblems is a function that gathers",
src: "package catalogue\ntype Manifest struct{}\nfunc (m Manifest) healthProblems() []string { return nil }\n"},
{name: "a function over the shelf", dir: "catalogue", refused: "CatalogueProblems is a function that gathers",
src: "package catalogue\ntype Shelf map[string]int\nfunc CatalogueProblems(s Shelf) []string { return nil }\n"},
{name: "a parser that judges", dir: "catalogue", refused: "ParseManifestWith is a function that judges a manifest",
src: "package catalogue\nfunc ParseManifestWith(raw []byte) error { return nil }\n"},
{name: "a table of what may be removed", dir: "catalogue", refused: "removable is a table of what a manifest may declare",
src: "package catalogue\nvar removable = map[string]bool{\"A\": true, \"B\": true}\n"},
{name: "a key parser", dir: "delivery", src: "package delivery\nfunc ParseManifestsKey(k string) error { return nil }\n"},
{name: "a decoder", dir: "catalogue", src: "package catalogue\ntype Manifest struct{}\nfunc Decode(raw []byte) (Manifest, error) { return Manifest{}, nil }\n"},
{name: "problems of something else", dir: "asks", src: "package asks\ntype Condition struct{}\nfunc DecodeProblems(c Condition) []string { return nil }\n"},
{name: "a table beside the catalogue", dir: "asks", src: "package asks\nvar allowed = []string{\"a\", \"b\"}\n"},
{name: "a test file", dir: "catalogue", file: "x_test.go",
src: "package catalogue\ntype Manifest struct{}\nfunc StreamProblems(m Manifest) []string { return nil }\n"},
} {
root := t.TempDir()
dir := filepath.Join(root, row.dir)
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if row.file == "" {
row.file = "x.go"
}
if err := os.WriteFile(filepath.Join(dir, row.file), []byte(row.src), 0o644); err != nil {
t.Fatal(err)
}
got, err := Problems(root, nil)
if err != nil {
t.Fatal(err)
}
joined := strings.Join(got, "\n")
if (row.refused == "") != (len(got) == 0) || !strings.Contains(joined, row.refused) {
t.Errorf("%s: refused %q, want %q", row.name, joined, row.refused)
}
}
dir, err := BesideDir()
if err != nil {
t.Fatal(err)
}
got, err := Problems(dir, composition)
if err != nil {
t.Fatal(err)
}
if len(got) != 0 {
t.Fatalf("mesh-sdk holds a manifest judge:\n%s", strings.Join(got, "\n"))
}
}