Skip to content

Commit

Permalink
Essentially working. Tests can be defined and executed when a test ma…
Browse files Browse the repository at this point in the history
…tches a URL that is spidered. Need docs, cli more tests and possibly small refactoring.
  • Loading branch information
allanmboyd committed Mar 20, 2012
1 parent 46ae23f commit 28bdcdd
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 165 deletions.
4 changes: 2 additions & 2 deletions examples/tests/htmlTests.js
Expand Up @@ -4,8 +4,8 @@ exports.tests = {
"Common HTML Tests" : {
urlPattern: "\.html$",
tests: {
"All HTML responses should have a statusCode of 200": function(response) {
"200".should.equal(response.statusCode)
"All HTML responses should have a statusCode of 200": function(spiderPayload) {
should.equal(spiderPayload.response.statusCode, 200)
}
}
}
Expand Down
116 changes: 116 additions & 0 deletions lib/reporters/console.js
@@ -0,0 +1,116 @@
var exceptions = require("exceptions");
var formatErrors = require("formaterrors");
var failTheme = new formatErrors.StackTheme();
failTheme.messageLineHighlights = [" "];
failTheme.stackHighlights = [" "];
failTheme.stackHighlightPatterns = ["at"];

var errorTheme = new formatErrors.StackTheme();
errorTheme.messageLineHighlights = [" "];
errorTheme.stackHighlights = [" "];
errorTheme.stackHighlightPatterns = ["at"];

// todo consider making these options configurable as implied by the name
var options = {
"error_prefix": "\u001B[31m",
"error_suffix": "\u001B[39m",
"ok_prefix": "\u001B[32m",
"ok_suffix": "\u001B[39m",
"bold_prefix": "\u001B[1m",
"bold_suffix": "\u001B[22m",
"assertion_prefix": "\u001B[35m",
"assertion_suffix": "\u001B[39m"
};

exports.report = function(suites, Result) {

var error = function (str) {
return options.error_prefix + str + options.error_suffix;
};
var ok = function (str) {
return options.ok_prefix + str + options.ok_suffix;
};
var bold = function (str) {
return options.bold_prefix + str + options.bold_suffix;
};
var assertionMessage = function (str) {
return options.assertion_prefix + str + options.assertion_suffix;
};

var totalTests = 0;
var totalSuccess = 0;
var totalFailed = 0;
var totalErrors = 0;

for (var suiteName in suites) {
if (suites.hasOwnProperty(suiteName)) {
var suite = suites[suiteName];
var output = "\n" + suite.getName();
if (suite.getDescription()) {
output += " - " + suite.getDescription();
}
console.log(bold(output));
var topics = suite.getTopics();
for (var topicName in topics) {
if (topics.hasOwnProperty(topicName)) {
var topic = topics[topicName];
output = topic.getName();
if (topic.getDescription()) {
output += " - " + topic.getDescription();
}
if (topic.getResult() === Result.PASS) {
console.log(ok(bold('✔ ' + output)));
}
if (topic.getResult() === Result.FAIL) {
console.log(error(bold('✖ ' + output)));
}
if (topic.getResult() === Result.ERROR) {
console.log(error(bold('\u274e ' + output)));
}
if (topic.getResult() === Result.NO_TESTS) {
console.log(error(bold('? ' + output + " (NO TESTS FOUND)")));
}

var testResults = topic.getTestResults();
for (var i = 0; i < testResults.length; i += 1) {
var testResult = testResults[i];
if (testResult.getResult() === Result.PASS) {
console.log(' ✔ ' + testResult.getName());
} else if (testResult.getResult() === Result.FAIL) {
failTheme.stackFilters = [testResult.getTestFile()];
var failError = formatErrors.highlightError(testResult.getError(), failTheme);
console.log(' ✖ ' + testResult.getName());
console.log(failError.stack);
// console.log(ae.stack + '\n');
} else if (testResult.getResult() === Result.ERROR) {
var formattedError = formatErrors.highlightError(testResult.getError(), errorTheme);
console.log(" \u274e " + testResult.getName() + '\n');
console.log(formattedError.stack);
} else {
exceptions.ILLEGAL_STATE.thro("Unknown test result: " + testResult);
}
var detail = testResults[i].getName() + ": " + testResults[i].getResult();
if (testResults[i].getError() !== null) {
detail += " - " + testResults[i].getError();
}
}
}
}

totalTests += suite.getTestCount();
totalSuccess += suite.getSuccessCount();
totalFailed += suite.getFailedCount();
totalErrors += suite.getErrorCount();
}
}


var summary = "\nTests: " + totalTests + ", Passed: " + totalSuccess +
", Failed: " + totalFailed + ", Errors: " + totalErrors;

if (totalFailed === 0 && totalErrors === 0) {
console.log(bold(ok(summary)));
} else {
console.log(bold(error(summary)));
}
}
51 changes: 43 additions & 8 deletions lib/spiderTest.js
@@ -1,11 +1,12 @@
var fs = require("fs");
var spider = require("./spider");
var spiderOptions = require("./spiderOptions");
var suiteManager = require("./suiteManager");
var timers = require("timers");
var urlModule = require("url");
var util = require("util");

var SPIDER_COMPLETED_TIMEOUT = 1500; // todo make configurable
var SPIDER_COMPLETED_TIMEOUT = 1000; // todo make configurable
var timeout;

var autoSpiderAll = spider({
Expand All @@ -16,14 +17,20 @@ var autoSpiderAll = spider({

/**
* Run spider tests.
* @param startUrl
* @param testsDir
*
* @param startUrl the initial url from which to start spidering
* @param testsDir directory containing the test definitions - this should be absolute unless baseDir
* is also specifed
* @param {Function} callback optional callback function invoked when all tests are completed
* @param {String} baseDir optional working dir to change to if specified. If testsDir is relative then it is
* relative to this dir.
*/
exports.runTests = function (startUrl, testsDir, baseDir) {
exports.runTests = function (startUrl, testsDir, callback, baseDir) {

var origDir;

if(baseDir) {
origDir = process.cwd();
process.chdir(baseDir);
}
startUrl = resolveUrl(startUrl);
Expand All @@ -32,8 +39,13 @@ exports.runTests = function (startUrl, testsDir, baseDir) {
// todo handle case where there are no tests

var done = function() {
if(base)
console.log("DONE!!!");
if(origDir) {
process.chdir(origDir);
}
suiteManager.generateReport("console");
if(callback) {
callback();
}
};

// :-( cannot think of another to know when all the URLs have been spidered except to timeout
Expand Down Expand Up @@ -67,12 +79,35 @@ var executeMatchingTests = function (payload, $, testFiles) {
var tests = require(testFile).tests;
for (var topic in tests) {
if (tests.hasOwnProperty(topic)) {
console.log(testFile + ": " + topic);
// console.log(testFile + ": " + topic);
var topicName = topic;
topic = tests[topic];
var requestHref = payload.spider.currentUrl;
if(requestHref.match(topic.urlPattern)) {
// console.log("matched " + requestHref + " with " + topic.urlPattern);
var topicTests = topic.tests;
for(var topicTest in topicTests) {
if(topicTests.hasOwnProperty(topicTest)) {
var testName = topicTest;
topicTest = topicTests[topicTest];
var testDetails = {
testName: testName,
topicName: topicName,
suiteName: requestHref,
test: topicTest,
testFile: testFile,
spiderPayload: payload,
$: $
};
suiteManager.runSuiteTest(testDetails);
}
}
}
}
}
});

console.log(util.inspect(payload));
// console.log(util.inspect(payload));

};

Expand Down

0 comments on commit 28bdcdd

Please sign in to comment.