filematcher.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var path = require('path');
  2. var minimatch = require('minimatch');
  3. function adaptFilePath(srcPath, excludedPath) {
  4. var resolved = path.resolve(excludedPath);
  5. if (resolved.indexOf(srcPath) === 0) {
  6. return resolved;
  7. }
  8. if (excludedPath.charAt(excludedPath.length-1) === path.sep) {
  9. excludedPath = excludedPath.substring(0, excludedPath.length-1);
  10. }
  11. return path.join(srcPath, excludedPath);
  12. }
  13. function matchFilePath(filePath, pathArr) {
  14. for (var i = 0; i < pathArr.length; i++) {
  15. if (minimatch(filePath, pathArr[i])) {
  16. return true;
  17. }
  18. }
  19. return false;
  20. }
  21. module.exports = {
  22. tags: {
  23. /**
  24. * @param {string} testFilePath - file path of a test
  25. * @param {object} opts - test options
  26. * @returns {boolean} true if specified test matches given tag
  27. */
  28. match : function (testFilePath, opts) {
  29. var test = require(testFilePath);
  30. return this.checkModuleTags(test, opts);
  31. },
  32. /**
  33. * @param {object} test - test module
  34. * @param {object} opts - test options
  35. * @returns {boolean}
  36. */
  37. checkModuleTags: function (test, opts) {
  38. var moduleTags = test['@tags'] || test.tags;
  39. var match = true;
  40. if (!Array.isArray(moduleTags)) {
  41. return typeof opts.tag_filter == 'undefined' && typeof opts.skiptags != 'undefined';
  42. }
  43. if (opts.tag_filter || opts.skiptags){
  44. moduleTags = this.convertTagsToString(moduleTags);
  45. if (opts.tag_filter) {
  46. match = this.containsTag(moduleTags, this.convertTagsToString(opts.tag_filter));
  47. }
  48. if (opts.skiptags) {
  49. match = this.excludesTag(moduleTags, this.convertTagsToString(opts.skiptags));
  50. }
  51. }
  52. return match;
  53. },
  54. convertTagsToString : function(tags) {
  55. return [].concat(tags).map(function (tag) {
  56. return String(tag).toLowerCase();
  57. });
  58. },
  59. containsTag : function(moduleTags, tags) {
  60. return moduleTags.some(function (testTag) {
  61. return (tags.indexOf(testTag) > -1);
  62. });
  63. },
  64. excludesTag : function(moduleTags, tags) {
  65. return moduleTags.every(function (testTag) {
  66. return (tags.indexOf(testTag) == -1);
  67. });
  68. }
  69. },
  70. exclude : {
  71. adaptFilePath : function(filePath, excludedPath) {
  72. if (!Array.isArray(excludedPath)) {
  73. excludedPath = [excludedPath];
  74. }
  75. return excludedPath.map(function(item) {
  76. return adaptFilePath(filePath, item);
  77. });
  78. },
  79. match : matchFilePath
  80. },
  81. filter : {
  82. adaptFilePath : adaptFilePath,
  83. match : matchFilePath
  84. }
  85. };