Skip to content

Commit d7aa5f3

Browse files
author
Orta
authored
Show a diff when the public api baselines fail (microsoft#39108)
* Add semantic highlighting pt1 * Make the public API unit tests echo out a diff
1 parent 0bc29ac commit d7aa5f3

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"chalk": "latest",
6666
"convert-source-map": "latest",
6767
"del": "5.1.0",
68+
"diff": "^4.0.2",
6869
"eslint": "6.8.0",
6970
"eslint-formatter-autolinkable-stylish": "1.1.2",
7071
"eslint-plugin-import": "2.20.2",

src/harness/harnessIO.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,7 @@ namespace Harness {
12931293
export interface BaselineOptions {
12941294
Subfolder?: string;
12951295
Baselinefolder?: string;
1296+
PrintDiff?: true;
12961297
}
12971298

12981299
export function localPath(fileName: string, baselineFolder?: string, subfolder?: string) {
@@ -1347,7 +1348,7 @@ namespace Harness {
13471348
return { expected, actual };
13481349
}
13491350

1350-
function writeComparison(expected: string, actual: string, relativeFileName: string, actualFileName: string) {
1351+
function writeComparison(expected: string, actual: string, relativeFileName: string, actualFileName: string, opts?: BaselineOptions) {
13511352
// For now this is written using TypeScript, because sys is not available when running old test cases.
13521353
// But we need to move to sys once we have
13531354
// Creates the directory including its parent if not already present
@@ -1381,7 +1382,14 @@ namespace Harness {
13811382
else {
13821383
IO.writeFile(actualFileName, encodedActual);
13831384
}
1384-
throw new Error(`The baseline file ${relativeFileName} has changed.`);
1385+
if (require && opts && opts.PrintDiff) {
1386+
const Diff = require("diff");
1387+
const patch = Diff.createTwoFilesPatch("Expected", "Actual", expected, actual, "The current baseline", "The new version");
1388+
throw new Error(`The baseline file ${relativeFileName} has changed.${ts.ForegroundColorEscapeSequences.Grey}\n\n${patch}`);
1389+
}
1390+
else {
1391+
throw new Error(`The baseline file ${relativeFileName} has changed.`);
1392+
}
13851393
}
13861394
}
13871395

@@ -1391,7 +1399,7 @@ namespace Harness {
13911399
throw new Error("The generated content was \"undefined\". Return \"null\" if no baselining is required.\"");
13921400
}
13931401
const comparison = compareToBaseline(actual, relativeFileName, opts);
1394-
writeComparison(comparison.expected, comparison.actual, relativeFileName, actualFileName);
1402+
writeComparison(comparison.expected, comparison.actual, relativeFileName, actualFileName, opts);
13951403
}
13961404

13971405
export function runMultifileBaseline(relativeFileBase: string, extension: string, generateContent: () => IterableIterator<[string, string, number]> | IterableIterator<[string, string]> | null, opts?: BaselineOptions, referencedExtensions?: string[]): void {

src/testRunner/unittests/publicApi.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe("unittests:: Public APIs", () => {
1010
});
1111

1212
it("should be acknowledged when they change", () => {
13-
Harness.Baseline.runBaseline(api, fileContent);
13+
Harness.Baseline.runBaseline(api, fileContent, { PrintDiff: true });
1414
});
1515

1616
it("should compile", () => {

0 commit comments

Comments
 (0)