diff --git a/bin/runner.js b/bin/runner.js index 4f3beac506..edf7422ab6 100644 --- a/bin/runner.js +++ b/bin/runner.js @@ -5,7 +5,6 @@ var Logger = require('../lib/util/logger.js'); var cli = require('./_cli.js'); var CliRunner = require('./_clirunner.js'); - // CLI definitions // $ nightwatch -c diff --git a/lib/runner/filematcher.js b/lib/runner/filematcher.js new file mode 100644 index 0000000000..48a8fd7603 --- /dev/null +++ b/lib/runner/filematcher.js @@ -0,0 +1,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); + } + } +}; diff --git a/lib/runner/matcher.js b/lib/runner/matcher.js deleted file mode 100644 index c2bcba31c2..0000000000 --- a/lib/runner/matcher.js +++ /dev/null @@ -1,52 +0,0 @@ -module.exports = { - /** - * @param {string} testFilePath - file path of a test - * @param {array} tags - tags to match - * @returns {boolean} true if specified test matches given tag - */ - tags: 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(); - }); - - testTags - .map(function (testTag) { - return testTag.toLowerCase(); - }) - .some(function (testTag) { - match = (tags.indexOf(testTag) !== -1); - return match; - }); - - return match; - } -}; diff --git a/lib/runner/run.js b/lib/runner/run.js index d41957a765..5b40c166ed 100644 --- a/lib/runner/run.js +++ b/lib/runner/run.js @@ -7,7 +7,7 @@ var mkpath = require('mkpath'); var minimatch = require('minimatch'); var Nightwatch = require('../../index.js'); var Logger = require('../util/logger.js'); -var matcher = require('./matcher'); +var fileMatcher = require('./filematcher.js'); module.exports = new (function() { var globalStartTime; @@ -254,30 +254,18 @@ module.exports = new (function() { function runFiles(paths, cb, opts) { var extensionPattern = /\.js$/; - if (paths.length == 1 && extensionPattern.test(paths[0])) { + if (paths.length === 1 && extensionPattern.test(paths[0])) { paths[0] = paths[0].replace(extensionPattern, ''); return cb(null, paths); } paths.forEach(function(p) { if (opts.exclude) { - if (!Array.isArray(opts.exclude)) { - opts.exclude = [opts.exclude]; - } - opts.exclude = opts.exclude.map(function(item) { - // remove trailing slash - if (item.charAt(item.length-1) === path.sep) { - item = item.substring(0, item.length-1); - } - return path.join(p, item); - }); + opts.exclude = fileMatcher.exclude.adaptFilePath(p, opts.exclude); } if (opts.filter) { - if (opts.filter.charAt(opts.filter.length-1) === path.sep) { - opts.filter = opts.filter.substring(0, opts.filter.length-1); - } - opts.filter = path.join(p, opts.filter); + opts.filter = fileMatcher.filter.adaptFilePath(p, opts.filter); } walk(p, function(err, list) { @@ -289,25 +277,20 @@ module.exports = new (function() { var modules = list.filter(function (filePath) { var filename = filePath.split(path.sep).slice(-1)[0]; - if (opts.exclude) { - for (var i = 0; i < opts.exclude.length; i++) { - if (minimatch(filePath, opts.exclude[i])) { - return false; - } - } + if (opts.exclude && fileMatcher.exclude.match(filePath, opts.exclude)) { + return false; } if (opts.filter) { - return minimatch(filePath, opts.filter); + return fileMatcher.filter.match(filePath, opts.filter); } if (opts.filename_filter) { - console.log('filter', filename, opts.filename_filter); return minimatch(filename, opts.filename_filter); } if (opts.tag_filter) { - return matcher.tags(filePath, opts.tag_filter); + return fileMatcher.tags.match(filePath, opts.tag_filter); } return extensionPattern.exec(filePath); @@ -398,7 +381,7 @@ module.exports = new (function() { if (!match) { return ''; } - return (offset > 0 && (string.charAt(offset-1) != ' ') ? ' ':'') + $1; + return (offset > 0 && (string.charAt(offset-1) !== ' ') ? ' ':'') + $1; }); words = moduleName.split(' ').map(function(word) { @@ -440,7 +423,7 @@ module.exports = new (function() { } runFiles(paths, function runTestModule(err, fullpaths) { - var errorMessage = ['No tests defined! using source folder', paths]; + var errorMessage = ['No tests defined! using source folder:', paths]; if (opts.tag_filter) { errorMessage.push('; using tags:', opts.tag_filter); diff --git a/tests/src/runner/testMatcher.js b/tests/src/runner/testFileMatcher.js similarity index 69% rename from tests/src/runner/testMatcher.js rename to tests/src/runner/testFileMatcher.js index 101aa142b3..d61f2ac453 100644 --- a/tests/src/runner/testMatcher.js +++ b/tests/src/runner/testFileMatcher.js @@ -1,7 +1,7 @@ var BASE_PATH = process.env.NIGHTWATCH_COV ? 'lib-cov' : 'lib'; -var matcher = require('../../../'+ BASE_PATH +'/runner/matcher.js'); +var matcher = require('../../../'+ BASE_PATH +'/runner/filematcher.js'); module.exports = { 'tag: test matching tags': function (test) { @@ -10,7 +10,7 @@ module.exports = { tags: ['home', 'siberia'] }; - var matched = matcher.checkModuleTags(testModule, tags); + var matched = matcher.tags.checkModuleTags(testModule, tags); test.ok(matched === true); test.done(); @@ -22,7 +22,7 @@ module.exports = { tags: ['boroboro', 'siberia'] }; - var matched = matcher.checkModuleTags(testModule, tags); + var matched = matcher.tags.checkModuleTags(testModule, tags); test.ok(matched === false); test.done(); @@ -32,7 +32,7 @@ module.exports = { var tags = ['home', 'login', 'sign-up']; var testModule = {}; - var matched = matcher.checkModuleTags(testModule, tags); + var matched = matcher.tags.checkModuleTags(testModule, tags); test.ok(matched === false); test.done(); @@ -40,9 +40,9 @@ module.exports = { 'tag: test loading module with tags': function (test) { var tags = ['home', 'login', 'sign-up']; - var testModule = require('../../sampletests/tags/sample'); - var matched = matcher.checkModuleTags(testModule, tags); + + var matched = matcher.tags.match(__dirname + '/../../sampletests/tags/sample.js', tags); test.ok(matched === true); test.done(); diff --git a/tests/src/runner/testRunner.js b/tests/src/runner/testRunner.js index 72e09d9a01..51a1b7b5eb 100644 --- a/tests/src/runner/testRunner.js +++ b/tests/src/runner/testRunner.js @@ -121,5 +121,5 @@ module.exports = { test.ok(Object.keys(results.modules).length === 1); test.done(); }); - }, + } };