From 8e4c8b84b1fa98bf5b240b13a26ed8ae00c290ed Mon Sep 17 00:00:00 2001 From: "J. Lee Coltrane" Date: Mon, 25 Feb 2013 15:54:38 -0500 Subject: [PATCH] use node-glob instead of wildcard for better pattern support node-glob is located here: It supports common filename glob patterns, including patterns like: `'**/*.js'`. This commit allows vows to be called, for example, like this: `vows --spec '**/test-*.js'`. --- bin/vows | 10 ++-- lib/utils/wildcard.js | 114 ------------------------------------------ package.json | 2 +- 3 files changed, 4 insertions(+), 122 deletions(-) delete mode 100644 lib/utils/wildcard.js diff --git a/bin/vows b/bin/vows index 253157e..e7040c1 100755 --- a/bin/vows +++ b/bin/vows @@ -4,7 +4,7 @@ var path = require('path'), fs = require('fs'), util = require('util'), - wildcard = require('../lib/utils/wildcard').wildcard, + glob = require('glob'), NopStream = require('../lib/utils/nopstream').NopStream, events = require('events'); @@ -270,12 +270,8 @@ if (! options.watch) { // 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() files.forEach(function(a) { - if(a.indexOf('*') !== -1) { - wildcardFiles = wildcardFiles.concat(wildcard(a)); - } else { - wildcardFiles.push(a); - } - }); + wildcardFiles = wildcardFiles.concat(glob.sync(a)); + }); // now set up the file list for vows including all the wildcard files files = wildcardFiles.map(function (a) { diff --git a/lib/utils/wildcard.js b/lib/utils/wildcard.js deleted file mode 100644 index 5356723..0000000 --- a/lib/utils/wildcard.js +++ /dev/null @@ -1,114 +0,0 @@ -/** - * (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); - - - - diff --git a/package.json b/package.json index bd16063..75f90dd 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "keywords" : ["testing", "spec", "test", "BDD"], "author" : "Alexis Sellier ", "contributors" : [{ "name": "Charlie Robbins", "email": "charlie.robbins@gmail.com" }], - "dependencies" : {"eyes": ">=0.1.6", "diff": "~1.0.3"}, + "dependencies" : {"eyes": ">=0.1.6", "diff": "~1.0.3", "glob": "3.1.x"}, "main" : "./lib/vows", "bin" : {"vows": "./bin/vows"}, "directories" : {"test": "./test", "bin": "./bin"},