Skip to content

Commit

Permalink
Fixes #43 add failWithOutput option for CircleCI and other CIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Cataldo committed Sep 2, 2015
1 parent 0f61110 commit eaf0722
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 133 deletions.
20 changes: 20 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "standard",
"env": {
"node": true,
"mocha": true
},
"globals": {
"expect": true
},
"rules": {
"semi": [2, "always"],
"one-var": [2, "always"],
"indent": [2, 2, {"VariableDeclarator": 2}],
"no-multi-spaces": [2, {
"exceptions": {
"VariableDeclarator": true
}
}]
}
}
14 changes: 0 additions & 14 deletions .jshintrc

This file was deleted.

24 changes: 9 additions & 15 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,25 @@

'use strict';

module.exports = function(grunt) {
module.exports = function (grunt) {

// Project configuration.
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'tasks/*.js'
],
options: {
jshintrc: '.jshintrc',
},
eslint: {
all: ['Gruntfile.js', 'tasks/*.js', 'test/*.js']
},
connect: {
server: {
options: {
port: 8000,
base: '.',
base: '.'
}
}
},
mocha_phantomjs: {
no_output: {
options: {
reporter: 'dot',
reporter: 'dot'
},
files: {
src: ['test/index.html']
Expand Down Expand Up @@ -63,8 +57,8 @@ module.exports = function(grunt) {
reporter: 'dot',
config: {
viewportSize: {
width: 1024,
height: 768
width: 1024,
height: 768
}
}
},
Expand Down Expand Up @@ -99,9 +93,9 @@ module.exports = function(grunt) {
});

grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-eslint');
grunt.loadTasks('tasks');

grunt.registerTask('default', ['jshint', 'connect', 'mocha_phantomjs']);
grunt.registerTask('default', ['eslint', 'connect', 'mocha_phantomjs']);

};
37 changes: 18 additions & 19 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
Copyright (c) 2013 Justin Cataldo
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
Copyright (c) Justin Cataldo <jdcataldo@gmail.com>

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ Type: `String`

The file that the task should output the results to. If `output` is specified, the task will always complete and not throw an error code if errors are found. The CI will determine if the build failed or not.

#### failWithOutput
Type: `Boolean`

Setting `failWithOutput` to true when used with the `output` option will fail a build along with creating a test artifact. This is useful for CIs (CircleCI) that don't fail a build based on the test artifact.

#### silent
Type: `Boolean`

Setting `silent` to true will prevent the results from being printed using stdout.


#### urls
Type: `Array`
Default: `[]`
Expand Down Expand Up @@ -146,8 +150,9 @@ grunt.registerTask('test', ['connect', 'mocha_phantomjs']);

## Release History

* 2015-09-09   v1.0.2   Fix path to phantomjs binary on windows
* 2015-09-09   v1.0.1   Fix issue when using --color=false
* 2015-09-02   v2.0.0   adds a failWithOutput option for failing a build and also providing a test artifact
* 2015-08-09   v1.0.2   Fix path to phantomjs binary on windows
* 2015-08-09   v1.0.1   Fix issue when using --color=false
* 2015-07-16   v1.0.0   *BREAKING* use mocha-phantomjs-core
* 2015-07-16   v0.7.0   Upgrade mocha-phantomjs to 3.6.0
* 2015-07-15   v0.6.2   Lock down phantomjs to match mocha-phantomjs peerDependency
Expand Down
56 changes: 26 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,47 @@
{
"name": "grunt-mocha-phantomjs",
"version": "2.0.0",
"description": "Run client-side mocha tests using phantomjs",
"version": "1.0.2",
"homepage": "https://github.com/jdcataldo/grunt-mocha-phantomjs",
"author": {
"name": "Justin Cataldo",
"email": "jdcataldo@gmail.com"
},
"repository": {
"type": "git",
"url": "git://github.com/jdcataldo/grunt-mocha-phantomjs.git"
},
"bugs": {
"url": "https://github.com/jdcataldo/grunt-mocha-phantomjs/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/jdcataldo/grunt-mocha-phantomjs/blob/master/LICENSE-MIT"
}
],
"scripts": {
"test": "grunt"
},
"main": "Gruntfile.js",
"engines": {
"node": ">= 0.10.0"
"directories": {
"test": "test"
},
"dependencies": {
"async": "^1.4.0",
"lodash": "^3.10.0",
"mocha-phantomjs-core": "^1.0.0",
"object-assign": "^4.0.1",
"phantomjs": "^1.9.17"
},
"devDependencies": {
"chai": "*",
"grunt-contrib-connect": "^0.10.1",
"grunt-contrib-jshint": "~0.8.0",
"mocha": "*"
"chai": "^3.2.0",
"eslint": "^1.3.1",
"eslint-config-standard": "^4.1.0",
"eslint-plugin-standard": "^1.3.0",
"grunt-contrib-connect": "^0.11.2",
"grunt-contrib-jshint": "^0.11.2",
"grunt-eslint": "^17.1.0",
"mocha": "^2.3.0"
},
"peerDependencies": {
"grunt": "~0.4.0"
},
"scripts": {
"test": "grunt"
},
"repository": {
"type": "git",
"url": "https://github.com/jdcataldo/grunt-mocha-phantomjs.git"
},
"keywords": [
"gruntplugin",
"mocha",
"mocha-phantomjs",
"phantomjs"
]
],
"author": "Justin Cataldo",
"license": "MIT",
"bugs": {
"url": "https://github.com/jdcataldo/grunt-mocha-phantomjs/issues"
},
"homepage": "https://github.com/jdcataldo/grunt-mocha-phantomjs"
}
84 changes: 44 additions & 40 deletions tasks/mocha_phantomjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,96 @@
* grunt-mocha-phantomjs
* https://github.com/jdcataldo/grunt-mocha-phantomjs
*
* Copyright (c) 2013 Justin Cataldo
* Copyright (c) Justin Cataldo <jdcataldo@gmail.com>
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {
var _ = require('lodash'),
async = require('async'),
path = require("path"),
fs = require('fs');
module.exports = function (grunt) {
var objectAssign = require('object-assign'),
async = require('async'),
path = require('path'),
fs = require('fs'),
lookup = function (script, executable) {
var i = 90,
absPath;

var lookup = function(script, executable) {
for (var i = 0; i < module.paths.length; i++) {
var absPath = path.join(module.paths[i], script);
if (executable && process.platform === 'win32') {
absPath += '.cmd';
for (i = 0; i < module.paths.length; i++) {
absPath = path.join(module.paths[i], script);
if (executable && process.platform === 'win32') {
absPath += '.cmd';
}
if (fs.existsSync(absPath)) {
return absPath;
}
}
if (fs.existsSync(absPath)) {
return absPath;
}
}
grunt.fail.warn('Unable to find ' + script);
};
grunt.fail.warn('Unable to find ' + script);
},
flatten = function (arr) {
var flattened = [];
return flattened.concat.apply(flattened, arr);
};

grunt.registerMultiTask('mocha_phantomjs', 'Run client-side mocha test with phantomjs.', function() {
grunt.registerMultiTask('mocha_phantomjs', 'Run client-side mocha test with phantomjs.', function () {
// Merge options
var options = this.options({
reporter: 'spec',
// Non file urls to test
urls: [],

urls: [] // Non file urls to test
}),
config = _.extend({ useColors: true }, options.config),
files = this.filesSrc,
args = [],
config = objectAssign({ useColors: true }, options.config),
phantomPath = lookup('.bin/phantomjs', true),
mochaPhantomPath = lookup('mocha-phantomjs-core/mocha-phantomjs-core.js'),
urls = options.urls.concat(this.filesSrc),
done = this.async(),
errors = 0,
results = '',
output = options.output || false,
silent = options.silent || false;
failWithOutput = options.failWithOutput || false,
silent = options.silent || false,
reportFailure = !output || (output && failWithOutput),
writeStream;

if(output) {
if (output) {
grunt.file.mkdir(path.dirname(output));
var writeStream = fs.createWriteStream(output);
writeStream = fs.createWriteStream(output);
}

if(grunt.option('color') === false) {
if (grunt.option('color') === false) {
config.useColors = false;
}

async.eachSeries(urls, function(f, next) {
async.eachSeries(urls, function (f, next) {
var phantomjs = grunt.util.spawn({
cmd: phantomPath,
args: _.flatten([mochaPhantomPath, f, options.reporter, JSON.stringify(config)])
}, function(error, result, code) {
args: flatten([mochaPhantomPath, f, options.reporter, JSON.stringify(config)])
}, function () {
next();
});

if(!silent) {
if (!silent) {
phantomjs.stdout.pipe(process.stdout);
phantomjs.stderr.pipe(process.stderr);
}

// Write output to file
if(output) {
if (output) {
phantomjs.stdout.pipe(writeStream, { end: false });
}

phantomjs.on('exit', function(code){
phantomjs.on('exit', function (code) {
if (code === 127) {
grunt.fail.warn("Phantomjs isn't installed");
grunt.fail.warn('Phantomjs isn\'t installed');
}
errors += code;
});

}, function(){
}, function () {
// Fail if errors are reported and we aren't outputing to a file
if(!output && errors > 0) {
grunt.fail.warn(errors + " tests failed");
if (reportFailure && errors > 0) {
grunt.fail.warn(errors + ' tests failed');
}

if(output) {
if (output) {
writeStream.end();
}

Expand Down
4 changes: 0 additions & 4 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
mocha.ui('bdd');
expect = chai.expect;
</script>



<script src="test.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript" charset="utf-8">
mocha.run();
</script>
Expand Down
Loading

0 comments on commit eaf0722

Please sign in to comment.