Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions packages/core/test/size.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,32 @@ const rows = [
{ key: 'overlay', label: 'The badge', minified: 12_849, gzip: 4_772 },
];

test('the size table is written between its markers and read back to the tenth of a kilobyte', () => {
const table = formatTable(rows, 'rolldown 1.2.8');
assert.ok(table.startsWith(START) && table.endsWith(END));
assert.match(table, /\| `\/auto` \| 59\.0 KB \| 21\.2 KB \|/);
const readme = `# Title\n\nBefore.\n\n${START}\nold table\n${END}\n\nAfter.\n`;
const written = replaceTable(readme, table);
assert.ok(written.startsWith('# Title\n\nBefore.\n\n') && written.endsWith('\n\nAfter.\n'));
assert.deepEqual(parseTable(written), [
{ label: '`/auto`', minified: 59, gzip: 21.2 },
{ label: 'The badge', minified: 12.8, gzip: 4.8 },
]);
assert.equal(parseTable('# No table here'), null);
assert.throws(() => replaceTable('# No table here', table), /no <!-- size:start -->/);
});
for (const lineEnding of ['\n', '\r\n']) {
test(`the size table is written between its markers and read back to the tenth of a kilobyte (${lineEnding === '\n' ? 'LF' : 'CRLF'})`, () => {
const tool = 'rolldown 1.2.8';
const table = formatTable(rows, tool);
assert.ok(table.startsWith(START) && table.endsWith(END));
assert.match(table, /\| `\/auto` \| 59\.0 KB \| 21\.2 KB \|/);
const before = `# Title${lineEnding}${lineEnding}Before.${lineEnding}${lineEnding}`;
const after = `${lineEnding}${lineEnding}After.${lineEnding}`;
const readme = `${before}${START}${lineEnding}old table${lineEnding}${END}${after}`;
const written = replaceTable(readme, table);
assert.equal(written.slice(0, before.length), before);
assert.equal(written.slice(-after.length), after);
if (lineEnding === '\r\n') {
assert.equal(written.replaceAll('\r\n', '').includes('\n'), false);
} else {
assert.equal(written.includes('\r\n'), false);
}
assert.deepEqual(problems(written, rows, tool), []);
assert.deepEqual(parseTable(written), [
{ label: '`/auto`', minified: 59, gzip: 21.2 },
{ label: 'The badge', minified: 12.8, gzip: 4.8 },
]);
assert.equal(parseTable('# No table here'), null);
assert.throws(() => replaceTable('# No table here', table), /no <!-- size:start -->/);
});
}

for (const lineEnding of ['\n', '\r\n']) {
test(`the check accepts ${lineEnding === '\n' ? 'LF' : 'CRLF'} tables without relaxing stale checks`, () => {
Expand Down
6 changes: 4 additions & 2 deletions scripts/size-table.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ export function parseTable(readme) {
return rows;
}

/** `readme` with the table between the markers replaced by `table`. */
/** `readme` with the table between the markers replaced by `table`, keeping the block's LF or CRLF line endings. */
export function replaceTable(readme, table) {
const start = readme.indexOf(START);
const end = readme.indexOf(END);
if (start < 0 || end < start) throw new Error(`README.md has no ${START} ... ${END} block for the size table`);
return readme.slice(0, start) + table + readme.slice(end + END.length);
const crlf = readme.slice(start, end + END.length).includes('\r\n');
const replacement = crlf ? table.replaceAll('\r\n', '\n').replaceAll('\n', '\r\n') : table.replaceAll('\r\n', '\n');
return readme.slice(0, start) + replacement + readme.slice(end + END.length);
}

/** The table between the markers in `readme`, markers included, as written; null without them. */
Expand Down
Loading