Skip to content

Commit

Permalink
refactored the runner and matcher to included other matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Jun 25, 2014
1 parent bef7516 commit 425c686
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 87 deletions.
1 change: 0 additions & 1 deletion bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 93 additions & 0 deletions lib/runner/filematcher.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
52 changes: 0 additions & 52 deletions lib/runner/matcher.js

This file was deleted.

37 changes: 10 additions & 27 deletions lib/runner/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -32,17 +32,17 @@ 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();
},

'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();
Expand Down
2 changes: 1 addition & 1 deletion tests/src/runner/testRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@ module.exports = {
test.ok(Object.keys(results.modules).length === 1);
test.done();
});
},
}
};

0 comments on commit 425c686

Please sign in to comment.