diff --git a/CHANGELOG.md b/CHANGELOG.md index 715cfde..27e845d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,3 +35,7 @@ * mocha 0.14.x * blanket 1.1.5 * Changed search location for `mocha` (resolved #17) + +### 0.1.1 (Dec 6, 2013) + +* Will now create any missing directories when writing output to a file (resolved #19) diff --git a/lib/mocha.js b/lib/mocha.js index 83a9155..5fb8d1f 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -8,6 +8,7 @@ var grunt = require('grunt'), path = require('path'), fs = require('fs'), + mkdirp = require('mkdirp'), coveralls = require('./coveralls'); var BOOL_OPTIONS = [ @@ -153,9 +154,19 @@ function mocha(options, callback) { // Save report to disk } else if (options.output) { - var filename = path.resolve(options.output); - fs.writeFile(filename, String(result), 'utf8', function (err) { - callback(err); + var filename = path.resolve(options.output), + dir = path.dirname(filename); + + fs.stat(dir, function (err) { + + if (err) { + mkdirp.sync(dir); + } + + fs.writeFile(filename, String(result), 'utf8', function (err) { + callback(err); + }); + }); // Send report to stdout diff --git a/package.json b/package.json index e40246f..61d86e0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "grunt-mocha-cov", "description": "Run Mocha server-side tests in Grunt with code coverage support and optional integration to coveralls.io.", - "version": "0.1.0", + "version": "0.1.1", "author": "Mike Moulton (http://meltmedia.com)", "contributors": [ "Gregg Caines (https://github.com/cainus/node-coveralls)", @@ -35,7 +35,8 @@ "blanket": "1.1.5", "lcov-parse": "0.0.5", "request": "2.27.0", - "mocha-lcov-reporter": "0.0.1" + "mocha-lcov-reporter": "0.0.1", + "mkdirp": "0.3.5" }, "peerDependencies": { "grunt": "0.4.x" diff --git a/test/mocha.js b/test/mocha.js index 17d899b..cd794dc 100644 --- a/test/mocha.js +++ b/test/mocha.js @@ -143,6 +143,25 @@ describe('Unit Tests', function () { }); }); + it('should test coverage output when directory does not exist', function (done) { + + mocha({ + files: [__dirname + '/fixture/pass.js'], + quiet: true, + reporter: 'json-cov', + output: 'test/new/out.json' + }, function (error) { + should.not.exist(error); + var filename = path.resolve('test/new/out.json'), + dir = path.dirname(filename); + var jsonOutput = JSON.parse(fs.readFileSync(filename)); + fs.unlinkSync(filename); + fs.rmdirSync(dir); + jsonOutput.coverage.should.equals(100); + done(); + }); + }); + it('should test coveralls integration', function (done) { mocha({