Skip to content

Commit

Permalink
chore: add test for flattened 'files' option and clean up code
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Krinkle committed Aug 27, 2018
1 parent a5a2fc0 commit bd1cc47
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
12 changes: 11 additions & 1 deletion gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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'])
Expand Down
31 changes: 18 additions & 13 deletions tasks/grunt-karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bd1cc47

Please sign in to comment.