Refactor: migrate POSIX scaffolding to CBOR and JSONC
This commit is contained in:
@@ -7,6 +7,21 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/fxamacker/cbor/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
testSettingsFileName = "settings.cbor"
|
||||
testLayoutFileName = "layout.cbor"
|
||||
testHomeFileName = "home.cbor"
|
||||
testUsersFileName = "users.cbor"
|
||||
testACLFileName = "acl.cbor"
|
||||
testFolderFileName = "folder.cbor"
|
||||
testItemFileName = "item.cbor"
|
||||
testDataFileName = "data.cbor"
|
||||
testSchemaFileName = "schema.json"
|
||||
testManifestFileName = "manifest.jsonc"
|
||||
)
|
||||
|
||||
func TestScanRootBuildsProjectedNodesFromBootstrapShape(t *testing.T) {
|
||||
@@ -14,6 +29,8 @@ func TestScanRootBuildsProjectedNodesFromBootstrapShape(t *testing.T) {
|
||||
|
||||
mustMkdirAll(t, filepath.Join(root, "catalog", "packs"))
|
||||
mustMkdirAll(t, filepath.Join(root, "catalog", "standalone"))
|
||||
mustMkdirAll(t, filepath.Join(root, "catalog", "packs", "pack-core", "entries", "app-shell"))
|
||||
mustMkdirAll(t, filepath.Join(root, "catalog", "standalone", "app-shell"))
|
||||
mustMkdirAll(t, filepath.Join(root, "departments", "department-primary-department", "teams", "team-primary-team"))
|
||||
mustMkdirAll(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", "children", "project-web", "children"))
|
||||
mustMkdirAll(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", "children", "project-web", "tree"))
|
||||
@@ -22,7 +39,7 @@ func TestScanRootBuildsProjectedNodesFromBootstrapShape(t *testing.T) {
|
||||
mustMkdirAll(t, filepath.Join(root, "projects", "project-primary-project", "tree", "folder-docs", "item-roadmap"))
|
||||
mustMkdirAll(t, filepath.Join(root, "users", "personals"))
|
||||
|
||||
mustWriteJSON(t, filepath.Join(root, "settings.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, testSettingsFileName), map[string]any{
|
||||
"installation": map[string]any{
|
||||
"id": "installation-1",
|
||||
"name": "MangoPig",
|
||||
@@ -34,81 +51,103 @@ func TestScanRootBuildsProjectedNodesFromBootstrapShape(t *testing.T) {
|
||||
"slug": "primary-organization",
|
||||
},
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "layout.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, testLayoutFileName), map[string]any{
|
||||
"type": "tenant-layout",
|
||||
"home": map[string]any{"defaultProjectSlug": "primary-project"},
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "departments", "department-primary-department", "settings.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "catalog", "packs", "pack-core", testManifestFileName), map[string]any{
|
||||
"id": "pack-core",
|
||||
"slug": "core",
|
||||
"type": "pack",
|
||||
"name": "Core Pack",
|
||||
"entries": []map[string]any{{"slug": "app-shell", "path": "entries/app-shell"}},
|
||||
})
|
||||
mustWriteStructured(t, filepath.Join(root, "catalog", "packs", "pack-core", "entries", "app-shell", testManifestFileName), map[string]any{
|
||||
"id": "app-shell",
|
||||
"slug": "app-shell",
|
||||
"type": "app",
|
||||
"runtime": map[string]any{
|
||||
"kind": "route",
|
||||
"path": "/v1/app-shell",
|
||||
},
|
||||
})
|
||||
mustWriteStructured(t, filepath.Join(root, "catalog", "standalone", "app-shell", testManifestFileName), map[string]any{
|
||||
"id": "app-shell",
|
||||
"slug": "app-shell",
|
||||
"type": "app",
|
||||
"source": "standalone",
|
||||
})
|
||||
mustWriteStructured(t, filepath.Join(root, "departments", "department-primary-department", testSettingsFileName), map[string]any{
|
||||
"id": "dept-1",
|
||||
"name": "Primary Department",
|
||||
"slug": "primary-department",
|
||||
"type": "department",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "departments", "department-primary-department", "users.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "departments", "department-primary-department", testUsersFileName), map[string]any{
|
||||
"users": []map[string]any{{"id": "admin-1"}},
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "departments", "department-primary-department", "teams", "team-primary-team", "settings.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "departments", "department-primary-department", "teams", "team-primary-team", testSettingsFileName), map[string]any{
|
||||
"id": "team-1",
|
||||
"name": "Primary Team",
|
||||
"slug": "primary-team",
|
||||
"type": "team",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "settings.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", testSettingsFileName), map[string]any{
|
||||
"id": "project-1",
|
||||
"name": "Primary Project",
|
||||
"slug": "primary-project",
|
||||
"type": "project",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "home.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", testHomeFileName), map[string]any{
|
||||
"type": "project-home",
|
||||
"project": "primary-project",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "acl.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", testACLFileName), map[string]any{
|
||||
"inherits": true,
|
||||
"rules": []any{},
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", "folder.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", testFolderFileName), map[string]any{
|
||||
"name": "Design",
|
||||
"slug": "design",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", "acl.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", testACLFileName), map[string]any{
|
||||
"inherits": true,
|
||||
"rules": []any{},
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", "children", "project-web", "settings.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", "children", "project-web", testSettingsFileName), map[string]any{
|
||||
"id": "project-2",
|
||||
"name": "Web Project",
|
||||
"slug": "web",
|
||||
"type": "project",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", "children", "project-web", "home.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", "children", "project-web", testHomeFileName), map[string]any{
|
||||
"type": "project-home",
|
||||
"project": "web",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", "children", "project-web", "acl.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", "children", "folder-design", "children", "project-web", testACLFileName), map[string]any{
|
||||
"inherits": true,
|
||||
"rules": []any{},
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "tree", "folder-docs", "folder.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", "tree", "folder-docs", testFolderFileName), map[string]any{
|
||||
"name": "Docs",
|
||||
"slug": "docs",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "tree", "folder-docs", "item-roadmap", "item.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", "tree", "folder-docs", "item-roadmap", testItemFileName), map[string]any{
|
||||
"id": "item-1",
|
||||
"name": "Roadmap",
|
||||
"slug": "roadmap",
|
||||
"type": "board",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "tree", "folder-docs", "item-roadmap", "schema.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", "tree", "folder-docs", "item-roadmap", testSchemaFileName), map[string]any{
|
||||
"type": "object",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "tree", "folder-docs", "item-roadmap", "data.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "projects", "project-primary-project", "tree", "folder-docs", "item-roadmap", testDataFileName), map[string]any{
|
||||
"title": "Roadmap",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "users", "settings.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "users", testSettingsFileName), map[string]any{
|
||||
"primaryAdminId": "admin-1",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "users", "data.json"), map[string]any{
|
||||
mustWriteStructured(t, filepath.Join(root, "users", testDataFileName), map[string]any{
|
||||
"users": []map[string]any{{"id": "admin-1", "email": "ronald@example.com"}},
|
||||
})
|
||||
|
||||
@@ -133,7 +172,7 @@ func TestScanRootBuildsProjectedNodesFromBootstrapShape(t *testing.T) {
|
||||
t.Fatalf("expected root organization slug primary-organization, got %q", rootNode.OrganizationSlug)
|
||||
}
|
||||
|
||||
tenantSettings := index["settings.json"]
|
||||
tenantSettings := index[testSettingsFileName]
|
||||
if tenantSettings.LogicalType != "tenant" || tenantSettings.FileRole != "settings" {
|
||||
t.Fatalf("unexpected tenant settings classification: %#v", tenantSettings)
|
||||
}
|
||||
@@ -141,7 +180,31 @@ func TestScanRootBuildsProjectedNodesFromBootstrapShape(t *testing.T) {
|
||||
t.Fatalf("expected installation id installation-1, got %q", tenantSettings.InstallationID)
|
||||
}
|
||||
|
||||
deptSettings := index["departments/department-primary-department/settings.json"]
|
||||
packManifest := index[filepath.ToSlash(filepath.Join("catalog", "packs", "pack-core", testManifestFileName))]
|
||||
if packManifest.LogicalType != "catalog" || packManifest.FileRole != "manifest" {
|
||||
t.Fatalf("unexpected pack manifest classification: %#v", packManifest)
|
||||
}
|
||||
if packManifest.ResourceSlug != "core" {
|
||||
t.Fatalf("expected pack manifest resource slug core, got %q", packManifest.ResourceSlug)
|
||||
}
|
||||
|
||||
entryManifest := index[filepath.ToSlash(filepath.Join("catalog", "packs", "pack-core", "entries", "app-shell", testManifestFileName))]
|
||||
if entryManifest.LogicalType != "catalog" || entryManifest.FileRole != "manifest" {
|
||||
t.Fatalf("unexpected catalog entry manifest classification: %#v", entryManifest)
|
||||
}
|
||||
if entryManifest.ResourceID != "app-shell" {
|
||||
t.Fatalf("expected catalog entry manifest resource id app-shell, got %q", entryManifest.ResourceID)
|
||||
}
|
||||
|
||||
standaloneManifest := index[filepath.ToSlash(filepath.Join("catalog", "standalone", "app-shell", testManifestFileName))]
|
||||
if standaloneManifest.LogicalType != "catalog" || standaloneManifest.FileRole != "manifest" {
|
||||
t.Fatalf("unexpected standalone manifest classification: %#v", standaloneManifest)
|
||||
}
|
||||
if standaloneManifest.ResourceSlug != "app-shell" {
|
||||
t.Fatalf("expected standalone manifest resource slug app-shell, got %q", standaloneManifest.ResourceSlug)
|
||||
}
|
||||
|
||||
deptSettings := index[filepath.ToSlash(filepath.Join("departments", "department-primary-department", testSettingsFileName))]
|
||||
if deptSettings.DepartmentSlug != "primary-department" {
|
||||
t.Fatalf("expected department slug primary-department, got %q", deptSettings.DepartmentSlug)
|
||||
}
|
||||
@@ -149,12 +212,12 @@ func TestScanRootBuildsProjectedNodesFromBootstrapShape(t *testing.T) {
|
||||
t.Fatalf("expected department resource id dept-1, got %q", deptSettings.ResourceID)
|
||||
}
|
||||
|
||||
teamSettings := index["departments/department-primary-department/teams/team-primary-team/settings.json"]
|
||||
teamSettings := index[filepath.ToSlash(filepath.Join("departments", "department-primary-department", "teams", "team-primary-team", testSettingsFileName))]
|
||||
if teamSettings.TeamSlug != "primary-team" {
|
||||
t.Fatalf("expected team slug primary-team, got %q", teamSettings.TeamSlug)
|
||||
}
|
||||
|
||||
projectSettings := index["projects/project-primary-project/settings.json"]
|
||||
projectSettings := index[filepath.ToSlash(filepath.Join("projects", "project-primary-project", testSettingsFileName))]
|
||||
if projectSettings.ProjectSlug != "primary-project" {
|
||||
t.Fatalf("expected project slug primary-project, got %q", projectSettings.ProjectSlug)
|
||||
}
|
||||
@@ -177,12 +240,12 @@ func TestScanRootBuildsProjectedNodesFromBootstrapShape(t *testing.T) {
|
||||
t.Fatalf("unexpected hierarchy folder node: %#v", hierarchyFolder)
|
||||
}
|
||||
|
||||
hierarchyFolderACL := index["projects/project-primary-project/children/folder-design/acl.json"]
|
||||
hierarchyFolderACL := index[filepath.ToSlash(filepath.Join("projects", "project-primary-project", "children", "folder-design", testACLFileName))]
|
||||
if hierarchyFolderACL.LogicalType != "hierarchy_folder" || hierarchyFolderACL.FileRole != "acl" {
|
||||
t.Fatalf("unexpected hierarchy folder acl classification: %#v", hierarchyFolderACL)
|
||||
}
|
||||
|
||||
childProjectSettings := index["projects/project-primary-project/children/folder-design/children/project-web/settings.json"]
|
||||
childProjectSettings := index[filepath.ToSlash(filepath.Join("projects", "project-primary-project", "children", "folder-design", "children", "project-web", testSettingsFileName))]
|
||||
if childProjectSettings.LogicalType != "project" || childProjectSettings.ProjectSlug != "web" {
|
||||
t.Fatalf("unexpected child project classification: %#v", childProjectSettings)
|
||||
}
|
||||
@@ -192,12 +255,12 @@ func TestScanRootBuildsProjectedNodesFromBootstrapShape(t *testing.T) {
|
||||
t.Fatalf("unexpected tree folder node: %#v", treeFolder)
|
||||
}
|
||||
|
||||
treeFolderACL := index["projects/project-primary-project/tree/folder-docs/folder.json"]
|
||||
treeFolderACL := index[filepath.ToSlash(filepath.Join("projects", "project-primary-project", "tree", "folder-docs", testFolderFileName))]
|
||||
if treeFolderACL.LogicalType != "hierarchy_folder" || treeFolderACL.FileRole != "folder" {
|
||||
t.Fatalf("unexpected tree folder file classification: %#v", treeFolderACL)
|
||||
}
|
||||
|
||||
treeItem := index["projects/project-primary-project/tree/folder-docs/item-roadmap/item.json"]
|
||||
treeItem := index[filepath.ToSlash(filepath.Join("projects", "project-primary-project", "tree", "folder-docs", "item-roadmap", testItemFileName))]
|
||||
if treeItem.LogicalType != "item" || treeItem.FileRole != "item" {
|
||||
t.Fatalf("unexpected tree item classification: %#v", treeItem)
|
||||
}
|
||||
@@ -205,12 +268,12 @@ func TestScanRootBuildsProjectedNodesFromBootstrapShape(t *testing.T) {
|
||||
t.Fatalf("expected tree item resource slug roadmap, got %#v", treeItem.ResourceSlug)
|
||||
}
|
||||
|
||||
usersData := index["users/data.json"]
|
||||
usersData := index[filepath.ToSlash(filepath.Join("users", testDataFileName))]
|
||||
if usersData.LogicalType != "users" || usersData.FileRole != "data" {
|
||||
t.Fatalf("unexpected users data classification: %#v", usersData)
|
||||
}
|
||||
if usersData.Checksum == "" || usersData.SizeBytes == 0 {
|
||||
t.Fatalf("expected users/data.json checksum and size to be populated: %#v", usersData)
|
||||
t.Fatalf("expected users/%s checksum and size to be populated: %#v", testDataFileName, usersData)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,13 +284,32 @@ func mustMkdirAll(t *testing.T, path string) {
|
||||
}
|
||||
}
|
||||
|
||||
func mustWriteJSON(t *testing.T, path string, payload any) {
|
||||
func mustWriteStructured(t *testing.T, path string, payload any) {
|
||||
t.Helper()
|
||||
bytes, err := json.MarshalIndent(payload, "", " ")
|
||||
if err != nil {
|
||||
t.Fatalf("MarshalIndent(%q) error = %v", path, err)
|
||||
var (
|
||||
bytes []byte
|
||||
err error
|
||||
)
|
||||
switch filepath.Ext(path) {
|
||||
case ".cbor":
|
||||
bytes, err = cbor.Marshal(payload)
|
||||
case ".json":
|
||||
bytes, err = json.MarshalIndent(payload, "", " ")
|
||||
if err == nil {
|
||||
bytes = append(bytes, '\n')
|
||||
}
|
||||
case ".jsonc":
|
||||
bytes, err = json.MarshalIndent(payload, "", " ")
|
||||
if err == nil {
|
||||
bytes = append([]byte("// Fixture JSONC with comments and trailing commas enabled.\n"), bytes...)
|
||||
bytes = append(bytes[:len(bytes)-2], []byte(",\n}\n")...)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("unsupported structured fixture file %q", path)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("marshal %q error = %v", path, err)
|
||||
}
|
||||
bytes = append(bytes, '\n')
|
||||
if err := os.WriteFile(path, bytes, 0o644); err != nil {
|
||||
t.Fatalf("WriteFile(%q) error = %v", path, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user