diff --git a/lib/ci/ci_utils.js b/lib/ci/ci_utils.js
index 50183540..e5835ffd 100644
--- a/lib/ci/ci_utils.js
+++ b/lib/ci/ci_utils.js
@@ -40,7 +40,13 @@ export function getNodeName(url) {
}
export function fold(summary, code) {
- const dataBlock = '```\n' + code + '\n```';
+ // Keep diagnostic output from closing its own code block.
+ let fenceLength = 3;
+ for (const match of String(code).matchAll(/`{3,}/g)) {
+ fenceLength = Math.max(fenceLength, match[0].length + 1);
+ }
+ const fence = '`'.repeat(fenceLength);
+ const dataBlock = fence + '\n' + code + '\n' + fence;
const summaryBlock = `\n${summary}\n`;
return `${summaryBlock}\n${dataBlock}\n `;
}
diff --git a/test/unit/ci_utils.test.js b/test/unit/ci_utils.test.js
new file mode 100644
index 00000000..9f36c5dd
--- /dev/null
+++ b/test/unit/ci_utils.test.js
@@ -0,0 +1,33 @@
+import { describe, it } from 'node:test';
+import assert from 'node:assert/strict';
+
+import { fold } from '../../lib/ci/ci_utils.js';
+
+describe('fold', () => {
+ for (const { name, code, fence } of [
+ { name: 'ordinary output', code: 'test failed', fence: '```' },
+ { name: 'empty output', code: '', fence: '```' },
+ { name: 'inline backticks', code: '`one` and ``two``', fence: '```' },
+ {
+ name: 'a fence followed by HTML and Markdown',
+ code: 'failure\n```\n\n\n# injected heading',
+ fence: '````'
+ },
+ {
+ name: 'multiple backtick runs including a longer fence',
+ code: '```\n``````\n````',
+ fence: '```````'
+ },
+ {
+ name: 'a long backtick run within a line',
+ code: 'before ````` after',
+ fence: '``````'
+ }
+ ]) {
+ it(`safely fences ${name}`, () => {
+ assert.equal(fold('See failures', code),
+ '\nSee failures
\n\n' +
+ `${fence}\n${code}\n${fence}\n `);
+ });
+ }
+});