61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package architecture
|
|
|
|
import (
|
|
"gopkg.in/yaml.v3"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPublicContractReferencesAreSelfContained(t *testing.T) {
|
|
root := filepath.Join("..", "..", "contracts", "sendico")
|
|
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.IsDir() || filepath.Ext(path) != ".yaml" {
|
|
return nil
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var doc yaml.Node
|
|
if err := yaml.Unmarshal(data, &doc); err != nil {
|
|
return err
|
|
}
|
|
var visit func(*yaml.Node)
|
|
visit = func(n *yaml.Node) {
|
|
if n.Kind == yaml.MappingNode {
|
|
for i := 0; i+1 < len(n.Content); i += 2 {
|
|
if n.Content[i].Value == "$ref" {
|
|
ref := strings.SplitN(n.Content[i+1].Value, "#", 2)[0]
|
|
if ref == "" {
|
|
continue
|
|
}
|
|
target := filepath.Clean(filepath.Join(filepath.Dir(path), ref))
|
|
relative, err := filepath.Rel(root, target)
|
|
if err != nil || strings.HasPrefix(relative, "..") || strings.Contains(ref, "://") {
|
|
t.Errorf("%s: nonlocal reference %s", path, ref)
|
|
continue
|
|
}
|
|
if _, err := os.Stat(target); err != nil {
|
|
t.Errorf("%s: missing reference %s", path, ref)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for _, child := range n.Content {
|
|
visit(child)
|
|
}
|
|
}
|
|
visit(&doc)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|