diff --git a/CHANGELOG.md b/CHANGELOG.md index dbee3b6f..5a84bdf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,8 +11,9 @@ _This release is scheduled to be released on 2021-10-01._ ### Added -- Added showTime parameter to clock module for enabling/disabling time display in analog clock -- Added custom electron switches from user config (`config.electronSwitches`) +- Added showTime parameter to clock module for enabling/disabling time display in analog clock. +- Added custom electron switches from user config (`config.electronSwitches`). +- Added unit tests for updatenotification module. ### Updated diff --git a/modules/default/updatenotification/git_helper.js b/modules/default/updatenotification/git_helper.js index 6bda7337..b165e7bc 100644 --- a/modules/default/updatenotification/git_helper.js +++ b/modules/default/updatenotification/git_helper.js @@ -75,7 +75,12 @@ class gitHelper { } gitInfo.hash = res.stdout; } - res = await this.execShell("cd " + repo.folder + " && git status -sb"); + if (repo.res) { + // mocking + res = repo.res; + } else { + res = await this.execShell("cd " + repo.folder + " && git status -sb"); + } if (res.stderr) { Log.error("Failed to get git status for " + repo.module + ": " + res.stderr); // exit without git status info @@ -105,14 +110,26 @@ class gitHelper { } async getRepoInfo(repo) { - let gitInfo = await this.getStatusInfo(repo); + let gitInfo; + if (repo.gitInfo) { + // mocking + gitInfo = repo.gitInfo; + } else { + gitInfo = await this.getStatusInfo(repo); + } if (!gitInfo) { return; } if (gitInfo.isBehindInStatus) { return gitInfo; } - let res = await this.execShell("cd " + repo.folder + " && git fetch --dry-run"); + let res; + if (repo.res) { + // mocking + res = repo.res; + } else { + res = await this.execShell("cd " + repo.folder + " && git fetch --dry-run"); + } // example output: // From https://github.com/MichMich/MagicMirror // e40ddd4..06389e3 develop -> origin/develop @@ -123,9 +140,13 @@ class gitHelper { return; } // get behind with refs - res = await this.execShell("cd " + repo.folder + " && git rev-list --ancestry-path --count " + matches[0]); - gitInfo.behind = parseInt(res.stdout); - return gitInfo; + try { + res = await this.execShell("cd " + repo.folder + " && git rev-list --ancestry-path --count " + matches[0]); + gitInfo.behind = parseInt(res.stdout); + return gitInfo; + } catch (err) { + Log.error("Failed to get git revisions for " + repo.module + ": " + err); + } } async getStatus() { diff --git a/tests/unit/functions/updatenotification_spec.js b/tests/unit/functions/updatenotification_spec.js index 91715c71..f6214c1a 100644 --- a/tests/unit/functions/updatenotification_spec.js +++ b/tests/unit/functions/updatenotification_spec.js @@ -3,6 +3,78 @@ const git_Helper = require("../../../modules/default/updatenotification/git_help const gitHelper = new git_Helper.gitHelper(); gitHelper.add("default"); +const test1 = { + module: "test1", + folder: "", + res: { + stdout: "## master...origin/master [behind 8]", + stderr: "" + }, + gitInfo: { + module: "default", + // commits behind: + behind: 0, + // branch name: + current: "develop", + // current hash: + hash: "", + // remote branch: + tracking: "", + isBehindInStatus: false + } +}; + +const test2 = { + module: "test2", + folder: "", + res: { + stdout: "## develop...origin/develop", + stderr: "" + } +}; + +const test3 = { + module: "test3", + folder: "", + res: { + stdout: "", + stderr: "error" + }, + gitInfo: { + module: "default", + // commits behind: + behind: 2, + // branch name: + current: "develop", + // current hash: + hash: "", + // remote branch: + tracking: "", + isBehindInStatus: true + } +}; + +const test4 = { + module: "default", + folder: path.join(__dirname, "../../.."), + res: { + stdout: "", + stderr: " e40ddd4..06389e3 develop -> origin/develop" + }, + gitInfo: { + module: "default", + // commits behind: + behind: 0, + // branch name: + current: "develop", + // current hash: + hash: "", + // remote branch: + tracking: "", + isBehindInStatus: false + } +}; + describe("Updatenotification", function () { it("should return valid output for git status", async function () { const arr = await gitHelper.getStatus(); @@ -11,4 +83,44 @@ describe("Updatenotification", function () { expect(gitInfo.current).not.toBe(""); expect(gitInfo.hash).not.toBe(""); }, 15000); + + it("should return behind=8 for test1", async function () { + const gitInfo = await gitHelper.getStatusInfo(test1); + expect(gitInfo.behind).toBe(8); + expect(gitInfo.isBehindInStatus).toBe(true); + }); + + it("should return behind=0 for test2", async function () { + const gitInfo = await gitHelper.getStatusInfo(test2); + expect(gitInfo.behind).toBe(0); + expect(gitInfo.isBehindInStatus).toBe(false); + }); + + it("should return empty status object for test3", async function () { + const gitInfo = await gitHelper.getStatusInfo(test3); + expect(gitInfo).toBe(undefined); + }); + + it("should return empty repo object for test2", async function () { + // no gitInfo provided in res, so returns undefined + const gitInfo = await gitHelper.getRepoInfo(test2); + expect(gitInfo).toBe(undefined); + }); + + it("should return empty repo object for test1", async function () { + // no regex match for refs in empty string, so returns undefined + const gitInfo = await gitHelper.getRepoInfo(test1); + expect(gitInfo).toBe(undefined); + }); + + it("should return empty repo object for test4", async function () { + // git ref list throws error, so returns undefined + const gitInfo = await gitHelper.getRepoInfo(test4); + expect(gitInfo).toBe(undefined); + }); + + it("should return behind=2 for test3", async function () { + const gitInfo = await gitHelper.getRepoInfo(test3); + expect(gitInfo.behind).toBe(2); + }); });