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

Render 'No newline at end of file' at the end of files #556

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions src/__tests__/diff2html-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,26 +157,32 @@ describe('Diff2Html', () => {
"type": "delete",
},
{
"content": "+ TokenRevoked, MissingToken,",
"content": "\\ No newline at end of file",
"newNumber": 53,
"oldNumber": 55,
"type": "context",
},
{
"content": "+ TokenRevoked, MissingToken,",
"newNumber": 54,
"oldNumber": undefined,
"type": "insert",
},
{
"content": "+ IndexLock, RepositoryError, NotValidRepo, PullRequestNotMergeable, BranchError,",
"newNumber": 54,
"newNumber": 55,
"oldNumber": undefined,
"type": "insert",
},
{
"content": "+ PluginError, CodeParserError, EngineError = Value",
"newNumber": 55,
"newNumber": 56,
"oldNumber": undefined,
"type": "insert",
},
{
"content": "+}",
"newNumber": 56,
"newNumber": 57,
"oldNumber": undefined,
"type": "insert",
},
Expand Down
351 changes: 351 additions & 0 deletions src/__tests__/side-by-side-printer-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,357 @@ describe('SideBySideRenderer', () => {
`);
});

it('should handle files without newlines at the end', () => {
const exampleJson: DiffFile[] = [
{
blocks: [
// Scenario 1: Old file missing newline, new file has newline
{
lines: [
{
content: '-oldLine1',
type: LineType.DELETE,
oldNumber: 1,
newNumber: undefined,
},
{
content: '\\ No newline at end of file',
type: LineType.CONTEXT,
oldNumber: 1,
newNumber: 1,
},
{
content: '+newLine1',
type: LineType.INSERT,
oldNumber: undefined,
newNumber: 1,
},
],
oldStartLine: 1,
newStartLine: 1,
header: '@@ -1 +1 @@',
},
// Scenario 2: Old file has newline, new file missing newline
{
lines: [
{
content: '-oldLine2',
type: LineType.DELETE,
oldNumber: 2,
newNumber: undefined,
},
{
content: '+newLine2',
type: LineType.INSERT,
oldNumber: undefined,
newNumber: 2,
},
{
content: '\\ No newline at end of file',
type: LineType.CONTEXT,
oldNumber: 2,
newNumber: 2,
},
],
oldStartLine: 2,
newStartLine: 2,
header: '@@ -2 +2 @@',
},
// Scenario 3: Both files missing newline
{
lines: [
{
content: '-oldLine3',
type: LineType.DELETE,
oldNumber: 3,
newNumber: undefined,
},
{
content: '\\ No newline at end of file',
type: LineType.CONTEXT,
oldNumber: 3,
newNumber: 3,
},
{
content: '+newLine3',
type: LineType.INSERT,
oldNumber: undefined,
newNumber: 3,
},
{
content: '\\ No newline at end of file',
type: LineType.CONTEXT,
oldNumber: 3,
newNumber: 3,
},
],
oldStartLine: 3,
newStartLine: 3,
header: '@@ -3 +3 @@',
},
],
deletedLines: 3,
addedLines: 3,
oldName: 'sample',
language: 'txt',
newName: 'sample',
isCombined: false,
isGitDiff: true,
},
];

const hoganUtils = new HoganJsUtils({});
const sideBySideRenderer = new SideBySideRenderer(hoganUtils, {});
const html = sideBySideRenderer.render(exampleJson);
expect(html).toMatchInlineSnapshot(`
"<div class="d2h-wrapper d2h-light-color-scheme">
<div id="d2h-675094" class="d2h-file-wrapper" data-lang="txt">
<div class="d2h-file-header">
<span class="d2h-file-name-wrapper">
<svg aria-hidden="true" class="d2h-icon" height="16" version="1.1" viewBox="0 0 12 16" width="12">
<path d="M6 5H2v-1h4v1zM2 8h7v-1H2v1z m0 2h7v-1H2v1z m0 2h7v-1H2v1z m10-7.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V2c0-0.55 0.45-1 1-1h7.5l3.5 3.5z m-1 0.5L8 2H1v12h10V5z"></path>
</svg> <span class="d2h-file-name">sample</span>
<span class="d2h-tag d2h-changed d2h-changed-tag">CHANGED</span></span>
<label class="d2h-file-collapse">
<input class="d2h-file-collapse-input" type="checkbox" name="viewed" value="viewed">
Viewed
</label>
</div>
<div class="d2h-files-diff">
<div class="d2h-file-side-diff">
<div class="d2h-code-wrapper">
<table class="d2h-diff-table">
<tbody class="d2h-diff-tbody">
<tr>
<td class="d2h-code-side-linenumber d2h-info"></td>
<td class="d2h-info">
<div class="d2h-code-side-line">@@ -1 +1 @@</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-del">
1
</td>
<td class="d2h-del">
<div class="d2h-code-side-line">
<span class="d2h-code-line-prefix">-</span>
<span class="d2h-code-line-ctn">oldLine1</span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-cntx">
1
</td>
<td class="d2h-cntx">
<div class="d2h-code-side-line">
<span class="d2h-code-line-prefix">\\</span>
<span class="d2h-code-line-ctn"> No newline at end of file</span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-code-side-emptyplaceholder d2h-cntx d2h-emptyplaceholder">

</td>
<td class="d2h-cntx d2h-emptyplaceholder">
<div class="d2h-code-side-line d2h-code-side-emptyplaceholder">
<span class="d2h-code-line-prefix">&nbsp;</span>
<span class="d2h-code-line-ctn"><br></span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-info"></td>
<td class="d2h-info">
<div class="d2h-code-side-line">@@ -2 +2 @@</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-del d2h-change">
2
</td>
<td class="d2h-del d2h-change">
<div class="d2h-code-side-line">
<span class="d2h-code-line-prefix">-</span>
<span class="d2h-code-line-ctn"><del>oldLine2</del></span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-code-side-emptyplaceholder d2h-cntx d2h-emptyplaceholder">

</td>
<td class="d2h-cntx d2h-emptyplaceholder">
<div class="d2h-code-side-line d2h-code-side-emptyplaceholder">
<span class="d2h-code-line-prefix">&nbsp;</span>
<span class="d2h-code-line-ctn"><br></span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-info"></td>
<td class="d2h-info">
<div class="d2h-code-side-line">@@ -3 +3 @@</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-del">
3
</td>
<td class="d2h-del">
<div class="d2h-code-side-line">
<span class="d2h-code-line-prefix">-</span>
<span class="d2h-code-line-ctn">oldLine3</span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-cntx">
3
</td>
<td class="d2h-cntx">
<div class="d2h-code-side-line">
<span class="d2h-code-line-prefix">\\</span>
<span class="d2h-code-line-ctn"> No newline at end of file</span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-code-side-emptyplaceholder d2h-cntx d2h-emptyplaceholder">

</td>
<td class="d2h-cntx d2h-emptyplaceholder">
<div class="d2h-code-side-line d2h-code-side-emptyplaceholder">
<span class="d2h-code-line-prefix">&nbsp;</span>
<span class="d2h-code-line-ctn"><br></span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-code-side-emptyplaceholder d2h-cntx d2h-emptyplaceholder">

</td>
<td class="d2h-cntx d2h-emptyplaceholder">
<div class="d2h-code-side-line d2h-code-side-emptyplaceholder">
<span class="d2h-code-line-prefix">&nbsp;</span>
<span class="d2h-code-line-ctn"><br></span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="d2h-file-side-diff">
<div class="d2h-code-wrapper">
<table class="d2h-diff-table">
<tbody class="d2h-diff-tbody">
<tr>
<td class="d2h-code-side-linenumber d2h-info"></td>
<td class="d2h-info">
<div class="d2h-code-side-line">&nbsp;</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-code-side-emptyplaceholder d2h-cntx d2h-emptyplaceholder">

</td>
<td class="d2h-cntx d2h-emptyplaceholder">
<div class="d2h-code-side-line d2h-code-side-emptyplaceholder">
<span class="d2h-code-line-prefix">&nbsp;</span>
<span class="d2h-code-line-ctn"><br></span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-code-side-emptyplaceholder d2h-cntx d2h-emptyplaceholder">

</td>
<td class="d2h-cntx d2h-emptyplaceholder">
<div class="d2h-code-side-line d2h-code-side-emptyplaceholder">
<span class="d2h-code-line-prefix">&nbsp;</span>
<span class="d2h-code-line-ctn"><br></span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-ins">
1
</td>
<td class="d2h-ins">
<div class="d2h-code-side-line">
<span class="d2h-code-line-prefix">+</span>
<span class="d2h-code-line-ctn">newLine1</span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-info"></td>
<td class="d2h-info">
<div class="d2h-code-side-line">&nbsp;</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-ins d2h-change">
2
</td>
<td class="d2h-ins d2h-change">
<div class="d2h-code-side-line">
<span class="d2h-code-line-prefix">+</span>
<span class="d2h-code-line-ctn"><ins>newLine2</ins></span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-cntx">
2
</td>
<td class="d2h-cntx">
<div class="d2h-code-side-line">
<span class="d2h-code-line-prefix">\\</span>
<span class="d2h-code-line-ctn"> No newline at end of file</span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-info"></td>
<td class="d2h-info">
<div class="d2h-code-side-line">&nbsp;</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-code-side-emptyplaceholder d2h-cntx d2h-emptyplaceholder">

</td>
<td class="d2h-cntx d2h-emptyplaceholder">
<div class="d2h-code-side-line d2h-code-side-emptyplaceholder">
<span class="d2h-code-line-prefix">&nbsp;</span>
<span class="d2h-code-line-ctn"><br></span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-code-side-emptyplaceholder d2h-cntx d2h-emptyplaceholder">

</td>
<td class="d2h-cntx d2h-emptyplaceholder">
<div class="d2h-code-side-line d2h-code-side-emptyplaceholder">
<span class="d2h-code-line-prefix">&nbsp;</span>
<span class="d2h-code-line-ctn"><br></span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-ins">
3
</td>
<td class="d2h-ins">
<div class="d2h-code-side-line">
<span class="d2h-code-line-prefix">+</span>
<span class="d2h-code-line-ctn">newLine3</span>
</div>
</td>
</tr><tr>
<td class="d2h-code-side-linenumber d2h-cntx">
3
</td>
<td class="d2h-cntx">
<div class="d2h-code-side-line">
<span class="d2h-code-line-prefix">\\</span>
<span class="d2h-code-line-ctn"> No newline at end of file</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>"
`);
});

it('should work for too big file diff', () => {
const exampleJson = [
{
Expand Down
Loading