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

Patrick Kollitsch
9 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
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { test } from "node:test";
 
const gate = resolve("tests/coverage-gate.mjs");
function runFixture({
  params = "",
  shortcode = false,
  reference = "tests/e2e/example.spec.ts",
  makeTest = true,
} = {}) {
  const root = mkdtempSync(join(tmpdir(), "ananke-coverage-gate-"));
  const write = (file, content) => {
    mkdirSync(dirname(join(root, file)), { recursive: true });
    writeFileSync(join(root, file), content);
  };
  try {
    write(
      "config/_default/params.toml",
      `[ananke]\nexisting = true\n${params}`,
    );
    write(
      "tests/catalog.yaml",
      `params:\n  ananke.existing:\n    tests: ["${reference}"]\n`,
    );
    mkdirSync(join(root, "layouts/_shortcodes"), { recursive: true });
    if (shortcode) write("layouts/_shortcodes/new.html", "<p>new</p>");
    if (makeTest)
      write("tests/e2e/example.spec.ts", "// fixture test reference\n");
    return spawnSync(process.execPath, [gate, root], { encoding: "utf8" });
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
}
 
test("registered features pass", () => assert.equal(runFixture().status, 0));
test("an unregistered parameter fails", () => {
  const result = runFixture({ params: "new = false\n" });
  assert.equal(result.status, 1);
  assert.match(result.stderr, /ananke.new.*has no test/);
});
test("an unregistered shortcode fails", () => {
  const result = runFixture({ shortcode: true });
  assert.equal(result.status, 1);
  assert.match(result.stderr, /shortcode 'new'.*has no test/);
});
test("a deleted test fails", () => {
  const result = runFixture({ makeTest: false });
  assert.equal(result.status, 1);
  assert.match(result.stderr, /does not exist/);
});
test("a reference outside the repository fails", () => {
  const result = runFixture({ reference: "../example.spec.ts" });
  assert.equal(result.status, 1);
  assert.match(result.stderr, /invalid test path/);
});
 
test("multiline arrays and commented table headers cannot bypass the gate", () => {
  const result = runFixture({
    params: '[ananke.navigation] # comment\nnew = [\n  "one",\n  "two",\n]\n',
  });
  assert.equal(result.status, 1);
  assert.match(result.stderr, /ananke.navigation.new.*has no test/);
});