From 3560bb684850c4a39638c018baec4c887f9a6172 Mon Sep 17 00:00:00 2001 From: Elvis Date: Sun, 27 Dec 2015 02:33:56 -0200 Subject: [PATCH 1/7] fixing --- tasks/lib/MochaWrapper.js | 32 +++++++++++++++++++++++++++++++- tasks/mocha-test.js | 28 ++-------------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/tasks/lib/MochaWrapper.js b/tasks/lib/MochaWrapper.js index fceb2fc..9785fcd 100644 --- a/tasks/lib/MochaWrapper.js +++ b/tasks/lib/MochaWrapper.js @@ -2,6 +2,8 @@ var domain = require('domain'); var fs = require('fs'); var path = require('path'); var Mocha = require('mocha'); +var hooker = require('hooker'); +var mkdirpSync = require('mkdirp').sync; function MochaWrapper(params) { // If require option is specified then require that file. @@ -53,7 +55,35 @@ function MochaWrapper(params) { var mochaSuite = mocha.suite; var mochaOptions = mocha.options; var mochaRunner = new Mocha.Runner(mochaSuite); - var mochaReporter = new mocha._reporter(mochaRunner, mochaOptions); + + var fd; + mochaRunner.on('end', function(){ + if (params.options.captureFile) { + mkdirpSync(path.dirname(params.options.captureFile)); + fd = fs.openSync(params.options.captureFile, 'w'); + } + // Hook process.stdout.write + hooker.hook(process.stdout, 'write', { + // This gets executed before the original process.stdout.write + pre: function(result) { + // Write result to file if it was opened + if (fd) { + fs.writeSync(fd, result); + } + + } + }); + }); + var mochaReporter = new mocha._reporter(mochaRunner, mochaOptions, mocha); + mochaRunner.on('end', function(){ + // close the file if it was opened + if (fd) { + fs.closeSync(fd); + } + // Restore process.stdout.write to its original value + hooker.unhook(process.stdout, 'write'); + }); + mochaRunner.ignoreLeaks = false !== mochaOptions.ignoreLeaks; mochaRunner.asyncOnly = mochaOptions.asyncOnly; if (mochaOptions.grep) { diff --git a/tasks/mocha-test.js b/tasks/mocha-test.js index b92c873..58d718c 100644 --- a/tasks/mocha-test.js +++ b/tasks/mocha-test.js @@ -4,39 +4,14 @@ var MochaWrapper = require('./lib/MochaWrapper'); var fs = require('fs'); var path = require('path'); var hooker = require('hooker'); -var mkdirpSync = require('mkdirp').sync; module.exports = function(grunt) { // Helper to capture task output (adapted from tests for grunt-contrib-jshint) var capture = function(captureFile, quiet, run, done) { - var fd; - if (captureFile) { - mkdirpSync(path.dirname(captureFile)); - fd = fs.openSync(captureFile, 'w'); - } - // Hook process.stdout.write - hooker.hook(process.stdout, 'write', { - // This gets executed before the original process.stdout.write - pre: function(result) { - // Write result to file if it was opened - if (fd) { - fs.writeSync(fd, result); - } - // Prevent the original process.stdout.write from executing if quiet was specified - if (quiet) { - return hooker.preempt(); - } - } - }); + // Execute the code whose output is to be captured run(function(error, failureCount) { - // close the file if it was opened - if (fd) { - fs.closeSync(fd); - } - // Restore process.stdout.write to its original value - hooker.unhook(process.stdout, 'write'); // Actually test the actually-logged stdout string to the expected value done(error, failureCount); }); @@ -91,6 +66,7 @@ module.exports = function(grunt) { } else { complete(null, failureCount); } + }); }, function(error, failureCount) { // restore the uncaught exception handlers From acb6041bfe1c8fff4989e79d7bd1af350260ad68 Mon Sep 17 00:00:00 2001 From: Elvis Date: Sun, 27 Dec 2015 02:45:29 -0200 Subject: [PATCH 2/7] activating quiet --- tasks/lib/MochaWrapper.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tasks/lib/MochaWrapper.js b/tasks/lib/MochaWrapper.js index 9785fcd..9b0d978 100644 --- a/tasks/lib/MochaWrapper.js +++ b/tasks/lib/MochaWrapper.js @@ -70,7 +70,10 @@ function MochaWrapper(params) { if (fd) { fs.writeSync(fd, result); } - + // Prevent the original process.stdout.write from executing if quiet was specified + if (params.options.quiet) { + return hooker.preempt(); + } } }); }); From d14bb4c7d1de0636899281d20e42fb2b2c209889 Mon Sep 17 00:00:00 2001 From: Elvis Date: Sun, 27 Dec 2015 02:33:56 -0200 Subject: [PATCH 3/7] fixing --- tasks/lib/MochaWrapper.js | 32 +++++++++++++++++++++++++++++++- tasks/mocha-test.js | 28 ++-------------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/tasks/lib/MochaWrapper.js b/tasks/lib/MochaWrapper.js index fceb2fc..9785fcd 100644 --- a/tasks/lib/MochaWrapper.js +++ b/tasks/lib/MochaWrapper.js @@ -2,6 +2,8 @@ var domain = require('domain'); var fs = require('fs'); var path = require('path'); var Mocha = require('mocha'); +var hooker = require('hooker'); +var mkdirpSync = require('mkdirp').sync; function MochaWrapper(params) { // If require option is specified then require that file. @@ -53,7 +55,35 @@ function MochaWrapper(params) { var mochaSuite = mocha.suite; var mochaOptions = mocha.options; var mochaRunner = new Mocha.Runner(mochaSuite); - var mochaReporter = new mocha._reporter(mochaRunner, mochaOptions); + + var fd; + mochaRunner.on('end', function(){ + if (params.options.captureFile) { + mkdirpSync(path.dirname(params.options.captureFile)); + fd = fs.openSync(params.options.captureFile, 'w'); + } + // Hook process.stdout.write + hooker.hook(process.stdout, 'write', { + // This gets executed before the original process.stdout.write + pre: function(result) { + // Write result to file if it was opened + if (fd) { + fs.writeSync(fd, result); + } + + } + }); + }); + var mochaReporter = new mocha._reporter(mochaRunner, mochaOptions, mocha); + mochaRunner.on('end', function(){ + // close the file if it was opened + if (fd) { + fs.closeSync(fd); + } + // Restore process.stdout.write to its original value + hooker.unhook(process.stdout, 'write'); + }); + mochaRunner.ignoreLeaks = false !== mochaOptions.ignoreLeaks; mochaRunner.asyncOnly = mochaOptions.asyncOnly; if (mochaOptions.grep) { diff --git a/tasks/mocha-test.js b/tasks/mocha-test.js index 47274d1..8e1f27b 100644 --- a/tasks/mocha-test.js +++ b/tasks/mocha-test.js @@ -2,39 +2,14 @@ var MochaWrapper = require('./lib/MochaWrapper'); var fs = require('fs'); var path = require('path'); var hooker = require('hooker'); -var mkdirpSync = require('mkdirp').sync; module.exports = function(grunt) { // Helper to capture task output (adapted from tests for grunt-contrib-jshint) var capture = function(captureFile, quiet, run, done) { - var fd; - if (captureFile) { - mkdirpSync(path.dirname(captureFile)); - fd = fs.openSync(captureFile, 'w'); - } - // Hook process.stdout.write - hooker.hook(process.stdout, 'write', { - // This gets executed before the original process.stdout.write - pre: function(result) { - // Write result to file if it was opened - if (fd) { - fs.writeSync(fd, result); - } - // Prevent the original process.stdout.write from executing if quiet was specified - if (quiet) { - return hooker.preempt(); - } - } - }); + // Execute the code whose output is to be captured run(function(error, failureCount) { - // close the file if it was opened - if (fd) { - fs.closeSync(fd); - } - // Restore process.stdout.write to its original value - hooker.unhook(process.stdout, 'write'); // Actually test the actually-logged stdout string to the expected value done(error, failureCount); }); @@ -89,6 +64,7 @@ module.exports = function(grunt) { } else { complete(null, failureCount); } + }); }, function(error, failureCount) { // restore the uncaught exception handlers From 600bbc3588dc76e7cb93fc055061ebb76be3a3cb Mon Sep 17 00:00:00 2001 From: Elvis Date: Sun, 27 Dec 2015 02:45:29 -0200 Subject: [PATCH 4/7] activating quiet --- tasks/lib/MochaWrapper.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tasks/lib/MochaWrapper.js b/tasks/lib/MochaWrapper.js index 9785fcd..9b0d978 100644 --- a/tasks/lib/MochaWrapper.js +++ b/tasks/lib/MochaWrapper.js @@ -70,7 +70,10 @@ function MochaWrapper(params) { if (fd) { fs.writeSync(fd, result); } - + // Prevent the original process.stdout.write from executing if quiet was specified + if (params.options.quiet) { + return hooker.preempt(); + } } }); }); From b7b997bf0ec078ff68be3fbd0bda862bc87446c3 Mon Sep 17 00:00:00 2001 From: Elvis Date: Sun, 27 Dec 2015 02:52:29 -0200 Subject: [PATCH 5/7] documenting --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index cd74816..b9bbbc4 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,29 @@ +# Main difference +On original `grunt-mocha-test` if you have a mocha test that use `console.log` and configuration like that + + mochaTest: { + 'coverage': { + options: { + reporter: 'html-cov', + quiet: true, + captureFile: 'unit-tests.html', + }, + src: ['test.js'] + }, + } + + the test + + describe('grunt-mocha-test', function() { + it('should return 1', function(done) { + console.log('It will out on report file'); + done(); + }); + }); + +In this case the console output will be reported on report file, this fork **fix it** + + # grunt-mocha-test [![Build Status](https://travis-ci.org/pghalliday/grunt-mocha-test.svg)](https://travis-ci.org/pghalliday/grunt-mocha-test) From b82915c873569ab65f5a9a322335a1be94786b72 Mon Sep 17 00:00:00 2001 From: Elvis Date: Sun, 27 Dec 2015 02:55:16 -0200 Subject: [PATCH 6/7] setting package --- package.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 67c94bf..fc9dc1c 100644 --- a/package.json +++ b/package.json @@ -1,19 +1,19 @@ { - "name": "grunt-mocha-test", + "name": "grunt-mocha-test-y", "description": "A grunt task for running server side mocha tests", - "version": "0.12.7", - "homepage": "https://github.com/pghalliday/grunt-mocha-test", + "version": "0.12.8", + "homepage": "https://github.com/mageddo/grunt-mocha-test", "author": { - "name": "Peter Halliday", - "email": "pghalliday@gmail.com", - "url": "http://pghalliday.github.io/" + "name": "Elvis de Freitas", + "email": "edigitalb@gmail.com", + "url": "http://github.com/mageddo/gunt-mocha-test/" }, "repository": { "type": "git", - "url": "git://github.com/pghalliday/grunt-mocha-test.git" + "url": "git://github.com/mageddo/grunt-mocha-test.git" }, "bugs": { - "url": "https://github.com/pghalliday/grunt-mocha-test/issues" + "url": "https://github.com/mageddo/grunt-mocha-test/issues" }, "license": "MIT", "engines": { From b569a3d4d50f5a92137c272ad30d02b191bacf6a Mon Sep 17 00:00:00 2001 From: Elvis Date: Sun, 27 Dec 2015 03:00:37 -0200 Subject: [PATCH 7/7] increasing --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b9bbbc4..2e56e57 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ On original `grunt-mocha-test` if you have a mocha test that use `console.log` a }); }); -In this case the console output will be reported on report file, this fork **fix it** +In this case the console output will be reported on report file, this fork **fix it**. To it work the reporter need to write on the `runner.on('end')` event # grunt-mocha-test