2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-03-12 13:52:17 +01:00
|
|
|
import { FileChangeType, Hunk, FileDiff } from "@scm-manager/ui-types";
|
2020-09-02 07:40:52 +02:00
|
|
|
import { getPath, createHunkIdentifier, createHunkIdentifierFromContext, escapeWhitespace } from "./diffs";
|
2019-10-19 16:38:07 +02:00
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
describe("tests for diff util functions", () => {
|
2021-03-12 13:52:17 +01:00
|
|
|
const file = (type: FileChangeType, oldPath: string, newPath: string): FileDiff => {
|
2019-10-19 16:38:07 +02:00
|
|
|
return {
|
|
|
|
|
hunks: [],
|
|
|
|
|
type: type,
|
|
|
|
|
oldPath,
|
|
|
|
|
newPath,
|
|
|
|
|
newEndingNewLine: true,
|
2019-10-20 16:59:02 +02:00
|
|
|
oldEndingNewLine: true
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const add = (path: string) => {
|
2019-10-20 16:59:02 +02:00
|
|
|
return file("add", "/dev/null", path);
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const rm = (path: string) => {
|
2019-10-20 16:59:02 +02:00
|
|
|
return file("delete", path, "/dev/null");
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const modify = (path: string) => {
|
2019-10-20 16:59:02 +02:00
|
|
|
return file("modify", path, path);
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const createHunk = (content: string): Hunk => {
|
|
|
|
|
return {
|
|
|
|
|
content,
|
2019-10-20 16:59:02 +02:00
|
|
|
changes: []
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
describe("getPath tests", () => {
|
|
|
|
|
it("should pick the new path, for type add", () => {
|
|
|
|
|
const file = add("/etc/passwd");
|
2019-10-19 16:38:07 +02:00
|
|
|
const path = getPath(file);
|
2019-10-20 16:59:02 +02:00
|
|
|
expect(path).toBe("/etc/passwd");
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
it("should pick the old path, for type delete", () => {
|
|
|
|
|
const file = rm("/etc/passwd");
|
2019-10-19 16:38:07 +02:00
|
|
|
const path = getPath(file);
|
2019-10-20 16:59:02 +02:00
|
|
|
expect(path).toBe("/etc/passwd");
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
describe("createHunkIdentifier tests", () => {
|
|
|
|
|
it("should create identifier", () => {
|
|
|
|
|
const file = modify("/etc/passwd");
|
|
|
|
|
const hunk = createHunk("@@ -1,18 +1,15 @@");
|
2019-10-19 16:38:07 +02:00
|
|
|
const identifier = createHunkIdentifier(file, hunk);
|
2019-10-20 16:59:02 +02:00
|
|
|
expect(identifier).toBe("modify_/etc/passwd_@@ -1,18 +1,15 @@");
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
describe("createHunkIdentifierFromContext tests", () => {
|
|
|
|
|
it("should create identifier", () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const identifier = createHunkIdentifierFromContext({
|
2019-10-20 16:59:02 +02:00
|
|
|
file: rm("/etc/passwd"),
|
|
|
|
|
hunk: createHunk("@@ -1,42 +1,39 @@")
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
2019-10-20 16:59:02 +02:00
|
|
|
expect(identifier).toBe("delete_/etc/passwd_@@ -1,42 +1,39 @@");
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
2020-09-02 07:40:52 +02:00
|
|
|
|
|
|
|
|
describe("escapeWhitespace tests", () => {
|
|
|
|
|
it("should escape whitespaces", () => {
|
|
|
|
|
const escaped = escapeWhitespace("spaceship hog");
|
|
|
|
|
expect(escaped).toBe("spaceship-hog");
|
|
|
|
|
});
|
|
|
|
|
it("should escape multiple whitespaces", () => {
|
|
|
|
|
const escaped = escapeWhitespace("spaceship heart of gold");
|
|
|
|
|
expect(escaped).toBe("spaceship-heart-of-gold");
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|