diff --git a/src/__tests__/line-by-line-tests.ts b/src/__tests__/line-by-line-tests.ts index ea7ad89..5571931 100644 --- a/src/__tests__/line-by-line-tests.ts +++ b/src/__tests__/line-by-line-tests.ts @@ -21,6 +21,39 @@ describe('LineByLineRenderer', () => { }); }); + describe('_generateTooBigDiff', () => { + it('should return a diff with default "too big" message when no `diffTooBigMessage` is defined', () => { + const hoganUtils = new HoganJsUtils({}); + const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); + const fileHtml = lineByLineRenderer.generateTooBigDiff(0); + expect(fileHtml).toMatchInlineSnapshot(` + " + +
+ Diff too big to be displayed +
+ + " + `); + }); + + it('should return a diff with custom "too big" message when `diffTooBigMessage` is defined', () => { + const hoganUtils = new HoganJsUtils({}); + const customMessageFn = (fIndex: number) => `Custom message for file ${fIndex} diff too big`; + const lineByLineRenderer = new LineByLineRenderer(hoganUtils, { diffTooBigMessage: customMessageFn }); + const fileHtml = lineByLineRenderer.generateTooBigDiff(0); + expect(fileHtml).toMatchInlineSnapshot(` + " + +
+ Custom message for file 0 diff too big +
+ + " + `); + }); + }); + describe('makeLineHtml', () => { it('should work for insertions', () => { const hoganUtils = new HoganJsUtils({}); @@ -505,6 +538,111 @@ describe('LineByLineRenderer', () => { " `); }); + + it('should work for too big file diff and no custom message fn', () => { + const exampleJson = [ + { + blocks: [], + deletedLines: 0, + addedLines: 0, + oldName: 'sample', + language: 'js', + newName: 'sample', + isCombined: false, + isGitDiff: false, + isTooBig: true, + }, + ]; + + const hoganUtils = new HoganJsUtils({}); + const lineByLineRenderer = new LineByLineRenderer(hoganUtils); + const html = lineByLineRenderer.render(exampleJson); + expect(html).toMatchInlineSnapshot(` + "
+
+
+ + + + sample + CHANGED + +
+
+
+ + + + + + +
+
+ Diff too big to be displayed +
+
+
+
+
+
" + `); + }); + + it('should work for too big file diff and custom message fn', () => { + const exampleJson = [ + { + blocks: [], + deletedLines: 0, + addedLines: 0, + oldName: 'sample', + language: 'js', + newName: 'sample', + isCombined: false, + isGitDiff: false, + isTooBig: true, + }, + ]; + + const hoganUtils = new HoganJsUtils({}); + const customMessageFn = (fIndex: number) => `Custom message for file ${fIndex} diff too big`; + const lineByLineRenderer = new LineByLineRenderer(hoganUtils, { diffTooBigMessage: customMessageFn }); + const html = lineByLineRenderer.render(exampleJson); + expect(html).toMatchInlineSnapshot(` + "
+
+
+ + + + sample + CHANGED + +
+
+
+ + + + + + +
+
+ Custom message for file 0 diff too big +
+
+
+
+
+
" + `); + }); }); describe('_generateFileHtml', () => { diff --git a/src/__tests__/side-by-side-printer-tests.ts b/src/__tests__/side-by-side-printer-tests.ts index 6863a1a..b04c61b 100644 --- a/src/__tests__/side-by-side-printer-tests.ts +++ b/src/__tests__/side-by-side-printer-tests.ts @@ -24,6 +24,45 @@ describe('SideBySideRenderer', () => { }); }); + describe('_generateTooBigDiff', () => { + it('should return a diff with default "too big" message when no `diffTooBigMessage` is defined', () => { + const hoganUtils = new HoganJsUtils({}); + const sideBySideRenderer = new SideBySideRenderer(hoganUtils, {}); + const fileHtml = sideBySideRenderer.generateTooBigDiff(0); + expect(fileHtml).toMatchInlineSnapshot(` + Object { + "left": " + +
+ Diff too big to be displayed +
+ + ", + "right": "", + } + `); + }); + + it('should return a diff with custom "too big" message when `diffTooBigMessage` is defined', () => { + const hoganUtils = new HoganJsUtils({}); + const customMessageFn = (fIndex: number) => `Custom message for file ${fIndex} diff too big`; + const sideBySideRenderer = new SideBySideRenderer(hoganUtils, { diffTooBigMessage: customMessageFn }); + const fileHtml = sideBySideRenderer.generateTooBigDiff(0); + expect(fileHtml).toMatchInlineSnapshot(` + Object { + "left": " + +
+ Custom message for file 0 diff too big +
+ + ", + "right": "", + } + `); + }); + }); + describe('generateSideBySideFileHtml', () => { it('should generate lines with the right prefixes', () => { const hoganUtils = new HoganJsUtils({}); @@ -406,6 +445,133 @@ describe('SideBySideRenderer', () => { " `); }); + + it('should work for too big file diff and no custom message fn', () => { + const exampleJson = [ + { + blocks: [], + deletedLines: 0, + addedLines: 0, + oldName: 'sample', + language: 'js', + newName: 'sample', + isCombined: false, + isGitDiff: false, + isTooBig: true, + }, + ]; + + const hoganUtils = new HoganJsUtils({}); + const sideBySideRenderer = new SideBySideRenderer(hoganUtils); + const html = sideBySideRenderer.render(exampleJson); + expect(html).toMatchInlineSnapshot(` + "
+
+
+ + + + sample + CHANGED + +
+
+
+
+ + + + + + +
+
+ Diff too big to be displayed +
+
+
+
+
+
+ + + + +
+
+
+
+
+
" + `); + }); + + it('should work for too big file diff and custom message fn', () => { + const exampleJson = [ + { + blocks: [], + deletedLines: 0, + addedLines: 0, + oldName: 'sample', + language: 'js', + newName: 'sample', + isCombined: false, + isGitDiff: false, + isTooBig: true, + }, + ]; + + const hoganUtils = new HoganJsUtils({}); + const customMessageFn = (fIndex: number) => `Custom message for file ${fIndex} diff too big`; + const sideBySideRenderer = new SideBySideRenderer(hoganUtils, { diffTooBigMessage: customMessageFn }); + const html = sideBySideRenderer.render(exampleJson); + expect(html).toMatchInlineSnapshot(` + "
+
+
+ + + + sample + CHANGED + +
+
+
+
+ + + + + + +
+
+ Custom message for file 0 diff too big +
+
+
+
+
+
+ + + + +
+
+
+
+
+
" + `); + }); }); describe('processLines', () => { diff --git a/src/line-by-line-renderer.ts b/src/line-by-line-renderer.ts index 969782a..79b9798 100644 --- a/src/line-by-line-renderer.ts +++ b/src/line-by-line-renderer.ts @@ -41,9 +41,11 @@ export default class LineByLineRenderer { render(diffFiles: DiffFile[]): string { const diffsHtml = diffFiles - .map(file => { + .map((file, fileIndex) => { let diffs; - if (file.blocks.length) { + if (file.isTooBig) { + diffs = this.generateTooBigDiff(fileIndex); + } else if (file.blocks.length) { diffs = this.generateFileHtml(file); } else { diffs = this.generateEmptyDiff(); @@ -79,6 +81,14 @@ export default class LineByLineRenderer { }); } + generateTooBigDiff(fileIndex: number): string { + return this.hoganUtils.render(genericTemplatesPath, 'too-big-diff', { + contentClass: 'd2h-code-line', + CSSLineClass: renderUtils.CSSLineClass, + message: this.config.diffTooBigMessage(fileIndex), + }); + } + generateEmptyDiff(): string { return this.hoganUtils.render(genericTemplatesPath, 'empty-diff', { contentClass: 'd2h-code-line', diff --git a/src/render-utils.ts b/src/render-utils.ts index 830628e..957ca1f 100644 --- a/src/render-utils.ts +++ b/src/render-utils.ts @@ -36,6 +36,7 @@ export interface RenderConfig { matching?: LineMatchingType; matchWordsThreshold?: number; maxLineLengthHighlight?: number; + diffTooBigMessage?: (fileIndex: number) => string; diffStyle?: DiffStyleType; } @@ -44,6 +45,8 @@ export const defaultRenderConfig = { matchWordsThreshold: 0.25, maxLineLengthHighlight: 10000, diffStyle: DiffStyleType.WORD, + // eslint-disable-next-line + diffTooBigMessage: (_fileIndex: number): string => 'Diff too big to be displayed', }; const separator = '/'; diff --git a/src/side-by-side-renderer.ts b/src/side-by-side-renderer.ts index e38f7d0..948e14d 100644 --- a/src/side-by-side-renderer.ts +++ b/src/side-by-side-renderer.ts @@ -41,9 +41,11 @@ export default class SideBySideRenderer { render(diffFiles: DiffFile[]): string { const diffsHtml = diffFiles - .map(file => { + .map((file, fileIndex) => { let diffs; - if (file.blocks.length) { + if (file.isTooBig) { + diffs = this.generateTooBigDiff(fileIndex); + } else if (file.blocks.length) { diffs = this.generateFileHtml(file); } else { diffs = this.generateEmptyDiff(); @@ -79,6 +81,17 @@ export default class SideBySideRenderer { }); } + generateTooBigDiff(fileIndex: number): FileHtml { + return { + right: '', + left: this.hoganUtils.render(genericTemplatesPath, 'too-big-diff', { + contentClass: 'd2h-code-side-line', + CSSLineClass: renderUtils.CSSLineClass, + message: this.config.diffTooBigMessage(fileIndex), + }), + }; + } + generateEmptyDiff(): FileHtml { return { right: '', diff --git a/src/templates/generic-too-big-diff.mustache b/src/templates/generic-too-big-diff.mustache new file mode 100644 index 0000000..7210a13 --- /dev/null +++ b/src/templates/generic-too-big-diff.mustache @@ -0,0 +1,7 @@ + + +
+ {{{message}}} +
+ +