gpii-windows
Version:
Components of the GPII personalization infrastructure for use on Microsoft's "Windows" ™
248 lines (217 loc) • 9.04 kB
JavaScript
/*!
GPII Node.js Processes Bridge Unit Tests
Copyright 2014-2017 Inclusive Design Research Centre, OCAD University
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
You may obtain a copy of the License at
https://github.com/gpii/universal/LICENSE.txt
*/
/*global require*/
;
var path = require("path"),
spawn = require("child_process").spawn,
fluid = require("gpii-universal"),
jqUnit = fluid.require("node-jqunit");
require("../processesBridge.js");
var gpii = fluid.registerNamespace("gpii");
var procTests = fluid.registerNamespace("gpii.tests.processes");
// Delay for the given number of milliseconds.
procTests.waitMsec = function (msec) {
var t0 = Date.now();
var longEnough = false;
while (!longEnough) {
longEnough = ((Date.now() - t0) > msec);
}
};
var processesBridge = gpii.processes.windows();
jqUnit.module("Processes Bridge for Windows module");
jqUnit.test(
"Test getProcesses('<command>')/findProcessByPid() using the 'process' object itself",
function () {
var processCommand = path.parse(process.execPath).base;
var procInfos = processesBridge.getProcessList(processCommand);
jqUnit.assertNotEquals(
"Listing all processes", 0, procInfos.length
);
// Check for the presence of this nodejs processs itself -- it must
// be in the process list since this code is running inside that
// process.
var nodeProc = processesBridge.findProcessByPid(process.pid, procInfos);
jqUnit.assertNotNull("Searching for 'node' process", nodeProc);
}
);
jqUnit.test(
"Test getProcesses()/findProcessByPid() with the 'process' object itself",
function () {
var procInfos = processesBridge.getProcessList();
jqUnit.assertNotEquals(
"Listing all processes", 0, procInfos.length
);
// Check for the presence of this nodejs processs itself -- it must
// be in the process list since this code is running inside that
// process.
var nodeProc = processesBridge.findProcessByPid(process.pid, procInfos);
jqUnit.assertNotNull("Searching for 'node' process", nodeProc);
}
);
jqUnit.test(
"Test findProcessByPid() with non-running process id",
function () {
jqUnit.assertNull(
"Search negative process id value", processesBridge.findProcessByPid(-1)
);
}
);
jqUnit.test(
"Test findProcessByPid() against the 'process' object.",
function () {
var nodeProcInfo = processesBridge.findProcessByPid(process.pid);
jqUnit.assertEquals("Node process 'name'",
path.parse(process.execPath).base, nodeProcInfo.command);
jqUnit.assertEquals("Node process 'pid'",
process.pid, nodeProcInfo.pid);
// Filter the list of arguments to avoid picking up wrappers added by coverage tools like `nyc`.
var filteredArgs = nodeProcInfo.argv.filter(function (arg) { return arg.indexOf("node-spawn-wrap") === -1; });
console.log("process argv: " + JSON.stringify(nodeProcInfo.argv));
jqUnit.assertEquals("Node process 'argv' length'",
process.argv.length + process.execArgv.length, filteredArgs.length);
jqUnit.assertEquals("Node process status",
"Running", nodeProcInfo.state);
// The "fullPath" property is added by the process node add-on.
// It should match the full path to process.execPath.
jqUnit.assertEquals("Node process fullPath",
process.execPath, nodeProcInfo.fullPath
);
// The order of process.argv and nodeProcInfo.argv is not
// necessarily the same, nor are the number of arguments the same.
// Only the first argument of the vectors match.
jqUnit.assertEquals("Node process argv[0]",
path.basename(process.argv[0]),
path.basename(nodeProcInfo.argv[0])
);
}
);
jqUnit.test(
"Test findProcessesByCmd()/findFirstProcessByCmd() with nodejs itself",
function () {
var nodeProcInfos = processesBridge.findProcessesByCommand("node.exe");
jqUnit.assertNotEquals(
"Getting all 'node' processes", 0, nodeProcInfos.length
);
nodeProcInfos.forEach(function (aProcInfo) {
jqUnit.assertEquals(
"Node commmand name", "node.exe", aProcInfo.command
);
});
var procInfo = processesBridge.findFirstProcessByCommand("node.exe");
jqUnit.assertNotNull(
"Looking for first 'node' processes", procInfo);
jqUnit.assertEquals("Node commmand name", "node.exe", procInfo.command);
}
);
jqUnit.test(
"Test findProcessesByCmd()/findFirstProcessByCmd() with nodejs itself - ignoring case",
function () {
var nodeProcInfos = processesBridge.findProcessesByCommand("NoDE.eXe");
jqUnit.assertNotEquals(
"Getting all 'NoDE.eXe' processes", 0, nodeProcInfos.length
);
nodeProcInfos.forEach(function (aProcInfo) {
jqUnit.assertEquals(
"Node commmand name", "node.exe", aProcInfo.command
);
});
var procInfo = processesBridge.findFirstProcessByCommand("NoDE.eXe");
jqUnit.assertNotNull(
"Looking for first 'NoDE.eXe' processes", procInfo);
jqUnit.assertEquals("Node commmand name", "node.exe", procInfo.command);
}
);
jqUnit.test(
"Test initProcInfoNotRunning()",
function () {
var notRunning = processesBridge.initProcInfoNotRunning("grep");
jqUnit.assertEquals("Command name", notRunning.command, "grep");
jqUnit.assertEquals("Negative process id", notRunning.pid, -1);
jqUnit.assertEquals(
"'NoSuchProcess' state", notRunning.state, "NoSuchProcess"
);
jqUnit.assertNull(
"Search negative process id value",
processesBridge.findProcessByPid(notRunning.pid)
);
}
);
jqUnit.test(
"Test isRunning() with 'process' object itself, and nonexistent process",
function () {
var procInfo = processesBridge.findProcessByPid(process.pid);
jqUnit.assertNotNull("Searching for 'node' process", procInfo);
jqUnit.assertTrue(
"Check nodejs is running",
processesBridge.isRunning(procInfo.state)
);
procInfo = processesBridge.initProcInfoNotRunning("grep");
jqUnit.assertFalse(
"Check nonexistent process running",
processesBridge.isRunning(procInfo)
);
}
);
jqUnit.test(
"Test updateProcInfo() against non-changing process",
function () {
var procInfo = processesBridge.findProcessByPid(process.pid);
jqUnit.assertNotNull("Looking for process.pid processes", procInfo);
var newProcInfo = processesBridge.updateProcInfo(procInfo);
jqUnit.assertDeepEq(
"Check change in process info", procInfo, newProcInfo
);
}
);
jqUnit.test(
"Test updateProcInfo() against changing process",
function () {
var notePad = spawn("notepad.exe");
var notePadInfo = processesBridge.findProcessByPid(notePad.pid);
jqUnit.assertNotNull("Search 'notepad' process", notePadInfo);
jqUnit.assertTrue("Stop Notepad", notePad.kill("SIGKILL"));
procTests.waitMsec(500); // Allow system cleanup.
var newNotePadInfo = processesBridge.updateProcInfo(notePadInfo);
jqUnit.assertNotEquals(
"Update process state", newNotePadInfo.state, notePadInfo.state
);
}
);
jqUnit.test(
"Test findSolutionsByCommands()",
function () {
// Node is running. Add a running notepad process. No such command as why.
var notePad = spawn("notepad.exe");
var solutions = ["node.exe", "notepad.exe", "why.exe"];
var procInfos = processesBridge.findSolutionsByCommands(solutions);
jqUnit.assertTrue("Node and Notepad processes", procInfos.length >= 2);
procInfos.forEach(function (item) {
var isNode = item.command === "node.exe";
var isNotePad = item.command === "notepad.exe";
jqUnit.assertTrue("Process name node or notepad", isNode || isNotePad);
});
notePad.kill("SIGKILL");
}
);
jqUnit.test(
"Test findSolutionsByPids()",
function () {
// Node is running. Add a running notepad process.
var notePad = spawn("notepad");
var pids = [process.pid, notePad.pid];
var procInfos = processesBridge.findSolutionsByPids(pids);
jqUnit.assertTrue("Node and Notepad processes", procInfos.length >= 2);
procInfos.forEach(function (item) {
var isNode = item.pid === process.pid;
var isNotepad = item.pid === notePad.pid;
jqUnit.assertTrue("Process pid node or notepad", isNode || isNotepad);
});
notePad.kill("SIGKILL");
}
);