Skip to content

Commit 70865cc

Browse files
committed
added wildcard * support to files
1 parent e85197b commit 70865cc

File tree

3 files changed

+131
-4
lines changed

3 files changed

+131
-4
lines changed

bin/vows

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env node
22

3+
34
var path = require('path'),
45
fs = require('fs'),
56
util = require('util'),
7+
wildcard = require('../lib/utils/wildcard').wildcard,
68
events = require('events');
79

810
//
@@ -70,11 +72,13 @@ var options = {
7072
};
7173

7274
var files = [];
75+
var wildcardFiles = [];
7376

7477
// Get rid of process runner
7578
// ('node' in most cases)
7679
var arg, args = [], argv = process.argv.slice(2);
7780

81+
7882
// Current directory index,
7983
// and path of test folder.
8084
var root, testFolder;
@@ -251,12 +255,20 @@ if (! options.watch) {
251255
reporter.reset = function () { _reporter.reset && _reporter.reset() };
252256
reporter.print = _reporter.print;
253257

254-
files = args.map(function (a) {
258+
// preprocess the list of files for any wildcards. win32 does not handle wildcards before calling vows
259+
// any paths not containing wildcards are simple returned by wildcard()
260+
args.forEach(function(a) {
261+
wildcardFiles = wildcardFiles.concat(wildcard(path.join(process.cwd(), a)));
262+
});
263+
264+
// now set up the file list for vows including all the wildcard files
265+
files = wildcardFiles.map(function (a) {
255266
return (!a.match(/^\//))
256267
? path.join(process.cwd(), a.replace(fileExt, ''))
257268
: a.replace(fileExt, '');
258269
});
259270

271+
260272
if (options.shuffle) {
261273
var source = files.slice(0);
262274
files.length = 0;
@@ -503,7 +515,7 @@ function importSuites(files) {
503515
buffer = [data.pop()];
504516

505517
data.forEach(function (data) {
506-
if (data) {
518+
if (data && data !== 'undefined') {
507519
data = JSON.parse(data);
508520
if (data && data[0] === 'finish') {
509521
result = data[1];
@@ -520,11 +532,13 @@ function importSuites(files) {
520532
}
521533

522534
return files.reduce(options.isolate ? function (suites, f) {
535+
523536
return suites.concat({
524537
run: wrapSpawn(f)
525538
});
526539
} : function (suites, f) {
527-
f = path.relative(__dirname,f);
540+
console.log(f);
541+
f = path.relative(__dirname, f);
528542
f = f.replace(/\\/g,'/');
529543
f = f.replace('../C:','C:');
530544
var obj = require(f);

lib/utils/wildcard.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* (C) Microsoft Open Technologies, Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var PATH = require('path');
17+
var fs = require('fs');
18+
19+
/**
20+
* @type {Function} wildcard(pattern)
21+
* wildcard searches for files matching pattern.
22+
*
23+
* @pattern {String} search pattern with optional * wildcards
24+
* @return an array containing the paths to the matching files
25+
*/
26+
var wildcard = exports.wildcard = function(pattern) {
27+
28+
// process pattern string for * wildcard
29+
var matchers = [],
30+
tokens,
31+
path;
32+
33+
var index = pattern.indexOf('*');
34+
if(index === -1) {
35+
return [pattern];
36+
}
37+
38+
path = pattern.substr(0, index-1);
39+
pattern = pattern.substr(index);
40+
pattern = pattern.replace(/\*/g, '(?=.*)');
41+
tokens = pattern.split(PATH.sep);
42+
43+
// create matcher regex for each path component in pattern
44+
tokens.forEach(function(token, index, array) {
45+
var matcher = {};
46+
matcher.index = index;
47+
matcher.isDir = index < array.length -1;
48+
matcher.regex = new RegExp(token);
49+
matchers.push(matcher);
50+
});
51+
52+
return process(path, matchers);
53+
};
54+
55+
// searches starting from the path directory and returns files matching wildcard pattern
56+
// search only proceeds to directory depth equal to the numbe rof matchers.
57+
var process = function(path, matchers) {
58+
59+
var files = [];
60+
var traverse = function(path, level) {
61+
var dirs,
62+
matcher;
63+
64+
// check we have not exceeded search directory depth
65+
if(level >= matchers.length) {
66+
return;
67+
}
68+
69+
// read the dirs and files from the current path
70+
dirs = fs.readdirSync(path);
71+
matcher = matchers[level];
72+
73+
// check if each dir or file matches the matcher for the current directory level
74+
for(var i = 0; i < dirs.length; i++) {
75+
var dir = dirs[i];
76+
77+
if(dir.match(matcher.regex) === null) {
78+
continue;
79+
}
80+
81+
var pathName = PATH.join(path,dir);
82+
83+
var stats = fs.statSync(pathName);
84+
if(stats.isDirectory()) {
85+
if(matcher.isDir) {
86+
traverse(pathName, level + 1);
87+
} else {
88+
continue;
89+
}
90+
}
91+
else if(level === matchers.length - 1) {
92+
// found a matching file
93+
if(stats.isFile()) {
94+
files.push(pathName);
95+
}
96+
}
97+
}
98+
};
99+
100+
traverse(path, 0);
101+
return files;
102+
};
103+
104+
105+
106+
//var pattern = '/Users/stammen/dev/microsoft/pkgcloud/test/*/*/*-test.js';
107+
//
108+
//var result = wildcard(pattern);
109+
//console.log(result);
110+
//console.log(result.length);
111+
112+
113+
114+

test/isolate-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ function generateTopic(args, file) {
1010
' ./test/fixtures/isolate/' + file,
1111
options = {cwd: path.resolve(__dirname + '/../')},
1212
callback = this.callback;
13-
console.log(cmd);
1413
exec(cmd, options, function (err, stdout, stderr) {
1514
callback(null, {
1615
err: err,

0 commit comments

Comments
 (0)