diff --git a/ospec/README.md b/ospec/README.md index 9b0b416c..ff2060ec 100644 --- a/ospec/README.md +++ b/ospec/README.md @@ -290,23 +290,49 @@ _o.run() ### Running the test suite from the command-line -ospec will automatically evaluate all `*.js` files in any folder named `/tests`. - -`o.run()` is automatically called by the cli - no need to call it in your test code. - -#### Create an npm script in your package: +Create a script in your package.json: ``` "scripts": { - ... "test": "ospec", ... } ``` +...and run it from the command line: ``` - $ npm test +$ npm test ``` +ospec will by default evaluate all `*.js` files in any sub-folder named `/tests` - ignoring files inside the `node_modules` folder. + +So, running ospec without arguments is thus effectively the same as: + +``` +ospec '**/tests/**/*.js' +``` + +**NOTE:** `o.run()` is automatically called by the cli - no need to call it in your test code. + +ospec accepts a list of file-patterns (globs) giving you full control over which files are evaluated: + +``` +ospec '**/tests/**/*.js' '**/*.test.js' +``` + +Also, if you wish to skip some files (**in addition to** those under `node_modules`) add a `--ignore` flag with a list of file-patterns to ignore, like so: + +``` +ospec --ignore 'folder1/**' 'folder2/**' +``` + +...or: + +``` +ospec '**/*.test.js' '**/*-test.js' --ignore 'folder1/**' 'folder2/**' +``` + + + #### Direct use from the command line Ospec doesn't work when installed globally. Using global scripts is generally a bad idea since you can end up with different, incompatible versions of the same package installed locally and globally. diff --git a/ospec/bin/ospec b/ospec/bin/ospec old mode 100644 new mode 100755 index 00ab7fab..247d5ca6 --- a/ospec/bin/ospec +++ b/ospec/bin/ospec @@ -1,48 +1,37 @@ #!/usr/bin/env node "use strict" -var fs = require("fs") -var path = require("path") +const o = require("../ospec") +const path = require("path") +const glob = require("glob") -var o = require("../ospec") -function traverseDirectory(pathname, callback) { - pathname = pathname.replace(/\\/g, "/") - return new Promise(function(resolve, reject) { - fs.lstat(pathname, function(err, stat) { - if (err) reject(err) - if (stat && stat.isDirectory()) { - fs.readdir(pathname, function(err, pathnames) { - if (err) reject(err) - var promises = [] - for (var i = 0; i < pathnames.length; i++) { - if (pathnames[i] === "node_modules") continue - if (pathnames[i][0] === ".") continue - pathnames[i] = path.join(pathname, pathnames[i]) - promises.push(traverseDirectory(pathnames[i], callback)) - } - callback(pathname, stat, pathnames) - resolve(Promise.all(promises)) - }) - } - else { - callback(pathname, stat) - resolve(pathname) - } - }) +const parseArgs = (argv) => { + argv = ["--globs"].concat(argv.slice(2)) + const args = {} + let name + argv.forEach((arg) => { + if (/^--/.test(arg)) { + name = arg.substr(2) + args[name] = args[name] || [] + } else { + args[name].push(arg) + } }) + return args } -traverseDirectory(".", function(pathname) { - if (pathname.match(/(?:^|\/)tests\/.*\.js$/)) { - require(path.normalize(process.cwd()) + "/" + pathname) // eslint-disable-line global-require - } -}) -.then(o.run) -.catch(function(e) { - console.log(e.stack) -}) -process.on("unhandledRejection", function(e) { - console.log("Uncaught (in promise) " + e.stack) -}) +const args = parseArgs(process.argv) +const globList = args.globs && args.globs.length ? args.globs : ["**/tests/**/*.js"] +const ignore = ["**/node_modules/**"].concat(args.ignore||[]) +const cwd = process.cwd() + +globList.forEach((globPattern) => { + glob(globPattern, {ignore}) + .on("match", (fileName) => { require(path.join(cwd, fileName)) }) // eslint-disable-line global-require + .on("error", (e) => console.error(e)) + .on("end", o.run) +}); + +process.on("unhandledRejection", (e) => { console.log("Uncaught (in promise) " + e.stack) }) diff --git a/ospec/package.json b/ospec/package.json index acc0ae47..449d8fd2 100644 --- a/ospec/package.json +++ b/ospec/package.json @@ -12,5 +12,8 @@ "bin": { "ospec": "./bin/ospec" }, - "repository": "MithrilJS/mithril.js" + "repository": "MithrilJS/mithril.js", + "devDependencies": { + "glob": "^7.1.2" + } } diff --git a/package.json b/package.json index 6597d49e..566156eb 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "dedent": "^0.7.0", "eslint": "^3.19.0", "gh-pages": "^0.12.0", + "glob": "^7.1.2", "istanbul": "^0.4.5", "lint-staged": "^4.0.4", "locater": "^1.3.0",