mirror of https://github.com/theNewDynamic/gohugo-theme-ananke.git

Patrick Kollitsch
14 hours ago 90cd3ec73013be16fb62b7f2de618b6dd90e1c87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env node
/**
 * Feature -> test coverage gate.
 *
 * Fails when a user-facing feature has no test catalogued:
 *   - every direct key under the tables in GATED_TABLES in
 *     config/_default/params.toml
 *   - every shortcode in layouts/_shortcodes/
 * must appear in tests/catalog.yaml with a non-empty `tests` list.
 *
 * An optional `docs` value names the documentation page of the feature as a
 * site-relative path; it is checked for format only.
 *
 * It also reports catalog entries that no longer match a real param/shortcode
 * (stale entries), so the catalog stays in sync as features are added, renamed,
 * or removed.
 *
 * Scope note: only the direct keys of the tables in GATED_TABLES are gated
 * here (not the deep [ananke.social.networks.*] catalog). Widen GATED_TABLES
 * as the gate matures.
 */
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import TOML from "@iarna/toml";
import { parse } from "yaml";
 
const here = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(process.argv[2] || join(here, ".."));
 
/** Tables in config/_default/params.toml whose direct keys are gated. */
const GATED_TABLES = [
  "ananke",
  "ananke.navigation",
  "ananke.navigation.classes",
];
 
/** Direct `key = value` entries under the gated tables. */
function collectAnankeParams() {
  const toml = readFileSync(
    join(repoRoot, "config", "_default", "params.toml"),
    "utf8",
  );
  const config = TOML.parse(toml);
  const keys = new Set();
  for (const table of GATED_TABLES) {
    const values = table.split(".").reduce((node, key) => node?.[key], config);
    for (const [key, value] of Object.entries(values || {})) {
      if (value === null || typeof value !== "object" || Array.isArray(value)) {
        keys.add(`${table}.${key}`);
      }
    }
  }
  return keys;
}
 
/** Shortcode names from layouts/_shortcodes/*.html. */
function collectShortcodes() {
  const dir = join(repoRoot, "layouts", "_shortcodes");
  return new Set(
    readdirSync(dir)
      .filter((f) => f.endsWith(".html"))
      .map((f) => f.replace(/\.html$/, "")),
  );
}
 
function catalogued(section) {
  const out = new Set();
  for (const [name, value] of Object.entries(section || {})) {
    if (value && Array.isArray(value.tests) && value.tests.length > 0) {
      out.add(name);
    }
  }
  return out;
}
 
const catalog =
  parse(readFileSync(join(repoRoot, "tests", "catalog.yaml"), "utf8")) || {};
const params = collectAnankeParams();
const shortcodes = collectShortcodes();
const catParams = catalogued(catalog.params);
const catShortcodes = catalogued(catalog.shortcodes);
 
const DOCS_PATH = /^\/(?:[a-z0-9-]+\/)*(?:#[a-z0-9-]+)?$/;
const errors = [];
for (const section of [catalog.params, catalog.shortcodes]) {
  for (const [feature, entry] of Object.entries(section || {})) {
    // Optional documentation target: a site-relative path on the
    // documentation site, for example "/configuration/reference/#homepage".
    if (entry?.docs !== undefined && !DOCS_PATH.test(String(entry.docs))) {
      errors.push(`${feature}: invalid docs path '${entry.docs}'`);
    }
    for (const ref of entry?.tests || []) {
      if (typeof ref !== "string") {
        errors.push(`${feature}: test reference must be a path string`);
        continue;
      }
      const path = resolve(repoRoot, ref);
      const local = relative(repoRoot, path);
      if (
        isAbsolute(ref) ||
        local.startsWith("..") ||
        local.split(/[\\/]/).includes("scratch") ||
        !(
          /\.(spec|test)\.(ts|mjs)$/.test(ref) ||
          ref === "scripts/test-hugo-quickstart.ts"
        )
      ) {
        errors.push(`${feature}: invalid test path '${ref}'`);
      } else if (!existsSync(path) || !statSync(path).isFile()) {
        errors.push(`${feature}: test file '${ref}' does not exist`);
      }
    }
  }
}
for (const p of params) {
  if (!catParams.has(p))
    errors.push(`param '${p}' has no test in tests/catalog.yaml`);
}
for (const s of shortcodes) {
  if (!catShortcodes.has(s))
    errors.push(`shortcode '${s}' has no test in tests/catalog.yaml`);
}
// Stale catalog entries (kept as warnings so renames are noticed promptly).
const warnings = [];
for (const p of Object.keys(catalog.params || {})) {
  if (!params.has(p)) warnings.push(`catalog param '${p}' no longer exists`);
}
for (const s of Object.keys(catalog.shortcodes || {})) {
  if (!shortcodes.has(s))
    warnings.push(`catalog shortcode '${s}' no longer exists`);
}
 
for (const w of warnings) console.warn(`warning: ${w}`);
if (errors.length > 0) {
  console.error("\nCoverage gate failed:");
  for (const e of errors) console.error(`  - ${e}`);
  console.error(
    "\nAdd the feature and at least one test reference to tests/catalog.yaml.",
  );
  process.exit(1);
}
console.log(
  `Coverage gate passed: ${params.size} ananke param(s), ${shortcodes.size} shortcode(s) all have tests.`,
);