Skip to content
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

WIP: replaced diff to diff-match-patch (#3675) #3989

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 25 additions & 2 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

var tty = require('tty');
var diff = require('diff');
var DiffMatchPatch = require('diff-match-patch');
var milliseconds = require('ms');
var utils = require('../utils');
var supportsColor = process.browser ? null : require('supports-color');
Expand Down Expand Up @@ -393,6 +394,28 @@ function inlineDiff(actual, expected) {
return msg;
}

/**
* diff-match-patch
*
* @private
* @param {String} actual
* @param {String} expected
* @return {string} The diff.
*/

function createPatch(actual, expected) {
var dmp = new DiffMatchPatch();
var a = dmp.diff_linesToChars_(actual, expected);
var lineText1 = a.chars1;
var lineText2 = a.chars2;
var lineArray = a.lineArray;
var diffs = dmp.diff_main(lineText1, lineText2);
dmp.diff_charsToLines_(diffs, lineArray);
var patch = dmp.patch_make(diffs);
var msg = dmp.patch_toText(patch);
return decodeURI(msg);
}

/**
* Returns unified diff between two strings with coloured ANSI output.
*
Expand Down Expand Up @@ -421,8 +444,8 @@ function unifiedDiff(actual, expected) {
function notBlank(line) {
return typeof line !== 'undefined' && line !== null;
}
var msg = diff.createPatch('string', actual, expected);
var lines = msg.split('\n').splice(5);
var msg = createPatch(actual, expected);
var lines = msg.split('\n').splice(1);
return (
'\n ' +
colorLines('diff added', '+ expected') +
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@
"browser-stdout": "1.3.1",
"debug": "3.2.6",
"diff": "3.5.0",
"diff-match-patch": "^1.0.4",
"escape-string-regexp": "1.0.5",
"find-up": "3.0.0",
"glob": "7.1.3",
Expand Down