Skip to content

Commit

Permalink
added wildcard * support to files
Browse files Browse the repository at this point in the history
  • Loading branch information
stammen committed Nov 16, 2012
1 parent e85197b commit 70865cc
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 4 deletions.
20 changes: 17 additions & 3 deletions bin/vows
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env node


var path = require('path'),
fs = require('fs'),
util = require('util'),
wildcard = require('../lib/utils/wildcard').wildcard,
events = require('events');

//
Expand Down Expand Up @@ -70,11 +72,13 @@ var options = {
};

var files = [];
var wildcardFiles = [];

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


// Current directory index,
// and path of test folder.
var root, testFolder;
Expand Down Expand Up @@ -251,12 +255,20 @@ if (! options.watch) {
reporter.reset = function () { _reporter.reset && _reporter.reset() };
reporter.print = _reporter.print;

files = args.map(function (a) {
// preprocess the list of files for any wildcards. win32 does not handle wildcards before calling vows
// any paths not containing wildcards are simple returned by wildcard()
args.forEach(function(a) {
wildcardFiles = wildcardFiles.concat(wildcard(path.join(process.cwd(), a)));
});

// now set up the file list for vows including all the wildcard files
files = wildcardFiles.map(function (a) {
return (!a.match(/^\//))
? path.join(process.cwd(), a.replace(fileExt, ''))
: a.replace(fileExt, '');
});


if (options.shuffle) {
var source = files.slice(0);
files.length = 0;
Expand Down Expand Up @@ -503,7 +515,7 @@ function importSuites(files) {
buffer = [data.pop()];

data.forEach(function (data) {
if (data) {
if (data && data !== 'undefined') {
data = JSON.parse(data);
if (data && data[0] === 'finish') {
result = data[1];
Expand All @@ -520,11 +532,13 @@ function importSuites(files) {
}

return files.reduce(options.isolate ? function (suites, f) {

return suites.concat({
run: wrapSpawn(f)
});
} : function (suites, f) {
f = path.relative(__dirname,f);
console.log(f);
f = path.relative(__dirname, f);
f = f.replace(/\\/g,'/');
f = f.replace('../C:','C:');
var obj = require(f);
Expand Down
114 changes: 114 additions & 0 deletions lib/utils/wildcard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* (C) Microsoft Open Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var PATH = require('path');
var fs = require('fs');

/**
* @type {Function} wildcard(pattern)
* wildcard searches for files matching pattern.
*
* @pattern {String} search pattern with optional * wildcards
* @return an array containing the paths to the matching files
*/
var wildcard = exports.wildcard = function(pattern) {

// process pattern string for * wildcard
var matchers = [],
tokens,
path;

var index = pattern.indexOf('*');
if(index === -1) {
return [pattern];
}

path = pattern.substr(0, index-1);
pattern = pattern.substr(index);
pattern = pattern.replace(/\*/g, '(?=.*)');
tokens = pattern.split(PATH.sep);

// create matcher regex for each path component in pattern
tokens.forEach(function(token, index, array) {
var matcher = {};
matcher.index = index;
matcher.isDir = index < array.length -1;
matcher.regex = new RegExp(token);
matchers.push(matcher);
});

return process(path, matchers);
};

// searches starting from the path directory and returns files matching wildcard pattern
// search only proceeds to directory depth equal to the numbe rof matchers.
var process = function(path, matchers) {

var files = [];
var traverse = function(path, level) {
var dirs,
matcher;

// check we have not exceeded search directory depth
if(level >= matchers.length) {
return;
}

// read the dirs and files from the current path
dirs = fs.readdirSync(path);
matcher = matchers[level];

// check if each dir or file matches the matcher for the current directory level
for(var i = 0; i < dirs.length; i++) {
var dir = dirs[i];

if(dir.match(matcher.regex) === null) {
continue;
}

var pathName = PATH.join(path,dir);

var stats = fs.statSync(pathName);
if(stats.isDirectory()) {
if(matcher.isDir) {
traverse(pathName, level + 1);
} else {
continue;
}
}
else if(level === matchers.length - 1) {
// found a matching file
if(stats.isFile()) {
files.push(pathName);
}
}
}
};

traverse(path, 0);
return files;
};



//var pattern = '/Users/stammen/dev/microsoft/pkgcloud/test/*/*/*-test.js';
//
//var result = wildcard(pattern);
//console.log(result);
//console.log(result.length);




1 change: 0 additions & 1 deletion test/isolate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ function generateTopic(args, file) {
' ./test/fixtures/isolate/' + file,
options = {cwd: path.resolve(__dirname + '/../')},
callback = this.callback;
console.log(cmd);
exec(cmd, options, function (err, stdout, stderr) {
callback(null, {
err: err,
Expand Down

0 comments on commit 70865cc

Please sign in to comment.