Set up Grunt tasks to build web and Node versions of CyberChef using webpack

This commit is contained in:
n1474335 2017-03-21 22:41:44 +00:00
parent 99f306dc18
commit 885fbe13ac
56 changed files with 299 additions and 29138 deletions

View file

@ -1,24 +0,0 @@
/* eslint-env node */
/**
* NodeRunner.js
*
* The purpose of this file is to execute via PhantomJS the file
* PhantomRunner.js, because PhantomJS is managed by node.
*
* @author tlwr [toby@toby.codes]
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
var path = require("path"),
phantomjs = require("phantomjs-prebuilt"),
phantomEntryPoint = path.join(__dirname, "PhantomRunner.js"),
program = phantomjs.exec(phantomEntryPoint);
program.stdout.pipe(process.stdout);
program.stderr.pipe(process.stderr);
program.on("exit", function(status) {
process.exit(status);
});

View file

@ -1,99 +0,0 @@
/* eslint-env node */
/* globals phantom */
/**
* PhantomRunner.js
*
* This file navigates to build/test/index.html and logs the test results.
*
* @author tlwr [toby@toby.codes]
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
var page = require("webpage").create(),
allTestsPassing = true,
testStatusCounts = {
total: 0,
};
/**
* Helper function to convert a status to an icon.
*
* @param {string} status
* @returns {string}
*/
function statusToIcon(status) {
var icons = {
erroring: "🔥",
failing: "❌",
passing: "✔️️",
};
return icons[status] || "?";
}
/**
* Callback function to handle test results.
*/
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) {
console.log("\nNot all tests are passing");
}
phantom.exit(allTestsPassing ? 0 : 1);
}
};
/**
* Open the test webpage in PhantomJS.
*/
page.open("build/test/index.html", function(status) {
if (status !== "success") {
console.log("STATUS: ", status);
phantom.exit(1);
}
});
/**
* Fail if the process takes longer than 10 seconds.
*/
setTimeout(function() {
console.log("Tests took longer than 10 seconds to run, returning.");
phantom.exit(1);
}, 10 * 1000);

View file

@ -8,6 +8,7 @@
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
var Chef = require("../src/js/core/Chef.js");
(function() {
/**
@ -85,5 +86,6 @@
// Singleton TestRegister, keeping things simple and obvious.
window.TestRegister = new TestRegister();
global.TestRegister = global.TestRegister || new TestRegister();
module.exports = global.TestRegister;
})();

View file

@ -1,38 +1,92 @@
/**
* TestRunner.js
*
* This is for actually running the tests in the test register.
* For running the tests in the test register.
*
* @author tlwr [toby@toby.codes]
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
var TestRegister = require("./TestRegister.js"),
allTestsPassing = true,
testStatusCounts = {
total: 0,
};
document.addEventListener("DOMContentLoaded", function() {
TestRegister.runTests()
/**
* Requires and returns all modules that match.
*
* @param {Object} requireContext
* @returns {Object[]}
*/
function requireAll(requireContext) {
return requireContext.keys().map(requireContext);
}
// Import all tests
requireAll(require.context("./tests", true, /.+\.js$/));
/**
* Helper function to convert a status to an icon.
*
* @param {string} status
* @returns {string}
*/
function statusToIcon(status) {
var icons = {
erroring: "🔥",
failing: "❌",
passing: "✔️️",
};
return icons[status] || "?";
}
/**
* Displays a given test result in the console.
*
* @param {Object} testResult
*/
function handleTestResult(testResult) {
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")
);
}
}
TestRegister.runTests()
.then(function(results) {
results.forEach(function(testResult) {
results.forEach(handleTestResult);
if (typeof window.callPhantom === "function") {
// If we're running this in PhantomJS
window.callPhantom(
"testResult",
testResult
);
} else {
// If we're just viewing this in a normal browser
var output = [
"----------",
testResult.test.name,
testResult.status,
testResult.output,
].join("<br>");
document.querySelector("main").innerHTML += output;
console.log("\n");
for (var testStatus in testStatusCounts) {
var count = testStatusCounts[testStatus];
if (count > 0) {
console.log(testStatus.toUpperCase(), count);
}
});
if (typeof window.callPhantom === "function") {
window.callPhantom("exit");
}
if (!allTestsPassing) {
console.log("\nNot all tests are passing");
}
process.exit(allTestsPassing ? 0 : 1);
});
});

View file

@ -1,33 +0,0 @@
<!--
CyberChef test suite
@author tlwr [toby@toby.codes]
@copyright Crown Copyright 2017
@license Apache-2.0
Copyright 2017 Crown Copyright
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CyberChef test suite</title>
</head>
<body>
<main style="white-space: pre"></main>
<script type="application/javascript" src="tests.js"></script>
</body>
</html>

View file

@ -6,6 +6,8 @@
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
var TestRegister = require("../../TestRegister.js");
TestRegister.addTests([
{
name: "To Base58 (Bitcoin): nothing",

View file

@ -5,6 +5,8 @@
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
var TestRegister = require("../../TestRegister.js");
TestRegister.addTests([
{
name: "Bzip2 decompress",

View file

@ -6,6 +6,8 @@
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
var TestRegister = require("../../TestRegister.js");
TestRegister.addTests([
{
name: "Fork: nothing",

View file

@ -6,6 +6,8 @@
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
var TestRegister = require("../../TestRegister.js");
TestRegister.addTests([
{
name: "To Morse Code: 'SOS'",

View file

@ -5,6 +5,8 @@
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
var TestRegister = require("../../TestRegister.js");
TestRegister.addTests([
{
name: "Regex, non-HTML op",