| 1 |
/*jslint nomen: true */ |
| 2 |
var path = require('path'), |
| 3 |
glob = require('glob'), |
| 4 |
root = path.resolve(__dirname, 'data', 'matcher'), |
| 5 |
src = '../../lib/util/file-matcher.js', |
| 6 |
fileMatcher = require(src), |
| 7 |
allFiles; |
| 8 |
|
| 9 |
module.exports = { |
| 10 |
setUp: function (cb) { |
| 11 |
if (!allFiles) { |
| 12 |
glob('**/*.js', { cwd: root}, function (err, files) { |
| 13 |
allFiles = files.map(function (file) { return path.resolve(root, file); }); |
| 14 |
cb(); |
| 15 |
}); |
| 16 |
} else { |
| 17 |
cb(); |
| 18 |
} |
| 19 |
}, |
| 20 |
"should return all files except those under node_modules by default": function (test) { |
| 21 |
fileMatcher.filesFor(function (err, files) { |
| 22 |
test.ok(!err); |
| 23 |
allFiles.forEach(function (file) { |
| 24 |
var matcher = function (f) { return f === file; }, |
| 25 |
shouldMatch = file.indexOf('file.js') < 0; |
| 26 |
if (shouldMatch) { |
| 27 |
test.ok(files.filter(matcher).length, 'Should match [' + file + '] but did not'); |
| 28 |
} else { |
| 29 |
test.ok(!files.filter(matcher).length, 'Should NOT match [' + file + '] but did'); |
| 30 |
} |
| 31 |
}); |
| 32 |
test.done(); |
| 33 |
}); |
| 34 |
}, |
| 35 |
"should return relative filenames when requested": function (test) { |
| 36 |
fileMatcher.filesFor({ root: root, relative: true }, function (err, files) { |
| 37 |
test.ok(!err); |
| 38 |
allFiles.forEach(function (file) { |
| 39 |
var matcher = function (f) { return path.resolve(root, f) === file; }, |
| 40 |
shouldMatch = file.indexOf('file.js') < 0; |
| 41 |
if (shouldMatch) { |
| 42 |
test.ok(files.filter(matcher).length, 'Should match [' + file + '] but did not'); |
| 43 |
} else { |
| 44 |
test.ok(!files.filter(matcher).length, 'Should NOT match [' + file + '] but did'); |
| 45 |
} |
| 46 |
}); |
| 47 |
test.done(); |
| 48 |
}); |
| 49 |
}, |
| 50 |
/* |
| 51 |
"should match stuff under cwd": function (test) { |
| 52 |
fileMatcher.matcherFor(function (err, matchFn) { |
| 53 |
test.ok(!err); |
| 54 |
test.ok(matchFn(path.resolve(__dirname, src)), 'should match itself'); |
| 55 |
test.done(); |
| 56 |
}); |
| 57 |
}, |
| 58 |
"should match stuff under cwd overriding relative opts passed in": function (test) { |
| 59 |
fileMatcher.matcherFor({ relative: true }, function (err, matchFn) { |
| 60 |
test.ok(!err); |
| 61 |
test.ok(matchFn(path.resolve(__dirname, src)), 'should match itself'); |
| 62 |
test.done(); |
| 63 |
}); |
| 64 |
}, |
| 65 |
*/ |
| 66 |
"should ignore node_modules": function (test) { |
| 67 |
fileMatcher.matcherFor({ root: root }, function (err, matchFn) { |
| 68 |
test.ok(!err); |
| 69 |
test.ok(matchFn.files); |
| 70 |
test.deepEqual(allFiles.filter(function (f) { return !f.match(/node_modules/); }).sort(), |
| 71 |
matchFn.files.sort()); |
| 72 |
allFiles.forEach(function (file) { |
| 73 |
var shouldMatch = file.indexOf('file.js') < 0; |
| 74 |
if (shouldMatch) { |
| 75 |
test.ok(matchFn(file), 'Should match [' + file + '] but did not'); |
| 76 |
} else { |
| 77 |
test.ok(!matchFn(file), 'Should NOT match [' + file + '] but did'); |
| 78 |
} |
| 79 |
}); |
| 80 |
test.done(); |
| 81 |
}); |
| 82 |
}, |
| 83 |
"should match stuff with explicit includes and excludes": function (test) { |
| 84 |
fileMatcher.matcherFor({ root: root, includes: [ '**/general/**/*.js' ], excludes: [ '**/general.js' ] }, function (err, matchFn) { |
| 85 |
test.ok(!err); |
| 86 |
allFiles.forEach(function (file) { |
| 87 |
if (file.indexOf('/general/') < 0) { return; } |
| 88 |
var shouldMatch = file.indexOf('file.js') >= 0; |
| 89 |
if (shouldMatch) { |
| 90 |
test.ok(matchFn(file), 'Should match [' + file + '] but did not'); |
| 91 |
} else { |
| 92 |
test.ok(!matchFn(file), 'Should NOT match [' + file + '] but did'); |
| 93 |
} |
| 94 |
}); |
| 95 |
test.done(); |
| 96 |
}); |
| 97 |
} |
| 98 |
}; |