Skip to content

Commit

Permalink
test: implement setproctitle for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
refack committed Sep 20, 2017
1 parent b1c8f15 commit 98a7501
Showing 1 changed file with 49 additions and 20 deletions.
69 changes: 49 additions & 20 deletions test/parallel/test-setproctitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,65 @@ if (common.isSunOS)
common.skip(`Unsupported platform [${process.platform}]`);

const assert = require('assert');
const exec = require('child_process').exec;
const { exec, spawn } = require('child_process');
const path = require('path');

// The title shouldn't be too long; libuv's uv_set_process_title() out of
// security considerations no longer overwrites envp, only argv, so the
// maximum title length is possibly quite short.
let title = String(process.pid);
const title = String(Date.now()).substr(-4, 4);

assert.notStrictEqual(process.title, title);
process.title = title;
assert.strictEqual(process.title, title);
if (process.argv[2] === 'child') {
const childTitle = process.argv[3];
assert.notStrictEqual(process.title, childTitle);
process.title = childTitle;
assert.strictEqual(process.title, childTitle);
// Just wait a little bit before exiting
setTimeout(() => {}, common.platformTimeout(300));
return;
}

// Test setting the title but do not try to run `ps` on Windows.
if (common.isWindows)
common.skip('Windows does not have "ps" utility');
// This should run only in the parent
assert.strictEqual(process.argv.length, 2);
let child;
const cmd = (() => {
if (common.isWindows) {
// For windows we need to spawn a new `window` so it will get the title.
// For that we need a `shell`, and to use `start`
child = spawn(
'start /MIN /WAIT',
[process.execPath, __filename, 'child', title],
{ shell: true, stdio: 'ignore' },
);
const PSL = 'Powershell -NoProfile -ExecutionPolicy Unrestricted -Command';
const winTitle = '$_.mainWindowTitle';
return `${PSL} "ps | ? {${winTitle} -eq '${title}'} | % {${winTitle}}"`;
} else {
// for non Windows we change our own title
assert.notStrictEqual(process.title, title);
process.title = title;
assert.strictEqual(process.title, title);

// To pass this test on alpine, since Busybox `ps` does not
// support `-p` switch, use `ps -o` and `grep` instead.
const cmd = common.isLinux ?
`ps -o pid,args | grep '${process.pid} ${title}' | grep -v grep` :
`ps -p ${process.pid} -o args=`;
// To pass this test on alpine, since Busybox `ps` does not
// support `-p` switch, use `ps -o` and `grep` instead.
return common.isLinux ?
`ps -o pid,args | grep '${process.pid} ${title}' | grep -v grep` :
`ps -p ${child.pid} -o args=`;
}
})();

exec(cmd, common.mustCall((error, stdout, stderr) => {
// We don't need the child anymore...
if (child) {
child.kill();
child.unref();
}
assert.ifError(error);
assert.strictEqual(stderr, '');
const ret = stdout.trim();

// freebsd always add ' (procname)' to the process title
if (common.isFreeBSD)
title += ` (${path.basename(process.execPath)})`;

// omitting trailing whitespace and \n
assert.strictEqual(stdout.replace(/\s+$/, '').endsWith(title), true);
// freeBSD adds ' (procname)' to the process title
const bsdSuffix = ` (${path.basename(process.execPath)})`;
const expected = title + (common.isFreeBSD ? bsdSuffix : '');
assert.strictEqual(ret.endsWith(expected), true);
assert.strictEqual(ret.endsWith(expected), true);
}));

0 comments on commit 98a7501

Please sign in to comment.