This repository has been archived on 2020-01-20. You can view files and clone it, but cannot push or open issues or pull requests.
node-spearhead/test/lib/testcommon.js

85 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2015-09-01 10:31:00 +03:00
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (c) 2015, Joyent, Inc.
*/
var assert = require('assert-plus');
var exec = require('child_process').exec;
var VError = require('verror').VError;
// ---- exports
/**
* A convenience wrapper around `child_process.exec` to take away some
* logging and error handling boilerplate.
*
* @param args {Object}
* - command {String} Required.
* - log {Bunyan Logger} Required. Use to log details at trace level.
* - execOpts {Array} Optional. child_process.exec options.
* - errMsg {String} Optional. Error string to use in error message on
* failure.
* @param cb {Function} `function (err, stdout, stderr)` where `err` here is
* an `VError` wrapper around the child_process error.
*/
function execPlus(args, cb) {
assert.object(args, 'args');
assert.string(args.command, 'args.command');
assert.optionalString(args.errMsg, 'args.errMsg');
assert.optionalObject(args.execOpts, 'args.execOpts');
assert.object(args.log, 'args.log');
assert.func(cb);
var command = args.command;
var execOpts = args.execOpts;
// args.log.trace({exec: true, command: command, execOpts: execOpts},
// 'exec start');
exec(command, execOpts, function (err, stdout, stderr) {
args.log.trace({exec: true, command: command, execOpts: execOpts,
err: err, stdout: stdout, stderr: stderr}, 'exec done');
if (err) {
cb(
new VError(err,
'%s:\n'
+ '\tcommand: %s\n'
+ '\texit status: %s\n'
+ '\tstdout:\n%s\n'
+ '\tstderr:\n%s',
args.errMsg || 'exec error', command, err.code,
stdout.trim(), stderr.trim()),
stdout, stderr);
} else {
cb(null, stdout, stderr);
}
});
}
/**
* Calls t.ifError, outputs the error body for diagnostic purposes, and
* returns true if there was an error
*/
function ifErr(t, err, desc) {
t.ifError(err, desc);
if (err) {
t.deepEqual(err.body, {}, desc + ': error body');
return true;
}
return false;
}
module.exports = {
execPlus: execPlus,
ifErr: ifErr
};