Is there a way to configure a sequence of tasks so that specific subsequent ones (I don't want --force on the whole batch) run even if one fails? For example, consider a case like this
- Create some temporary files
- Run some unit tests which involve those temporary files
- Clean up those temporary files
I can do this:
grunt.registerTask('testTheTemp', ['makeTempFiles', 'qunit', 'removeTempFiles']);
But if qunit fails then the removeTempFiles task never runs.
Echoing Marius' comment, the grunt-force-task plugin now provides this functionality. Full details by following the link above, but in a nutshell this is what you need to achieve the desired effect
Then import it into your gruntfile
Finally, just add the force: prefix to the task(s) before the one you always want to run.
Now removeTempFiles will always run even if the test fails.
Perhaps you can create an async grunt task and grunt.util.spawn your desired tasks serially. You can then write some conditional logic for the success/error codes. Something similar to the answer to this question
Here's one workaround. It's not pretty, but it does solve the issue.
You create two extra tasks which you can wrap at the beginning/end of any sequence that you want to continue even over failure. The check for existing value of
grunt.option('force')
is so that you do not overwrite any--force
passed from the command line.I've also submitted a feature request for Grunt to support this natively.
For posterity sake, this might be an improved hack while we wait for that PR from @explunit to land in grunt:
The one issue with using the grunt-force-task plugin mentioned above is that the grunt process will now unconditionally exit with 0 (which means pass).
This is an issue if you want to use grunt in a CI (continuous integration) environment and fail the CI task based on whether your test/build (
qunit
in the OP) passes or fails. I have worked around this issue by adding a new task that uses grunt'sthis.requires
function to test whetherqunit
passed or failed:Now if
qunit
fails grunt will exit with 3, which indicatesTask Error
. Without theexitWithQunitStatus
, the grunt process will exit with 0.this.requires
is documented here : http://gruntjs.com/api/inside-tasks#this.requires . Basically it will fail the current task unless all of the specified "required" tasks have already run and passed.