Skip to content

Instantly share code, notes, and snippets.

@jeffreytgilbert
Last active January 1, 2016 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffreytgilbert/8154212 to your computer and use it in GitHub Desktop.
Save jeffreytgilbert/8154212 to your computer and use it in GitHub Desktop.
Filter warning messages from the phantomjs output that happen on Mavericks due to using an older Qt library
#!/usr/bin/env node
var program = require('commander'),
spawn = require('child_process').spawn,
print = require('util').print,
fs = require('fs'),
path = require('path'),
exists = fs.existsSync || path.existsSync,
cwd = process.cwd(),
which = require('which'),
stream = require('stream'),
util = require('util'),
cookies = [],
headers = {},
settings = {};
function keyValue(val, store) {
val = val.split('=');
if (val[1] === 'true') {
val[1] = true;
} else if (val[1] === 'false') {
val[1] = false;
}
store[val[0]] = val[1];
return val;
}
function cookiesParser(val) {
val = JSON.parse(val);
cookies.push(val);
return val;
}
function header(val) {
return keyValue(val, headers);
}
function setting(val) {
return keyValue(val, settings);
}
function viewport(val) {
val = val.split('x');
return {
width: parseFloat(val[0]),
height: parseFloat(val[1])
};
}
function resolveHooks(val) {
var absPath = path.resolve(process.cwd(), val);
if (!exists(absPath)) {
for (var i = 0; i < module.paths.length - 1; i++) {
absPath = path.join(module.paths[i], val);
if (exists(absPath)) return absPath;
};
}
return absPath;
}
program
.version(JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8')).version)
.usage('[options] page')
.option('-R, --reporter <name>', 'specify the reporter to use', 'spec')
.option('-f, --file <filename>', 'specify the file to dump reporter output')
.option('-t, --timeout <timeout>', 'specify the test startup timeout to use', parseInt, 6000)
.option('-A, --agent <userAgent>', 'specify the user agent to use')
.option('-c, --cookies <Object>', 'phantomjs cookie object http://git.io/RmPxgA', cookiesParser) // http://git.io/RmPxgA
.option('-h, --header <name>=<value>', 'specify custom header', header)
.option('-k, --hooks <path>', 'path to hooks module', resolveHooks)
.option('-s, --setting <key>=<value>', 'specify specific phantom settings', setting)
.option('-v, --view <width>x<height>', 'specify phantom viewport size', viewport)
.option('-C, --no-color', 'disable color escape codes')
.option('-p, --path <path>', 'path to PhantomJS binary');
program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ mocha-phantomjs -R dot /test/file.html');
console.log(' $ mocha-phantomjs http://testserver.com/file.html');
console.log(' $ mocha-phantomjs -p ~/bin/phantomjs /test/file.html');
console.log('');
});
program.parse(process.argv);
if (!program.args.length) { program.outputHelp(); process.exit(1); };
if (program.agent) { settings.userAgent = program.agent; }
var script = fs.realpathSync(__dirname + '/../lib/mocha-phantomjs.coffee');
var reporter = program.reporter;
var page = function(){
var arg = program.args[0];
if (arg.match(/file:\/\//)) { return arg; };
if (arg.match(/http:\/\//)) { return arg; };
if (arg.match(/https:\/\//)) { return arg; };
if (exists(arg)) { return arg; };
if (exists(cwd+'/'+arg)) { return fs.realpathSync(cwd+'/'+arg); };
return arg;
}();
var config = JSON.stringify({
hooks: program.hooks,
timeout: program.timeout,
cookies: cookies,
headers: headers,
settings: settings,
viewportSize: program.view,
useColors: program.color,
file: program.file
});
function startPhantomJS(cmdPath, args, callback) {
var cmd = getCmdFromPath(cmdPath);
if (cmd != null) { return spawnPhantomJS(cmd, args); }
getCmdByEnvironmentPath(function (err, cmdPath){
if (err || !cmdPath) {
getCmdByModulePath(function(err, cmdPath) {
if (err) {
console.error('PhantomJS was not found.');
process.exit(1);
}
spawnPhantomJS(cmdPath, args);
});
} else {
spawnPhantomJS(cmdPath, args);
}
});
}
function FilterThirdPartyWarnings(options){
// allow use without new
if (!(this instanceof FilterThirdPartyWarnings)) {
return new FilterThirdPartyWarnings(options);
}
stream.Transform.call(this, options);
};
util.inherits(FilterThirdPartyWarnings, stream.Transform);
FilterThirdPartyWarnings.prototype._transform = function(chunk, encoding, done){
var self = this;
if(self.decodeStrings === false){
var data = chunk; // this chunk is a string
} else {
var data = chunk.toString(); // this chunk is a buffer
}
if (self._lastLineData) { data = self._lastLineData + data; }
var lines = data.split('\n');
self._lastLineData = lines.splice(lines.length-1,1)[0]; // save the last line of data
lines.map(function(value){
if(value.match(/\*\*\* WARNING|CoreText/g)){ // skips the data if it's found to have a warning
return;
}
// otherwise push it back to the stack
self.push(value+'\n', encoding);
});
done();
};
// when done with the filtering, push the last line of data out too
FilterThirdPartyWarnings.prototype._flush = function (done) {
if (this._lastLineData) {
if(this._lastLineData.match(/\*\*\* WARNING|CoreText/g)){ // skips the data if it's found to have a warning
this.push('');
} else {
this.push(this._lastLineData);
}
}
this._lastLineData = null;
done();
};
function spawnPhantomJS(cmdPath, args) {
var phantomjs = spawn(cmdPath, args),
outFilter = new FilterThirdPartyWarnings( { objectMode:true } );
errFilter = new FilterThirdPartyWarnings( { objectMode:true } );
phantomjs.stdout.pipe(outFilter).pipe(process.stdout);
phantomjs.stderr.pipe(errFilter).pipe(process.stdout);
phantomjs.on('exit', function(code){
if (code === 127) { print("Perhaps phantomjs is not installed?\n"); }
process.exit(code);
});
}
function getCmdFromPath(cmdPath, callback) {
if (cmdPath === undefined) { return };
var result = null,
errorMessage = null,
tempPath = path.resolve(cmdPath);
if (exists(tempPath)) {
result = tempPath;
} else {
errorMessage = "PhantomJS does not exist at '" + cmdPath + "'. Looking for PhantomJS in the PATH."
if (!callback) { console.error(errorMessage); }
}
if (callback) {
callback(errorMessage, result);
} else {
return result;
}
}
function getCmdByEnvironmentPath(callback) {
which('phantomjs', callback);
}
function getCmdByModulePath(callback) {
for (var i=0; i < module.paths.length; i++) {
var bin = path.join(module.paths[i], '.bin/phantomjs');
if (process.platform === 'win32') {
bin += '.cmd';
}
if (exists(bin)) {
return callback(null, bin);
}
}
return callback('PhantomJS not found.', null);
}
startPhantomJS(program.path, [script, page, reporter, config]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment