Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change use of logger.js in jest tests #2655

Merged
merged 9 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ _This release is scheduled to be released on 2021-10-01._
- Update weathergov provider to try fetching not just current, but also foreacst, when API URLs available.
- Refactored clock layout.
- Refactored methods from weatherproviders into weatherobject (isDaytime, updateSunTime).
- Use of `logger.js` in jest tests.

### Fixed

Expand Down
9 changes: 6 additions & 3 deletions js/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
root.Log = factory(root.config);
}
})(this, function (config) {
const logLevel = {
let logLevel = {
debug: Function.prototype.bind.call(console.debug, console),
log: Function.prototype.bind.call(console.log, console),
info: Function.prototype.bind.call(console.info, console),
Expand All @@ -32,10 +32,13 @@
groupCollapsed: Function.prototype.bind.call(console.groupCollapsed, console),
groupEnd: Function.prototype.bind.call(console.groupEnd, console),
time: Function.prototype.bind.call(console.time, console),
timeEnd: Function.prototype.bind.call(console.timeEnd, console),
timeStamp: Function.prototype.bind.call(console.timeStamp, console)
timeEnd: Function.prototype.bind.call(console.timeEnd, console)
};

if ((typeof exports === "object" && process.env.JEST_WORKER_ID === undefined) || typeof exports !== "object") {
logLevel.timeStamp = Function.prototype.bind.call(console.timeStamp, console);
}

logLevel.setLogLevel = function (newLevel) {
if (newLevel) {
Object.keys(logLevel).forEach(function (key, index) {
Expand Down
15 changes: 5 additions & 10 deletions modules/default/updatenotification/git_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ const util = require("util");
const exec = util.promisify(require("child_process").exec);
const fs = require("fs");
const path = require("path");
const Log = require("logger");

class gitHelper {
constructor() {
this.gitRepos = [];
this.baseDir = path.normalize(__dirname + "/../../../");
if (process.env.JEST_WORKER_ID === undefined) {
this.Log = require("logger");
} else {
// if we are running with jest
this.Log = require("../../../tests/unit/mocks/logger.js");
}
}

getRefRegex(branch) {
Expand All @@ -31,7 +26,7 @@ class gitHelper {
async isGitRepo(moduleFolder) {
let res = await this.execShell("cd " + moduleFolder + " && git remote -v");
if (res.stderr) {
this.Log.error("Failed to fetch git data for " + moduleFolder + ": " + res.stderr);
Log.error("Failed to fetch git data for " + moduleFolder + ": " + res.stderr);
return false;
} else {
return true;
Expand All @@ -44,7 +39,7 @@ class gitHelper {
moduleFolder = moduleFolder + "modules/" + moduleName;
}
try {
this.Log.info("Checking git for module: " + moduleName);
Log.info("Checking git for module: " + moduleName);
// Throws error if file doesn't exist
fs.statSync(path.join(moduleFolder, ".git"));
// Fetch the git or throw error if no remotes
Expand Down Expand Up @@ -76,13 +71,13 @@ class gitHelper {
// the hash is only needed for the mm repo
res = await this.execShell("cd " + repo.folder + " && git rev-parse HEAD");
if (res.stderr) {
this.Log.error("Failed to get current commit hash for " + repo.module + ": " + res.stderr);
Log.error("Failed to get current commit hash for " + repo.module + ": " + res.stderr);
}
gitInfo.hash = res.stdout;
}
res = await this.execShell("cd " + repo.folder + " && git status -sb");
if (res.stderr) {
this.Log.error("Failed to get git status for " + repo.module + ": " + res.stderr);
Log.error("Failed to get git status for " + repo.module + ": " + res.stderr);
// exit without git status info
return;
}
Expand Down
40 changes: 20 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"jsdom": "^17.0.0",
"lodash": "^4.17.21",
"nyc": "^15.1.0",
"prettier": "^2.3.2",
"prettier": "^2.4.0",
"pretty-quick": "^3.1.1",
"sinon": "^11.1.2",
"spectron": "^15.0.0",
Expand All @@ -80,7 +80,7 @@
"iconv-lite": "^0.6.3",
"module-alias": "^2.2.2",
"moment": "^2.29.1",
"node-fetch": "^2.6.1",
"node-fetch": "^2.6.2",
"node-ical": "^0.13.0",
"socket.io": "^4.2.0"
},
Expand All @@ -96,6 +96,9 @@
"projects": [
{
"displayName": "unit",
"moduleNameMapper": {
"logger": "<rootDir>/js/logger.js"
},
"testMatch": [
"**/tests/unit/**/*.[jt]s?(x)"
],
Expand Down
17 changes: 1 addition & 16 deletions tests/unit/functions/updatenotification_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,13 @@ const path = require("path");
const git_Helper = require("../../../modules/default/updatenotification/git_helper.js");
const gitHelper = new git_Helper.gitHelper();
gitHelper.add("default");
let branch = "";

describe("Updatenotification", function () {
// it is assumed that we are at the HEAD of a branch when running this tests
// and we have no foreign modules installed.

it("should return 0 for repo count", async function () {
const arr = await gitHelper.getRepos();
expect(arr.length).toBe(0);
}, 15000);

it("should return valid output for git status", async function () {
const arr = await gitHelper.getStatus();
expect(arr.length).toBe(1);
const gitInfo = arr[0];
branch = gitInfo.current;
expect(gitInfo.current).not.toBe("");
expect(gitInfo.hash).not.toBe("");
}, 15000);

it("should return no refs for git fetch", async function () {
const baseDir = path.normalize(__dirname + "/../../../");
const res = await gitHelper.execShell("cd " + baseDir + " && git fetch --dry-run");
expect(res.stderr.match(gitHelper.getRefRegex(branch))).toBe(null);
});
});
39 changes: 2 additions & 37 deletions tests/unit/global_vars/defaults_modules_spec.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,14 @@
const fs = require("fs");
const path = require("path");
const vm = require("vm");

const basedir = path.join(__dirname, "../../..");
const root_path = path.join(__dirname, "../../..");

describe("Default modules set in modules/default/defaultmodules.js", function () {
let sandbox = null;

beforeAll(function () {
const fileName = "js/app.js";
const filePath = path.join(basedir, fileName);
const code = fs.readFileSync(filePath);

sandbox = {
module: {},
__dirname: path.dirname(filePath),
global: {},
process: {
on: function () {},
env: {}
}
};

sandbox.require = function (filename) {
// This modifies the global slightly,
// but supplies vm with essential code
if (filename === "logger") {
return require("../mocks/logger.js");
} else {
try {
return require(filename);
} catch (ignore) {
// ignore
}
}
};

vm.runInNewContext(code, sandbox, fileName);
});

const expectedDefaultModules = require("../../../modules/default/defaultmodules");

for (const defaultModule of expectedDefaultModules) {
it(`contains a folder for modules/default/${defaultModule}"`, function () {
expect(fs.existsSync(path.join(sandbox.global.root_path, "modules/default", defaultModule))).toBe(true);
expect(fs.existsSync(path.join(root_path, "modules/default", defaultModule))).toBe(true);
});
}
});
44 changes: 5 additions & 39 deletions tests/unit/global_vars/root_path_spec.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,15 @@
const fs = require("fs");
const path = require("path");
const vm = require("vm");

describe("'global.root_path' set in js/app.js", function () {
let sandbox = null;

beforeAll(function () {
const basedir = path.join(__dirname, "../../..");

const fileName = "js/app.js";
const filePath = path.join(basedir, fileName);
const code = fs.readFileSync(filePath);

sandbox = {
module: {},
__dirname: path.dirname(filePath),
global: {},
process: {
on: function () {},
env: {}
}
};

sandbox.require = function (filename) {
// This modifies the global slightly,
// but supplies vm with essential code
if (filename === "logger") {
return require("../mocks/logger.js");
} else {
try {
return require(filename);
} catch (ignore) {
// ignore
}
}
};

vm.runInNewContext(code, sandbox, fileName);
});
const root_path = path.join(__dirname, "../../..");
const version = require(`${__dirname}/../../../package.json`).version;

describe("'global.root_path' set in js/app.js", function () {
const expectedSubPaths = ["modules", "serveronly", "js", "js/app.js", "js/main.js", "js/electron.js", "config"];

expectedSubPaths.forEach((subpath) => {
it(`contains a file/folder "${subpath}"`, function () {
expect(fs.existsSync(path.join(sandbox.global.root_path, subpath))).toBe(true);
expect(fs.existsSync(path.join(root_path, subpath))).toBe(true);
});
});

Expand All @@ -57,6 +23,6 @@ describe("'global.root_path' set in js/app.js", function () {

it("should expect the global.version equals package.json file", function () {
const versionPackage = JSON.parse(fs.readFileSync("package.json", "utf8")).version;
expect(sandbox.global.version).toBe(versionPackage);
expect(version).toBe(versionPackage);
});
});
20 changes: 0 additions & 20 deletions tests/unit/mocks/logger.js

This file was deleted.