From 055a58e79fb225978d64a3c8e3e25377cc2a5ece Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 27 Jul 2021 16:51:54 +0200 Subject: Added new unit tests & the test setup script uses Nodegit instead of exec --- test/setup.ts | 21 ++++---- test/unit/branch.unit.test.ts | 60 ++++++++++++++++++++++ test/unit/patch.unit.test.ts | 104 ++++++++++++++++++++++++++++++++++++++ test/unit/repository.unit.test.ts | 33 ++++++++++++ test/unit/tag.unit.test.ts | 52 +++++++++++++++++++ 5 files changed, 258 insertions(+), 12 deletions(-) create mode 100644 test/unit/branch.unit.test.ts create mode 100644 test/unit/patch.unit.test.ts create mode 100644 test/unit/tag.unit.test.ts (limited to 'test') diff --git a/test/setup.ts b/test/setup.ts index 89eeead..d446296 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -1,10 +1,8 @@ -import { access, mkdir, remove } from "fs-extra"; -import { promisify } from "util"; -import { exec } from "child_process"; +import { access, mkdir, remove, writeFile } from "fs-extra"; import { config } from "dotenv"; import { EnvironmentVariables } from "./util"; +import { Clone } from "nodegit"; -const promiseExec = promisify(exec); config({ path: "test/test.env" }); const env = process.env as EnvironmentVariables; @@ -20,15 +18,14 @@ export default async function(): Promise { await mkdir(env.BASE_DIR); - const git_clone = await promiseExec(`git clone -q --bare ${env.AVAIL_REPO_URL} ${env.BASE_DIR}/${env.AVAIL_REPO}`); + const repository = await Clone.clone(env.AVAIL_REPO_URL, `${env.BASE_DIR}/${env.AVAIL_REPO}`, { bare: 1 }); - if(git_clone.stderr) { - throw(git_clone.stderr); - } + const config = await repository.config(); + await config.setString("user.name", "BobDylan"); + await config.setString("user.email", "bob@example.com"); - const git_fetch = await promiseExec(`git -C ${env.BASE_DIR}/${env.AVAIL_REPO} fetch -q --all`); + await repository.fetchAll(); + await repository.createTag((await repository.getMasterCommit()).id(), "1.2", "Fixed stuff"); - if(git_fetch.stderr) { - throw(git_fetch.stderr); - } + await writeFile(`${env.BASE_DIR}/${env.AVAIL_REPO}/owner`, "Bob"); } \ No newline at end of file diff --git a/test/unit/branch.unit.test.ts b/test/unit/branch.unit.test.ts new file mode 100644 index 0000000..3f067a8 --- /dev/null +++ b/test/unit/branch.unit.test.ts @@ -0,0 +1,60 @@ +import { Branch } from "../../packages/server/src/git/branch"; +import { Repository } from "../../packages/server/src/git/repository"; +import { EnvironmentVariables } from "../util"; + +const env = process.env as EnvironmentVariables; + +describe("Branch", () => { + describe("Class methods", () => { + it("Should lookup a branch", async() => { + expect.assertions(2); + + const repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO); + const branch = await Branch.lookup(repository, "master"); + + expect(branch).toBeDefined(); + expect(branch).toBeInstanceOf(Branch); + }); + + it("Should lookup if an existent branch exists and respond true", async() => { + expect.assertions(2); + + const repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO); + const branch_exists = await Branch.lookupExists(repository.ng_repository, "master"); + + expect(branch_exists).toBeDefined(); + expect(branch_exists).toBeTruthy(); + }); + + it("Should lookup if an nonexistent branch exists and respond false", async() => { + expect.assertions(2); + + const repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO); + const branch_exists = await Branch.lookupExists(repository.ng_repository, "wubbalubbadubdub"); + + expect(branch_exists).toBeDefined(); + expect(branch_exists).toBeFalsy(); + }); + }); + + describe("Instance methods", () => { + let branch: Branch; + + beforeAll(async() => { + const repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO); + + branch = await Branch.lookup(repository, "master"); + }); + + it("Should get the latest commit", async() => { + expect.assertions(4); + + const latest_commit = await branch.latestCommit(); + + expect(latest_commit).toBeDefined(); + expect(latest_commit).toHaveProperty("id"); + expect(latest_commit).toHaveProperty("message"); + expect(latest_commit).toHaveProperty("date"); + }); + }); +}); \ No newline at end of file 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 diff --git a/test/unit/repository.unit.test.ts b/test/unit/repository.unit.test.ts index 7d3f954..dfb158d 100644 --- a/test/unit/repository.unit.test.ts +++ b/test/unit/repository.unit.test.ts @@ -35,6 +35,12 @@ describe("Repository", () => { await expect(Repository.open(env.BASE_DIR, env.UNAVAIL_REPO)).rejects.toBeInstanceOf(BaseError); }); + it("Should fail to open a repository with a nonexistant branch", async() => { + expect.assertions(1); + + await expect(Repository.open(env.BASE_DIR, env.AVAIL_REPO, "wubbalubbadubdub")).rejects.toBeInstanceOf(BaseError); + }); + it("Should open all repositories", async() => { expect.hasAssertions(); @@ -57,6 +63,33 @@ describe("Repository", () => { repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO); }); + it("Should get the description", async() => { + expect.assertions(2); + + const description = await repository.description(); + + expect(description).toBeDefined(); + expect(description.length).toBeGreaterThan(1); + }); + + it("Should get the owner", async() => { + expect.assertions(2); + + const owner = await repository.owner(); + + expect(owner).toBeDefined(); + expect(owner).toEqual("Bob"); + }); + + it("Should get the branch", async() => { + expect.assertions(2); + + const branch = await repository.branch(); + + expect(branch).toBeDefined(); + expect(branch).toBeInstanceOf(Branch); + }); + it("Should look up if an existent object exists and respond true", async() => { expect.assertions(1); diff --git a/test/unit/tag.unit.test.ts b/test/unit/tag.unit.test.ts new file mode 100644 index 0000000..8321092 --- /dev/null +++ b/test/unit/tag.unit.test.ts @@ -0,0 +1,52 @@ +import { Repository } from "../../packages/server/src/git/repository"; +import { Tag } from "../../packages/server/src/git/tag"; +import { EnvironmentVariables } from "../util"; + +const env = process.env as EnvironmentVariables; + +describe("Tag", () => { + describe("Class methods", () => { + it("Should lookup a tag", async() => { + expect.assertions(2); + + const repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO); + + const tag = await Tag.lookup(repository, "1.2"); + + expect(tag).toBeDefined(); + expect(tag).toBeInstanceOf(Tag); + }); + }); + + describe("Instance methods", () => { + let tag: Tag; + + beforeAll(async() => { + const repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO); + tag = await Tag.lookup(repository, "1.2"); + }); + + it("Should get the author", async() => { + expect.assertions(5); + + const author = await tag.author(); + + expect(author).toBeDefined(); + + expect(author).toHaveProperty("name"); + expect(author.name).toEqual("BobDylan"); + + expect(author).toHaveProperty("email"); + expect(author.email).toEqual("bob@example.com"); + }); + + it("Should get the date", async() => { + expect.assertions(2); + + const date = await tag.date(); + + expect(date).toBeDefined(); + expect(typeof date).toEqual("number"); + }); + }); +}); \ No newline at end of file -- cgit v1.2.3-18-g5258