From bd1cc47adb19eeeedbcc44bed078ce20ae4a3556 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Mon, 27 Aug 2018 03:46:16 +0100 Subject: [PATCH] chore: add test for flattened 'files' option and clean up code - This feature was added in pull #150 without a test. It now has a test. - The code was applying the flatten operation to all input, but should really only be applied to `options.files`, not to this.files from Grunt, which is already processed. - The code was checking `data.files` but using `this.files`. This is working correctly, but looks confusing to a reader. `data.files` is the raw input, and `this.files` is the version of that input after processing by Grunt. To improve readability, use that same reference for the check as well. In order to keep behaviour identical, use the array's length property to determine whether it was set. The files array is always available from Grunt, and can be safely accessed without additional check. This is covered by the karma:config test case, which specifies neither options.files nor data.files. Ref #150. Closes #236. --- gruntfile.js | 12 +++++++++++- tasks/grunt-karma.js | 31 ++++++++++++++++++------------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/gruntfile.js b/gruntfile.js index fc78013..6a696eb 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -100,6 +100,15 @@ module.exports = function (grunt) { } ] }, + flatten: { + options: { + files: [ + [['node_modules/expect.js/index.js']], + [[['test/**/*.js']]] + ] + }, + singleRun: true + }, background: { background: true, files: [ @@ -138,7 +147,8 @@ module.exports = function (grunt) { 'eslint', 'karma:single', 'karma:config', - 'karma:merge' + 'karma:merge', + 'karma:flatten' ]) grunt.registerTask('default', ['test']) grunt.registerTask('bgtest', ['karma:background', 'watch:bgtest']) diff --git a/tasks/grunt-karma.js b/tasks/grunt-karma.js index 40bce1d..97b1cf0 100644 --- a/tasks/grunt-karma.js +++ b/tasks/grunt-karma.js @@ -71,22 +71,27 @@ module.exports = function (grunt) { data.configFile = path.resolve(data.configFile) } - if (data.files || options.files) { - data.files = [].concat.apply(options.files || [], this.files.map(function (file) { - return file.src.map(function (src) { - var obj = { - pattern: src - } - var opts = ['watched', 'served', 'included'] - opts.forEach(function (opt) { - if (opt in file) { - obj[opt] = file[opt] + if (this.files.length || options.files) { + data.files = [].concat.apply( + // For the 'files' option, we support nested arrays as a convenient + // way to specify files without needing the user to concat or + // flatten them ahead of time. + _.flattenDeep(options.files || []), + this.files.map(function (file) { + return file.src.map(function (src) { + var obj = { + pattern: src } + var opts = ['watched', 'served', 'included'] + opts.forEach(function (opt) { + if (opt in file) { + obj[opt] = file[opt] + } + }) + return obj }) - return obj }) - })) - data.files = _.flattenDeep(data.files) + ) } // Allow the use of templates in preprocessors