|
8 | 8 | */ |
9 | 9 | import { strict as assert } from "node:assert"; |
10 | 10 | import test from "node:test"; |
11 | | -import { assertWellFormed } from "./sync-package-readmes.mjs"; |
| 11 | +import { applyBlocks, assertWellFormed } from "./sync-package-readmes.mjs"; |
12 | 12 |
|
13 | 13 | const S = (n) => `<!-- polycss:shared:${n}:start -->`; |
14 | 14 | const E = (n) => `<!-- polycss:shared:${n}:end -->`; |
@@ -66,16 +66,31 @@ test("rejects a block name the root README does not define", () => { |
66 | 66 | assert.match(error, /not defined in the root README/); |
67 | 67 | }); |
68 | 68 |
|
69 | | -test("replacement copies content literally, including $ sequences", () => { |
70 | | - // Regression: passing the block as a replacement STRING expands `$&`, |
71 | | - // "$`" and `$'`, corrupting any README containing them. |
72 | | - const blockRe = /<!-- polycss:shared:links:start -->[\s\S]*?<!-- polycss:shared:links:end -->/; |
73 | | - const replacement = `${S("links")}\ncost: $5 — see $& and $\` and $'\n${E("links")}`; |
| 69 | +test("applyBlocks copies content literally, including $ sequences", () => { |
| 70 | + // Regression: a replacement STRING expands `$&`, "$`" and `$'`. This drives |
| 71 | + // the PRODUCTION path, so reverting it to the string form fails here. |
| 72 | + const block = `${S("links")}\ncost: $5 — see $& and $\` and $'\n${E("links")}`; |
74 | 73 | const target = `head\n${S("links")}\nold\n${E("links")}\ntail`; |
75 | 74 |
|
76 | | - const viaString = target.replace(blockRe, replacement); |
77 | | - const viaCallback = target.replace(blockRe, () => replacement); |
| 75 | + const out = applyBlocks(target, new Map([["links", block]]), "fixture", ALLOWED); |
78 | 76 |
|
79 | | - assert.ok(viaCallback.includes("$& and $` and $'"), "callback must copy verbatim"); |
80 | | - assert.notEqual(viaString, viaCallback, "string form is the buggy path this guards against"); |
| 77 | + assert.ok(out.includes("$& and $` and $'"), "block must be copied byte-for-byte"); |
| 78 | + assert.equal(out, `head\n${block}\ntail`); |
| 79 | +}); |
| 80 | + |
| 81 | +test("applyBlocks leaves a marker-less file untouched", () => { |
| 82 | + const target = "# Pkg\n\nnothing shared here\n"; |
| 83 | + assert.equal(applyBlocks(target, new Map(), "fixture", ALLOWED), target); |
| 84 | +}); |
| 85 | + |
| 86 | +test("rejects marker-like comments that are not canonical", () => { |
| 87 | + for (const bad of [ |
| 88 | + "<!-- polycss:shared:links2:start -->", |
| 89 | + "<!-- polycss:shared:Links:start -->", |
| 90 | + "<!-- polycss:shared:links:begin -->", |
| 91 | + "<!-- polycss:shared:links:start -->", |
| 92 | + ]) { |
| 93 | + const { error } = check(`head\n${bad}\nx\n${E("links")}\n`); |
| 94 | + assert.match(error ?? "", /malformed shared marker/, `should reject ${bad}`); |
| 95 | + } |
81 | 96 | }); |
0 commit comments