Merge pull request 'A converted definition resolves to the paths it named before' (#170) from feat/definitions-place-their-directories into main
This commit was merged in pull request #170.
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package catalogue
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestPlacedDirectoriesKeepTheirPaths is the check novox/hq issue 119 asks for before a definition
|
||||
// stops naming where its data lives: a converted manifest, resolved on a node with the default root,
|
||||
// names exactly the paths the manifest before it named. Data that a service is using must not move
|
||||
// because a definition stopped saying where it was.
|
||||
//
|
||||
// Two checkouts: MESH_CATALOGUE_BEFORE, the catalogue as it was, and MESH_CATALOGUE, as it is now.
|
||||
// Every module in both is resolved with the controller's own rule (dirsFor, dirFill) and compared
|
||||
// whole — not only the directories, but every string a directory's id was written into.
|
||||
func TestPlacedDirectoriesKeepTheirPaths(t *testing.T) {
|
||||
before, after := os.Getenv("MESH_CATALOGUE_BEFORE"), os.Getenv("MESH_CATALOGUE")
|
||||
if before == "" || after == "" {
|
||||
t.Skip("set MESH_CATALOGUE_BEFORE and MESH_CATALOGUE to two catalogue checkouts to run this")
|
||||
}
|
||||
found, _ := filepath.Glob(filepath.Join(after, "modules", "*", "module.json"))
|
||||
compared := 0
|
||||
for _, path := range found {
|
||||
module := filepath.Base(filepath.Dir(path))
|
||||
old, err := os.ReadFile(filepath.Join(before, "modules", module, "module.json"))
|
||||
if err != nil {
|
||||
continue // new since; nothing to keep
|
||||
}
|
||||
now, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(old) == string(now) {
|
||||
continue
|
||||
}
|
||||
m, err := ParseManifest(now)
|
||||
if err != nil {
|
||||
t.Errorf("%s: %v", module, err)
|
||||
continue
|
||||
}
|
||||
dirs := dirsFor(m, Rendering{})
|
||||
var was, is any
|
||||
if err := json.Unmarshal(old, &was); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(now, &is); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resolved := resolvedTree(is, dirs, module, t)
|
||||
if !reflect.DeepEqual(was, resolved) {
|
||||
wasJSON, _ := json.MarshalIndent(was, "", " ")
|
||||
isJSON, _ := json.MarshalIndent(resolved, "", " ")
|
||||
t.Errorf("%s: resolved on the default root, the converted manifest is not the one before it\n--- before\n%s\n--- resolved now\n%s",
|
||||
module, firstDifference(string(wasJSON), string(isJSON)), "")
|
||||
}
|
||||
compared++
|
||||
}
|
||||
t.Logf("%d converted manifest(s) resolve to the paths they named before", compared)
|
||||
}
|
||||
|
||||
// resolvedTree is the manifest as a machine would see it: every ${dir:…} filled, a pathless
|
||||
// directory given the path it resolves to, and the placement word removed.
|
||||
func resolvedTree(node any, dirs map[string]string, module string, t *testing.T) any {
|
||||
switch v := node.(type) {
|
||||
case map[string]any:
|
||||
out := map[string]any{}
|
||||
for k, child := range v {
|
||||
if k == "place" {
|
||||
continue
|
||||
}
|
||||
out[k] = resolvedTree(child, dirs, module, t)
|
||||
}
|
||||
if out["type"] == "directory" {
|
||||
if _, has := out["path"]; !has {
|
||||
if id, ok := out["id"].(string); ok {
|
||||
out["path"] = dirs[id]
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
case []any:
|
||||
out := make([]any, len(v))
|
||||
for i, child := range v {
|
||||
out[i] = resolvedTree(child, dirs, module, t)
|
||||
}
|
||||
return out
|
||||
case string:
|
||||
filled, err := dirFill(v, dirs, module)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
return filled
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
func firstDifference(a, b string) string {
|
||||
al, bl := strings.Split(a, "\n"), strings.Split(b, "\n")
|
||||
for i := range al {
|
||||
if i >= len(bl) || al[i] != bl[i] {
|
||||
from := i - 2
|
||||
if from < 0 {
|
||||
from = 0
|
||||
}
|
||||
to := i + 3
|
||||
if to > len(al) {
|
||||
to = len(al)
|
||||
}
|
||||
bt := i + 3
|
||||
if bt > len(bl) {
|
||||
bt = len(bl)
|
||||
}
|
||||
return "before:\n" + strings.Join(al[from:to], "\n") + "\nnow:\n" + strings.Join(bl[from:bt], "\n")
|
||||
}
|
||||
}
|
||||
return "(the difference is beyond the shorter document)"
|
||||
}
|
||||
Reference in New Issue
Block a user