aboutsummaryrefslogtreecommitdiff
path: root/test/unit/patch.unit.test.ts
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-07-27 16:51:54 +0200
committerHampusM <hampus@hampusmat.com>2021-07-27 16:51:54 +0200
commit055a58e79fb225978d64a3c8e3e25377cc2a5ece (patch)
tree8c37c249b93dfeb507afbf6b156955c96ad3bd49 /test/unit/patch.unit.test.ts
parentfa44d81b658024685e0496150d66a51f2f5fda8c (diff)
Added new unit tests & the test setup script uses Nodegit instead of exec
Diffstat (limited to 'test/unit/patch.unit.test.ts')
-rw-r--r--test/unit/patch.unit.test.ts104
1 files changed, 104 insertions, 0 deletions
diff --git a/test/unit/patch.unit.test.ts b/test/unit/patch.unit.test.ts
new file mode 100644
index 0000000..e4aa8d3
--- /dev/null
+++ b/test/unit/patch.unit.test.ts
@@ -0,0 +1,104 @@
+import { Commit } from "../../packages/server/src/git/commit";
+import { Patch } from "../../packages/server/src/git/patch";
+import { Repository } from "../../packages/server/src/git/repository";
+import { EnvironmentVariables } from "../util";
+
+const env = process.env as EnvironmentVariables;
+
+describe("Patch", () => {
+ describe("Class methods", () => {
+ it("Should get a patch from a diff", async() => {
+ expect.assertions(2);
+
+ const repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO);
+ const commit = await Commit.lookup(repository, "d856031c58e26992f3e0a481084a190a50b0bcf7");
+
+ const patch = await Patch.fromDiff(await commit.diff(), 1);
+
+ expect(patch).toBeDefined();
+ expect(patch).toBeInstanceOf(Patch);
+ });
+
+ it("Should get all patches from a diff", async() => {
+ expect.hasAssertions();
+
+ const repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO);
+ const commit = await Commit.lookup(repository, "7b3292af22a0496007e974b65cd2e34521c9c429");
+
+ const patches = await Patch.allFromDiff(await commit.diff());
+
+ expect(patches).toBeDefined();
+ expect(patches).toHaveLength(9);
+
+ for(const patch of patches) {
+ expect(patch).toBeDefined();
+ expect(patch).toBeInstanceOf(Patch);
+ }
+ });
+ });
+
+ describe("Instance methods", () => {
+ let repository: Repository;
+ let patch: Patch;
+
+ beforeAll(async() => {
+ repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO);
+ const commit = await Commit.lookup(repository, "7b3292af22a0496007e974b65cd2e34521c9c429");
+
+ patch = await Patch.fromDiff(await commit.diff(), 4);
+ });
+
+ it("Should get if the patch is too large and respond false", async() => {
+ expect.assertions(2);
+
+ const too_large = await patch.isTooLarge();
+
+ expect(too_large).toBeDefined();
+ expect(too_large).toBeFalsy();
+ });
+
+ it("Should get if a huge patch is too large and respond true", async() => {
+ expect.assertions(2);
+
+ const other_commit = await Commit.lookup(repository, "8645568c6c96300c1b7709c3a0d674c120d88a13");
+ const other_patch = await Patch.fromDiff(await other_commit.diff(), 2);
+
+ const too_large = await other_patch.isTooLarge();
+
+ expect(too_large).toBeDefined();
+ expect(too_large).toBeTruthy();
+ });
+
+ it("Should get the hunks", async() => {
+ expect.hasAssertions();
+
+ const hunks = await patch.getHunks();
+
+ expect(hunks).toBeDefined();
+ expect(hunks).toHaveLength(5);
+
+ for(const hunk of hunks) {
+ expect(hunk).toBeDefined();
+ expect(hunk).toHaveProperty("new_start");
+ expect(hunk).toHaveProperty("new_lines_cnt");
+ expect(hunk).toHaveProperty("old_start");
+ expect(hunk).toHaveProperty("old_lines_cnt");
+ expect(hunk).toHaveProperty("new_lines");
+ expect(hunk).toHaveProperty("deleted_lines");
+ expect(hunk).toHaveProperty("hunk");
+ }
+ });
+
+ it("Should get the hunks of an empty patch and respond with null", async() => {
+ expect.assertions(2);
+
+ const other_commit = await Commit.lookup(repository, "ef256e9e40b5fd0cc741c509e611808cc66bafad");
+ const other_patch = await Patch.fromDiff(await other_commit.diff(), 10);
+
+ const hunks = await other_patch.getHunks();
+
+ expect(hunks).toBeDefined();
+ expect(hunks).toBeNull();
+ });
+ });
+}); \ No newline at end of file