Skip to content

Commit 2fe393a

Browse files
devversionalxhub
authored andcommitted
refactor(migrations): improve tsurge diff helper to simplify output (angular#57961)
Instead of printing the full diff, which may be a super large file or golden, we only print context around lines with diff. This makes the diffs much more actionable and readable. PR Close angular#57961
1 parent c44f087 commit 2fe393a

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

packages/core/schematics/migrations/signal-migration/test/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ ts_library(
1010
"golden_test_runner.ts",
1111
],
1212
deps = [
13-
"@npm//@types/diff",
13+
"//packages/core/schematics/utils/tsurge",
1414
"@npm//chalk",
15-
"@npm//diff",
1615
"@npm//fast-glob",
1716
],
1817
)

packages/core/schematics/migrations/signal-migration/test/golden_test_runner.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
import fs from 'fs';
1515
import glob from 'fast-glob';
16-
import * as diff from 'diff';
1716
import chalk from 'chalk';
1817
import path from 'path';
18+
import {diffText} from '../../../utils/tsurge/testing/diff';
1919

2020
const [migratedDir, goldenPath] = process.argv.slice(2);
2121
const files = glob.sync('**/*', {cwd: migratedDir});
@@ -44,18 +44,7 @@ if (diskGolden !== golden) {
4444
process.exit(0);
4545
}
4646

47-
const goldenDiff = diff.diffChars(diskGolden, golden);
48-
49-
goldenDiff.forEach((part) => {
50-
// green for additions, red for deletions
51-
let text = part.added
52-
? chalk.green(part.value)
53-
: part.removed
54-
? chalk.red(part.value)
55-
: part.value;
56-
57-
process.stderr.write(text);
58-
});
47+
process.stderr.write(diffText(diskGolden, golden));
5948

6049
console.error();
6150
console.error();

packages/core/schematics/utils/tsurge/testing/diff.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import chalk from 'chalk';
1313
* Diffs the given two strings and creates a human-readable
1414
* colored diff string.
1515
*/
16-
export function diffText(expected: string, actual: string): string {
16+
export function diffText(expected: string, actual: string, diffLineContextRange = 10): string {
17+
const redColorCode = chalk.red('ɵɵ').split('ɵɵ')[0];
18+
const greenColorCode = chalk.green('ɵɵ').split('ɵɵ')[0];
1719
const goldenDiff = diff.diffChars(actual, expected);
18-
let result = '';
20+
let fullResult = '';
1921

2022
for (const part of goldenDiff) {
2123
// whitespace cannot be highlighted, so we use a tiny indicator character.
@@ -27,8 +29,50 @@ export function diffText(expected: string, actual: string): string {
2729
? chalk.red(valueForColor)
2830
: chalk.reset(part.value);
2931

30-
result += text;
32+
fullResult += text;
33+
}
34+
35+
const lines = fullResult.split(/\n/g);
36+
const linesToRender = new Set<number>();
37+
38+
// Find lines with diff, and include context lines around them.
39+
for (const [index, l] of lines.entries()) {
40+
if (l.includes(redColorCode) || l.includes(greenColorCode)) {
41+
const contextBottom = index - diffLineContextRange;
42+
const contextTop = index + diffLineContextRange;
43+
44+
numbersFromTo(Math.max(0, contextBottom), index).forEach((lineNum) =>
45+
linesToRender.add(lineNum),
46+
);
47+
numbersFromTo(index, Math.min(contextTop, lines.length - 1)).forEach((lineNum) =>
48+
linesToRender.add(lineNum),
49+
);
50+
}
51+
}
52+
53+
let result = '';
54+
let previous = -1;
55+
56+
// Compute full diff text. Add markers if lines were skipped.
57+
for (const lineIndex of Array.from(linesToRender).sort()) {
58+
if (lineIndex - 1 !== previous) {
59+
result += `${chalk.grey('... (lines above) ...')}\n`;
60+
}
61+
result += `${lines[lineIndex]}\n`;
62+
previous = lineIndex;
63+
}
64+
65+
if (previous < lines.length - 1) {
66+
result += `${chalk.grey('... (lines below) ...\n')}`;
3167
}
3268

3369
return result;
3470
}
71+
72+
function numbersFromTo(start: number, end: number): number[] {
73+
const list: number[] = [];
74+
for (let i = start; i <= end; i++) {
75+
list.push(i);
76+
}
77+
return list;
78+
}

0 commit comments

Comments
 (0)