Skip to content

Commit

Permalink
Have nonull: true cause the task to fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksonrayhamilton committed Jun 5, 2016
1 parent dcc347f commit 4d5f6b6
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 15 deletions.
30 changes: 25 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ module.exports = function(grunt) {
'tmp/custom_options': ['test/fixtures/file1', 'test/fixtures/file2']
}
},
handling_invalid_files: {
src: ['test/fixtures/file1', 'invalid_file/should_warn/but_not_fail', 'test/fixtures/file2'],
dest: 'tmp/handling_invalid_files',
nonull: true
},
process_function: {
options: {
process: function(src, filepath) {
Expand Down Expand Up @@ -160,6 +155,31 @@ module.exports = function(grunt) {

});

// Encapsulates tasks which invoke `grunt.fail.warn` and should abort.
grunt.registerTask('concat-warn', function() {
grunt.config('concat', {
handling_invalid_files: {
src: ['test/fixtures/file1', 'invalid_file/should_warn/and_abort', 'test/fixtures/file2'],
dest: 'tmp/handling_invalid_files',
nonull: true
}
});
grunt.task.run(['concat']);
});

// Encapsulates tasks which invoke `grunt.fail.warn` and should abort, but the
// test is checking behavior in the case of a user-specified `--force` flag.
grunt.registerTask('concat-force', function() {
grunt.config('concat', {
handling_invalid_files_force: {
src: ['test/fixtures/file1', 'invalid_file/should_warn/but_not_fail', 'test/fixtures/file2'],
dest: 'tmp/handling_invalid_files_force',
nonull: true
}
});
grunt.task.run(['concat']);
});

// Actually load this plugin's task(s).
grunt.loadTasks('tasks');

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ grunt.initConfig({
```

#### Invalid or Missing Files Warning
If you would like the `concat` task to warn if a given file is missing or invalid be sure to set `nonull` to `true`:
If you would like the `concat` task to warn and abort if a given file is missing or invalid be sure to set `nonull` to `true`:

```js
grunt.initConfig({
Expand All @@ -249,6 +249,8 @@ grunt.initConfig({
});
```

Additionally invoke grunt with `--force` to skip over missing files.

See [configuring files for a task](http://gruntjs.com/configuring-tasks#files) for how to configure file globbing in Grunt.


Expand Down Expand Up @@ -298,4 +300,4 @@ grunt.initConfig({

Task submitted by ["Cowboy" Ben Alman](http://benalman.com/)

*This file was generated on Wed Apr 20 2016 08:41:44.*
*This file was generated on Sun Jun 05 2016 08:36:20.*
4 changes: 3 additions & 1 deletion docs/concat-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ grunt.initConfig({
```

## Invalid or Missing Files Warning
If you would like the `concat` task to warn if a given file is missing or invalid be sure to set `nonull` to `true`:
If you would like the `concat` task to warn and abort if a given file is missing or invalid be sure to set `nonull` to `true`:

```js
grunt.initConfig({
Expand All @@ -150,6 +150,8 @@ grunt.initConfig({
});
```

Additionally invoke grunt with `--force` to skip over missing files.

See [configuring files for a task](http://gruntjs.com/configuring-tasks#files) for how to configure file globbing in Grunt.


Expand Down
5 changes: 3 additions & 2 deletions tasks/concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ module.exports = function(grunt) {

// Concat banner + specified files + footer.
var src = banner + f.src.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
// Warn on invalid source files (if nonull was set). They will be
// removed if --force is specified.
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
grunt.fail.warn('Source file "' + filepath + '" not found.');
return false;
}
return true;
Expand Down
32 changes: 27 additions & 5 deletions test/concat_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

var grunt = require('grunt');
var comment = require('../tasks/lib/comment').init(grunt);
var exec = require('child_process').exec;
var path = require('path');
var fs = require('fs');

var execOptions = {cwd: path.join(__dirname, '..')};

function getNormalizedFile(filepath) {
return grunt.util.normalizelf(grunt.file.read(filepath));
Expand All @@ -27,13 +32,30 @@ exports.concat = {
test.done();
},
handling_invalid_files: function(test) {
test.expect(1);
test.expect(3);

var actual = getNormalizedFile('tmp/handling_invalid_files');
var expected = getNormalizedFile('test/expected/handling_invalid_files');
test.equal(actual, expected, 'will have warned, but should not fail.');
exec('grunt concat-warn', execOptions, function(error, stdout) {
test.ok(stdout.indexOf('Warning:') > -1, 'should print a warning.');
test.ok(stdout.indexOf('Aborted due to warnings.') > -1, 'should abort.');

test.done();
fs.exists('tmp/handling_invalid_files', function(exists) {
test.ok(!exists, 'should not have created a file.');
test.done();
});
});
},
handling_invalid_files_force: function(test) {
test.expect(2);

exec('grunt concat-force --force', execOptions, function(error, stdout) {
test.ok(stdout.indexOf('Warning:') > -1, 'should print a warning.');

var actual = getNormalizedFile('tmp/handling_invalid_files_force');
var expected = getNormalizedFile('test/expected/handling_invalid_files_force');
test.equal(actual, expected, 'should not fail.');

test.done();
});
},
strip_banner: function(test) {
test.expect(10);
Expand Down
File renamed without changes.

0 comments on commit 4d5f6b6

Please sign in to comment.