Skip to content
This repository has been archived by the owner on Nov 27, 2017. It is now read-only.

Commit

Permalink
Fix: Stops task from crashing when an error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Bruce committed Feb 10, 2017
1 parent 40e66ea commit 4c0e900
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
6 changes: 6 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ module.exports = function(grunt) {
'tmp/out_with_important.html': 'test/fixtures/in.html'
},
},
does_not_exist: {
options: {},
files: {
'tmp/out_does_not_exist.html': 'test/fixtures/in_does_not_exist.html'
},
}
},

// Unit tests.
Expand Down
21 changes: 14 additions & 7 deletions tasks/inline_css.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,26 @@ module.exports = function(grunt) {
var index = 0;
var count = this.files.length;

var increaseCount = function () {
index++;
if (index === count) {
done();
}
};

// Iterate over all specified file groups.
this.files.forEach(function(f) {

var filepath = f.src.toString();
if (typeof filepath !== 'string' || filepath === '') {
grunt.log.error('src must be a single string');
increaseCount();
return false;
}

if (!grunt.file.exists(filepath)) {
grunt.log.error('Source file "' + filepath + '" not found.');
increaseCount();
return false;
}

Expand All @@ -41,18 +50,16 @@ module.exports = function(grunt) {
juice.juiceFile(filepath, options, function(err, html) {

if (err) {
return grunt.log.error(err);
grunt.log.error(err);
increaseCount();

return;
}

grunt.file.write(f.dest, html);
grunt.log.writeln('File "' + f.dest + '" created.');


index++;
if (index === count) {
done();
}

increaseCount();
});

});
Expand Down
17 changes: 17 additions & 0 deletions test/inline_css_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,22 @@ exports.inline_css = {
test.equal(actual, expected, 'should inline css');

test.done();
},

does_not_exist: function(test) {
test.expect(0);

try {
var actual = grunt.file.read('tmp/out_does_not_exist.html');
} catch(err) {

if (err.origError.code === 'ENOENT') {
return test.done();
}

return test.done(new Error('Should have errored with a file not found: ' + err.code));
}

test.done(new Error('Should have errored'));
}
};

0 comments on commit 4c0e900

Please sign in to comment.