2017-02-28 18:08:36 +01:00
|
|
|
/* eslint-env node */
|
|
|
|
/* globals phantom */
|
|
|
|
|
2017-02-25 00:50:17 +01:00
|
|
|
/**
|
|
|
|
* PhantomRunner.js
|
|
|
|
*
|
|
|
|
* This file navigates to build/test/index.html and logs the test results.
|
|
|
|
*
|
2017-02-28 18:08:36 +01:00
|
|
|
* @author tlwr [toby@toby.codes]
|
2017-02-25 00:50:17 +01:00
|
|
|
* @copyright Crown Copyright 2017
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2017-02-28 18:08:36 +01:00
|
|
|
var page = require("webpage").create(),
|
|
|
|
allTestsPassing = true,
|
|
|
|
testStatusCounts = {
|
|
|
|
total: 0,
|
|
|
|
};
|
2017-02-25 00:50:17 +01:00
|
|
|
|
2017-02-28 18:08:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to convert a status to an icon.
|
|
|
|
*
|
|
|
|
* @param {string} status
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-02-25 00:50:17 +01:00
|
|
|
function statusToIcon(status) {
|
|
|
|
var icons = {
|
|
|
|
erroring: "🔥",
|
|
|
|
failing: "❌",
|
|
|
|
passing: "✔️️",
|
|
|
|
};
|
|
|
|
return icons[status] || "?";
|
|
|
|
}
|
|
|
|
|
2017-02-28 18:08:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function to handle test results.
|
|
|
|
*/
|
2017-02-25 00:50:17 +01:00
|
|
|
page.onCallback = function(messageType) {
|
|
|
|
if (messageType === "testResult") {
|
|
|
|
var testResult = arguments[1];
|
|
|
|
|
|
|
|
allTestsPassing = allTestsPassing && testResult.status === "passing";
|
|
|
|
var newCount = (testStatusCounts[testResult.status] || 0) + 1;
|
|
|
|
testStatusCounts[testResult.status] = newCount;
|
|
|
|
testStatusCounts.total += 1;
|
|
|
|
|
|
|
|
console.log([
|
|
|
|
statusToIcon(testResult.status),
|
|
|
|
testResult.test.name
|
|
|
|
].join(" "));
|
|
|
|
|
|
|
|
if (testResult.output) {
|
|
|
|
console.log(
|
|
|
|
testResult.output
|
|
|
|
.trim()
|
|
|
|
.replace(/^/, "\t")
|
|
|
|
.replace(/\n/g, "\n\t")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (messageType === "exit") {
|
|
|
|
|
|
|
|
console.log("\n");
|
|
|
|
|
|
|
|
for (var testStatus in testStatusCounts) {
|
|
|
|
var count = testStatusCounts[testStatus];
|
|
|
|
if (count > 0) {
|
|
|
|
console.log(testStatus.toUpperCase(), count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allTestsPassing) {
|
2017-02-28 18:08:36 +01:00
|
|
|
console.log("\nNot all tests are passing");
|
2017-02-25 00:50:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
phantom.exit(allTestsPassing ? 0 : 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-28 18:08:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the test webpage in PhantomJS.
|
|
|
|
*/
|
|
|
|
page.open("build/test/index.html", function(status) {
|
2017-02-25 00:50:17 +01:00
|
|
|
if (status !== "success") {
|
2017-02-28 18:08:36 +01:00
|
|
|
console.log("STATUS: ", status);
|
2017-02-25 00:50:17 +01:00
|
|
|
phantom.exit(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-28 18:08:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fail if the process takes longer than 10 seconds.
|
|
|
|
*/
|
2017-02-25 00:50:17 +01:00
|
|
|
setTimeout(function() {
|
2017-02-28 18:08:36 +01:00
|
|
|
console.log("Tests took longer than 10 seconds to run, returning.");
|
2017-02-25 00:50:17 +01:00
|
|
|
phantom.exit(1);
|
|
|
|
}, 10 * 1000);
|