-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kbn/optimizer] extract string diffing logic (#127394)
- Loading branch information
Spencer
authored
Mar 9, 2022
1 parent
4b6f754
commit e378c0d
Showing
7 changed files
with
211 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { diffStrings } from './diff_strings'; | ||
|
||
const json = (x: any) => JSON.stringify(x, null, 2); | ||
|
||
describe('diffStrings()', () => { | ||
it('returns undefined if values are equal', () => { | ||
expect(diffStrings('1', '1')).toBe(undefined); | ||
expect(diffStrings(json(['1', '2', { a: 'b' }]), json(['1', '2', { a: 'b' }]))).toBe(undefined); | ||
expect( | ||
diffStrings( | ||
json({ | ||
a: '1', | ||
b: '2', | ||
}), | ||
json({ | ||
a: '1', | ||
b: '2', | ||
}) | ||
) | ||
).toBe(undefined); | ||
}); | ||
|
||
it('returns a diff if the values are different', () => { | ||
const diff = diffStrings(json(['1', '2', { a: 'b' }]), json(['1', '2', { b: 'a' }])); | ||
|
||
expect(diff).toMatchInlineSnapshot(` | ||
"[32m- Expected[39m | ||
[31m+ Received[39m | ||
[2m [[22m | ||
[2m \\"1\\",[22m | ||
[2m \\"2\\",[22m | ||
[2m {[22m | ||
[32m- \\"a\\": \\"b\\"[39m | ||
[31m+ \\"b\\": \\"a\\"[39m | ||
[2m }[22m | ||
[2m ][22m" | ||
`); | ||
|
||
const diff2 = diffStrings( | ||
json({ | ||
a: '1', | ||
b: '1', | ||
}), | ||
json({ | ||
b: '2', | ||
a: '2', | ||
}) | ||
); | ||
|
||
expect(diff2).toMatchInlineSnapshot(` | ||
"[32m- Expected[39m | ||
[31m+ Received[39m | ||
[2m {[22m | ||
[32m- \\"a\\": \\"1\\",[39m | ||
[32m- \\"b\\": \\"1\\"[39m | ||
[31m+ \\"b\\": \\"2\\",[39m | ||
[31m+ \\"a\\": \\"2\\"[39m | ||
[2m }[22m" | ||
`); | ||
}); | ||
|
||
it('formats large diffs to focus on the changed lines', () => { | ||
const diff = diffStrings( | ||
json({ | ||
a: ['1', '1', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1', '1', '1', '1', '1', '1'], | ||
}), | ||
json({ | ||
b: ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1'], | ||
}) | ||
); | ||
|
||
expect(diff).toMatchInlineSnapshot(` | ||
"[32m- Expected[39m | ||
[31m+ Received[39m | ||
[2m {[22m | ||
[32m- \\"a\\": [[39m | ||
[31m+ \\"b\\": [[39m | ||
[2m \\"1\\",[22m | ||
[2m \\"1\\",[22m | ||
[2m ...[22m | ||
[2m \\"1\\",[22m | ||
[2m \\"1\\",[22m | ||
[32m- \\"2\\",[39m | ||
[2m \\"1\\",[22m | ||
[2m \\"1\\",[22m | ||
[2m ...[22m | ||
[2m \\"1\\",[22m | ||
[2m \\"1\\",[22m | ||
[31m+ \\"2\\",[39m | ||
[2m \\"1\\",[22m | ||
[2m \\"1\\",[22m | ||
[2m ...[22m" | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import jestDiff from 'jest-diff'; | ||
import stripAnsi from 'strip-ansi'; | ||
import Chalk from 'chalk'; | ||
|
||
function reformatJestDiff(diff: string) { | ||
const diffLines = diff.split('\n'); | ||
|
||
if ( | ||
diffLines.length < 4 || | ||
stripAnsi(diffLines[0]) !== '- Expected' || | ||
stripAnsi(diffLines[1]) !== '+ Received' | ||
) { | ||
throw new Error(`unexpected diff format: ${diff}`); | ||
} | ||
|
||
const outputLines = [diffLines.shift(), diffLines.shift(), diffLines.shift()]; | ||
|
||
/** | ||
* buffer which contains between 0 and 5 lines from the diff which aren't additions or | ||
* deletions. The first three are the first three lines seen since the buffer was cleared | ||
* and the last two lines are the last two lines seen. | ||
* | ||
* When flushContext() is called we write the first two lines to output, an elipses if there | ||
* are five lines, and then the last two lines. | ||
* | ||
* At the very end we will write the last two lines of context if they're defined | ||
*/ | ||
const contextBuffer: string[] = []; | ||
|
||
/** | ||
* Convert a line to an empty line with elipses placed where the text on that line starts | ||
*/ | ||
const toElipses = (line: string) => { | ||
return stripAnsi(line).replace(/^(\s*).*/, '$1...'); | ||
}; | ||
|
||
while (diffLines.length) { | ||
const line = diffLines.shift()!; | ||
const plainLine = stripAnsi(line); | ||
if (plainLine.startsWith('+ ') || plainLine.startsWith('- ')) { | ||
// write contextBuffer to the outputLines | ||
if (contextBuffer.length) { | ||
outputLines.push( | ||
...contextBuffer.slice(0, 2), | ||
...(contextBuffer.length === 5 | ||
? [Chalk.dim(toElipses(contextBuffer[2])), ...contextBuffer.slice(3, 5)] | ||
: contextBuffer.slice(2, 4)) | ||
); | ||
|
||
contextBuffer.length = 0; | ||
} | ||
|
||
// add this line to the outputLines | ||
outputLines.push(line); | ||
} else { | ||
// update the contextBuffer with this line which doesn't represent a change | ||
if (contextBuffer.length === 5) { | ||
contextBuffer[3] = contextBuffer[4]; | ||
contextBuffer[4] = line; | ||
} else { | ||
contextBuffer.push(line); | ||
} | ||
} | ||
} | ||
|
||
if (contextBuffer.length) { | ||
outputLines.push( | ||
...contextBuffer.slice(0, 2), | ||
...(contextBuffer.length > 2 ? [Chalk.dim(toElipses(contextBuffer[2]))] : []) | ||
); | ||
} | ||
|
||
return outputLines.join('\n'); | ||
} | ||
|
||
/** | ||
* Produces a diff string which is nicely formatted to show the differences between two strings. This will | ||
* be a multi-line string so it's generally a good idea to include a `\n` before this first line of the diff | ||
* if you are concatenating it with another message. | ||
*/ | ||
export function diffStrings(expected: string, received: string) { | ||
const diff = jestDiff(expected, received); | ||
|
||
if (!diff || stripAnsi(diff) === 'Compared values have no visual difference.') { | ||
return undefined; | ||
} | ||
|
||
return reformatJestDiff(diff); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.