forked from nightwatchjs/nightwatch
/
filematcher.js
93 lines (80 loc) · 2.18 KB
/
filematcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var path = require('path');
var minimatch = require('minimatch');
module.exports = {
tags: {
/**
* @param {string} testFilePath - file path of a test
* @param {Array} tags - tags to match
* @returns {boolean} true if specified test matches given tag
*/
match : function (testFilePath, tags) {
var test;
try {
test = require(testFilePath);
} catch (e) {
// could not load test module
return false;
}
return this.checkModuleTags(test, tags);
},
/**
* @param {object} test - test module
* @param {Array} tags - tags to match
* @returns {boolean}
*/
checkModuleTags: function (test, tags) {
var testTags = test.tags;
var match = false;
if (typeof tags === 'string') {
tags = [tags];
}
if (!Array.isArray(testTags)) {
return false;
}
tags = tags.map(function (tag) {
return tag.toLowerCase();
});
match = testTags
.map(function (testTag) {
return testTag.toLowerCase();
})
.some(function (testTag) {
return (tags.indexOf(testTag) !== -1);
});
return match;
}
},
exclude : {
adaptFilePath : function(filePath, excludedPath) {
if (!Array.isArray(excludedPath)) {
excludedPath = [excludedPath];
}
return excludedPath.map(function(item) {
// remove trailing slash
if (item.charAt(item.length-1) === path.sep) {
item = item.substring(0, item.length-1);
}
return path.join(filePath, item);
});
},
match : function(filePath, excludePath) {
for (var i = 0; i < excludePath.length; i++) {
if (minimatch(filePath, excludePath[i])) {
return true;
}
}
return false;
}
},
filter : {
adaptFilePath : function(filePath, filterPath) {
if (filterPath.charAt(filterPath.length-1) === path.sep) {
filterPath = filterPath.substring(0, filterPath.length-1);
}
return path.join(filePath, filterPath);
},
match : function(filePath, filterPath) {
return minimatch(filePath, filterPath);
}
}
};