mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-08 07:21:02 -04:00
add formatter
This commit is contained in:
parent
c4e7c41a6e
commit
ce30989adc
693 changed files with 51226 additions and 26671 deletions
|
@ -21,7 +21,6 @@ import log from "loglevel";
|
|||
* @constructor
|
||||
*/
|
||||
class TestRegister {
|
||||
|
||||
/**
|
||||
* initialise with no tests
|
||||
*/
|
||||
|
@ -50,38 +49,39 @@ class TestRegister {
|
|||
/**
|
||||
* Runs all the tests in the register.
|
||||
*/
|
||||
async runTests () {
|
||||
async runTests() {
|
||||
// Turn off logging to avoid messy errors
|
||||
log.setLevel("silent", false);
|
||||
|
||||
const progBar = new cliProgress.SingleBar({
|
||||
format: formatter,
|
||||
stopOnComplete: true
|
||||
}, cliProgress.Presets.shades_classic);
|
||||
const progBar = new cliProgress.SingleBar(
|
||||
{
|
||||
format: formatter,
|
||||
stopOnComplete: true,
|
||||
},
|
||||
cliProgress.Presets.shades_classic,
|
||||
);
|
||||
const testResults = [];
|
||||
|
||||
console.log("Running operation tests...");
|
||||
progBar.start(this.tests.length, 0, {
|
||||
msg: "Setting up"
|
||||
msg: "Setting up",
|
||||
});
|
||||
|
||||
for (const test of this.tests) {
|
||||
progBar.update(testResults.length, {
|
||||
msg: test.name
|
||||
msg: test.name,
|
||||
});
|
||||
|
||||
const chef = new Chef();
|
||||
const result = await chef.bake(
|
||||
test.input,
|
||||
test.recipeConfig,
|
||||
{ returnType: "string" }
|
||||
);
|
||||
const result = await chef.bake(test.input, test.recipeConfig, {
|
||||
returnType: "string",
|
||||
});
|
||||
|
||||
const ret = {
|
||||
test: test,
|
||||
status: null,
|
||||
output: null,
|
||||
duration: result.duration
|
||||
duration: result.duration,
|
||||
};
|
||||
|
||||
if (result.error) {
|
||||
|
@ -94,7 +94,8 @@ class TestRegister {
|
|||
"Expected",
|
||||
"\t" + test.expectedOutput.replace(/\n/g, "\n\t"),
|
||||
"Received",
|
||||
"\t" + result.error.displayStr.replace(/\n/g, "\n\t"),
|
||||
"\t" +
|
||||
result.error.displayStr.replace(/\n/g, "\n\t"),
|
||||
].join("\n");
|
||||
}
|
||||
} else {
|
||||
|
@ -107,16 +108,25 @@ class TestRegister {
|
|||
ret.output = "Expected an error but did not receive one.";
|
||||
} else if (result.result === test.expectedOutput) {
|
||||
ret.status = "passing";
|
||||
} else if ("expectedMatch" in test && test.expectedMatch.test(result.result)) {
|
||||
} else if (
|
||||
"expectedMatch" in test &&
|
||||
test.expectedMatch.test(result.result)
|
||||
) {
|
||||
ret.status = "passing";
|
||||
} else if ("unexpectedMatch" in test && !test.unexpectedMatch.test(result.result)) {
|
||||
} else if (
|
||||
"unexpectedMatch" in test &&
|
||||
!test.unexpectedMatch.test(result.result)
|
||||
) {
|
||||
ret.status = "passing";
|
||||
} else {
|
||||
ret.status = "failing";
|
||||
const expected = test.expectedOutput ? test.expectedOutput :
|
||||
test.expectedMatch ? test.expectedMatch.toString() :
|
||||
test.unexpectedMatch ? "to not find " + test.unexpectedMatch.toString() :
|
||||
"unknown";
|
||||
const expected = test.expectedOutput
|
||||
? test.expectedOutput
|
||||
: test.expectedMatch
|
||||
? test.expectedMatch.toString()
|
||||
: test.unexpectedMatch
|
||||
? "to not find " + test.unexpectedMatch.toString()
|
||||
: "unknown";
|
||||
ret.output = [
|
||||
"Expected",
|
||||
"\t" + expected.replace(/\n/g, "\n\t"),
|
||||
|
@ -140,27 +150,30 @@ class TestRegister {
|
|||
* Run all api related tests and wrap results in report format
|
||||
*/
|
||||
async runApiTests() {
|
||||
const progBar = new cliProgress.SingleBar({
|
||||
format: formatter,
|
||||
stopOnComplete: true
|
||||
}, cliProgress.Presets.shades_classic);
|
||||
const progBar = new cliProgress.SingleBar(
|
||||
{
|
||||
format: formatter,
|
||||
stopOnComplete: true,
|
||||
},
|
||||
cliProgress.Presets.shades_classic,
|
||||
);
|
||||
const testResults = [];
|
||||
|
||||
console.log("Running Node API tests...");
|
||||
progBar.start(this.apiTests.length, 0, {
|
||||
msg: "Setting up"
|
||||
msg: "Setting up",
|
||||
});
|
||||
|
||||
global.TESTING = true;
|
||||
for (const test of this.apiTests) {
|
||||
progBar.update(testResults.length, {
|
||||
msg: test.name
|
||||
msg: test.name,
|
||||
});
|
||||
|
||||
const result = {
|
||||
test: test,
|
||||
status: null,
|
||||
output: null
|
||||
output: null,
|
||||
};
|
||||
try {
|
||||
await test.run();
|
||||
|
@ -178,7 +191,6 @@ class TestRegister {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Formatter for the progress bar
|
||||
*
|
||||
|
@ -188,8 +200,15 @@ class TestRegister {
|
|||
* @returns {string}
|
||||
*/
|
||||
function formatter(options, params, payload) {
|
||||
const bar = options.barCompleteString.substr(0, Math.round(params.progress * options.barsize)) +
|
||||
options.barIncompleteString.substr(0, Math.round((1-params.progress) * options.barsize));
|
||||
const bar =
|
||||
options.barCompleteString.substr(
|
||||
0,
|
||||
Math.round(params.progress * options.barsize),
|
||||
) +
|
||||
options.barIncompleteString.substr(
|
||||
0,
|
||||
Math.round((1 - params.progress) * options.barsize),
|
||||
);
|
||||
|
||||
const percentage = Math.floor(params.progress * 100),
|
||||
duration = Math.floor((Date.now() - params.startTime) / 1000);
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Helper function to convert a status to an icon.
|
||||
*
|
||||
|
@ -16,11 +15,13 @@
|
|||
* @returns {string}
|
||||
*/
|
||||
function statusToIcon(status) {
|
||||
return {
|
||||
erroring: "🔥",
|
||||
failing: "❌",
|
||||
passing: "✔️️",
|
||||
}[status] || "?";
|
||||
return (
|
||||
{
|
||||
erroring: "🔥",
|
||||
failing: "❌",
|
||||
passing: "✔️️",
|
||||
}[status] || "?"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -30,12 +31,18 @@ function statusToIcon(status) {
|
|||
* @param {Object} testResult
|
||||
*/
|
||||
function handleTestResult(testStatus, testResult) {
|
||||
testStatus.allTestsPassing = testStatus.allTestsPassing && testResult.status === "passing";
|
||||
testStatus.counts[testResult.status] = (testStatus.counts[testResult.status] || 0) + 1;
|
||||
testStatus.allTestsPassing =
|
||||
testStatus.allTestsPassing && testResult.status === "passing";
|
||||
testStatus.counts[testResult.status] =
|
||||
(testStatus.counts[testResult.status] || 0) + 1;
|
||||
testStatus.counts.total += 1;
|
||||
|
||||
if (testResult.duration > 2000) {
|
||||
console.log(`'${testResult.test.name}' took ${(testResult.duration / 1000).toFixed(1)}s to complete`);
|
||||
console.log(
|
||||
`'${testResult.test.name}' took ${(
|
||||
testResult.duration / 1000
|
||||
).toFixed(1)}s to complete`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,7 +53,7 @@ function handleTestResult(testStatus, testResult) {
|
|||
* @param {Object[]} results - results from TestRegister
|
||||
*/
|
||||
export function logTestReport(testStatus, results) {
|
||||
results.forEach(r => handleTestResult(testStatus, r));
|
||||
results.forEach((r) => handleTestResult(testStatus, r));
|
||||
|
||||
console.log();
|
||||
for (const testStatusCount in testStatus.counts) {
|
||||
|
@ -58,21 +65,24 @@ export function logTestReport(testStatus, results) {
|
|||
console.log();
|
||||
|
||||
// Print error messages for tests that didn't pass
|
||||
results.filter(res => res.status !== "passing").forEach(testResult => {
|
||||
console.log([
|
||||
statusToIcon(testResult.status),
|
||||
testResult.test.name
|
||||
].join(" "));
|
||||
|
||||
if (testResult.output) {
|
||||
results
|
||||
.filter((res) => res.status !== "passing")
|
||||
.forEach((testResult) => {
|
||||
console.log(
|
||||
testResult.output
|
||||
.trim()
|
||||
.replace(/^/, "\t")
|
||||
.replace(/\n/g, "\n\t")
|
||||
[statusToIcon(testResult.status), testResult.test.name].join(
|
||||
" ",
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
if (testResult.output) {
|
||||
console.log(
|
||||
testResult.output
|
||||
.trim()
|
||||
.replace(/^/, "\t")
|
||||
.replace(/\n/g, "\n\t"),
|
||||
);
|
||||
}
|
||||
});
|
||||
console.log();
|
||||
|
||||
process.exit(testStatus.allTestsPassing ? 0 : 1);
|
||||
|
@ -83,8 +93,10 @@ export function logTestReport(testStatus, results) {
|
|||
*/
|
||||
export function setLongTestFailure() {
|
||||
const timeLimit = 120;
|
||||
setTimeout(function() {
|
||||
console.log(`Tests took longer than ${timeLimit} seconds to run, returning.`);
|
||||
setTimeout(function () {
|
||||
console.log(
|
||||
`Tests took longer than ${timeLimit} seconds to run, returning.`,
|
||||
);
|
||||
process.exit(1);
|
||||
}, timeLimit * 1000);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue