package resolution import ( "encoding/json" "strings" "testing" mcat "git.novox.be/novox/mesh-sdk/go/catalogue" ) const inlineTwin = `{"module":"tidy","version":"1", "build":{"artifacts":[{"name":"files","kind":"archive","from":"files"}]}, "resources":[ {"id":"conf","type":"directory","path":"${dir:conf}"}, {"id":"main","type":"file","path":"${dir:conf}/tidy.conf","mode":"0644","content":"` + mainText + `"}, {"id":"extra","type":"file","path":"${dir:conf}/extra.conf","content":"` + extraText + `"}]}` const templatedTwin = `{"module":"tidy","version":"1", "build":{"artifacts":[{"name":"files","kind":"archive","from":"files"}]}, "resources":[ {"id":"conf","type":"directory","path":"${dir:conf}"}, {"id":"main","type":"file","path":"${dir:conf}/tidy.conf","mode":"0644","template":"files/tidy.conf"}, {"id":"extra","type":"file","path":"${dir:conf}/extra.conf","template":"files/deeper/extra.conf"}]}` const mainText = `# tidy\nroot = ${dir:conf}\nquoted = \"yes\" é\n` const extraText = `one\ntwo\n` func builtFiles(texts map[string]string) []mcat.Built { return []mcat.Built{{Name: "files", Kind: mcat.ArtifactArchive, Reference: mcat.ArtifactStoreScheme + "tidy/files/blobs/sha256:" + strings.Repeat("c", 64), Digest: "sha256:" + strings.Repeat("c", 64), Texts: texts}} } // held is a manifest as registration holds it: resolved with what was built, then read again strictly. func held(t *testing.T, raw string, built []mcat.Built) mcat.Manifest { t.Helper() m, err := decodeForTest([]byte(raw)) if err != nil { t.Fatal(err) } resolved, err := m.Resolve(built) if err != nil { t.Fatal(err) } again, err := json.Marshal(resolved) if err != nil { t.Fatal(err) } out, err := decodeForTest(again) if err != nil { t.Fatal(err) } if problem := HeldTemplateProblem(out); problem != "" { t.Fatal(problem) } return out } // What registration refuses: a manifest to be held that still names a template — one resolved by a builder that // predates templates would otherwise compose files with no text. func TestAManifestStillNamingATemplateIsNotHeld(t *testing.T) { m, err := decodeForTest([]byte(templatedTwin)) if err != nil { t.Fatal(err) } if problem := HeldTemplateProblem(m); !strings.Contains(problem, "main") || !strings.Contains(problem, "files/tidy.conf") { t.Fatalf("a manifest naming a template would be held: %q", problem) } if problem := HeldTemplateProblem(held(t, inlineTwin, builtFiles(nil))); problem != "" { t.Fatalf("an inline manifest is refused: %s", problem) } }