package main import ( "encoding/json" "os" "path/filepath" "strings" "testing" ) // Each file's text is a template in templates/, the directory the manifest names (novox/hq ADR 0349 ยง2, issue 731), // never in files/: files/ is the archive the machine unpacks, and stays exactly what the machine is given. func TestEveryFileTakesItsTextFromATemplateOutsideTheUnpackedFiles(t *testing.T) { raw, err := os.ReadFile("../../module.json") if err != nil { t.Fatal(err) } var m struct { Templates string `json:"templates"` Resources []map[string]any `json:"resources"` Build struct { Artifacts []map[string]any `json:"artifacts"` } `json:"build"` } if err := json.Unmarshal(raw, &m); err != nil { t.Fatal(err) } if m.Templates != "templates" { t.Fatalf("the manifest names the templates directory %q, not templates", m.Templates) } files := 0 for _, r := range m.Resources { if _, inline := r["content"]; inline { t.Errorf("resource %v keeps its text inline: keep it in templates/ and name it as the template", r["id"]) } if r["type"] != "file" { continue } files++ name, _ := r["template"].(string) if !strings.HasPrefix(name, "templates/") { t.Errorf("resource %v names the template %q, not one in templates/", r["id"], name) continue } if st, err := os.Lstat(filepath.Join("../..", name)); err != nil || !st.Mode().IsRegular() { t.Errorf("resource %v names the template %s, which is not a plain file: %v", r["id"], name, err) } } if files == 0 { t.Fatal("no file resource") } packs := 0 for _, a := range m.Build.Artifacts { if a["kind"] == "archive" && a["from"] == "templates" { packs++ if a["name"] != "templates" { t.Errorf("the templates archive is named %v, not templates", a["name"]) } } } if packs != 1 { t.Fatalf("the build packs templates/ into %d archives, not one", packs) } // The templates archive is read by the controller, never unpacked onto the machine. for _, r := range m.Resources { if r["artifact"] == "templates" { t.Errorf("resource %v places the templates archive on the machine", r["id"]) } } // And files/ holds what the machine runs, no template. err = filepath.WalkDir("../../files", func(at string, d os.DirEntry, err error) error { if err != nil { return err } if !d.IsDir() && !strings.HasPrefix(filepath.ToSlash(at), "../../files/bin/") { t.Errorf("files/ holds %s, which is not one of the scripts the machine unpacks", at) } return nil }) if err != nil { t.Fatal(err) } }