Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing filtering of task src files #1202

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/grunt/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ task.normalizeMultiTaskFiles = function(data, target) {
files.push(obj);
} else if (grunt.util.kindOf(data.files) === 'object') {
for (prop in data.files) {
files.push({src: data.files[prop], dest: grunt.config.process(prop)});
files.push({src: data.files[prop], dest: grunt.config.process(prop), filter: data.filter});
}
} else if (Array.isArray(data.files)) {
grunt.util._.flatten(data.files).forEach(function(obj) {
Expand All @@ -113,7 +113,7 @@ task.normalizeMultiTaskFiles = function(data, target) {
files.push(obj);
} else {
for (prop in obj) {
files.push({src: obj[prop], dest: grunt.config.process(prop)});
files.push({src: obj[prop], dest: grunt.config.process(prop), filter: data.filter});
}
}
});
Expand Down Expand Up @@ -161,11 +161,17 @@ task.normalizeMultiTaskFiles = function(data, target) {
});
}

if (!obj.filter) {
delete obj.filter;
}

// Copy obj properties to result, adding an .orig property.
var result = grunt.util._.extend({}, obj);
// Make a clone of the orig obj available.
result.orig = grunt.util._.extend({}, obj);

delete result.filter;

if ('src' in result) {
// Expose an expand-on-demand getter method as .src.
Object.defineProperty(result, 'src', {
Expand Down
63 changes: 63 additions & 0 deletions test/grunt/task_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,69 @@ exports['task.normalizeMultiTaskFiles'] = {

test.done();
},
'filter': function(test) {
test.expect(3);
var actual, expected, value;

var filter = function(file) {
return (/\/file[0-9]\.js$/).test(file);
};

value = {
dest: 'dest/file1.js',
src: [
'src/file1.js',
'src/file1-123.js'
],
filter: filter
};
actual = grunt.task.normalizeMultiTaskFiles(value, 'ignored');
expected = [
{
dest: 'dest/file1.js', src: ['src/file1.js'], orig: value
}
];
test.deepEqual(actual, expected, 'should filter plain src arrays.');

value = {
files: {
'dest/file1.js': ['src/file1.js', 'src/file1-123.js'],
'dest/file2.js': ['src/file2.js', 'src/file2-123.js']
},
filter: filter
};
actual = grunt.task.normalizeMultiTaskFiles(value, 'ignored');
expected = [
{
dest: 'dest/file1.js', src: ['src/file1.js'], orig: {
dest: 'dest/file1.js', src: value.files['dest/file1.js'],
filter: value.filter
}
},
{
dest: 'dest/file2.js', src: ['src/file2.js'], orig: {
dest: 'dest/file2.js', src: value.files['dest/file2.js'],
filter: value.filter
}
}
];
test.deepEqual(actual, expected, 'should filter file objects.');

value = {
files: [
{ dest: 'dest/file1.js', src: ['src/file1.js', 'src/file1-123.js'], filter: filter },
{ dest: 'dest/file2.js', src: ['src/file2.js', 'src/file2-123.js'], filter: filter }
]
};
actual = grunt.task.normalizeMultiTaskFiles(value, 'ignored');
expected = [
{ dest: 'dest/file1.js', src: ['src/file1.js'], orig: value.files[0] },
{ dest: 'dest/file2.js', src: ['src/file2.js'], orig: value.files[1] }
];
test.deepEqual(actual, expected, 'should filter file arrays.');

test.done();
},
'nonull': function(test) {
test.expect(2);
var actual, expected, value;
Expand Down