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

Patrick Kollitsch
10 hours ago c70b22c0c65d21fc25eda46a8182eb042da11da0
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
#!/usr/bin/env node
/**
 * Build the test fixture site against the *local* theme working tree.
 *
 * Hugo resolves a classic theme from `<themesDir>/<themeName>`. We create a
 * temporary themes directory containing a symlink `ananke -> <repo root>` so
 * the build exercises the current branch (including uncommitted changes)
 * without committing an absolute-path symlink. Output goes to
 * `tests/fixtures/site/public`, which the static server then serves.
 */
import { spawnSync } from "node:child_process";
import { mkdtempSync, rmSync, symlinkSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { stripVTControlCharacters } from "node:util";
 
const here = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(here, "..", "..");
const siteDir = resolve(here, "..", "fixtures", "site");
 
export function buildFixtureSite(options = {}) {
  const source = options.source || siteDir;
  const destination = options.destination || join(source, "public");
  const expectedModes = source === siteDir ? ["unknown", "invalid"] : [];
  const themesDir = mkdtempSync(join(tmpdir(), "ananke-test-themes-"));
  try {
    symlinkSync(repoRoot, join(themesDir, "ananke"), "dir");
    rmSync(destination, { recursive: true, force: true });
    const result = spawnSync(
      "hugo",
      [
        "--source",
        source,
        "--themesDir",
        themesDir,
        "--destination",
        destination,
        "--baseURL",
        `http://localhost:${process.env.ANANKE_TEST_PORT || 4321}/`,
        "--environment",
        "production",
        "--printPathWarnings",
        "--logLevel",
        "warn",
        ...(options.config ? ["--config", options.config] : []),
      ],
      { encoding: "utf8" },
    );
    const output = `${result.stdout || ""}\n${result.stderr || ""}`;
    process.stdout.write(output);
    if (result.error) throw result.error;
    const expectedWarnings =
      options.expectedWarnings ||
      expectedModes.map(
        (mode) =>
          `[ananke] Invalid ananke.navigation.submenu_mode "${mode}"; using split. Choose split, hover, click, or toplevel.`,
      );
    checkDiagnostics(output, expectedWarnings);
    options.onOutput?.(output);
    if (result.status !== 0) {
      throw new Error(`hugo build failed with code ${result.status}`);
    }
  } finally {
    rmSync(themesDir, { recursive: true, force: true });
  }
  return destination;
}
 
export function checkDiagnostics(output, expectedWarnings = []) {
  const warnings = stripVTControlCharacters(output)
    .split("\n")
    .filter((line) => /^(?:WARN|DEPRECATED)\s/.test(line));
  const matches = (line, expected) => {
    const message = line.replace(/^(?:WARN|DEPRECATED)\s+/, "");
    return typeof expected === "string"
      ? message === expected
      : expected.test(message);
  };
  for (const expected of expectedWarnings) {
    if (warnings.filter((line) => matches(line, expected)).length !== 1) {
      throw new Error(`Expected exactly one fixture warning: ${expected}`);
    }
  }
  const unexpected = warnings.filter(
    (line) => !expectedWarnings.some((expected) => matches(line, expected)),
  );
  if (unexpected.length) {
    throw new Error(
      `Unexpected Hugo warnings/deprecations:\n${unexpected.join("\n") || output}`,
    );
  }
}
 
// Allow running directly: `node tests/support/prepare-site.mjs`
if (import.meta.url === `file://${process.argv[1]}`) {
  buildFixtureSite();
}