2021-10-02 14:08:16 +02:00
|
|
|
jest.mock("util", () => ({
|
|
|
|
...jest.requireActual("util"),
|
|
|
|
promisify: jest.fn()
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("fs", () => ({
|
|
|
|
...jest.requireActual("fs"),
|
|
|
|
statSync: jest.fn()
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("logger", () => ({
|
|
|
|
...jest.requireActual("logger"),
|
|
|
|
error: jest.fn(),
|
|
|
|
info: jest.fn()
|
|
|
|
}));
|
2021-09-10 13:21:38 +02:00
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
describe("Updatenotification", () => {
|
2021-10-02 14:08:16 +02:00
|
|
|
const execMock = jest.fn();
|
2021-09-10 13:21:38 +02:00
|
|
|
|
2021-10-02 14:08:16 +02:00
|
|
|
let gitHelper;
|
|
|
|
|
|
|
|
let gitRemoteOut;
|
|
|
|
let gitRevParseOut;
|
|
|
|
let gitStatusOut;
|
|
|
|
let gitFetchOut;
|
|
|
|
let gitRevListOut;
|
|
|
|
let gitRemoteErr;
|
|
|
|
let gitRevParseErr;
|
|
|
|
let gitStatusErr;
|
|
|
|
let gitFetchErr;
|
|
|
|
let gitRevListErr;
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
beforeAll(async () => {
|
2021-10-02 14:08:16 +02:00
|
|
|
const { promisify } = require("util");
|
|
|
|
promisify.mockReturnValue(execMock);
|
2021-09-10 13:21:38 +02:00
|
|
|
|
2021-10-02 14:08:16 +02:00
|
|
|
const GitHelper = require(`../../../modules/default/updatenotification/git_helper`);
|
|
|
|
gitHelper = new GitHelper();
|
2021-09-10 13:21:38 +02:00
|
|
|
});
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
beforeEach(() => {
|
2021-10-02 14:08:16 +02:00
|
|
|
gitRemoteOut = "";
|
|
|
|
gitRevParseOut = "";
|
|
|
|
gitStatusOut = "";
|
|
|
|
gitFetchOut = "";
|
|
|
|
gitRevListOut = "";
|
|
|
|
gitRemoteErr = "";
|
|
|
|
gitRevParseErr = "";
|
|
|
|
gitStatusErr = "";
|
|
|
|
gitFetchErr = "";
|
|
|
|
gitRevListErr = "";
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
execMock.mockImplementation((command) => {
|
2021-10-02 14:08:16 +02:00
|
|
|
if (command.includes("git remote -v")) {
|
|
|
|
return { stdout: gitRemoteOut, stderr: gitRemoteErr };
|
|
|
|
} else if (command.includes("git rev-parse HEAD")) {
|
|
|
|
return { stdout: gitRevParseOut, stderr: gitRevParseErr };
|
|
|
|
} else if (command.includes("git status -sb")) {
|
|
|
|
return { stdout: gitStatusOut, stderr: gitStatusErr };
|
|
|
|
} else if (command.includes("git fetch --dry-run")) {
|
|
|
|
return { stdout: gitFetchOut, stderr: gitFetchErr };
|
|
|
|
} else if (command.includes("git rev-list --ancestry-path --count")) {
|
|
|
|
return { stdout: gitRevListOut, stderr: gitRevListErr };
|
|
|
|
}
|
|
|
|
});
|
2021-09-10 13:21:38 +02:00
|
|
|
});
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
afterEach(async () => {
|
2021-10-02 14:08:16 +02:00
|
|
|
gitHelper.gitRepos = [];
|
|
|
|
|
|
|
|
jest.clearAllMocks();
|
2021-09-10 13:21:38 +02:00
|
|
|
});
|
|
|
|
|
2021-10-02 14:08:16 +02:00
|
|
|
describe("default", () => {
|
|
|
|
const moduleName = "default";
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
beforeEach(async () => {
|
2021-10-02 14:08:16 +02:00
|
|
|
gitRemoteOut = "origin\tgit@github.com:MichMich/MagicMirror.git (fetch)\norigin\tgit@github.com:MichMich/MagicMirror.git (push)\n";
|
|
|
|
gitRevParseOut = "332e429a41f1a2339afd4f0ae96dd125da6beada";
|
|
|
|
gitStatusOut = "## develop...origin/develop\n M tests/unit/functions/updatenotification_spec.js\n";
|
|
|
|
gitFetchErr = "From github.com:MichMich/MagicMirror\n60e0377..332e429 develop -> origin/develop\n";
|
|
|
|
gitRevListOut = "5";
|
|
|
|
|
|
|
|
await gitHelper.add(moduleName);
|
|
|
|
});
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
it("returns status information", async () => {
|
2021-10-02 14:08:16 +02:00
|
|
|
const repos = await gitHelper.getRepos();
|
|
|
|
expect(repos[0]).toMatchSnapshot();
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(5);
|
|
|
|
});
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
it("returns status information early if isBehindInStatus", async () => {
|
2021-10-02 14:08:16 +02:00
|
|
|
gitStatusOut = "## develop...origin/develop [behind 5]";
|
|
|
|
|
|
|
|
const repos = await gitHelper.getRepos();
|
|
|
|
expect(repos[0]).toMatchSnapshot();
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(3);
|
|
|
|
});
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
it("excludes repo if status can't be retrieved", async () => {
|
2021-10-02 14:08:16 +02:00
|
|
|
const errorMessage = "Failed to retrieve status";
|
|
|
|
execMock.mockRejectedValueOnce(errorMessage);
|
|
|
|
|
|
|
|
const repos = await gitHelper.getRepos();
|
|
|
|
expect(repos.length).toBe(0);
|
|
|
|
|
|
|
|
const { error } = require("logger");
|
|
|
|
expect(error).toHaveBeenCalledWith(`Failed to retrieve repo info for ${moduleName}: Failed to retrieve status`);
|
|
|
|
});
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
it("excludes repo if refs don't match regex", async () => {
|
2021-10-02 14:08:16 +02:00
|
|
|
gitFetchErr = "";
|
|
|
|
|
|
|
|
const repos = await gitHelper.getRepos();
|
|
|
|
expect(repos.length).toBe(0);
|
|
|
|
});
|
2021-09-10 13:21:38 +02:00
|
|
|
});
|
|
|
|
|
2021-10-02 14:08:16 +02:00
|
|
|
describe("custom module", () => {
|
|
|
|
const moduleName = "MMM-Fuel";
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
beforeEach(async () => {
|
2021-10-02 14:08:16 +02:00
|
|
|
gitRemoteOut = `origin\thttps://github.com/fewieden/${moduleName}.git (fetch)\norigin\thttps://github.com/fewieden/${moduleName}.git (push)\n`;
|
|
|
|
gitRevParseOut = "9d8310163da94441073a93cead711ba43e8888d0";
|
|
|
|
gitStatusOut = "## master...origin/master";
|
|
|
|
gitFetchErr = `From https://github.com/fewieden/${moduleName}\n19f7faf..9d83101 master -> origin/master`;
|
|
|
|
gitRevListOut = "7";
|
|
|
|
|
|
|
|
await gitHelper.add(moduleName);
|
|
|
|
});
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
it("returns status information without hash", async () => {
|
2021-10-02 14:08:16 +02:00
|
|
|
const repos = await gitHelper.getRepos();
|
|
|
|
expect(repos[0]).toMatchSnapshot();
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(4);
|
|
|
|
});
|
2021-09-10 13:21:38 +02:00
|
|
|
});
|
2021-09-07 22:07:42 +02:00
|
|
|
});
|