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
1 change: 0 additions & 1 deletion .github/workflows/test-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ jobs:
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'

- name: Install Dependencies
run: pnpm install
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Toggle individual features (all enabled by default):
| `replaceHomeDir` | `true` | `os.homedir()` paths → `<HOME>` |
| `transformWin32Path` | `true` | Convert `D:\\foo\\bar` to `/d/foo/bar` |
| `transformCLR` | `true` | Normalize ANSI color escape codes |
| `normalizeTime` | `true` | Normalize durations in seconds: `1.5s` → `Xs`, `12 s` → `X s` |
| `escapeDoubleQuotes` | `true` | Escape `"` to `\"` |
| `escapeEOL` | `true` | Normalize `\r\n` to `\n` |
| `addDoubleQuotes` | `true` | Wrap output in double quotes |
Expand Down
39 changes: 39 additions & 0 deletions e2e/normalizeTime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect, test } from 'rstack/test';
import { createSnapshotSerializer } from 'path-serializer';

test.each([
[true, true, '<CLR=33,BOLD>X</CLR> s'],
[true, false, '<CLR=33,BOLD>1.5</CLR> s'],
[false, true, '\u001b[1m\u001b[33mX\u001b[39m\u001b[22m s'],
[false, false, '\u001b[1m\u001b[33m1.5\u001b[39m\u001b[22m s'],
] as const)(
'should independently control color (%s) and time (%s) normalization',
(transformCLR, normalizeTime, expected) => {
const serializer = createSnapshotSerializer({
features: { transformCLR, normalizeTime, addDoubleQuotes: false },
});
expect(
serializer.serialize('\u001b[1m\u001b[33m1.5\u001b[39m\u001b[22m s'),
).toBe(expected);
expect(serializer.serialize('built in 1.5s')).toBe(
normalizeTime ? 'built in Xs' : 'built in 1.5s',
);
},
);

test('should normalize time by default when color normalization is disabled', () => {
const serializer = createSnapshotSerializer({
features: { transformCLR: false, addDoubleQuotes: false },
});
expect(serializer.serialize('\u001b[33m1.5s\u001b[39m')).toBe(
'\u001b[33mXs\u001b[39m',
);
});

test('should preserve counts while normalizing seconds with default options', () => {
const serializer = createSnapshotSerializer();
expect(
serializer.serialize('Found 3 sources in 2 steps; built in 1.5s'),
).toBe('"Found 3 sources in 2 steps; built in Xs"');
expect(serializer.serialize('version 2 support')).toBe('"version 2 support"');
});
6 changes: 6 additions & 0 deletions src/createSnapshotSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
normalizeCLR,
normalizeCodeToPosix,
normalizePathToPosix,
normalizeTime,
} from './normalize';
import { replacePnpmInnerPath } from './pnpm';
import type { PathMatcher, SnapshotSerializerOptions } from './types';
Expand Down Expand Up @@ -40,6 +41,7 @@ export function createSnapshotSerializer(
escapeDoubleQuotes = true,
escapeEOL = true,
transformCLR = true,
normalizeTime: normalizeTimeEnabled = true,
} = features;

function createPathMatchers(): PathMatcher[] {
Expand Down Expand Up @@ -108,6 +110,10 @@ export function createSnapshotSerializer(
replaced = normalizeCLR(replaced);
}

if (normalizeTimeEnabled) {
replaced = normalizeTime(replaced);
}

if (escapeDoubleQuotes) {
replaced = replaced.replace(/"/g, '\\"');
}
Expand Down
58 changes: 54 additions & 4 deletions src/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
normalizeCLR,
normalizeCodeToPosix,
normalizePathToPosix,
normalizeTime,
} from './normalize';

describe('normalizePathToPosix', () => {
Expand Down Expand Up @@ -132,6 +133,31 @@ describe('normalizeCLR', () => {
expect(normalizeCLR(input)).toBe(expected);
});

test('should preserve time values', () => {
expect(normalizeCLR('built in 1.5s')).toBe('built in 1.5s');
expect(normalizeCLR('\u001b[33m1.5s\u001b[39m')).toBe(
'<CLR=33>1.5s<CLR=39>',
);
});
});

describe('normalizeTime', () => {
test('should preserve long numeric strings without time units', () => {
for (const input of ['0'.repeat(50_000), '0.'.repeat(25_000)]) {
expect(normalizeTime(input)).toBe(input);
}
});

test.each([
['<CLR=33,BOLD>1.5</CLR> s', '<CLR=33,BOLD>X</CLR> s'],
['<CLR=33>1.5<CLR=39> s', '<CLR=33>X<CLR=39> s'],
['\u001b[33m1.5\u001b[39m s', '\u001b[33mX\u001b[39m s'],
['built in 1500 ms', 'built in 1500 ms'],
['received 3 msgs', 'received 3 msgs'],
])('should preserve units and color markers in %s', (input, expected) => {
expect(normalizeTime(input)).toBe(expected);
});

test('should replace time values with "X" followed by time unit', () => {
const testCases = [
{
Expand All @@ -146,9 +172,17 @@ describe('normalizeCLR', () => {
input: '12 s',
expected: 'X s',
},
{
input: 'built 12 assets in 1.5s',
expected: 'built 12 assets in Xs',
},
{
input: '\u001b[1m\u001b[33m1.5\u001b[39m\u001b[22m s',
expected: '\u001b[1m\u001b[33mX\u001b[39m\u001b[22m s',
},
{
input: '\u001b[33m5.678s\u001b[39m',
expected: '<CLR=33>Xs<CLR=39>',
expected: '\u001b[33mXs\u001b[39m',
},
{
input: 'Time: 123.456s',
Expand All @@ -162,12 +196,12 @@ describe('normalizeCLR', () => {
// 测试带有颜色的多个时间值
{
input: '\u001b[33m2.5s\u001b[39m and \u001b[32m1.8s\u001b[39m',
expected: '<CLR=33>Xs<CLR=39> and <CLR=32>Xs<CLR=39>',
expected: '\u001b[33mXs\u001b[39m and \u001b[32mXs\u001b[39m',
},
];

for (const { input, expected } of testCases) {
expect(normalizeCLR(input)).toMatch(expected);
expect(normalizeTime(input)).toBe(expected);
}
});

Expand All @@ -181,6 +215,22 @@ describe('normalizeCLR', () => {
input: '123px',
expected: '123px',
},
{
input: 'Found 3 sources in 2 steps',
expected: 'Found 3 sources in 2 steps',
},
{
input: 'version 2 support',
expected: 'version 2 support',
},
{
input: '3sources and 2steps',
expected: '3sources and 2steps',
},
{
input: '\u001b[1m\u001b[33m3\u001b[39m\u001b[22m sources',
expected: '\u001b[1m\u001b[33m3\u001b[39m\u001b[22m sources',
},
{
input: 'version 1.2.3',
expected: 'version 1.2.3',
Expand All @@ -196,7 +246,7 @@ describe('normalizeCLR', () => {
];

for (const { input, expected } of testCases) {
expect(normalizeCLR(input)).toBe(expected);
expect(normalizeTime(input)).toBe(expected);
}
});
});
28 changes: 17 additions & 11 deletions src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ export const normalizeCodeToPosix = (code: string): string => {
};

export const normalizeCLR = (str: string): string => {
return (
str
.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, '<CLR=$1,BOLD>')
.replace(/\u001b\[1m/g, '<CLR=BOLD>')
.replace(/\u001b\[39m\u001b\[22m/g, '</CLR>')
.replace(/\u001b\[([0-9;]*)m/g, '<CLR=$1>')
// CHANGE: The time unit display in Rspack is second
// CHANGE2: avoid a bad case "./react/assets.svg" -> "./react/assetsXsvg"
// modified based on https://github.com/webpack/webpack/blob/001cab14692eb9a833c6b56709edbab547e291a1/test/StatsTestCases.basictest.js#L199
.replace(/[0-9]+(\.[0-9]+)*(<\/CLR>)?(\s?s)/g, 'X$2$3')
);
return str
.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, '<CLR=$1,BOLD>')
.replace(/\u001b\[1m/g, '<CLR=BOLD>')
.replace(/\u001b\[39m\u001b\[22m/g, '</CLR>')
.replace(/\u001b\[([0-9;]*)m/g, '<CLR=$1>');
};

export const normalizeTime = (str: string): string => {
// Preserve raw ANSI codes and normalized color markers between the value
// and unit so time normalization works independently of color normalization.
const unit = /(?:<\/CLR>|<CLR=[0-9;]*>|\u001b\[[0-9;]*m)*\s?s\b/y;
// Consume each number once, even when no time unit follows. Combining these
// patterns would retry the unit check at every digit of a long non-time value.
return str.replace(/[0-9]+(?:\.[0-9]+)*/g, (value, offset: number) => {
unit.lastIndex = offset + value.length;
return unit.test(str) ? 'X' : value;
});
};
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export interface Features {
* @default true
*/
transformCLR?: boolean;
/**
* `built in 1.5s` -> `built in Xs`
* Normalize durations in seconds independently of `transformCLR`.
* @default true
*/
normalizeTime?: boolean;
/**
* `""` -> `\"\"`
* @default true
Expand Down
Loading