diff --git a/scm-ui/ui-components/src/repos/DiffExpander.test.ts b/scm-ui/ui-components/src/repos/DiffExpander.test.ts index 3da9c69c76..68ee58f112 100644 --- a/scm-ui/ui-components/src/repos/DiffExpander.test.ts +++ b/scm-ui/ui-components/src/repos/DiffExpander.test.ts @@ -281,7 +281,7 @@ const HUNK_3 = { } ] }; -const TEST_CONTENT = { +const TEST_CONTENT_WITH_HUNKS = { oldPath: "src/main/js/CommitMessage.js", newPath: "src/main/js/CommitMessage.js", oldEndingNewLine: true, @@ -300,8 +300,67 @@ const TEST_CONTENT = { } }; -describe("diff expander", () => { - const diffExpander = new DiffExpander(TEST_CONTENT); +const TEST_CONTENT_WIT_NEW_BINARY_FILE = { + oldPath: "/dev/null", + newPath: "src/main/fileUploadV2.png", + oldEndingNewLine: true, + newEndingNewLine: true, + oldRevision: "0000000000000000000000000000000000000000", + newRevision: "86c370aae0727d628a5438f79a5cdd45752b9d99", + type: "add" +}; + +const TEST_CONTENT_WITH_NEW_TEXT_FILE = { + oldPath: "/dev/null", + newPath: "src/main/markdown/README.md", + oldEndingNewLine: true, + newEndingNewLine: true, + oldRevision: "0000000000000000000000000000000000000000", + newRevision: "4e173d365d796b9a9e7562fcd0ef90398ae37046", + type: "add", + language: "markdown", + hunks: [ + { + content: "@@ -0,0 +1,2 @@", + newStart: 1, + newLines: 2, + changes: [ + { content: "line 1", type: "insert", lineNumber: 1, isInsert: true }, + { content: "line 2", type: "insert", lineNumber: 2, isInsert: true } + ] + } + ], + _links: { + lines: { + href: + "http://localhost:8081/scm/api/v2/repositories/scm-manager/scm-editor-plugin/content/c63898d35520ee47bcc3a8291660979918715762/src/main/markdown/README.md?start={start}?end={end}", + templated: true + } + } +}; + +const TEST_CONTENT_WITH_DELETED_TEXT_FILE = { + oldPath: "README.md", + newPath: "/dev/null", + oldEndingNewLine: true, + newEndingNewLine: true, + oldRevision: "4875ab3b7a1bb117e1948895148557fc5c0b6f75", + newRevision: "0000000000000000000000000000000000000000", + type: "delete", + language: "markdown", + hunks: [ + { + content: "@@ -1 +0,0 @@", + oldStart: 1, + oldLines: 1, + changes: [{ content: "# scm-editor-plugin", type: "delete", lineNumber: 1, isDelete: true }] + } + ], + _links: { lines: { href: "http://localhost:8081/dev/null?start={start}?end={end}", templated: true } } +}; + +describe("with hunks the diff expander", () => { + const diffExpander = new DiffExpander(TEST_CONTENT_WITH_HUNKS); it("should have hunk count from origin", () => { expect(diffExpander.hunkCount()).toBe(4); }); @@ -326,3 +385,34 @@ describe("diff expander", () => { expect(diffExpander.getHunk(3).maxExpandBottomRange).toBeGreaterThan(99999); }); }); + +describe("for a new file with text input the diff expander", () => { + const diffExpander = new DiffExpander(TEST_CONTENT_WITH_NEW_TEXT_FILE); + it("should create answer for single hunk", () => { + expect(diffExpander.hunkCount()).toBe(1); + }); + it("should neither give expandable lines for top nor bottom", () => { + const hunk = diffExpander.getHunk(0); + expect(hunk.maxExpandHeadRange).toBe(0); + expect(hunk.maxExpandBottomRange).toBe(0); + }); +}); + +describe("for a deleted file with text input the diff expander", () => { + const diffExpander = new DiffExpander(TEST_CONTENT_WITH_DELETED_TEXT_FILE); + it("should create answer for single hunk", () => { + expect(diffExpander.hunkCount()).toBe(1); + }); + it("should neither give expandable lines for top nor bottom", () => { + const hunk = diffExpander.getHunk(0); + expect(hunk.maxExpandHeadRange).toBe(0); + expect(hunk.maxExpandBottomRange).toBe(0); + }); +}); + +describe("for a new file with binary input the diff expander", () => { + const diffExpander = new DiffExpander(TEST_CONTENT_WIT_NEW_BINARY_FILE); + it("should create answer for no hunk", () => { + expect(diffExpander.hunkCount()).toBe(0); + }); +}); diff --git a/scm-ui/ui-components/src/repos/DiffExpander.ts b/scm-ui/ui-components/src/repos/DiffExpander.ts index 2ef1f74a80..f5fd72ca98 100644 --- a/scm-ui/ui-components/src/repos/DiffExpander.ts +++ b/scm-ui/ui-components/src/repos/DiffExpander.ts @@ -32,26 +32,34 @@ class DiffExpander { } hunkCount = () => { - return this.file.hunks.length; + if (this.file.hunks) { + return this.file.hunks.length; + } else { + return 0; + } }; - minLineNumber = (n: number) => { - return this.file.hunks[n].newStart; + minLineNumber: (n: number) => number = (n: number) => { + return this.file.hunks![n]!.newStart!; }; - maxLineNumber = (n: number) => { - return this.file.hunks[n].newStart + this.file.hunks[n].newLines; + maxLineNumber: (n: number) => number = (n: number) => { + return this.file.hunks![n]!.newStart! + this.file.hunks![n]!.newLines!; }; computeMaxExpandHeadRange = (n: number) => { - if (n === 0) { + if (this.file.type === "delete") { + return 0; + } else if (n === 0) { return this.minLineNumber(n) - 1; } return this.minLineNumber(n) - this.maxLineNumber(n - 1); }; computeMaxExpandBottomRange = (n: number) => { - if (n === this.file.hunks.length - 1) { + if (this.file.type === "add" || this.file.type === "delete") { + return 0; + } else if (n === this.file!.hunks!.length - 1) { return Number.MAX_SAFE_INTEGER; } return this.minLineNumber(n + 1) - this.maxLineNumber(n); @@ -67,7 +75,7 @@ class DiffExpander { expandBottom: () => { return this; }, - hunk: this.file.hunks[n] + hunk: this.file?.hunks![n] }; }; } diff --git a/scm-ui/ui-components/src/repos/DiffFile.tsx b/scm-ui/ui-components/src/repos/DiffFile.tsx index 04bd541728..1c75191bb6 100644 --- a/scm-ui/ui-components/src/repos/DiffFile.tsx +++ b/scm-ui/ui-components/src/repos/DiffFile.tsx @@ -299,7 +299,7 @@ class DiffFile extends React.Component {
{fileAnnotations} - {(hunks: HunkType[]) => hunks.map((hunk, n) => this.renderHunk(file, diffExpander.getHunk(n), n))} + {(hunks: HunkType[]) => hunks?.map((hunk, n) => this.renderHunk(file, diffExpander.getHunk(n), n))}
); diff --git a/scm-ui/ui-components/src/repos/DiffTypes.ts b/scm-ui/ui-components/src/repos/DiffTypes.ts index aba76d422d..56d225d85f 100644 --- a/scm-ui/ui-components/src/repos/DiffTypes.ts +++ b/scm-ui/ui-components/src/repos/DiffTypes.ts @@ -34,7 +34,7 @@ import { Links } from "@scm-manager/ui-types"; export type FileChangeType = "add" | "modify" | "delete" | "copy" | "rename"; export type File = { - hunks: Hunk[]; + hunks?: Hunk[]; newEndingNewLine: boolean; newMode?: string; newPath: string; @@ -53,10 +53,10 @@ export type File = { export type Hunk = { changes: Change[]; content: string; - oldStart: number; - newStart: number; - oldLines: number; - newLines: number; + oldStart?: number; + newStart?: number; + oldLines?: number; + newLines?: number; }; export type ChangeType = "insert" | "delete" | "normal" | "conflict";