Skip to content

Show a diff when the public api baselines fail #39108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 17, 2020
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: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"chalk": "latest",
"convert-source-map": "latest",
"del": "5.1.0",
"diff": "^4.0.2",
"eslint": "6.8.0",
"eslint-formatter-autolinkable-stylish": "1.1.2",
"eslint-plugin-import": "2.20.2",
Expand Down
14 changes: 11 additions & 3 deletions src/harness/harnessIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,7 @@ namespace Harness {
export interface BaselineOptions {
Subfolder?: string;
Baselinefolder?: string;
PrintDiff?: true;
}

export function localPath(fileName: string, baselineFolder?: string, subfolder?: string) {
Expand Down Expand Up @@ -1347,7 +1348,7 @@ namespace Harness {
return { expected, actual };
}

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

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

export function runMultifileBaseline(relativeFileBase: string, extension: string, generateContent: () => IterableIterator<[string, string, number]> | IterableIterator<[string, string]> | null, opts?: BaselineOptions, referencedExtensions?: string[]): void {
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/publicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("unittests:: Public APIs", () => {
});

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

it("should compile", () => {
Expand Down