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

Growl: Web notifications & Desktop prerequisite software check #3542

Merged
merged 49 commits into from
Dec 23, 2018
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
e2204c1
feat(Growl): Check notification support _before_ use!
plroebuck Nov 1, 2018
b9e557f
docs(growl.js): Correct access level of `notify` method [skip ci]
plroebuck Nov 1, 2018
1902023
docs(growl.js,mocha.js): Minor changes to improve specificity [skip ci]
plroebuck Nov 1, 2018
08ec0a8
docs(mocha.js): Remove unneeded JSDoc tag [skip ci]
plroebuck Nov 1, 2018
ddeca7e
refactor(growl.js): Simplify completion callback error reporting
plroebuck Nov 1, 2018
9b0907a
refactor(growl.js): Change event listener style
plroebuck Nov 1, 2018
64c09eb
test(unit/mocha.spec.js): Convert `context` to `describe`
plroebuck Nov 4, 2018
4773475
refactor(lib/growl.js): Convert `switch` to KV lookup
plroebuck Nov 4, 2018
8a16c9a
refactor(lib/growl.js): Remove `path.exists` anachronism
plroebuck Nov 4, 2018
da25205
docs(docs/index.md): Add Growl section to user guide [skip travis][sk…
plroebuck Nov 10, 2018
f81efdc
Merge branch 'master' into growl
plroebuck Nov 10, 2018
a5df791
docs(docs/index.md): Make markdownlint happy
plroebuck Nov 10, 2018
cfb1182
Merge branch 'growl' of https://github.com/plroebuck/mocha into growl
plroebuck Nov 10, 2018
1271f8b
test(unit/mocha.spec.js): Minor tweaks to Growl-related tests
plroebuck Nov 18, 2018
c303f63
docs(docs/index.md): Updates to user guide for desktop notification s…
plroebuck Nov 18, 2018
20dddf0
feat(lib/browser/growl.js): Added web notification support for `Mocha…
plroebuck Nov 18, 2018
f1c81af
feat(lib/growl.js): Updated notification appearance
plroebuck Nov 18, 2018
fb9b696
refactor(lib/mocha.js): Change `Mocha#growl` notification support err…
plroebuck Nov 18, 2018
c9a8d9b
build(browserify): Updated browserify settings to bundle web notifica…
plroebuck Nov 18, 2018
519f2cd
docs(growl): Fix JSDoc module header
plroebuck Nov 19, 2018
b4ea1de
docs(docs/index.md): Unescape left tag delimmiters in HTML codefence
plroebuck Nov 19, 2018
7e7a67c
docs(user guide): Minor wordsmithing [skip travis][skip appveyor]
plroebuck Nov 19, 2018
528437a
docs(user guide): Make HTML examples more similar
plroebuck Nov 19, 2018
a1dce65
ci(appveyor.yml): Change URL for GrowlForWindows (GfW)
plroebuck Nov 23, 2018
3fe39c1
Merge branch 'master' into growl
plroebuck Nov 25, 2018
81c21da
Merge branch 'master' into growl
plroebuck Dec 1, 2018
63ca450
build(scripts/package-json-cullify.js): Renamed browserify-related sc…
plroebuck Dec 6, 2018
52b3f03
refactor(browser/growl.js): Adapt code to recommendations following r…
plroebuck Dec 6, 2018
8616d25
Merge branch 'growl' of https://github.com/plroebuck/mocha into growl
plroebuck Dec 6, 2018
2d73c2a
docs(docs/index.md): Update browser usage links to reference upcoming…
plroebuck Dec 6, 2018
f1a8db1
docs(browser/growl.js): Update JSDoc with minor tweaks
plroebuck Dec 13, 2018
621a604
refactor(browser/growl.js): Flatten `isPermitted` ask() return value
plroebuck Dec 13, 2018
372fab6
Merge branch 'master' into growl
plroebuck Dec 13, 2018
1f06d4e
Merge branch 'master' into growl
plroebuck Dec 14, 2018
8f72bfb
docs(docs/index.md): Update "unpkg.com" links to reference latest moc…
plroebuck Dec 15, 2018
fd7ea35
fix(lib/growl.js): Add file extension to Windows binary
plroebuck Dec 19, 2018
ba00cf8
chore(assets/mocha-logo-96.png): Add new resolution of PNG logo [ci s…
plroebuck Dec 22, 2018
e33da70
Merge branch 'growl' of https://github.com/plroebuck/mocha into growl
plroebuck Dec 22, 2018
7348df2
refactor(lib/growl.js): Use 96px resolution logo for desktop notifica…
plroebuck Dec 22, 2018
a64c15b
refactor(browser/growl.js): Hardcode language and direction
plroebuck Dec 22, 2018
30c9cd4
fix(browser/growl.js): Use notification logo
plroebuck Dec 22, 2018
ad2fad5
fix(package-scripts.js): Remove quotes around `dedefine` script
plroebuck Dec 22, 2018
dd8bc02
build(package.json): Add new property "notifyLogo"
plroebuck Dec 22, 2018
dbb2dfa
refactor(browser/growl.js): Remove notification tag generation
plroebuck Dec 22, 2018
0f2f5b3
Merge branch 'master' into growl
plroebuck Dec 22, 2018
20d1733
fix(browser/growl.js): Save timer references to avoid Sinon interfering
plroebuck Dec 23, 2018
0688e93
Merge branch 'growl' of https://github.com/plroebuck/mocha into growl
plroebuck Dec 23, 2018
5baeb00
docs(docs/index.md): Minor changes to documentation [ci skip]
plroebuck Dec 23, 2018
dd7bc9c
docs(browser/growl.js): Removed unnecessary JSDoc tag
plroebuck Dec 23, 2018
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
146 changes: 146 additions & 0 deletions lib/growl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
'use strict';
/**
* @module Growl
*/

var fs = require('fs');
var os = require('os');
var path = require('path');

/**
* @summary
* Checks if Growl support seems likely.
*
* @description
* Glosses over the distinction between an unsupported platform
* and one that lacks prerequisite software installations.
* Autofails when run in browser.
*
* @public
* @see {@link https://github.com/tj/node-growl/blob/master/README.md|Prerequisite Installs}
* @see {@link Mocha#growl}
* @see {@link Mocha#isGrowlCapable}
* @return {boolean} whether Growl support can be expected
*/
exports.isCapable = function() {
return !process.browser && which(getSupportBinaries()) !== '';
};

/**
* Implements desktop notifications as a pseudo-reporter.
*
* @public
* @see {@link Mocha#_growl}
* @param {Runner} runner - Runner instance.
*/
exports.notify = function(runner) {
var sendNotification = function() {
var growl = require('growl');
var stats = runner.stats;
var msg;
var options;

if (stats.failures) {
msg = stats.failures + ' of ' + runner.total + ' tests failed';
options = {
name: 'mocha',
title: 'Failed',
image: image('error')
};
} else {
msg = stats.passes + ' tests passed in ' + stats.duration + 'ms';
options = {
name: 'mocha',
title: 'Passed',
image: image('ok')
};
}
growl(msg, options, onCompletion);
};

/**
* @summary
* Callback for result of attempted Growl notification.
*
* @description
* Despite its appearance, this is <strong>not</strong> an Error-first
* callback -- all parameters are populated regardless of success.
*
* @callback Growl~growlCB
* @param {*} err - Error object, or <code>null</code> if successful.
* @param {string} stdout - <code>stdout</code> from notification delivery
* process.
* @param {string} stderr - <code>stderr</code> from notification delivery
* process. It will include timestamp and executed command with arguments.
*/

function onCompletion(err, stdout, stderr) {
if (err) {
var detail =
err.code === 'ENOENT' ? 'prerequisite software not found' : err.message;
console.error('Growl notification error:', detail);
}
}

runner.once('end', sendNotification);
};

/**
* Returns Growl image `name` path.
*
* @private
* @param {string} name - Basename of associated Growl image.
* @return {string} Pathname to Growl image
*/
function image(name) {
return path.join(__dirname, '..', 'assets', 'growl', name + '.png');
}

/**
* @summary
* Locates a binary in the user's `PATH` environment variable.
*
* @description
* Takes a list of command names and searches the path for each executable
* file that would be run had these commands actually been invoked.
*
* @private
* @param {string[]} binaries - Names of binary files to search for.
* @return {string} absolute path of first binary found, or empty string if none
*/
function which(binaries) {
plroebuck marked this conversation as resolved.
Show resolved Hide resolved
var paths = process.env.PATH.split(path.delimiter);

for (var n = 0, blen = binaries.length; n < blen; n++) {
var binary = binaries[n];
var loc;
for (var i = 0, plen = paths.length; i < plen; i++) {
loc = path.join(paths[i], binary);
if (fs.existsSync(loc)) {
return loc;
}
}
}
return '';
}

/**
* @summary
* Gets platform-specific Growl support binaries.
*
* @description
* Somewhat brittle dependency on `growl` package implementation, but it
* rarely changes.
*
* @private
* @see {@link https://github.com/tj/node-growl/blob/master/lib/growl.js#L28-L126|setupCmd}
* @return {string[]} names of Growl support binaries
*/
function getSupportBinaries() {
var binaries = {
Darwin: ['terminal-notifier', 'growlnotify'],
Linux: ['notify-send', 'growl'],
Windows_NT: ['growlnotify']
};
return binaries[os.type()] || [];
plroebuck marked this conversation as resolved.
Show resolved Hide resolved
}
78 changes: 38 additions & 40 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
var escapeRe = require('escape-string-regexp');
var path = require('path');
var builtinReporters = require('./reporters');
var growl = require('./growl');
plroebuck marked this conversation as resolved.
Show resolved Hide resolved
var utils = require('./utils');

exports = module.exports = Mocha;
Expand Down Expand Up @@ -49,17 +50,6 @@ exports.Suite = require('./suite');
exports.Hook = require('./hook');
exports.Test = require('./test');

/**
* Returns Growl image `name` path.
*
* @private
* @param {string} name - Basename of associated Growl image.
* @return {string} Pathname to Growl image
*/
function image(name) {
return path.join(__dirname, '..', 'assets', 'growl', name + '.png');
}

/**
* Constructs a new Mocha instance with `options`.
*
Expand Down Expand Up @@ -104,6 +94,9 @@ function Mocha(options) {
this.suite = new exports.Suite('', new exports.Context());
this.ui(options.ui);
this.bail(options.bail);
if (options.growl) {
this.growl();
}
this.reporter(options.reporter, options.reporterOptions);
if (typeof options.timeout !== 'undefined' && options.timeout !== null) {
this.timeout(options.timeout);
Expand Down Expand Up @@ -290,32 +283,6 @@ Mocha.prototype.loadFiles = function(fn) {
fn && fn();
};

/**
* Implements desktop notifications using a pseudo-reporter.
*
* @private
* @see {@link Mocha#growl}
* @param {Object} runner - Runner instance.
* @param {Object} reporter - Reporter instance.
*/
Mocha.prototype._growl = function(runner, reporter) {
var notify = require('growl');

runner.on('end', function() {
var stats = reporter.stats;
if (stats.failures) {
var msg = stats.failures + ' of ' + runner.total + ' tests failed';
notify(msg, {name: 'mocha', title: 'Failed', image: image('error')});
} else {
notify(stats.passes + ' tests passed in ' + stats.duration + 'ms', {
name: 'mocha',
title: 'Passed',
image: image('ok')
plroebuck marked this conversation as resolved.
Show resolved Hide resolved
});
}
});
};

/**
* Sets `grep` filter after escaping RegExp special characters.
*
Expand Down Expand Up @@ -440,18 +407,49 @@ Mocha.prototype.fullTrace = function() {
};

/**
* Enables desktop notification support.
* Enables desktop notification support if prerequisite software installed.
*
* @public
* @see {@link Mocha#isGrowlCapable}
* @see {@link Mocha#_growl}
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.growl = function() {
this.options.growl = true;
this.options.growl = this.isGrowlCapable();
if (!this.options.growl) {
var detail = process.browser
? 'Growl notification support not available in browser... cannot enable!'
: 'Growl notification support not installed... cannot enable!';
plroebuck marked this conversation as resolved.
Show resolved Hide resolved
console.error(detail);
}
return this;
};

/**
* @summary
* Determines if Growl support seems likely.
*
* @description
* <strong>Not available when run in browser.</strong>
*
* @private
* @see {@link Growl#isCapable}
* @see {@link Mocha#growl}
* @return {boolean} whether Growl support can be expected
*/
Mocha.prototype.isGrowlCapable = growl.isCapable;

/**
* Implements desktop notifications using a pseudo-reporter.
*
* @private
* @see {@link Mocha#growl}
* @see {@link Growl#notify}
* @param {Runner} runner - Runner instance.
*/
Mocha.prototype._growl = growl.notify;

/**
* Specifies whitelist of variable names to be expected in global scope.
*
Expand Down Expand Up @@ -721,7 +719,7 @@ Mocha.prototype.run = function(fn) {
runner.globals(options.globals);
}
if (options.growl) {
this._growl(runner, reporter);
this._growl(runner);
}
if (options.useColors !== undefined) {
exports.reporters.Base.useColors = options.useColors;
Expand Down
24 changes: 19 additions & 5 deletions test/unit/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,26 @@ describe('Mocha', function() {
});

describe('.growl()', function() {
it('should set the growl option to true', function() {
var mocha = new Mocha(blankOpts);
mocha.growl();
expect(mocha.options.growl, 'to be', true);
describe('if Growl capable', function() {
it('should set the growl option to true', function() {
var mocha = new Mocha(blankOpts);
mocha.isGrowlCapable = function() {
return true;
};
mocha.growl();
expect(mocha.options.growl, 'to be', true);
});
});
describe('if not Growl capable', function() {
it('should set the growl option to false', function() {
var mocha = new Mocha(blankOpts);
mocha.isGrowlCapable = function() {
return false;
};
mocha.growl();
expect(mocha.options.growl, 'to be', false);
});
});

it('should be chainable', function() {
var mocha = new Mocha(blankOpts);
expect(mocha.growl(), 'to be', mocha);
Expand Down